{"id":5579,"date":"2023-01-06T09:45:56","date_gmt":"2023-01-06T01:45:56","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5579"},"modified":"2023-01-06T09:45:56","modified_gmt":"2023-01-06T01:45:56","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8react-js%e5%b0%8dhtml%e5%ad%97%e4%b8%b2%e9%80%b2%e8%a1%8c%e7%b7%a8%e7%a2%bc","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8react-js%e5%b0%8dhtml%e5%ad%97%e4%b8%b2%e9%80%b2%e8%a1%8c%e7%b7%a8%e7%a2%bc\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528React.JS\u5c0dHTML\u5b57\u4e32\u9032\u884c\u7de8\u78bc"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.JS, HTML \u7f16\u7801, escapeHtml\"><\/p>\n<h1>\u5229\u7528 React.JS \u5b57\u4e32\u7684 HTML \u7f16\u7801(escapeHtml)<\/h1>\n<p>React.JS \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u754c\u9762\u7684 JavaScript \u5eab\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5e8f\u3002\u5b83\u7684\u4e00\u500b\u91cd\u8981\u529f\u80fd\u662f HTML \u7f16\u78bc\uff0c\u4e5f\u7a31\u70ba escapeHtml\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5c07\u5b57\u7b26\u4e32\u4e2d\u7684 HTML \u7279\u6b8a\u5b57\u7b26\u8f49\u63db\u70ba HTML \u5b9e\u4f53\uff0c\u4ee5\u9632\u6b62 XSS \u653b\u64ca\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5728 React.JS \u4e2d\uff0cescapeHtml \u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5c07\u5b57\u7b26\u4e32\u4e2d\u7684 HTML \u7279\u6b8a\u5b57\u7b26\u8f49\u63db\u70ba HTML \u5b9e\u4f53\uff0c\u4ee5\u9632\u6b62 XSS \u653b\u64ca\u3002<\/p>\n<pre class=\"brush: javascript\">\nfunction escapeHtml(str) {\n  return str.replace(\/[&<>\"']\/g, function (tag) {\n    const tagsToReplace = {\n      '&': '&',\n      '<': '<',\n      '>': '>',\n      '\"': '\"',\n      \"'\": '&#39;'\n    };\n    return tagsToReplace[tag] || tag;\n  });\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u662f\u4e00\u500b\u7c21\u55ae\u7684 escapeHtml \u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5c07\u5b57\u7b26\u4e32\u4e2d\u7684 HTML \u7279\u6b8a\u5b57\u7b26\u8f49\u63db\u70ba HTML \u5b9e\u4f53\uff0c\u4ee5\u9632\u6b62 XSS \u653b\u64ca\u3002<\/p>\n<h2>\u70ba\u4ec0\u9ebc\u8981\u4f7f\u7528 escapeHtml\uff1f<\/h2>\n<p>\u4f7f\u7528 escapeHtml \u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u4fdd\u8b77\u7db2\u7ad9\u514d\u53d7 XSS \u653b\u64ca\uff0cXSS \u653b\u64ca\u662f\u4e00\u7a2e\u5e38\u898b\u7684\u7db2\u7d61\u5b89\u5168\u6f0f\u6d1e\uff0c\u5b83\u53ef\u4ee5\u8b93\u653b\u64ca\u8005\u5728\u7db2\u7ad9\u4e0a\u57f7\u884c\u60e1\u610f\u4ee3\u78bc\uff0c\u5f9e\u800c\u7372\u53d6\u7528\u6236\u7684\u654f\u611f\u4fe1\u606f\uff0c\u751a\u81f3\u7834\u58de\u7db2\u7ad9\u7684\u5b89\u5168\u6027\u3002\u56e0\u6b64\uff0c\u4f7f\u7528 escapeHtml \u5c0d\u65bc\u4fdd\u8b77\u7db2\u7ad9\u514d\u53d7 XSS \u653b\u64ca\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 escapeHtml\uff1f<\/h2>\n<p>\u4f7f\u7528 escapeHtml \u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u5728 React.JS \u4e2d\u5c0e\u5165 escapeHtml \u51fd\u6578\uff0c\u7136\u5f8c\u5c07\u5b57\u7b26\u4e32\u50b3\u905e\u7d66\u5b83\uff0c\u5b83\u5c31\u6703\u5c07\u5b57\u7b26\u4e32\u4e2d\u7684 HTML \u7279\u6b8a\u5b57\u7b26\u8f49\u63db\u70ba HTML \u5b9e\u4f53\uff0c\u4ee5\u9632\u6b62 XSS \u653b\u64ca\u3002<\/p>\n<pre class=\"brush: javascript\">\nimport { escapeHtml } from 'react';\n\nconst str = '<script>alert(\"XSS Attack!\")<\/script>';\nconst escapedStr = escapeHtml(str);\n\nconsole.log(escapedStr); \/\/ <script>alert(\"XSS Attack!\")<\/script>\n<\/pre>\n<p>\u7e3d\u7d50\uff0cReact.JS \u4e2d\u7684 escapeHtml \u529f\u80fd\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5c07\u5b57\u7b26\u4e32\u4e2d\u7684 HTML \u7279\u6b8a\u5b57\u7b26\u8f49\u63db\u70ba HTML \u5b9e\u4f53\uff0c\u4ee5\u9632\u6b62 XSS \u653b\u64ca\uff0c\u56e0\u6b64\u4f7f\u7528 escapeHtml \u5c0d\u65bc\u4fdd\u8b77\u7db2\u7ad9\u514d\u53d7 XSS \u653b\u64ca\u975e\u5e38\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528React.JS\u5c0dHTML\u5b57\u4e32\u9032\u884c\u7de8\u78bc\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5c0d\u7db2\u9801\u5167\u5bb9\u9032\u884c\u7de8\u78bc\uff0c\u4ee5\u7b26\u5408GoogleSEO\u539f\u5247\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-5579","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1rZ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5579"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5579\/revisions"}],"predecessor-version":[{"id":5580,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5579\/revisions\/5580"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}