To HTMLENCODE or not to HTMLENCODE user input on web form (asp.net vb)

前端 未结 2 883
野性不改
野性不改 2021-01-28 08:55

I have many params making up an insert form for example:

x.Parameters.AddWithValue(\"@city\", City.Text)

I had a failed xss attack on the site

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-28 09:27

    I strongly recommending looking at the OWASP XSS Prevention Cheat Sheet. It helps classify the different areas of a html document you can inject into, and a recipe for how to encode your output appropriately for each location.

    Know that you can't just universally trust a function like htmlEncode() and expecct it to be a magic pill for all ills. To quote from the OWASP document linked:

    Why Can't I Just HTML Entity Encode Untrusted Data?

    HTML entity encoding is okay for untrusted data that you put in the body of the HTML document, such as inside a tag. It even sort of works for untrusted data that goes into attributes, particularly if you're religious about using quotes around your attributes. But HTML entity encoding doesn't work if you're putting untrusted data inside a tag anywhere, or an event handler attribute like onmouseover, or inside CSS, or in a URL. So even if you use an HTML entity encoding method everywhere, you are still most likely vulnerable to XSS. You MUST use the escape syntax for the part of the HTML document you're putting untrusted data into. That's what the rules below are all about.

    Take time to understand exactly how and why XSS works. Then just follow these 7 rules and you'll be safe.

提交回复
热议问题