Skip to content

Commit 7691cbc

Browse files
Add additional test cases
1 parent f84b2a9 commit 7691cbc

File tree

4 files changed

+113
-4
lines changed

4 files changed

+113
-4
lines changed

csharp/ql/lib/semmle/code/csharp/security/dataflow/XSSFlowSteps.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private predicate viewCallRefersToPageAbsolute(ViewCall vc, RazorPage rp) {
9999
}
100100

101101
private predicate viewCallRefersToPageRelative(ViewCall vc, RazorPage rp) {
102-
rp.getSourceFilepath() =
102+
["", "~"] + rp.getSourceFilepath() =
103103
min(int i, RelativeViewCallFilepath fp |
104104
fp.hasViewCallWithIndex(vc, i) and
105105
exists(RazorPage rp2 | rp2.getSourceFilepath() = fp.getNormalizedPath())

csharp/ql/test/query-tests/Security Features/CWE-079/XSSRazorPages/Controllers/TestController.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ public IActionResult test11(UserData tainted11) {
6767
return helper(tainted11);
6868
}
6969

70-
private IActionResult helper(UserData x) {
71-
return View("Test11", x);
72-
}
70+
private IActionResult helper(UserData x) { return View("Test11", x); }
7371

7472
public IActionResult Test12(UserData tainted12) {
7573
// Expected to find nothing.
@@ -79,4 +77,32 @@ public IActionResult Test12(UserData tainted12) {
7977
private IActionResult helper2(UserData x) {
8078
return View(x);
8179
}
80+
81+
private IActionResult test13(UserData tainted13) {
82+
// Expected to find file /Views/Other/Test13.cshtml
83+
return Helper.helper3(this, tainted13);
84+
}
85+
86+
private IActionResult test14(UserData tainted14) {
87+
// Expected to find file /Views/Shared/Test14.cshtml and NOT /Views/Test2/Test14.cshtml
88+
return Helper.helper4(this, tainted14);
89+
}
90+
91+
}
92+
93+
class Helper {
94+
public static IActionResult helper3(Controller c, UserData x) { return c.View("/Views/Other/Test13.cshtml", x); }
95+
96+
public static IActionResult helper4(Controller c, UserData x) { return c.View("Test14", x); }
97+
}
98+
99+
public class Test3Controller : Controller {
100+
public void Setup(RazorViewEngineOptions o) {
101+
o.ViewLocationFormats.Add("/Views/Custom/{1}/{0}.cshtml");
102+
}
103+
104+
private IActionResult Test15(UserData tainted14) {
105+
// Expected to find file /Views/Custom/Test3/Test15.cshtml
106+
return View(x);
107+
}
82108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// A test file that mimics the output of compiling a `.cshtml` file
2+
// <auto-generated/>
3+
#pragma warning disable 1591
4+
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(test.Views.Views_Custom_Test3_Test15), @"mvc.1.0.view", @"/Views/Custom/Test3/Test15.cshtml")]
5+
namespace test.Views
6+
{
7+
#line hidden
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
using System.Threading.Tasks;
12+
using Microsoft.AspNetCore.Mvc;
13+
using Microsoft.AspNetCore.Mvc.Rendering;
14+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
15+
#nullable restore
16+
using test;
17+
18+
#line default
19+
#line hidden
20+
#nullable disable
21+
[global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemMetadataAttribute("Identifier", "/Views/Custom/Test3/Test15.cshtml")]
22+
public class Views_Custom_Test3_Test15 : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<UserData>
23+
{
24+
#pragma warning disable 1998
25+
public async override global::System.Threading.Tasks.Task ExecuteAsync()
26+
{
27+
#line 6 "Views/Custom/Test3/Test15.cshtml"
28+
if (Model != null)
29+
{
30+
31+
#line default
32+
#line hidden
33+
#nullable disable
34+
WriteLiteral(" <h3>Hello \"");
35+
#nullable restore
36+
#line 8 "Views/Custom/Test3/Test15.cshtml"
37+
Write(Html.Raw(Model.Name));
38+
39+
#line default
40+
#line hidden
41+
#nullable disable
42+
WriteLiteral("\"</h3>\n");
43+
#nullable restore
44+
#line 9 "Views/Custom/Test3/Test15.cshtml"
45+
}
46+
47+
#line default
48+
#line hidden
49+
#nullable disable
50+
}
51+
#pragma warning restore 1998
52+
#nullable restore
53+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
54+
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; } = default!;
55+
#nullable disable
56+
#nullable restore
57+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
58+
public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; } = default!;
59+
#nullable disable
60+
#nullable restore
61+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
62+
public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; } = default!;
63+
#nullable disable
64+
#nullable restore
65+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
66+
public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; } = default!;
67+
#nullable disable
68+
#nullable restore
69+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
70+
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<UserData> Html { get; private set; } = default!;
71+
#nullable disable
72+
}
73+
}
74+
#pragma warning restore 1591
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@namespace test
2+
@model UserData
3+
@{
4+
}
5+
6+
@if (Model != null)
7+
{
8+
<h3>Hello "@Html.Raw(Model.Name)"</h3>
9+
}

0 commit comments

Comments
 (0)