Is escaping < and> sufficient to block XSS attacks?

后端 未结 4 1606
忘掉有多难
忘掉有多难 2021-02-05 12:46

I\'m sure that the answer to this question is No, but I can\'t seem to find a way that simply transforming < and > to < and <

4条回答
  •  一向
    一向 (楼主)
    2021-02-05 13:21

    No, it's not sufficient. Remember that XSS isn't just about untrusted data in HTML, you'll also find it in JavaScript and CSS. Think about a situation such as "var myVar = [input];" There are all sorts of malicious things you can do with that [input] value without going anywhere near angle brackets. There's many more examples over in the XSS cheat sheet: http://ha.ckers.org/xss.html

    You've mentioned ASP.NET in the tag; what you want to be looking at is the [AntiXSS library][1]. Grab this and use the appropriate output encoding:

    Encoder.CssEncode()
    Encoder.HtmlEncode()
    Encoder.HtmlAttributeEncode()
    Encoder.JavaScriptEncode()
    

    etc. etc. There's absolutely no reason to try and do your own character substitution in .NET.

提交回复
热议问题