Skip to content

Commit 787b8e5

Browse files
committed
C#: Post-processing query for inline test expectations
1 parent 385e14c commit 787b8e5

File tree

10 files changed

+111
-56
lines changed

10 files changed

+111
-56
lines changed

csharp/ql/test/TestUtilities/InlineExpectationsTest.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Inline expectation tests for CSharp.
2+
* Inline expectation tests for C#.
33
* See `shared/util/codeql/util/test/InlineExpectationsTest.qll`
44
*/
55

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @kind test-postprocess
3+
*/
4+
5+
private import csharp
6+
private import codeql.util.test.InlineExpectationsTest as T
7+
private import internal.InlineExpectationsTestImpl
8+
import T::TestPostProcessing
9+
import T::TestPostProcessing::Make<Impl, Input>
10+
11+
private module Input implements T::TestPostProcessing::InputSig<Impl> {
12+
string getRelativeUrl(Location location) {
13+
exists(File f, int startline, int startcolumn, int endline, int endcolumn |
14+
location.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
15+
f = location.getFile()
16+
|
17+
result =
18+
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
19+
)
20+
}
21+
}

csharp/ql/test/query-tests/Security Features/CWE-079/XSS/Index.cshtml.g.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ public class Pages_Index : global::Microsoft.AspNetCore.Mvc.RazorPages.Page
3030
#line 3 "Index.cshtml"
3131

3232
ViewData["Title"] = "ASP.NET Core";
33-
var message = Request.Query["m"];
33+
var message = Request.Query["m"]; // $ Source=message
3434

3535
#line default
3636
#line hidden
3737
#nullable disable
3838
WriteLiteral("<div class=\"cli\">\n <div class=\"cli-example\"> \n");
3939
#nullable restore
4040
#line 14 "Index.cshtml"
41-
Write(Html.Raw(message)); // BAD
41+
Write(Html.Raw(message)); // $ Alert=message
4242

4343
#line default
4444
#line hidden

csharp/ql/test/query-tests/Security Features/CWE-079/XSS/XSS.expected

Lines changed: 59 additions & 35 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
query: Security Features/CWE-079/XSS.ql
2-
postprocess: TestUtilities/PrettyPrintModels.ql
2+
postprocess:
3+
- TestUtilities/PrettyPrintModels.ql
4+
- TestUtilities/InlineExpectationsTestQuery.ql

csharp/ql/test/query-tests/Security Features/CWE-079/XSS/XSSAspNet.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public override void Execute()
1616
{
1717
Layout = "~/_SiteLayout.cshtml";
1818
Page.Title = "Contact";
19-
var sayHi = Request.QueryString["sayHi"];
19+
var sayHi = Request.QueryString["sayHi"]; // $ Source=sayHi
2020
if (sayHi.IsEmpty())
2121
{
2222
WriteLiteral("<script>alert(\"XSS via WriteLiteral\")</script>"); // GOOD: hard-coded, not user input
2323
}
2424
else
2525
{
26-
WriteLiteral(sayHi); // BAD: user input flows to HTML unencoded
26+
WriteLiteral(sayHi); // $ Alert=sayHi
2727
WriteLiteral(HttpUtility.HtmlEncode(sayHi)); // Good: user input is encoded before it flows to HTML
2828
}
2929

@@ -33,15 +33,16 @@ public override void Execute()
3333
}
3434
else
3535
{
36-
WriteLiteralTo(Output, sayHi); // BAD: user input flows to HTML unencoded
36+
WriteLiteralTo(Output, sayHi); // $ Alert=sayHi
3737
WriteLiteralTo(Output, Html.Encode(sayHi)); // Good: user input is encoded before it flows to HTML
3838
}
3939

4040
BeginContext("~/Views/Home/Contact.cshtml", 288, 32, false);
4141

4242
Write(Html.Raw("<script>alert(\"XSS via Html.Raw()\")</script>")); // GOOD: hard-coded, not user input
43-
Write(Html.Raw(Request.QueryString["sayHi"])); // BAD: user input flows to HTML unencoded
44-
Write(Html.Raw(HttpContext.Current.Server.HtmlEncode(Request.QueryString["sayHi"]))); // Good: user input is encoded before it flows to HTML
43+
var sayHi2 = Request.QueryString["sayHi"]; // $ Source=sayHi2
44+
Write(Html.Raw(sayHi2)); // $ Alert=sayHi2
45+
Write(Html.Raw(HttpContext.Current.Server.HtmlEncode(sayHi2))); // Good: user input is encoded before it flows to HTML
4546
EndContext("~/Views/Home/Contact.cshtml", 288, 32, false);
4647
}
4748
}

csharp/ql/test/query-tests/Security Features/CWE-079/XSS/XSSAspNetCore.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public IActionResult Index()
1818
{
1919
// BAD: flow of content type to.
2020
var v = new ViewResult();
21-
v.ViewData["BadData"] = new HtmlString(Request.Query["Bad data"]);
21+
var source = Request.Query["Bad data"]; // $ Source=req1
22+
v.ViewData["BadData"] = new HtmlString(source); // $ Alert=req1
2223

2324
StringValues vOut;
2425
Request.Query.TryGetValue("Foo", out vOut);
@@ -37,39 +38,44 @@ public IActionResult Index()
3738

3839
[HttpPost("Test")]
3940
[ValidateAntiForgeryToken]
40-
public IActionResult Submit([FromQuery] string foo)
41+
public IActionResult Submit([FromQuery] string foo) // $ Source=foo
4142
{
4243
var view = new ViewResult();
4344
//BAD: flow of submitted value to view in HtmlString.
44-
view.ViewData["FOO"] = new HtmlString(foo);
45+
view.ViewData["FOO"] = new HtmlString(foo); // $ Alert=foo
4546
return view;
4647
}
4748

4849
public IActionResult IndexToModel()
4950
{
5051
//BAD: flow of submitted value to view in HtmlString.
51-
HtmlString v = new HtmlString(Request.QueryString.Value);
52+
var req2 = Request.QueryString.Value; // $ Source=req2
53+
HtmlString v = new HtmlString(req2); // $ Alert=req2
5254
return View(new HomeViewModel() { Message = "Message from Index", Description = v });
5355
}
5456

5557
public IActionResult About()
5658
{
5759
//BAD: flow of submitted value to view in HtmlString.
58-
HtmlString v = new HtmlString(Request.Query["Foo"].ToString());
60+
var req3 = Request.Query["Foo"].ToString(); // $ Source=req3
61+
HtmlString v = new HtmlString(req3); // $ Alert=req3
5962

6063
//BAD: flow of submitted value to view in HtmlString.
61-
HtmlString v1 = new HtmlString(Request.Query["Foo"][0]);
64+
var req4 = Request.Query["Foo"][0]; // $ Source=req4
65+
HtmlString v1 = new HtmlString(req4); // $ Alert=req4
6266

6367
return View(new HomeViewModel() { Message = "Message from About", Description = v });
6468
}
6569

6670
public IActionResult Contact()
6771
{
6872
//BAD: flow of user content type to view in HtmlString.
69-
HtmlString v = new HtmlString(Request.ContentType);
73+
var ct = Request.ContentType; // $ Source=ct
74+
HtmlString v = new HtmlString(ct); // $ Alert=ct
7075

7176
//BAD: flow of headers to view in HtmlString.
72-
HtmlString v1 = new HtmlString(value: Request.Headers["Foo"]);
77+
var header = Request.Headers["Foo"]; // $ Source=header
78+
HtmlString v1 = new HtmlString(value: header); // $ Alert=header
7379

7480
return View(new HomeViewModel() { Message = "Message from Contact", Description = v });
7581
}

csharp/ql/test/query-tests/Useless Code/UnusedLabel/UnusedLabel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ class UnusedLabelTest
33
void F1()
44
{
55
goto a;
6-
a: // GOOD
6+
a: // GOOD
77
;
88
}
99

1010
void F2()
1111
{
12-
a: // BAD
12+
a: // $ Alert
1313
;
1414
}
1515
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| UnusedLabel.cs:12:9:12:9 | a: | This label is not used. |
1+
| UnusedLabel.cs:12:5:12:5 | a: | This label is not used. |
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Useless code/UnusedLabel.ql
1+
query: Useless code/UnusedLabel.ql
2+
postprocess: TestUtilities/InlineExpectationsTestQuery.ql

0 commit comments

Comments
 (0)