Enable asp.net core request validation

后端 未结 1 1818
轮回少年
轮回少年 2020-12-15 18:20

Am I missing something or asp.net core allows to post script tag in user text fields? In Previous versions of asp.net mvc I needed to allow it by [AllowHtml] attribute.

相关标签:
1条回答
  • 2020-12-15 18:44

    ASP.NET Core does not have a feature similar to Request validation, as Microsoft decided, that it’s not a good idea. For more information see the discussion on the ASP.NET Core issue 'Default middleware for request validation, like IIS has'.

    That means that validation has to take place on the inbound model. And that in the Razor (.cshtml) you should output user provided input like @Model.Content, which encodes the given string.

    Please bear in mind that those escaping techniques might not work when the text that was output is not inside a Html part.

    So don't use @Html.Raw(..) unless you know that the data provided has been sanitized.

    Supplement:

    • You might want to consider a Web Application Firewall (WAF) for a generic protection against malicious requests (e.g. XSS or SQL Injection).
    • For protecting your users against an XSS attack you might also have a look at providing a Content Security Policy (CSP).
    0 讨论(0)
提交回复
热议问题