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`.
This commit is contained in:
Paolo Tranquilli
2025-10-17 11:27:31 +02:00
parent e120e5c3ba
commit c3fd06c8a4
6 changed files with 44 additions and 7 deletions

View File

@@ -30,13 +30,16 @@ predicate hasWebConfigXFrameOptions(WebConfigXml webConfig) {
// </httpProtocol>
// </system.webServer>
// ```
webConfig
.getARootElement()
.getAChild("system.webServer")
.getAChild("httpProtocol")
.getAChild("customHeaders")
.getAChild("add")
.getAttributeValue("name") = "X-Frame-Options"
// This can also be in a `location`
exists(XmlElement root |
root = webConfig.getARootElement() and
[root, root.getAChild("location")]
.getAChild("system.webServer")
.getAChild("httpProtocol")
.getAChild("customHeaders")
.getAChild("add")
.getAttributeValue("name") = "X-Frame-Options"
)
}
/**

View File

@@ -0,0 +1,18 @@
using System;
using System.Web;
public class AddXFrameOptions : IHttpHandler
{
public void ProcessRequest(HttpContext ctx)
{
}
public bool IsReusable
{
get
{
return true;
}
}
}

View File

@@ -0,0 +1 @@
Security Features/CWE-451/MissingXFrameOptions.ql

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Frame-Options" value="SAMEORIGIN" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
</configuration>

View File

@@ -0,0 +1,3 @@
semmle-extractor-options: /nostdlib /noconfig
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj
semmle-extractor-options: ${testdir}/../../../../../resources/stubs/System.Web.cs