{"id":558,"date":"2009-08-01T17:44:00","date_gmt":"2009-08-01T09:44:00","guid":{"rendered":""},"modified":"2013-11-17T12:57:55","modified_gmt":"2013-11-17T04:57:55","slug":"%e4%bb%8e%e5%ae%a2%e6%88%b7%e7%ab%af%e6%a3%80%e6%b5%8b%e5%88%b0%e6%9c%89%e6%bd%9c%e5%9c%a8%e5%8d%b1%e9%99%a9%e7%9a%84request-form-%e5%80%bc","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/558.html","title":{"rendered":"\u4ece\u5ba2\u6237\u7aef\u68c0\u6d4b\u5230\u6709\u6f5c\u5728\u5371\u9669\u7684Request.Form \u503c"},"content":{"rendered":"<p>asp.net\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u9047\u5230\u201c\u4ece\u5ba2\u6237\u7aef\u68c0\u6d4b\u5230\u6709\u6f5c\u5728\u5371\u9669\u7684Request.Form \u503c\u201d\u9519\u8bef\u63d0\u793a\uff0c\u5f88\u591a\u4eba\u7ed9\u51fa\u7684\u89e3\u51b3\u65b9\u6848\u662f\uff1a<\/p>\n<p>1\u3001web.config\u6587\u6863<system.web>\u540e\u9762\u52a0\u5165\u8fd9\u4e00\u53e5\uff1a <pages validaterequest=\"false\"\/>\n\u793a\u4f8b\uff1a <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;gb2312&quot; ?&gt; \r\n&lt;configuration&gt; \r\n&lt;system.web&gt; \r\n&lt;pages validaterequest=&quot;false&quot;\/&gt; \r\n&lt;\/system.web&gt; \r\n&lt;\/configuration&gt;\r\n<\/pre>\n<p>2\u3001\u5728*.aspx\u6587\u6863\u5934\u7684page\u4e2d\u52a0\u5165validaterequest=&#8221;false&#8221;,\u793a\u4f8b\u5982\u4e0b\uff1a <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&lt;%@ page validaterequest=&quot;false&quot; language=&quot;c#&quot; codebehind=&quot;index.aspx.cs&quot; autoeventwireup=&quot;false&quot; inherits=&quot;mybbs.webform1&quot; %&gt; \r\n<\/pre>\n<p>\u5176\u5b9e\u8fd9\u6837\u505a\u662f\u4e0d\u6b63\u786e\u7684\uff0c\u4f1a\u7ed9\u7a0b\u5e8f\u5b89\u5168\u5e26\u6765\u98ce\u9669\u3002<\/p>\n<p>ASP.Net 1.1\u540e\u5f15\u5165\u4e86\u5bf9\u63d0\u4ea4\u8868\u5355\u81ea\u52a8\u68c0\u67e5\u662f\u5426\u5b58\u5728XSS(\u8de8\u7ad9\u811a\u672c\u653b\u51fb)\u7684\u80fd\u529b\u3002\u5f53\u7528\u6237\u8bd5\u56fe\u7528\u4e4b\u7c7b\u7684\u8f93\u5165\u5f71\u54cd\u9875\u9762\u8fd4\u56de\u7ed3\u679c\u7684\u65f6\u5019\uff0cASP.Net\u7684\u5f15\u64ce\u4f1a\u5f15\u53d1\u4e00\u4e2a HttpRequestValidationExceptioin\u3002\u8fd9\u662fASP.Net\u63d0\u4f9b\u7684\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u5b89\u5168\u7279\u6027\u3002\u56e0\u4e3a\u5f88\u591a\u7a0b\u5e8f\u5458\u5bf9\u5b89\u5168\u6ca1\u6709\u6982\u5ff5\uff0c\u751a\u81f3\u90fd\u4e0d\u77e5\u9053XSS\u8fd9\u79cd\u653b\u51fb\u7684\u5b58\u5728\uff0c\u77e5\u9053\u4e3b\u52a8\u53bb\u9632\u62a4\u7684\u5c31\u66f4\u5c11\u4e86\u3002ASP.Net\u5728\u8fd9\u4e00\u70b9\u4e0a\u505a\u5230\u9ed8\u8ba4\u5b89\u5168\u3002\u8fd9\u6837\u8ba9\u5bf9\u5b89\u5168\u4e0d\u662f\u5f88\u4e86\u89e3\u7684\u7a0b\u5e8f\u5458\u4f9d\u65e7\u53ef\u4ee5\u5199\u51fa\u6709\u4e00\u5b9a\u5b89\u5168\u9632\u62a4\u80fd\u529b\u7684\u7f51\u7ad9\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5f53\u6211Google\u641c\u7d22 HttpRequestValidationException \u6216\u8005 &#8220;A potentially dangerous Request.Form value was detected from the client&#8221;\u7684\u65f6\u5019\uff0c\u60ca\u5947\u7684\u53d1\u73b0\u5927\u90e8\u5206\u4eba\u7ed9\u51fa\u7684\u89e3\u51b3\u65b9\u6848\u7adf\u7136\u662f\u5728ASP.Net\u9875\u9762\u63cf\u8ff0\u4e2d\u901a\u8fc7\u8bbe\u7f6e validateRequest=false \u6765\u7981\u7528\u8fd9\u4e2a\u7279\u6027\uff0c\u800c\u4e0d\u53bb\u5173\u5fc3\u90a3\u4e2a\u7a0b\u5e8f\u5458\u7684\u7f51\u7ad9\u662f\u5426\u771f\u7684\u4e0d\u9700\u8981\u8fd9\u4e2a\u7279\u6027\u3002\u770b\u5f97\u6211\u8fd9\u53eb\u4e00\u4e2a\u80c6\u6218\u5fc3\u60ca\u3002\u5b89\u5168\u610f\u8bc6\u5e94\u8be5\u65f6\u65f6\u523b\u523b\u5728\u6bcf\u4e00\u4e2a\u7a0b\u5e8f\u5458\u7684\u5fc3\u91cc\uff0c\u4e0d\u7ba1\u4f60\u5bf9\u5b89\u5168\u7684\u6982\u5ff5\u4e86\u89e3\u591a\u5c11\uff0c\u4e00\u4e2a\u4e3b\u52a8\u7684\u610f\u8bc6\u5728\u8111\u5b50\u91cc\uff0c\u4f60\u7684\u7ad9\u70b9\u5c31\u4f1a\u5b89\u5168\u5f88\u591a\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u5f88\u591a\u7a0b\u5e8f\u5458\u60f3\u8981\u7981\u6b62 validateRequest \u5462?\u6709\u4e00\u90e8\u5206\u662f\u771f\u7684\u9700\u8981\u7528\u6237\u8f93\u5165&#8221;<>&#8220;\u4e4b\u7c7b\u7684\u5b57\u7b26\u3002\u8fd9\u5c31\u4e0d\u5fc5\u8bf4\u4e86\u3002\u8fd8\u6709\u4e00\u90e8\u5206\u5176\u5b9e\u5e76\u4e0d\u662f\u7528\u6237\u5141\u8bb8\u8f93\u5165\u90a3\u4e9b\u5bb9\u6613\u5f15\u8d77XSS\u7684\u5b57\u7b26\uff0c\u800c\u662f\u8ba8\u538c\u8fd9\u79cd\u62a5\u9519\u7684\u5f62\u5f0f\uff0c\u6bd5\u7adf\u4e00\u5927\u6bb5\u82f1\u6587\u52a0\u4e0a\u4e00\u4e2aASP.Net\u5178\u578b\u5f02\u5e38\u9519\u8bef\u4fe1\u606f\uff0c\u663e\u5f97\u8fd9\u4e2a\u7ad9\u70b9\u51fa\u9519\u4e86\uff0c\u800c\u4e0d\u662f\u7528\u6237\u8f93\u5165\u4e86\u975e\u6cd5\u7684\u5b57\u7b26\uff0c\u53ef\u662f\u81ea\u5df1\u53c8\u4e0d\u77e5\u9053\u600e\u4e48\u4e0d\u8ba9\u5b83\u62a5\u9519\uff0c\u81ea\u5df1\u6765\u5904\u7406\u62a5\u9519\u3002<\/p>\n<p>\u5bf9\u4e8e\u5e0c\u671b\u5f88\u597d\u7684\u5904\u7406\u8fd9\u4e2a\u9519\u8bef\u4fe1\u606f\uff0c\u800c\u4e0d\u4f7f\u7528\u9ed8\u8ba4ASP.Net\u5f02\u5e38\u62a5\u9519\u4fe1\u606f\u7684\u7a0b\u5e8f\u5458\u4eec\uff0c\u4f60\u4eec\u4e0d\u8981\u7981\u7528validateRequest=false\u3002<\/p>\n<p>\u6b63\u786e\u7684\u505a\u6cd5\u662f\u5728\u4f60\u5f53\u524d\u9875\u9762\u6dfb\u52a0Page_Error()\u51fd\u6570\uff0c\u6765\u6355\u83b7\u6240\u6709\u9875\u9762\u5904\u7406\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u800c\u6ca1\u6709\u5904\u7406\u7684\u5f02\u5e38\u3002\u7136\u540e\u7ed9\u7528\u6237\u4e00\u4e2a\u5408\u6cd5\u7684\u62a5\u9519\u4fe1\u606f\u3002\u5982\u679c\u5f53\u524d\u9875\u9762\u6ca1\u6709Page_Error()\uff0c\u8fd9\u4e2a\u5f02\u5e38\u5c06\u4f1a\u9001\u5230Global.asax\u7684Application_Error()\u6765\u5904\u7406\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728\u90a3\u91cc\u5199\u901a\u7528\u7684\u5f02\u5e38\u62a5\u9519\u5904\u7406\u51fd\u6570\u3002\u5982\u679c\u4e24\u4e2a\u5730\u65b9\u90fd\u6ca1\u6709\u5199\u5f02\u5e38\u5904\u7406\u51fd\u6570\uff0c\u624d\u4f1a\u663e\u793a\u8fd9\u4e2a\u9ed8\u8ba4\u7684\u62a5\u9519\u9875\u9762\u5462\u3002<\/p>\n<p>\u4e3e\u4f8b\u800c\u8a00\uff0c\u5904\u7406\u8fd9\u4e2a\u5f02\u5e38\u5176\u5b9e\u53ea\u9700\u8981\u5f88\u7b80\u77ed\u7684\u4e00\u5c0f\u6bb5\u4ee3\u7801\u5c31\u591f\u4e86\u3002\u5728\u9875\u9762\u7684Code-behind\u9875\u9762\u4e2d\u52a0\u5165\u8fd9\u4e48\u4e00\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprotected void Page_Error(object sender, EventArgs e) \r\n{ \r\nException ex = Server.GetLastError(); \r\nif (HttpContext.Current.Server.GetLastError() is HttpRequestValidationException) \r\n{ \r\nHttpContext.Current.Response.Write(&quot;\u8bf7\u8f93\u5165\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u3010&lt;a href=\\&quot;javascript:history.back(0);\\&quot;&gt;\u8fd4\u56de&lt;\/a&gt;\u3011&quot;); \r\nHttpContext.Current.Server.ClearError(); \r\n} \r\n} \r\n<\/pre>\n<p>\u8fd9\u6837\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u622a\u83b7 HttpRequestValidationException \u5f02\u5e38\uff0c\u800c\u4e14\u53ef\u4ee5\u6309\u7167\u7a0b\u5e8f\u5458\u7684\u610f\u613f\u8fd4\u56de\u4e00\u4e2a\u5408\u7406\u7684\u62a5\u9519\u4fe1\u606f\u3002<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5f88\u7b80\u5355\uff0c\u6240\u4ee5\u6211\u5e0c\u671b\u6240\u6709\u4e0d\u662f\u771f\u7684\u8981\u5141\u8bb8\u7528\u6237\u8f93\u5165\u4e4b\u7c7b\u5b57\u7b26\u7684\u670b\u53cb\uff0c\u5343\u4e07\u4e0d\u8981\u968f\u610f\u7684\u7981\u6b62\u8fd9\u4e2a\u5b89\u5168\u7279\u6027\uff0c\u5982\u679c\u53ea\u662f\u9700\u8981\u5f02\u5e38\u5904\u7406\uff0c\u90a3\u4e48\u8bf7\u7528\u7c7b\u4f3c\u4e8e\u4e0a\u9762\u7684\u4ee3\u7801\u6765\u5904\u7406\u5373\u53ef\u3002<\/p>\n<p>\u800c\u5bf9\u4e8e\u90a3\u4e9b\u901a\u8fc7 \u660e\u786e\u7981\u6b62\u4e86\u8fd9\u4e2a\u7279\u6027\u7684\u7a0b\u5e8f\u5458\uff0c\u81ea\u5df1\u4e00\u5b9a\u8981\u660e\u767d\u81ea\u5df1\u5728\u505a\u4ec0\u4e48\uff0c\u800c\u4e14\u4e00\u5b9a\u8981\u81ea\u5df1\u624b\u52a8\u7684\u68c0\u67e5\u5fc5\u987b\u8fc7\u6ee4\u7684\u5b57\u7b26\u4e32\uff0c\u5426\u5219\u4f60\u7684\u7ad9\u70b9\u5f88\u5bb9\u6613\u5f15\u53d1\u8de8\u7ad9\u811a\u672c\u653b\u51fb\u3002<\/p>\n<p>\u5173\u4e8e\u5b58\u5728Rich Text Editor\u7684\u9875\u9762\u5e94\u8be5\u5982\u4f55\u5904\u7406?<\/p>\n<p>\u5982\u679c\u9875\u9762\u6709\u5bcc\u6587\u672c\u7f16\u8f91\u5668\u7684\u63a7\u4ef6\u7684\uff0c\u90a3\u4e48\u5fc5\u7136\u4f1a\u5bfc\u81f4\u6709\u7c7b\u7684HTML\u6807\u7b7e\u63d0\u4ea4\u56de\u6765\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e0d\u5f97\u4e0d\u5c06validateRequest=&#8221;false&#8221;\u3002\u90a3\u4e48\u5b89\u5168\u6027\u600e\u4e48\u5904\u7406?\u5982\u4f55\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6700\u5927\u9650\u5ea6\u7684\u9884\u9632\u8de8\u7ad9\u811a\u672c\u653b\u51fb\u5462?<\/p>\n<p>\u6839\u636e\u5fae\u8f6f\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u5e94\u8be5\u91c7\u53d6\u5b89\u5168\u4e0a\u79f0\u4e3a\u201c\u9ed8\u8ba4\u7981\u6b62\uff0c\u663e\u5f0f\u5141\u8bb8\u201d\u7684\u7b56\u7565\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u7528 HttpUtility.HtmlEncode()\u6765\u7f16\u7801\uff0c\u5c06\u5176\u4e2d\u7684HTML\u6807\u7b7e\u5f7b\u5e95\u7981\u6b62\u3002<\/p>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u518d\u5bf9\u6211\u4eec\u6240\u611f\u5174\u8da3\u7684\u3001\u5e76\u4e14\u662f\u5b89\u5168\u6807\u7b7e\uff0c\u901a\u8fc7Replace()\u8fdb\u884c\u66ff\u6362\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u5e0c\u671b\u6709&#8221;&#8221;\u6807\u7b7e\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5c06&#8221;&#8221;\u663e\u5f0f\u7684\u66ff\u6362\u56de&#8221;&#8221;\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvoid submitBtn_Click(object sender, EventArgs e) \r\n{ \r\n\/\/\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u7f16\u7801\uff0c\u8fd9\u6837\u6240\u6709\u7684HTML\u6807\u7b7e\u90fd\u5931\u6548\u4e86\u3002 \r\nStringBuilder sb = new StringBuilder(HttpUtility.HtmlEncode(htmlInputTxt.Text)); \r\n\/\/\u7136\u540e\u6211\u4eec\u9009\u62e9\u6027\u7684\u5141\u8bb8&lt;b&gt; \u548c &lt;i&gt; \r\nsb.Replace(&quot;&amp;lt;b&amp;gt;&quot;, &quot;&lt;b&gt;&quot;); \r\nsb.Replace(&quot;&amp;lt;\/b&amp;gt;&quot;, &quot;&lt;\/b&gt;&quot;); \r\nsb.Replace(&quot;&amp;lt;i&amp;gt;&quot;, &quot;&lt;i&gt;&quot;); \r\nsb.Replace(&quot;&amp;lt;\/i&amp;gt;&quot;, &quot;&lt;\/i&gt;&quot;); \r\nResponse.Write(sb.ToString()); \r\n}\r\n<\/pre>\n<p>\u8fd9\u6837\u6211\u4eec\u5373\u5141\u8bb8\u4e86\u90e8\u5206HTML\u6807\u7b7e\uff0c\u53c8\u7981\u6b62\u4e86\u5371\u9669\u7684\u6807\u7b7e\u3002<\/p>\n<p>\u6839\u636e\u5fae\u8f6f\u63d0\u4f9b\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u8981\u614e\u91cd\u5141\u8bb8\u4e0b\u5217HTML\u6807\u7b7e\uff0c\u56e0\u4e3a\u8fd9\u4e9bHTML\u6807\u7b7e\u90fd\u662f\u6709\u53ef\u80fd\u5bfc\u81f4\u8de8\u7ad9\u811a\u672c\u653b\u51fb\u7684\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;applet&gt; \r\n&lt;body&gt; \r\n&lt;embed&gt; \r\n&lt;frame&gt; \r\n&lt;script&gt; \r\n&lt;frameset&gt; \r\n&lt;html&gt; \r\n&lt;iframe&gt; \r\n&lt;img&gt; \r\n&lt;style&gt; \r\n&lt;layer&gt; \r\n&lt;link&gt; \r\n&lt;ilayer&gt; \r\n&lt;meta&gt; \r\n&lt;object&gt;\r\n<\/pre>\n<p>\u53ef\u80fd\u8fd9\u91cc\u6700\u8ba9\u4eba\u4e0d\u80fd\u7406\u89e3\u7684\u662f img \u3002\u4f46\u662f\uff0c\u770b\u8fc7\u4e0b\u5217\u4ee3\u7801\u540e\uff0c\u5c31\u5e94\u8be5\u660e\u767d\u5176\u5371\u9669\u6027\u4e86\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;img src=&quot;javascript:alert(\u2018hello\u2019);&quot;&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>asp.net\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u9047\u5230\u201c\u4ece\u5ba2\u6237\u7aef\u68c0\u6d4b\u5230\u6709\u6f5c\u5728\u5371\u9669\u7684Request.Form \u503c\u201d\u9519\u8bef\u63d0\u793a\uff0c\u5f88\u591a\u4eba\u7ed9\u51fa\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-558","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/558","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=558"}],"version-history":[{"count":2,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/558\/revisions"}],"predecessor-version":[{"id":4611,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/558\/revisions\/4611"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}