【问题标题】:Why potentially dangerous request error even ValidateRequest = false为什么即使 ValidateRequest = false 也会出现潜在危险的请求错误
【发布时间】:2012-01-25 02:03:22
【问题描述】:

这是我的 Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>
<html>
    <head runat="server">
        <title>xss demonstration</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            We are looking for your feedback. 
            <asp:TextBox ID="txtFeedback" runat="server" TextMode="MultiLine" />
            <br />
            <asp:Button ID="submit" runat="server" Text="Submit" onclick="submit_Click" />
            <br />
            Comment: 
            <br />
            <asp:Literal ID="ltlFeedback" runat="server" />
        </div>
        </form>
    </body>
</html>

下面是Default.aspx.cs

public partial class _Default : System.Web.UI.Page 
{
    protected void submit_Click(object sender, EventArgs e)
    {
        this.ltlFeedback.Text = this.txtFeedback.Text;
    }
}

当我运行应用程序并在文本框中输入以下内容时。

<script>alert('Hello')</script>

我收到以下错误。

从 客户端 (txtFeedback="alert('Hello...").

我的问题是为什么即使页面中的 ValidateRequest 设置为 false,我也会收到此错误?

【问题讨论】:

    标签: .net-4.0 asp.net-4.0


    【解决方案1】:

    在 .net framework 4.0 中,您必须在 web.config 中设置 &lt;httpRuntime requestValidationMode="2.0"/&gt; 标记。

    <system.web>
         <compilation debug="false" targetFramework="4.0" />
         <httpRuntime requestValidationMode="2.0"/>
    </system.web>
    

    查看参考文章 - ASP.NET 4 Breaking Changes #1: requestValidationMode cause ValidateRequest=False to fail。

    【讨论】:

      【解决方案2】:

      您只需要在您的网络配置中添加以下代码

         <system.web>
               <compilation debug="false" targetFramework="4.0" />
               <httpRuntime requestValidationMode="2.0"/>
       </system.web>
      

      但要小心,这样做可能会成为跨站脚本攻击的受害者

      【讨论】:

        【解决方案3】:

        在通过 Server.Execute 检索到的任何页面上禁用 ValidateRequest

        我最近遇到了这个错误的一个相当深奥的版本。为了在不同站点的页面之间包含共享内容,我在另一个 ASPX 页面上调用 Server.Execute。默认情况下,Server.Execute 会将 POST 数据转发给子请求,因此该页面也需要ValidateRequest="false"。

        只要设置了web.config 中的&lt;httpRuntime requestValidationMode="2.0"/&gt;,异常的堆栈跟踪就会指出哪个页面类引发了错误。

        【讨论】:

          猜你喜欢
          • 2017-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多