Skip to content

Commit 00892e1

Browse files
Add unit test + fix flow step
1 parent c3cd40f commit 00892e1

File tree

7 files changed

+114
-5
lines changed

7 files changed

+114
-5
lines changed

csharp/ql/lib/semmle/code/csharp/frameworks/Razor.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,12 @@ class PageModelClass extends Class {
233233
)
234234
}
235235

236-
/** Gets the Razor Page that this PageModel refers to. */
236+
/** Gets the Razor Page that has this PageModel. */
237237
RazorViewClass getPage() {
238-
exists(Field modelField |
239-
modelField.hasName("Model") and
240-
modelField.getType() = this and
241-
modelField.getDeclaringType() = result
238+
exists(Property modelProp |
239+
modelProp.hasName("Model") and
240+
modelProp.getType() = this and
241+
modelProp.getDeclaringType() = result
242242
)
243243
}
244244
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.AspNetCore.Mvc.RazorPages;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.Net;
4+
using System.Threading.Tasks;
5+
namespace test;
6+
7+
class TestModel : PageModel {
8+
public string Name {get; set; } = "abc";
9+
10+
private string source() { return "x"; }
11+
12+
public async Task<IActionResult> OnGetAsync() {
13+
Name = source();
14+
return Page();
15+
}
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@page
2+
@model TestModel
3+
@namespace test
4+
5+
<h3>Hello "@Html.Raw(Model.Name)"</h3>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// A hand-written test file that mimics the output of compiling a `.cshtml` file
2+
3+
// <auto-generated/>
4+
#pragma warning disable 1591
5+
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(test.Pages.Pages_TestPage), @"mvc.1.0.razor-page", @"TestPage.cshrml")]
6+
namespace test.Pages
7+
{
8+
#line hidden
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using System.Threading.Tasks;
13+
using Microsoft.AspNetCore.Mvc;
14+
using Microsoft.AspNetCore.Mvc.Rendering;
15+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
16+
#nullable restore
17+
using test;
18+
19+
#line default
20+
#line hidden
21+
#nullable disable
22+
[global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"c4ae76542f1958092cebd8f57beef899d20fc548", @"TestPage.cshtml")]
23+
// [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"c13da96c2597d5ddb7d415fb4892c644a268f50b", @"/Pages/_ViewImports.cshtml")]
24+
internal sealed class Pages_TestPage : global::Microsoft.AspNetCore.Mvc.RazorPages.Page
25+
{
26+
#pragma warning disable 1998
27+
public async override global::System.Threading.Tasks.Task ExecuteAsync()
28+
{
29+
#nullable disable
30+
WriteLiteral("<h3>Hello \"");
31+
#nullable restore
32+
#line 5 "TestPage.cshtml"
33+
Write(Html.Raw(Model.Name));
34+
35+
#line default
36+
#line hidden
37+
#nullable disable
38+
WriteLiteral("\"</h3>\n");
39+
#nullable restore
40+
}
41+
#pragma warning restore 1998
42+
#nullable restore
43+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
44+
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; } = default!;
45+
#nullable disable
46+
#nullable restore
47+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
48+
public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; } = default!;
49+
#nullable disable
50+
#nullable restore
51+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
52+
public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; } = default!;
53+
#nullable disable
54+
#nullable restore
55+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
56+
public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; } = default!;
57+
#nullable disable
58+
#nullable restore
59+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
60+
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<TestModel> Html { get; private set; } = default!;
61+
#nullable disable
62+
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestModel> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestModel>)PageContext?.ViewData;
63+
public TestModel Model => ViewData.Model; }
64+
}
65+
#pragma warning restore 1591
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
semmle-extractor-options: /nostdlib /noconfig
2+
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj
3+
semmle-extractor-options: --load-sources-from-project:../../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| TestPage.cshtml:5:16:5:25 | access to property Name | TestModel.cs:13:16:13:23 | call to method source | TestPage.cshtml:5:16:5:25 | access to property Name | Xss |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import csharp
2+
import semmle.code.csharp.security.dataflow.XSSQuery
3+
import semmle.code.csharp.security.dataflow.XSSSinks
4+
5+
module TestXssTrackingConfig implements DataFlow::ConfigSig {
6+
predicate isSource(DataFlow::Node source) {
7+
source.asExpr().(MethodCall).getTarget().getName() = "source"
8+
}
9+
10+
predicate isSink = XssTrackingConfig::isSink/1;
11+
12+
predicate isBarrier = XssTrackingConfig::isBarrier/1;
13+
}
14+
15+
module TestXss = TaintTracking::Global<TestXssTrackingConfig>;
16+
17+
from DataFlow::Node source, DataFlow::Node sink
18+
where TestXss::flow(source, sink)
19+
select sink, source, sink, "Xss"

0 commit comments

Comments
 (0)