Skip to content

Commit 105f810

Browse files
authored
Merge pull request #20658 from github/redsun82/csharp-fix-xframe-options-in-location
Csharp: fix `cs/web/missing-x-frame-options` to also consider `location` elements
2 parents 2e0e9e0 + 3f98d32 commit 105f810

File tree

7 files changed

+51
-2
lines changed

7 files changed

+51
-2
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ import csharp
1616
import semmle.code.asp.WebConfig
1717
import semmle.code.csharp.frameworks.system.Web
1818

19+
XmlElement getAWebConfigRoot(WebConfigXml webConfig) {
20+
result = webConfig.getARootElement()
21+
or
22+
result = webConfig.getARootElement().getAChild("location") and
23+
(
24+
not result.hasAttribute("path") // equivalent to path="."
25+
or
26+
result.getAttributeValue("path") = ["", "."]
27+
)
28+
}
29+
1930
/**
2031
* Holds if the `Web.config` file `webConfig` adds an `X-Frame-Options` header.
2132
*/
@@ -30,8 +41,8 @@ predicate hasWebConfigXFrameOptions(WebConfigXml webConfig) {
3041
// </httpProtocol>
3142
// </system.webServer>
3243
// ```
33-
webConfig
34-
.getARootElement()
44+
// This can also be in a `location`
45+
getAWebConfigRoot(webConfig)
3546
.getAChild("system.webServer")
3647
.getAChild("httpProtocol")
3748
.getAChild("customHeaders")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* the `cs/web/missing-x-frame-options` query now correctly handles configuration nested in root `<location>` elements.
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)