{"id":47834,"date":"2024-12-02T17:57:07","date_gmt":"2024-12-02T09:57:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47834\/"},"modified":"2024-12-02T17:57:07","modified_gmt":"2024-12-02T09:57:07","slug":"%e5%88%a9%e7%94%a8%e9%9d%99%e6%80%81%e4%bb%a3%e7%a0%81%e5%88%86%e6%9e%90%e5%b7%a5%e5%85%b7%e7%a1%ae%e4%bf%9d-php-%e5%87%bd%e6%95%b0%e5%ae%89%e5%85%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47834\/","title":{"rendered":"\u5229\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u786e\u4fdd PHP \u51fd\u6570\u5b89\u5168"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5229\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u786e\u4fdd PHP \u51fd\u6570\u5b89\u5168<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5229\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u786e\u4fdd PHP \u51fd\u6570\u5b89\u5168\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729938810671cc57ab5a8e.jpg\" class=\"aligncenter\" title=\"\u5229\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u786e\u4fdd PHP \u51fd\u6570\u5b89\u5168\u63d2\u56fe\" alt=\"\u5229\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u786e\u4fdd PHP \u51fd\u6570\u5b89\u5168\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5229\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u786e\u4fdd PHP \u51fd\u6570\u5b89\u5168<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u662f\u5b9d\u8d35\u7684\u5de5\u5177\uff0c\u53ef\u5e2e\u52a9\u60a8\u8bc6\u522b\u548c\u4fee\u590d PHP \u4ee3\u7801\u4e2d\u7684\u6f5c\u5728\u5b89\u5168\u6f0f\u6d1e\u3002\u901a\u8fc7\u81ea\u52a8\u626b\u63cf\u4ee3\u7801\u5e93\u5e76\u67e5\u627e\u5df2\u77e5\u6f0f\u6d1e\u6a21\u5f0f\uff0c\u60a8\u53ef\u4ee5\u4e3b\u52a8\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p><strong>\u4f7f\u7528 PHPStan<\/strong><\/p>\n<p>PHPStan \u662f\u4e00\u4e2a\u6d41\u884c\u7684\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u68c0\u67e5 PHP \u4ee3\u7801\u7684\u8bed\u6cd5\u548c\u903b\u8f91\u9519\u8bef\u3002\u5b83\u8fd8\u53ef\u4ee5\u8bc6\u522b\u6f5c\u5728\u7684\u5b89\u5168\u95ee\u9898\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u4e0d\u53ef\u4fe1\u7684\u6570\u636e\u8f93\u5165<\/li>\n<li>\u672a\u5237\u65b0\u7684\u8f93\u51fa<\/li>\n<li>SQL \u6ce8\u5165<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 PHPStan \u68c0\u67e5\u6f5c\u5728\u5b89\u5168\u6f0f\u6d1e\u7684\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<pre>&lt;?php\n\/\/ \u4e0d\u5b89\u5168\u7684\u4ee3\u7801\n$input = $_GET['user_input'];\necho $input;\n\n\/\/ \u4f7f\u7528 PHPStan\nuse PHPStan\\Rules\\Rule;\nuse PHPStan\\Testing\\RuleTestCase;\nclass UnsafeInputRuleTest extends RuleTestCase\n{\n    protected function getRules(): array\n    {\n        return [\n            new \\PHPStan\\Rules\\Security\\UnsafeInputRule()\n        ];\n    }\n\n    public function testUnsafeInput(): void\n    {\n        \/\/ PHPStan \u4f1a\u6807\u8bb0\u4ee5\u4e0b\u884c\u5b58\u5728\u6f0f\u6d1e\n        $input = $_GET['user_input'];\n        echo $input;\n    }\n}\n?&gt;<\/pre>\n<p>\u901a\u8fc7\u8fd0\u884c\u6b64\u6d4b\u8bd5\uff0c\u60a8\u4f1a\u53d1\u73b0 PHPStan \u6807\u8bb0 <code>$input<\/code> \u7684\u4f7f\u7528\u4e0d\u5b89\u5168\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5305\u542b\u4e0d\u53ef\u4fe1\u7684\u7528\u6237\u8f93\u5165\u3002<\/p>\n<p><strong>\u5176\u4ed6\u5de5\u5177<\/strong><\/p>\n<p>\u9664\u4e86 PHPStan \u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u53ef\u7528\u4e8e\u786e\u4fdd PHP \u51fd\u6570\u5b89\u5168\u7684\u5de5\u5177\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>PSRule<\/li>\n<li>Psalm<\/li>\n<li>Phan<\/li>\n<\/ul>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p>\u5728\u4f7f\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u65f6\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<ul>\n<li>\u5b9a\u671f\u8fd0\u884c\u626b\u63cf\uff0c\u4ee5\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u4fdd\u6301\u5b89\u5168\u3002<\/li>\n<li>\u8bbe\u7f6e\u9608\u503c\u4ee5\u8fc7\u6ee4\u6389\u65e0\u5173\u7684\u8b66\u544a\u3002<\/li>\n<li>\u5c06\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u96c6\u6210\u5230\u60a8\u7684\u6784\u5efa\u7ba1\u9053\u4e2d\u3002<\/li>\n<li>\u4f7f\u7528\u5176\u4ed6\u5b89\u5168\u63aa\u65bd\uff0c\u4f8b\u5982\u8f93\u5165\u9a8c\u8bc1\u548c\u8f93\u51fa\u5237\u6d17\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5229\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u786e\u4fdd PHP \u51fd\u6570\u5b89\u5168\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5229\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u786e\u4fdd PHP \u51fd\u6570\u5b89\u5168 \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5229\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u786e\u4fdd PHP \u51fd\u6570\u5b89\u5168\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ \u5229\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u786e\u4fdd PHP \u51fd\u6570\u5b89\u5168 \u7b80\u4ecb \u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u662f\u5b9d\u8d35\u7684\u5de5\u5177\uff0c\u53ef\u5e2e\u52a9\u60a8\u8bc6\u522b\u548c\u4fee\u590d PHP \u4ee3\u7801\u4e2d\u7684\u6f5c\u5728\u5b89\u5168\u6f0f\u6d1e\u3002\u901a\u8fc7\u81ea\u52a8\u626b\u63cf\u4ee3\u7801\u5e93\u5e76\u67e5\u627e\u5df2\u77e5\u6f0f\u6d1e\u6a21\u5f0f\uff0c\u60a8\u53ef\u4ee5\u4e3b\u52a8\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u3002 \u4f7f\u7528 PHPStan PHPStan \u662f\u4e00\u4e2a\u6d41\u884c\u7684\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u68c0\u67e5 PHP \u4ee3\u7801\u7684\u8bed\u6cd5\u548c\u903b\u8f91\u9519\u8bef\u3002\u5b83\u8fd8\u53ef\u4ee5\u8bc6\u522b\u6f5c\u5728\u7684\u5b89\u5168\u95ee\u9898\uff0c\u4f8b\u5982\uff1a \u4e0d\u53ef\u4fe1\u7684\u6570\u636e\u8f93\u5165 \u672a\u5237\u65b0\u7684\u8f93\u51fa SQL \u6ce8\u5165 \u5b9e\u6218\u6848\u4f8b \u4ee5\u4e0b\u662f\u4f7f\u7528 PHPStan \u68c0\u67e5\u6f5c\u5728\u5b89\u5168\u6f0f\u6d1e\u7684\u5b9e\u6218\u6848\u4f8b\uff1a &lt;?php \/\/ \u4e0d\u5b89\u5168\u7684\u4ee3\u7801 $input = $_GET[&#8216;user_input&#8217;]; echo $input; \/\/ \u4f7f\u7528 PHPStan use PHPStan\\Rules\\Rule; use PHPStan\\Testing\\RuleTestCase; class UnsafeInputRuleTest extends RuleTestCase { protected function getRules(): array { return [ new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47834","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47834"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47834\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}