{"id":2580,"date":"2024-08-14T00:06:00","date_gmt":"2024-08-13T16:06:00","guid":{"rendered":"https:\/\/systw.net\/note\/?p=2580"},"modified":"2025-08-14T10:38:28","modified_gmt":"2025-08-14T02:38:28","slug":"sc-lack-of-input-validation","status":"publish","type":"post","link":"https:\/\/systw.net\/note\/archives\/2580","title":{"rendered":"SC Lack of Input Validation"},"content":{"rendered":"\n<p>\u8f38\u5165\u9a57\u8b49\uff08Input Validation\uff09\u662f\u78ba\u4fdd\u667a\u80fd\u5408\u7d04\u50c5\u8655\u7406\u6709\u6548\u4e14\u9810\u671f\u7684\u8cc7\u6599\u3002\u7576\u667a\u80fd\u5408\u7d04\u672a\u5c0d\u8f38\u5165\u8cc7\u6599\u9032\u884c\u9a57\u8b49\u6642\uff0c\u53ef\u80fd\u6703\u66b4\u9732\u65bc\u591a\u7a2e\u5b89\u5168\u98a8\u96aa\uff0c\u4f8b\u5982\u908f\u8f2f\u64cd\u63a7\u3001\u672a\u6388\u6b0a\u5b58\u53d6\u6216\u610f\u5916\u884c\u70ba\u3002\u5982\u679c\u5408\u7d04\u5047\u8a2d\u7528\u6236\u8f38\u5165\u7684\u8cc7\u6599\u7e3d\u662f\u6709\u6548\u800c\u672a\u9032\u884c\u6aa2\u67e5\uff0c\u653b\u64ca\u8005\u53ef\u80fd\u5229\u7528\u9019\u4e00\u9ede\u8f38\u5165\u60e1\u610f\u8cc7\u6599\uff0c\u5f9e\u800c\u5371\u5bb3\u5408\u7d04\u7684\u5b89\u5168\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7bc4\u4f8b\uff08\u5b58\u5728\u6f0f\u6d1e\u7684\u5408\u7d04\uff09<\/h4>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7f3a\u4e4f\u8f38\u5165\u9a57\u8b49\u7684\u667a\u80fd\u5408\u7d04\u7bc4\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ncontract Solidity_LackOfInputValidation {\n    mapping(address =&gt; uint256) public balances;\n\n    function setBalance(address user, uint256 amount) public {\n        \/\/ The function allows anyone to set arbitrary balances for any user without validation.\n        balances&#91;user] = amount;\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u6f0f\u6d1e\u5206\u6790<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>setBalance<\/code> \u51fd\u6578<\/strong>\uff1a\u8a72\u51fd\u6578\u5141\u8a31\u4efb\u4f55\u4eba\u70ba\u4efb\u4f55\u5730\u5740\uff08<code>user<\/code>\uff09\u8a2d\u7f6e\u4efb\u610f\u7684\u9918\u984d\uff08<code>amount<\/code>\uff09\uff0c\u4e14\u672a\u5c0d\u8f38\u5165\u9032\u884c\u4efb\u4f55\u9a57\u8b49\u3002<\/li>\n\n\n\n<li><strong>\u554f\u984c<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u6c92\u6709\u6aa2\u67e5 <code>user<\/code> \u662f\u5426\u70ba\u6709\u6548\u5730\u5740\uff08\u4f8b\u5982\uff0c\u662f\u5426\u70ba\u96f6\u5730\u5740\uff09\u3002<\/li>\n\n\n\n<li>\u6c92\u6709\u9650\u5236\u8ab0\u53ef\u4ee5\u8abf\u7528\u8a72\u51fd\u6578\uff0c\u4efb\u4f55\u4eba\u90fd\u80fd\u4fee\u6539\u4efb\u4f55\u5730\u5740\u7684\u9918\u984d\u3002<\/li>\n\n\n\n<li>\u6c92\u6709\u5c0d <code>amount<\/code> \u9032\u884c\u7bc4\u570d\u6216\u6709\u6548\u6027\u6aa2\u67e5\uff0c\u53ef\u80fd\u5c0e\u81f4\u4e0d\u5408\u7406\u7684\u9918\u984d\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u9019\u4f7f\u5f97\u653b\u64ca\u8005\u53ef\u4ee5\u8f15\u6613\u64cd\u63a7\u5408\u7d04\u72c0\u614b\uff0c\u4f8b\u5982\u5c07\u81ea\u5df1\u7684\u9918\u984d\u8a2d\u7f6e\u70ba\u4efb\u610f\u5927\u503c\uff0c\u6216\u5c07\u5176\u4ed6\u7528\u6236\u7684\u9918\u984d\u6e05\u96f6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5f71\u97ff<\/h4>\n\n\n\n<p>\u7f3a\u4e4f\u8f38\u5165\u9a57\u8b49\u53ef\u80fd\u5c0e\u81f4\u4ee5\u4e0b\u56b4\u91cd\u5f8c\u679c\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8cc7\u91d1\u640d\u5931<\/strong>\uff1a\u653b\u64ca\u8005\u53ef\u901a\u904e\u64cd\u63a7\u8f38\u5165\u4f86\u7aca\u53d6\u8cc7\u91d1\u6216\u4ee3\u5e63\u3002<\/li>\n\n\n\n<li><strong>\u72c0\u614b\u8b8a\u6578\u640d\u58de<\/strong>\uff1a\u4e0d\u7576\u8f38\u5165\u53ef\u80fd\u5c0e\u81f4\u5408\u7d04\u72c0\u614b\u8b8a\u6578\uff08\u5982\u9918\u984d\uff09\u88ab\u932f\u8aa4\u4fee\u6539\uff0c\u9020\u6210\u4e0d\u53ef\u9760\u6216\u4e0d\u5b89\u5168\u7684\u884c\u70ba\u3002<\/li>\n\n\n\n<li><strong>\u672a\u6388\u6b0a\u64cd\u4f5c<\/strong>\uff1a\u653b\u64ca\u8005\u53ef\u80fd\u57f7\u884c\u672a\u7d93\u6388\u6b0a\u7684\u4ea4\u6613\u6216\u64cd\u4f5c\uff0c\u5f71\u97ff\u7528\u6236\u53ca\u6574\u500b\u7cfb\u7d71\u7684\u904b\u4f5c\u3002<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fee\u5fa9\u65b9\u6cd5<\/h4>\n\n\n\n<p>\u70ba\u907f\u514d\u7f3a\u4e4f\u8f38\u5165\u9a57\u8b49\u7684\u554f\u984c\uff0c\u958b\u767c\u8005\u61c9\u63a1\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u78ba\u4fdd\u8f38\u5165\u7b26\u5408\u9810\u671f\u985e\u578b<\/strong>\uff1a\u6aa2\u67e5\u8f38\u5165\u8cc7\u6599\u662f\u5426\u70ba\u6b63\u78ba\u7684\u8cc7\u6599\u985e\u578b\uff08\u4f8b\u5982\uff0c\u5730\u5740\u3001\u6574\u6578\u7b49\uff09\u3002<\/li>\n\n\n\n<li><strong>\u9a57\u8b49\u8f38\u5165\u7bc4\u570d<\/strong>\uff1a\u78ba\u4fdd\u8f38\u5165\u503c\u5728\u53ef\u63a5\u53d7\u7684\u7bc4\u570d\u5167\uff08\u4f8b\u5982\uff0c\u91d1\u984d\u5fc5\u9808\u70ba\u6b63\u6578\uff09\u3002<\/li>\n\n\n\n<li><strong>\u9650\u5236\u51fd\u6578\u8a2a\u554f\u6b0a\u9650<\/strong>\uff1a\u78ba\u4fdd\u53ea\u6709\u6388\u6b0a\u7684\u5be6\u9ad4\uff08\u5982\u5408\u7d04\u64c1\u6709\u8005\uff09\u53ef\u4ee5\u8abf\u7528\u7279\u5b9a\u529f\u80fd\u3002<\/li>\n\n\n\n<li><strong>\u6aa2\u67e5\u8f38\u5165\u7d50\u69cb<\/strong>\uff1a\u9a57\u8b49\u8f38\u5165\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u5730\u5740\u662f\u5426\u6709\u6548\uff0c\u6216\u5b57\u4e32\u9577\u5ea6\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002<\/li>\n\n\n\n<li><strong>\u932f\u8aa4\u8655\u7406<\/strong>\uff1a\u7576\u8f38\u5165\u7121\u6548\u6642\uff0c\u7acb\u5373\u505c\u6b62\u57f7\u884c\u4e26\u8fd4\u56de\u6e05\u6670\u7684\u932f\u8aa4\u8a0a\u606f\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fee\u5fa9\u5f8c\u7684\u7bc4\u4f8b\u5408\u7d04<\/h4>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4fee\u5fa9\u4e86\u8f38\u5165\u9a57\u8b49\u554f\u984c\u7684\u5408\u7d04\u7bc4\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ncontract LackOfInputValidation {\n    mapping(address =&gt; uint256) public balances;\n    address public owner;\n\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">    constructor() {\n        owner = msg.sender;\n    }\n\n    modifier onlyOwner() {\n        require(msg.sender == owner, \"Caller is not authorized\");\n        _;\n    }<\/mark>\n\n    function setBalance(address user, uint256 amount) public <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">onlyOwner<\/mark> {\n        <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">require(user != address(0), \"Invalid address\");<\/mark>\n        balances&#91;user] = amount;\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u4fee\u5fa9\u5167\u5bb9<\/strong>\uff1a<\/p>\n\n\n\n<p>1.<strong>\u65b0\u589e\u64c1\u6709\u8005\u63a7\u5236<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u904e <code>constructor<\/code> \u8a2d\u7f6e\u5408\u7d04\u64c1\u6709\u8005\uff08<code>owner<\/code>\uff09\uff0c\u4e26\u4f7f\u7528 <code>onlyOwner<\/code> \u4fee\u98fe\u7b26\u9650\u5236 <code>setBalance<\/code> \u51fd\u6578\u50c5\u80fd\u7531\u64c1\u6709\u8005\u8abf\u7528\u3002\u53ea\u6709\u5408\u7d04\u64c1\u6709\u8005\u53ef\u4ee5\u4fee\u6539\u9918\u984d\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u65b0\u589e\u7a0b\u5f0f\u78bc\uff1a \u69cb\u9020\u51fd\u6578\uff0c\u8a2d\u7f6e\u5408\u7d04\u64c1\u6709\u8005<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    constructor() {\n        owner = msg.sender;\n    }<\/code><\/pre>\n\n\n\n<p>\u65b0\u589e\u7a0b\u5f0f\u78bc\uff1a\u50c5\u9650\u64c1\u6709\u8005\u8abf\u7528\u7684\u4fee\u98fe\u7b26<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    modifier onlyOwner() {\n        require(msg.sender == owner, \"Caller is not authorized\");\n        _;\n    }<\/code><\/pre>\n\n\n\n<p>2.<strong>\u9a57\u8b49\u5730\u5740\u6709\u6548\u6027<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 <code>setBalance<\/code> \u51fd\u6578\u4e2d\uff0c\u65b0\u589e\u4e86\u5c0d <code>user<\/code> \u5730\u5740\u7684\u6aa2\u67e5\uff0c\u78ba\u4fdd\u5b83\u4e0d\u662f\u96f6\u5730\u5740\uff08<code>address(0)<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u65b0\u589e\u7a0b\u5f0f\u78bc\uff1a<code>require(user != address(0), \"Invalid address\");<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7e3d\u7d50<\/h4>\n\n\n\n<p>\u7f3a\u4e4f\u8f38\u5165\u9a57\u8b49\u662f\u667a\u80fd\u5408\u7d04\u4e2d\u5e38\u898b\u7684\u5b89\u5168\u6f0f\u6d1e\uff0c\u53ef\u80fd\u5c0e\u81f4\u8cc7\u91d1\u640d\u5931\u3001\u72c0\u614b\u640d\u58de\u6216\u672a\u6388\u6b0a\u64cd\u4f5c\u3002\u900f\u904e\u5c0d\u8f38\u5165\u8cc7\u6599\u9032\u884c\u56b4\u683c\u7684\u985e\u578b\u3001\u7bc4\u570d\u3001\u683c\u5f0f\u548c\u6b0a\u9650\u9a57\u8b49\uff0c\u4e26\u5728\u7121\u6548\u8f38\u5165\u6642\u63d0\u4f9b\u660e\u78ba\u7684\u932f\u8aa4\u8a0a\u606f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u5408\u7d04\u7684\u5b89\u5168\u6027\u548c\u53ef\u9760\u6027\u3002\u4fee\u5fa9\u5f8c\u7684\u5408\u7d04\u5c55\u793a\u4e86\u5982\u4f55\u901a\u904e\u6b0a\u9650\u63a7\u5236\u548c\u8f38\u5165\u6aa2\u67e5\u4f86\u89e3\u6c7a\u9019\u4e9b\u554f\u984c\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f38\u5165\u9a57\u8b49\uff08Input Validation\uff09\u662f\u78ba\u4fdd\u667a\u80fd\u5408\u7d04\u50c5 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[371],"tags":[],"class_list":["post-2580","post","type-post","status-publish","format-standard","hentry","category-web3-security"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/2580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/comments?post=2580"}],"version-history":[{"count":3,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/2580\/revisions"}],"predecessor-version":[{"id":2607,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/2580\/revisions\/2607"}],"wp:attachment":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/media?parent=2580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/categories?post=2580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/tags?post=2580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}