【问题标题】:ValidateInputAttribute not working in Post Request of ASP.NET MVC controllerValidateInputAttribute 在 ASP.NET MVC 控制器的发布请求中不起作用
【发布时间】:2014-10-09 04:26:28
【问题描述】:

我的理解是 OOTB,MVC 将验证输入以防止 XSS 攻击和 SQL 注入。

例如,在我的一个应用程序中,当我输入 HTTP Get 请求时,将收到“检测到危险输入”错误。但是,post 操作可以让这些值通过 html 输入元素成功发布而不会出错。即使在我将控制器操作标记为 [ValidateInput(true)] 之后。如何让他们验证这些帖子输入?

任何建议将不胜感激!

【问题讨论】:

    标签: asp.net-mvc security validation xss sql-injection


    【解决方案1】:

    没有看到您的 GET 处理程序,或者您发送给它的内容,很难说出它为什么会这样。但是,OOTB MVC 通过使用实体框架来防范 SQL 注入,并通过 ModelState 验证来防范 XSS。

    在处理此表单提交的 POST 操作的主体中,您需要使用类似于以下的代码:

    if (ModelState.IsValid)
    {
        //do the stuff I want to do when things are valid and free of XSS
    }
    else
    {
        //something went wrong.  Probably shouldn't process this one.  Have the user try again
    }
    

    更新:请忽略我肮脏的谎言。 ValidateInput(true) 不是必需的,因为它默认开启。因此,我能想到的唯一事情是您的类或属性上有 AllowHtml 属性,或者您没有为 modelBinding 发回模型,因此不会发生输入验证。此时,您可能需要提供一些代码以获得进一步的帮助。现在的未知数太多了。

    【讨论】:

    • 我完全同意。那些东西伴侣没有这样的属性。有时间我会进一步调查,稍后通知您。
    【解决方案2】:

    我遇到了类似的问题 - 我们让 JQuery 使用 $.ajax 将 JSON 发布到 MVC 操作。默认模型绑定器不会验证已发布的 JSON,从而允许针对我们的操作发布不安全的 XSS。

    为了解决这个问题,我发现RequestValidator 有一个静态方法InvokeIsValidRequestString 允许

    public class ValidateJsonXssAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var request = filterContext.HttpContext?.Request;
            if (request != null && "application/json".Equals(request.ContentType, StringComparison.OrdinalIgnoreCase))
            {
                if (request.ContentLength > 0 && request.Form.Count == 0) // 
                {
                    if (request.InputStream.Position > 0)
                        request.InputStream.Position = 0; // InputStream has already been read once from "ProcessRequest"
                    using (var reader = new StreamReader(request.InputStream))
                    {
                        var postedContent = reader.ReadToEnd(); // Get posted JSON content
                        var isValid = RequestValidator.Current.InvokeIsValidRequestString(HttpContext.Current, postedContent,
                            RequestValidationSource.Form, "postedJson", out var failureIndex); // Invoke XSS validation
                        if (!isValid) // Not valid, so throw request validation exception
                            throw new HttpRequestValidationException("Potentially unsafe input detected");
                    }
                }
            }
        }
    }
    

    然后,您可以装饰相关的 MVC 操作,期待可能绕过标准 XSS 预防的 JSON 发布数据:

    [HttpPost]
    [ValidateJsonXss]
    public ActionResult PublishRecord(RecordViewModel vm) { ... }
    

    您可以通过扩展 RequestValidator 对象来查看使用 OWASP .NET 建议自定义请求验证的其他选项,该对象公开了由 MVC 自动用于查询字符串、表单集合和 cookie 的其他场景的 ValidateInput 完成的字符串验证价值观。

    欲了解更多信息:https://www.owasp.org/index.php/ASP.NET_Request_Validation

    【讨论】:

      猜你喜欢
      • 2014-04-04
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多