Skip to content

Commit 706dee9

Browse files
authored
Merge pull request #15160 from github/max-schaefer/csharp-xss
C#: Mention more XSS sanitisation options in query help.
2 parents 1b9f59e + fea6926 commit 706dee9

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

csharp/ql/src/Security Features/CWE-079/XSS.qhelp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,24 @@ without properly sanitizing the input first, allows for a cross-site scripting v
1111
</overview>
1212
<recommendation>
1313

14-
<p>To guard against cross-site scripting, consider using contextual output encoding/escaping before
15-
writing user input to the page, or one of the other solutions that are mentioned in the
16-
references.</p>
14+
<p>
15+
To guard against cross-site scripting, consider using a library that provides suitable encoding
16+
functionality, such as the <code>System.Net.WebUtility</code> class, to sanitize the untrusted input before writing it to the page.
17+
For other possible solutions, see the references.
18+
</p>
1719

1820
</recommendation>
1921
<example>
2022

21-
<p>The following example shows the page parameter being written directly to the server error page,
22-
leaving the website vulnerable to cross-site scripting.</p>
23-
24-
<sample src="XSS.cs" />
23+
<p>
24+
The following example shows the page parameter being written directly to the server error page,
25+
leaving the website vulnerable to cross-site scripting.
26+
</p>
27+
<sample src="XSSBad.cs" />
28+
<p>
29+
Sanitizing the user-controlled data using the <code>WebUtility.HtmlEncode</code> method prevents the vulnerability:
30+
</p>
31+
<sample src="XSSGood.cs" />
2532

2633
</example>
2734
<references>
@@ -36,6 +43,5 @@ OWASP:
3643
Wikipedia: <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross-site scripting</a>.
3744
</li>
3845

39-
4046
</references>
4147
</qhelp>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Web;
3+
using System.Net;
4+
5+
public class XSSHandler : IHttpHandler
6+
{
7+
public void ProcessRequest(HttpContext ctx)
8+
{
9+
string page = WebUtility.HtmlEncode(ctx.Request.QueryString["page"]);
10+
ctx.Response.Write(
11+
"The page \"" + page + "\" was not found.");
12+
}
13+
}

0 commit comments

Comments
 (0)