Skip to content

Commit c3fd06c

Browse files
committed
Csharp: fix cs/web/missing-x-frame-options to also consider location elements
As explained in https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178692(v=vs.100), it is possible to add `system.webServer` elements nested inside `location` elements in `Web.config`.
1 parent e120e5c commit c3fd06c

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed

csharp/ql/src/Security Features/CWE-451/MissingXFrameOptions.ql

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ predicate hasWebConfigXFrameOptions(WebConfigXml webConfig) {
3030
// </httpProtocol>
3131
// </system.webServer>
3232
// ```
33-
webConfig
34-
.getARootElement()
35-
.getAChild("system.webServer")
36-
.getAChild("httpProtocol")
37-
.getAChild("customHeaders")
38-
.getAChild("add")
39-
.getAttributeValue("name") = "X-Frame-Options"
33+
// This can also be in a `location`
34+
exists(XmlElement root |
35+
root = webConfig.getARootElement() and
36+
[root, root.getAChild("location")]
37+
.getAChild("system.webServer")
38+
.getAChild("httpProtocol")
39+
.getAChild("customHeaders")
40+
.getAChild("add")
41+
.getAttributeValue("name") = "X-Frame-Options"
42+
)
4043
}
4144

4245
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Web;
3+
4+
public class AddXFrameOptions : IHttpHandler
5+
{
6+
7+
public void ProcessRequest(HttpContext ctx)
8+
{
9+
}
10+
11+
public bool IsReusable
12+
{
13+
get
14+
{
15+
return true;
16+
}
17+
}
18+
}

csharp/ql/test/query-tests/Security Features/CWE-451/MissingXFrameOptions/WebConfigAddedHeaderInLocation/MissingXFrameOptions.expected

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security Features/CWE-451/MissingXFrameOptions.ql
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<location path="." inheritInChildApplications="false">
4+
<system.webServer>
5+
<httpProtocol>
6+
<customHeaders>
7+
<add name="X-Frame-Options" value="SAMEORIGIN" />
8+
</customHeaders>
9+
</httpProtocol>
10+
</system.webServer>
11+
</location>
12+
</configuration>
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: ${testdir}/../../../../../resources/stubs/System.Web.cs

0 commit comments

Comments
 (0)