{"id":304,"date":"2026-11-02T11:22:15","date_gmt":"2026-11-02T03:22:15","guid":{"rendered":"https:\/\/yykcj.com\/?p=304"},"modified":"2026-11-02T11:22:15","modified_gmt":"2026-11-02T03:22:15","slug":"%e5%89%8d%e7%ab%af%e5%ae%89%e5%85%a8%e5%ae%9e%e6%88%98%ef%bc%9a%e4%bb%8exss%e5%88%b0csrf%e7%9a%84%e6%b7%b1%e5%ba%a6%e9%98%b2%e5%be%a1%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=304","title":{"rendered":"\u524d\u7aef\u5b89\u5168\u5b9e\u6218\uff1a\u4eceXSS\u5230CSRF\u7684\u6df1\u5ea6\u9632\u5fa1\u6307\u5357"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>\u5728Web\u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u5b89\u5168\u5f80\u5f80\u662f\u6700\u5bb9\u6613\u88ab\u5ffd\u89c6\u7684\u73af\u8282\u3002\u5f88\u591a\u5f00\u53d1\u8005\u8ba4\u4e3a\u201c\u6211\u7684\u5e94\u7528\u6ca1\u6709\u654f\u611f\u6570\u636e\uff0c\u4e0d\u4f1a\u88ab\u653b\u51fb\u201d\uff0c\u4f46\u4e8b\u5b9e\u4e0a\uff0cXSS\u548cCSRF\u662f\u4e24\u79cd\u6700\u5e38\u89c1\u4e14\u5371\u5bb3\u5de8\u5927\u7684\u6f0f\u6d1e\uff0c\u8f7b\u5219\u7a83\u53d6\u7528\u6237\u4f1a\u8bdd\uff0c\u91cd\u5219\u5bfc\u81f4\u6570\u636e\u6cc4\u9732\u751a\u81f3\u670d\u52a1\u5668\u6ca6\u9677\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u653b\u51fb\u8005\u89c6\u89d2\u7406\u89e3XSS\u548cCSRF\uff0c\u5e76\u901a\u8fc7\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u4e00\u6b65\u6b65\u6784\u5efa\u9632\u5fa1\u4f53\u7cfb\u3002<\/p>\n<h2>\u4e00\u3001XSS\u653b\u51fb\u4e0e\u9632\u5fa1<\/h2>\n<h3>1.1 XSS\u662f\u4ec0\u4e48\uff1f<\/h3>\n<p>XSS\uff08\u8de8\u7ad9\u811a\u672c\u653b\u51fb\uff09\u662f\u6307\u653b\u51fb\u8005\u5c06\u6076\u610f\u811a\u672c\u6ce8\u5165\u5230\u7f51\u9875\u4e2d\uff0c\u5f53\u5176\u4ed6\u7528\u6237\u6d4f\u89c8\u65f6\uff0c\u811a\u672c\u6267\u884c\uff0c\u4ece\u800c\u7a83\u53d6Cookie\u3001\u4f1a\u8bdd\u4ee4\u724c\u6216\u8fdb\u884c\u5176\u4ed6\u6076\u610f\u64cd\u4f5c\u3002XSS\u5206\u4e3a\u4e09\u7c7b\uff1a\u5b58\u50a8\u578b\u3001\u53cd\u5c04\u578b\u548cDOM\u578b\u3002<\/p>\n<p><strong>\u5b58\u50a8\u578bXSS<\/strong>\uff1a\u6076\u610f\u4ee3\u7801\u88ab\u5b58\u50a8\u5230\u670d\u52a1\u5668\uff08\u5982\u8bc4\u8bba\u3001\u7528\u6237\u8d44\u6599\uff09\uff0c\u6240\u6709\u8bbf\u95ee\u8be5\u9875\u9762\u7684\u7528\u6237\u90fd\u4f1a\u4e2d\u62db\u3002 <strong>\u53cd\u5c04\u578bXSS<\/strong>\uff1a\u6076\u610f\u4ee3\u7801\u901a\u8fc7URL\u53c2\u6570\u7b49\u65b9\u5f0f\u8bf1\u5bfc\u7528\u6237\u70b9\u51fb\uff0c\u670d\u52a1\u5668\u672a\u8fc7\u6ee4\u76f4\u63a5\u8fd4\u56de\u3002 <strong>DOM\u578bXSS<\/strong>\uff1a\u524d\u7aefJavaScript\u76f4\u63a5\u5c06\u4e0d\u53ef\u4fe1\u6570\u636e\u63d2\u5165DOM\uff0c\u4e0d\u7ecf\u8fc7\u670d\u52a1\u5668\u3002<\/p>\n<h3>1.2 \u9632\u5fa1\u6838\u5fc3\u539f\u5219\uff1a\u8f93\u5165\u8fc7\u6ee4 + \u8f93\u51fa\u7f16\u7801<\/h3>\n<p>\u9632\u5fa1XSS\u7684\u6838\u5fc3\u662f\u201c\u6c38\u8fdc\u4e0d\u8981\u4fe1\u4efb\u7528\u6237\u8f93\u5165\u201d\uff0c\u5e76\u5728\u8f93\u51fa\u65f6\u8fdb\u884c\u6b63\u786e\u7684\u7f16\u7801\u3002<\/p>\n<h4>1.2.1 \u8f93\u5165\u8fc7\u6ee4<\/h4>\n<p>\u5bf9\u4e8e\u8f93\u5165\uff0c\u6211\u4eec\u5e94\u8be5\u8fdb\u884c\u4e25\u683c\u7684\u9a8c\u8bc1\u548c\u8fc7\u6ee4\uff0c\u4f8b\u5982\u4f7f\u7528\u767d\u540d\u5355\u673a\u5236\u3002\u4f46\u6ce8\u610f\uff0c\u8f93\u5165\u8fc7\u6ee4\u4e0d\u80fd\u66ff\u4ee3\u8f93\u51fa\u7f16\u7801\uff0c\u56e0\u4e3a\u6570\u636e\u53ef\u80fd\u6765\u81ea\u591a\u79cd\u6e20\u9053\u3002<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ \u4f7f\u7528 validator \u5e93\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\nconst validator = require(&#x27;validator&#x27;);\n\nfunction sanitizeInput(input) {\n  \/\/ \u8f6c\u4e49HTML\u7279\u6b8a\u5b57\u7b26\n  return validator.escape(input);\n}\n\n\/\/ \u793a\u4f8b\uff1a\u7528\u6237\u63d0\u4ea4\u7684\u8bc4\u8bba\nconst userComment = &#x27;&lt;script&gt;alert(1)&lt;\/script&gt;&#x27;;\nconst safeComment = sanitizeInput(userComment);\nconsole.log(safeComment); \/\/ &amp;lt;script&amp;gt;alert(1)&amp;lt;\/script&amp;gt;\n<\/code><\/pre>\n<h4>1.2.2 \u8f93\u51fa\u7f16\u7801<\/h4>\n<p>\u8f93\u51fa\u7f16\u7801\u662f\u9632\u5fa1XSS\u7684\u6700\u540e\u4e00\u9053\u9632\u7ebf\u3002\u6839\u636e\u8f93\u51fa\u4f4d\u7f6e\uff08HTML\u3001JavaScript\u3001CSS\u3001URL\uff09\u91c7\u7528\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u5f0f\u3002<\/p>\n<p><strong>HTML\u4e0a\u4e0b\u6587<\/strong>\uff1a\u4f7f\u7528HTML\u5b9e\u4f53\u7f16\u7801\u3002<\/p>\n<pre><code class=\"language-javascript\">\nfunction escapeHtml(unsafe) {\n  return unsafe\n    .replace(\/&amp;\/g, &quot;&amp;amp;&quot;)\n    .replace(\/&lt;\/g, &quot;&amp;lt;&quot;)\n    .replace(\/&gt;\/g, &quot;&amp;gt;&quot;)\n    .replace(\/&quot;\/g, &quot;&amp;quot;&quot;)\n    .replace(\/&#x27;\/g, &quot;&amp;#039;&quot;);\n}\n\n\/\/ \u5728\u6e32\u67d3\u65f6\u4f7f\u7528\ndocument.getElementById(&#x27;output&#x27;).innerHTML = escapeHtml(userInput);\n<\/code><\/pre>\n<p><strong>JavaScript\u4e0a\u4e0b\u6587<\/strong>\uff1a\u4f7f\u7528JSON\u5e8f\u5217\u5316\u3002<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ \u4e0d\u5b89\u5168\uff1a\u76f4\u63a5\u62fc\u63a5\n\/\/ var data = &#x27;&lt;%= userData %&gt;&#x27;;\n\n\/\/ \u5b89\u5168\uff1a\u4f7f\u7528JSON.stringify\nvar data = JSON.parse(&#x27;&lt;%- JSON.stringify(userData) %&gt;&#x27;);\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u5728HTML\u5c5e\u6027\u4e2d\uff0c\u9664\u4e86\u7f16\u7801\u5916\uff0c\u8fd8\u8981\u4f7f\u7528\u5f15\u53f7\u5305\u88f9\u5c5e\u6027\u503c\uff0c\u907f\u514d\u5c5e\u6027\u9003\u9038\u3002<\/p>\n<h4>1.2.3 \u4f7f\u7528CSP\uff08\u5185\u5bb9\u5b89\u5168\u7b56\u7565\uff09<\/h4>\n<p>CSP\u662f\u9632\u5fa1XSS\u7684\u5f3a\u529b\u6b66\u5668\uff0c\u5b83\u901a\u8fc7\u9650\u5236\u6d4f\u89c8\u5668\u53ef\u4ee5\u52a0\u8f7d\u548c\u6267\u884c\u7684\u8d44\u6e90\u6765\u6e90\uff0c\u6709\u6548\u7f13\u89e3XSS\u653b\u51fb\u3002<\/p>\n<pre><code class=\"language-http\">\nContent-Security-Policy: default-src &#x27;self&#x27;; script-src &#x27;self&#x27; https:\/\/trusted.cdn.com; style-src &#x27;self&#x27; &#x27;unsafe-inline&#x27;; img-src &#x27;self&#x27; data:;\n<\/code><\/pre>\n<p>\u5728Express\u4e2d\u8bbe\u7f6eCSP\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst helmet = require(&#x27;helmet&#x27;);\napp.use(helmet.contentSecurityPolicy({\n  directives: {\n    defaultSrc: [&quot;&#x27;self&#x27;&quot;],\n    scriptSrc: [&quot;&#x27;self&#x27;&quot;, &quot;https:\/\/trusted.cdn.com&quot;],\n    styleSrc: [&quot;&#x27;self&#x27;&quot;, &quot;&#x27;unsafe-inline&#x27;&quot;],\n    imgSrc: [&quot;&#x27;self&#x27;&quot;, &quot;data:&quot;],\n  }\n}));\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u8e29\u5751\u7ecf\u9a8c<\/strong>\uff1aCSP\u4e2d\u7684<code>'unsafe-inline'<\/code>\u4f1a\u524a\u5f31\u9632\u62a4\uff0c\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u3002\u5982\u679c\u5fc5\u987b\u4f7f\u7528\u5185\u8054\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528nonce\u6216hash\u3002<\/p>\n<h4>1.2.4 \u73b0\u4ee3\u6846\u67b6\u7684\u81ea\u52a8\u5316\u9632\u5fa1<\/h4>\n<p>React\u3001Vue\u7b49\u6846\u67b6\u9ed8\u8ba4\u5bf9\u63d2\u503c\u8fdb\u884c\u8f6c\u4e49\uff0c\u4f46\u4ecd\u6709\u5371\u9669\u533a\u57df\uff1a<\/p>\n<ul>\n<li>React\u7684<code>dangerouslySetInnerHTML<\/code><\/li>\n<li>Vue\u7684<code>v-html<\/code><\/li>\n<li>\u76f4\u63a5\u64cd\u4f5cDOM\u7684<code>innerHTML<\/code><\/li>\n<\/ul>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u4f7f\u7528<code>DOMPurify<\/code>\u5e93\u51c0\u5316HTML\u3002<\/p>\n<pre><code class=\"language-javascript\">\nimport DOMPurify from &#x27;dompurify&#x27;;\n\nconst clean = DOMPurify.sanitize(dirtyHtml);\n\/\/ \u7136\u540e\u5b89\u5168\u5730\u63d2\u5165\n<\/code><\/pre>\n<h2>\u4e8c\u3001CSRF\u653b\u51fb\u4e0e\u9632\u5fa1<\/h2>\n<h3>2.1 CSRF\u662f\u4ec0\u4e48\uff1f<\/h3>\n<p>CSRF\uff08\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff09\u662f\u6307\u653b\u51fb\u8005\u8bf1\u5bfc\u7528\u6237\u8bbf\u95ee\u6076\u610f\u9875\u9762\uff0c\u8be5\u9875\u9762\u81ea\u52a8\u5411\u7528\u6237\u5df2\u767b\u5f55\u7684\u7f51\u7ad9\u53d1\u9001\u8bf7\u6c42\uff0c\u5229\u7528\u7528\u6237\u7684\u8eab\u4efd\u6267\u884c\u975e\u9884\u671f\u7684\u64cd\u4f5c\uff08\u5982\u8f6c\u8d26\u3001\u6539\u5bc6\u7801\uff09\u3002<\/p>\n<p><strong>\u5178\u578b\u573a\u666f<\/strong>\uff1a\u7528\u6237\u767b\u5f55\u4e86\u94f6\u884c\u7f51\u7ad9A\uff0c\u672a\u9000\u51fa\uff1b\u8bbf\u95ee\u4e86\u6076\u610f\u7f51\u7ad9B\uff0cB\u4e2d\u6709\u4e00\u4e2a\u9690\u85cf\u7684\u8868\u5355\u81ea\u52a8\u5411A\u63d0\u4ea4\u8f6c\u8d26\u8bf7\u6c42\uff0c\u6d4f\u89c8\u5668\u4f1a\u81ea\u52a8\u5e26\u4e0aA\u7684Cookie\uff0c\u5bfc\u81f4\u8f6c\u8d26\u6210\u529f\u3002<\/p>\n<h3>2.2 \u9632\u5fa1\u6838\u5fc3\uff1a\u9a8c\u8bc1\u8bf7\u6c42\u6765\u6e90<\/h3>\n<p>CSRF\u9632\u5fa1\u7684\u6838\u5fc3\u662f\u786e\u4fdd\u8bf7\u6c42\u662f\u7528\u6237\u81ea\u613f\u53d1\u8d77\u7684\u3002\u5e38\u7528\u65b9\u6cd5\u6709\uff1a<\/p>\n<ul>\n<li><strong>CSRF Token<\/strong>\uff1a\u5728\u8868\u5355\u548c\u8bf7\u6c42\u5934\u4e2d\u52a0\u5165\u968f\u673a\u4ee4\u724c\uff0c\u670d\u52a1\u5668\u9a8c\u8bc1\u3002<\/li>\n<li><strong>SameSite Cookie<\/strong>\uff1a\u9650\u5236Cookie\u5728\u8de8\u7ad9\u8bf7\u6c42\u4e2d\u53d1\u9001\u3002<\/li>\n<li><strong>\u9a8c\u8bc1Referer\/Origin<\/strong>\uff1a\u68c0\u67e5\u8bf7\u6c42\u6765\u6e90\u3002<\/li>\n<\/ul>\n<h4>2.2.1 CSRF Token\u5b9e\u6218<\/h4>\n<p>\u4ee5Express + express-session\u4e3a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst csrf = require(&#x27;csurf&#x27;);\nconst cookieParser = require(&#x27;cookie-parser&#x27;);\n\napp.use(cookieParser());\napp.use(csrf({ cookie: true }));\n\n\/\/ \u5728\u6e32\u67d3\u8868\u5355\u65f6\u6ce8\u5165token\napp.get(&#x27;\/form&#x27;, (req, res) =&gt; {\n  res.send(`\n    &lt;form action=&quot;\/transfer&quot; method=&quot;POST&quot;&gt;\n      &lt;input type=&quot;hidden&quot; name=&quot;_csrf&quot; value=&quot;${req.csrfToken()}&quot;&gt;\n      &lt;input type=&quot;text&quot; name=&quot;amount&quot;&gt;\n      &lt;button type=&quot;submit&quot;&gt;\u8f6c\u8d26&lt;\/button&gt;\n    &lt;\/form&gt;\n  `);\n});\n\n\/\/ \u9a8c\u8bc1token\napp.post(&#x27;\/transfer&#x27;, (req, res) =&gt; {\n  \/\/ csurf\u4e2d\u95f4\u4ef6\u4f1a\u81ea\u52a8\u9a8c\u8bc1req.body._csrf\u6216req.headers[&#x27;csrf-token&#x27;]\n  res.send(&#x27;\u8f6c\u8d26\u6210\u529f&#x27;);\n});\n<\/code><\/pre>\n<p>\u5bf9\u4e8eAJAX\u8bf7\u6c42\uff0c\u53ef\u4ee5\u5c06token\u653e\u5728\u8bf7\u6c42\u5934\u4e2d\uff1a<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ \u4ecemeta\u6807\u7b7e\u83b7\u53d6token\nconst csrfToken = document.querySelector(&#x27;meta[name=&quot;csrf-token&quot;]&#x27;).content;\n\nfetch(&#x27;\/api\/transfer&#x27;, {\n  method: &#x27;POST&#x27;,\n  headers: {\n    &#x27;Content-Type&#x27;: &#x27;application\/json&#x27;,\n    &#x27;CSRF-Token&#x27;: csrfToken\n  },\n  body: JSON.stringify({ amount: 100 })\n});\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1aCSRF Token\u5fc5\u987b\u4e0e\u7528\u6237\u4f1a\u8bdd\u7ed1\u5b9a\uff0c\u4e14\u6bcf\u6b21\u8bf7\u6c42\u540e\u53ef\u4ee5\u66f4\u65b0\uff08\u4f46\u8981\u6ce8\u610f\u5e76\u53d1\u95ee\u9898\uff09\u3002<\/p>\n<h4>2.2.2 SameSite Cookie<\/h4>\n<p>\u8bbe\u7f6eCookie\u7684SameSite\u5c5e\u6027\u4e3a<code>Lax<\/code>\u6216<code>Strict<\/code>\uff0c\u53ef\u4ee5\u963b\u6b62\u8de8\u7ad9\u8bf7\u6c42\u643a\u5e26Cookie\u3002<\/p>\n<pre><code class=\"language-javascript\">\napp.use(session({\n  cookie: {\n    sameSite: &#x27;lax&#x27;, \/\/ \u6216 &#x27;strict&#x27;\n    secure: true, \/\/ \u4ec5HTTPS\n    httpOnly: true \/\/ \u9632\u6b62XSS\u7a83\u53d6\n  }\n}));\n<\/code><\/pre>\n<p><strong>SameSite\u7684\u5c40\u9650<\/strong>\uff1aLax\u6a21\u5f0f\u4e0b\uff0c\u9876\u7ea7\u5bfc\u822a\u7684GET\u8bf7\u6c42\u4ecd\u4f1a\u643a\u5e26Cookie\uff0c\u56e0\u6b64\u5bf9\u4e8e\u654f\u611f\u64cd\u4f5c\uff0c\u5e94\u4f7f\u7528POST\u5e76\u914d\u5408CSRF Token\u3002<\/p>\n<h4>2.2.3 \u9a8c\u8bc1Origin\/Referer<\/h4>\n<p>\u4f5c\u4e3a\u8865\u5145\uff0c\u53ef\u4ee5\u9a8c\u8bc1\u8bf7\u6c42\u5934\u4e2d\u7684Origin\u6216Referer\u3002<\/p>\n<pre><code class=\"language-javascript\">\nfunction checkOrigin(req, res, next) {\n  const origin = req.headers.origin || req.headers.referer;\n  if (!origin || !origin.startsWith(&#x27;https:\/\/yourdomain.com&#x27;)) {\n    return res.status(403).send(&#x27;Forbidden&#x27;);\n  }\n  next();\n}\n<\/code><\/pre>\n<h2>\u4e09\u3001\u7efc\u5408\u5b9e\u6218\uff1a\u6784\u5efa\u5b89\u5168\u7684\u767b\u5f55\u4e0e\u8f6c\u8d26\u6d41\u7a0b<\/h2>\n<p>\u4e0b\u9762\u6211\u4eec\u7ed3\u5408XSS\u548cCSRF\u9632\u5fa1\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u8f6c\u8d26\u529f\u80fd\u3002<\/p>\n<h3>3.1 \u540e\u7aef\u4ee3\u7801\uff08Express\uff09<\/h3>\n<pre><code class=\"language-javascript\">\nconst express = require(&#x27;express&#x27;);\nconst session = require(&#x27;express-session&#x27;);\nconst csrf = require(&#x27;csurf&#x27;);\nconst helmet = require(&#x27;helmet&#x27;);\nconst cookieParser = require(&#x27;cookie-parser&#x27;);\n\nconst app = express();\napp.use(express.urlencoded({ extended: true }));\napp.use(express.json());\napp.use(cookieParser());\n\n\/\/ \u5b89\u5168\u5934\napp.use(helmet());\napp.use(helmet.contentSecurityPolicy({\n  directives: {\n    defaultSrc: [&quot;&#x27;self&#x27;&quot;],\n    scriptSrc: [&quot;&#x27;self&#x27;&quot;],\n  }\n}));\n\n\/\/ \u4f1a\u8bdd\napp.use(session({\n  secret: &#x27;your-secret-key&#x27;,\n  resave: false,\n  saveUninitialized: true,\n  cookie: { sameSite: &#x27;lax&#x27;, secure: false, httpOnly: true } \/\/ \u751f\u4ea7\u73af\u5883secure\u5e94\u4e3atrue\n}));\n\n\/\/ CSRF\u4fdd\u62a4\napp.use(csrf({ cookie: true }));\n\n\/\/ \u6a21\u62df\u7528\u6237\u767b\u5f55\napp.post(&#x27;\/login&#x27;, (req, res) =&gt; {\n  req.session.user = { id: 1, name: &#x27;Alice&#x27;, balance: 1000 };\n  res.redirect(&#x27;\/&#x27;);\n});\n\n\/\/ \u9996\u9875\uff0c\u663e\u793a\u8f6c\u8d26\u8868\u5355\napp.get(&#x27;\/&#x27;, (req, res) =&gt; {\n  if (!req.session.user) return res.redirect(&#x27;\/login&#x27;);\n  const user = req.session.user;\n  res.send(`\n    &lt;!DOCTYPE html&gt;\n    &lt;html&gt;\n    &lt;head&gt;\n      &lt;meta charset=&quot;utf-8&quot;&gt;\n      &lt;meta name=&quot;csrf-token&quot; content=&quot;${req.csrfToken()}&quot;&gt;\n      &lt;title&gt;\u8f6c\u8d26&lt;\/title&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n      &lt;h1&gt;\u6b22\u8fce, ${user.name}&lt;\/h1&gt;\n      &lt;p&gt;\u4f59\u989d: ${user.balance}&lt;\/p&gt;\n      &lt;form action=&quot;\/transfer&quot; method=&quot;POST&quot;&gt;\n        &lt;input type=&quot;hidden&quot; name=&quot;_csrf&quot; value=&quot;${req.csrfToken()}&quot;&gt;\n        &lt;input type=&quot;text&quot; name=&quot;to&quot; placeholder=&quot;\u6536\u6b3e\u4eba&quot;&gt;\n        &lt;input type=&quot;number&quot; name=&quot;amount&quot; placeholder=&quot;\u91d1\u989d&quot;&gt;\n        &lt;button type=&quot;submit&quot;&gt;\u8f6c\u8d26&lt;\/button&gt;\n      &lt;\/form&gt;\n      &lt;script src=&quot;\/app.js&quot;&gt;&lt;\/script&gt;\n    &lt;\/body&gt;\n    &lt;\/html&gt;\n  `);\n});\n\n\/\/ \u5904\u7406\u8f6c\u8d26\napp.post(&#x27;\/transfer&#x27;, (req, res) =&gt; {\n  if (!req.session.user) return res.status(401).send(&#x27;\u672a\u767b\u5f55&#x27;);\n  const { to, amount } = req.body;\n  \/\/ \u7b80\u5355\u9a8c\u8bc1\n  if (!to || !amount || amount &lt;= 0) return res.status(400).send(&#x27;\u53c2\u6570\u9519\u8bef&#x27;);\n  \/\/ \u6a21\u62df\u8f6c\u8d26\n  req.session.user.balance -= amount;\n  res.send(`\u8f6c\u8d26\u6210\u529f\uff0c\u4f59\u989d: ${req.session.user.balance}`);\n});\n\napp.listen(3000, () =&gt; console.log(&#x27;Server running on port 3000&#x27;));\n<\/code><\/pre>\n<h3>3.2 \u524d\u7aefAJAX\u8bf7\u6c42\uff08app.js\uff09<\/h3>\n<pre><code class=\"language-javascript\">\n\/\/ \u4ecemeta\u6807\u7b7e\u83b7\u53d6CSRF Token\nconst csrfToken = document.querySelector(&#x27;meta[name=&quot;csrf-token&quot;]&#x27;).content;\n\n\/\/ \u793a\u4f8b\uff1a\u4f7f\u7528fetch\u8fdb\u884c\u8f6c\u8d26\nasync function transfer(to, amount) {\n  const response = await fetch(&#x27;\/transfer&#x27;, {\n    method: &#x27;POST&#x27;,\n    headers: {\n      &#x27;Content-Type&#x27;: &#x27;application\/json&#x27;,\n      &#x27;CSRF-Token&#x27;: csrfToken\n    },\n    body: JSON.stringify({ to, amount })\n  });\n  return response.text();\n}\n\n\/\/ \u8c03\u7528\ntransfer(&#x27;Bob&#x27;, 100).then(console.log);\n<\/code><\/pre>\n<h2>\u56db\u3001\u5e38\u89c1\u5751\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<ol>\n<li><strong>\u4e0d\u8981\u53ea\u4f9d\u8d56\u524d\u7aef\u9a8c\u8bc1<\/strong>\uff1a\u6240\u6709\u5b89\u5168\u6821\u9a8c\u5fc5\u987b\u5728\u540e\u7aef\u8fdb\u884c\u3002<\/li>\n<li><strong>HttpOnly\u548cSecure Cookie<\/strong>\uff1a\u9632\u6b62XSS\u7a83\u53d6Cookie\uff0c\u5e76\u786e\u4fddCookie\u4ec5\u901a\u8fc7HTTPS\u4f20\u8f93\u3002<\/li>\n<li><strong>\u5b9a\u671f\u66f4\u65b0\u4f9d\u8d56<\/strong>\uff1a\u4f7f\u7528<code>npm audit<\/code>\u68c0\u67e5\u6f0f\u6d1e\u3002<\/li>\n<li><strong>CSP\u62a5\u544a<\/strong>\uff1a\u4f7f\u7528<code>report-uri<\/code>\u6536\u96c6\u8fdd\u89c4\u62a5\u544a\uff0c\u9010\u6b65\u5b8c\u5584\u7b56\u7565\u3002<\/li>\n<li><strong>\u654f\u611f\u64cd\u4f5c\u4e8c\u6b21\u9a8c\u8bc1<\/strong>\uff1a\u5982\u8f6c\u8d26\u3001\u6539\u5bc6\u7801\uff0c\u8981\u6c42\u8f93\u5165\u5bc6\u7801\u6216\u9a8c\u8bc1\u7801\u3002<\/li>\n<li><strong>\u907f\u514d\u4f7f\u7528eval\u548cinnerHTML<\/strong>\uff1a\u4f7f\u7528\u66f4\u5b89\u5168\u7684\u66ff\u4ee3\u65b9\u6848\u3002<\/li>\n<\/ol>\n<h2>\u603b\u7ed3<\/h2>\n<p>XSS\u548cCSRF\u662fWeb\u5b89\u5168\u7684\u57fa\u7840\u5a01\u80c1\uff0c\u4f46\u901a\u8fc7\u6b63\u786e\u7684\u7f16\u7801\u3001CSP\u3001CSRF Token\u548cSameSite Cookie\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u9632\u5fa1\u3002\u5b89\u5168\u662f\u4e00\u4e2a\u6301\u7eed\u7684\u8fc7\u7a0b\uff0c\u9700\u8981\u4e0d\u65ad\u5b66\u4e60\u548c\u66f4\u65b0\u3002\u5e0c\u671b\u672c\u6587\u7684\u5b9e\u6218\u4ee3\u7801\u80fd\u5e2e\u52a9\u4f60\u5728\u9879\u76ee\u4e2d\u843d\u5730\u8fd9\u4e9b\u9632\u5fa1\u63aa\u65bd\u3002<\/p>\n<p><strong>\u5ef6\u4f38\u9605\u8bfb<\/strong>\uff1a<\/p>\n<ul>\n<li>OWASP XSS\u9632\u62a4\u901f\u67e5\u8868<\/li>\n<li>OWASP CSRF\u9632\u62a4\u901f\u67e5\u8868<\/li>\n<li>Content Security Policy Level 3\u89c4\u8303<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u5256\u6790XSS\u4e0eCSRF\u7684\u653b\u51fb\u539f\u7406\uff0c\u63d0\u4f9b\u4ece\u8f93\u5165\u8fc7\u6ee4\u3001\u8f93\u51fa\u7f16\u7801\u5230CSP\u3001SameSite Cookie\u7684\u5b8c\u6574\u9632\u5fa1\u65b9\u6848\uff0c\u5e76\u5206\u4eab\u5b9e\u6218\u4e2d\u7684\u8e29\u5751\u7ecf\u9a8c\u4e0e\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9\u4f60\u5728\u524d\u540e\u7aef\u5f00\u53d1\u4e2d\u6784\u5efa\u575a\u56fa\u7684\u5b89\u5168\u9632\u7ebf\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[178,44,181,207,206],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-csrf","tag-express","tag-web","tag-xss","tag-206"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}