-
Notifications
You must be signed in to change notification settings - Fork 2.1k
C#: Reduce false positives in cs/web/missing-x-frame-options #22553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Bubby4j
wants to merge
1
commit into
github:main
Choose a base branch
from
Bubby4j:fix/cs-missing-x-frame-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
csharp/ql/src/Security Features/CWE-451/MissingXFrameOptionsAspNetCore.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| void Configure(IApplicationBuilder app) | ||
| { | ||
| app.Use(async (context, next) => | ||
| { | ||
| context.Response.Headers["Content-Security-Policy"] = "frame-ancestors 'none'"; | ||
| await next(); | ||
| }); | ||
| } |
118 changes: 118 additions & 0 deletions
118
csharp/ql/src/Security Features/MissingXFrameOptionsLib.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /** Provides predicates for recognizing clickjacking-related response-header configuration. */ | ||
|
|
||
| import csharp | ||
| import semmle.code.csharp.dataflow.DataFlow | ||
| import semmle.code.csharp.frameworks.microsoft.AspNetCore | ||
| import semmle.code.csharp.frameworks.system.Web | ||
|
|
||
| /** Holds if `name` is the `X-Frame-Options` header name, ignoring case. */ | ||
| bindingset[name] | ||
| predicate isXFrameOptionsHeaderName(string name) { name.toLowerCase() = "x-frame-options" } | ||
|
|
||
| /** Holds if `name` is the enforced `Content-Security-Policy` header name, ignoring case. */ | ||
| bindingset[name] | ||
| predicate isContentSecurityPolicyHeaderName(string name) { | ||
| name.toLowerCase() = "content-security-policy" | ||
| } | ||
|
|
||
| /** | ||
| * Holds if `value` contains a `frame-ancestors` directive at the start of a CSP policy or | ||
| * after a directive or policy separator. | ||
| */ | ||
| bindingset[value] | ||
| predicate containsFrameAncestorsDirective(string value) { | ||
| value.regexpMatch("(?is)(^|.*[;,])\\s*frame-ancestors(\\s|;|$).*") | ||
|
|
||
| } | ||
|
|
||
| private predicate isHeaderNamesField(Expr name, string fieldName) { | ||
| exists(FieldAccess access | | ||
| name.stripImplicit() = access and | ||
| access.getTarget().hasFullyQualifiedName("Microsoft.Net.Http.Headers", "HeaderNames", fieldName) | ||
| ) | ||
| } | ||
|
|
||
| private predicate isXFrameOptionsHeaderNameExpr(Expr name) { | ||
| isXFrameOptionsHeaderName(name.stripImplicit().getValue()) | ||
| or | ||
| isHeaderNamesField(name, "XFrameOptions") | ||
| } | ||
|
|
||
| private predicate isContentSecurityPolicyHeaderNameExpr(Expr name) { | ||
| isContentSecurityPolicyHeaderName(name.stripImplicit().getValue()) | ||
| or | ||
| isHeaderNamesField(name, "ContentSecurityPolicy") | ||
| } | ||
|
|
||
| private predicate containsFrameAncestorsDirectiveExpr(Expr value) { | ||
| containsFrameAncestorsDirective(value.stripImplicit().getValue()) | ||
| } | ||
|
|
||
| private predicate isClickjackingHeader(Expr name, Expr value) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| isXFrameOptionsHeaderNameExpr(name) | ||
| or | ||
| isContentSecurityPolicyHeaderNameExpr(name) and containsFrameAncestorsDirectiveExpr(value) | ||
| } | ||
|
|
||
| private predicate isDirectResponseHeadersAccess(Expr expr) { | ||
| exists(PropertyAccessExpr headers, MicrosoftAspNetCoreHttpHttpResponse response | | ||
| expr.stripImplicit() = headers and headers.getProperty() = response.getHeadersProperty() | ||
| ) | ||
| } | ||
|
|
||
| private predicate isResponseHeadersAccess(Expr expr) { | ||
| exists(Expr directAccess | | ||
| isDirectResponseHeadersAccess(directAccess) and | ||
| DataFlow::localExprFlow(directAccess, expr.stripImplicit()) | ||
| ) | ||
| } | ||
|
|
||
| private Expr getHeaderDictionaryReceiver(MethodCall call) { | ||
| result = call.getQualifier() | ||
| or | ||
| call.getTarget().isExtensionMethod() and | ||
| result = call.getArgumentForParameter(call.getTarget().getParameter(0)) | ||
| } | ||
|
|
||
| private predicate isClickjackingHeaderCall(MethodCall call) { | ||
| ( | ||
| call.getTarget() = any(SystemWebHttpResponseClass r).getAppendHeaderMethod() or | ||
| call.getTarget() = any(SystemWebHttpResponseClass r).getAddHeaderMethod() | ||
| ) and | ||
| isClickjackingHeader(call.getArgumentForName("name"), call.getArgumentForName("value")) | ||
| or | ||
| call.getTarget().hasUndecoratedName(["Append", "Add", "TryAdd"]) and | ||
| isResponseHeadersAccess(getHeaderDictionaryReceiver(call)) and | ||
| isClickjackingHeader(call.getArgumentForName("key"), call.getArgumentForName("value")) | ||
| } | ||
|
|
||
| private predicate isClickjackingHeaderIndexerAssignment(AssignExpr assignment) { | ||
| exists(IndexerCall indexer | | ||
| assignment.getLeftOperand() = indexer and | ||
| isResponseHeadersAccess(indexer.getQualifier()) and | ||
| isClickjackingHeader(indexer.getArgument(0), assignment.getRightOperand()) | ||
| ) | ||
| } | ||
|
|
||
| private predicate isClickjackingNamedHeaderPropertyAssignment(AssignExpr assignment) { | ||
| exists(PropertyAccessExpr header | | ||
| assignment.getLeftOperand() = header and | ||
| isResponseHeadersAccess(header.(QualifiableExpr).getQualifier()) and | ||
| ( | ||
| header.getProperty().hasName("XFrameOptions") | ||
| or | ||
| header.getProperty().hasName("ContentSecurityPolicy") and | ||
| containsFrameAncestorsDirectiveExpr(assignment.getRightOperand()) | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| /** Gets an expression that configures a clickjacking-related response header. */ | ||
| Expr getAClickjackingHeaderWrite() { | ||
| result = any(MethodCall call | isClickjackingHeaderCall(call)) | ||
| or | ||
| result = | ||
| any(AssignExpr assignment | | ||
| isClickjackingHeaderIndexerAssignment(assignment) or | ||
| isClickjackingNamedHeaderPropertyAssignment(assignment) | ||
| ) | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
csharp/ql/src/change-notes/2026-09-10-missing-x-frame-options.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * The `cs/web/missing-x-frame-options` query now recognizes clickjacking protection configured | ||
| through ASP.NET Core response headers and enforced Content Security Policy `frame-ancestors` | ||
| directives. |
3 changes: 1 addition & 2 deletions
3
...Security Features/CWE-451/MissingXFrameOptions/CodeAddedHeader/MissingXFrameOptions.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1 @@ | ||
| query: Security Features/CWE-451/MissingXFrameOptions.ql | ||
| postprocess: utils/test/InlineExpectationsTestQuery.ql | ||
| Security Features/CWE-451/MissingXFrameOptions.ql |
52 changes: 52 additions & 0 deletions
52
...t/query-tests/Security Features/CWE-451/MissingXFrameOptions/HeaderWrites/HeaderWrites.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using System.Collections.Generic; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Net.Http.Headers; | ||
| using AspNetCoreHttpContext = Microsoft.AspNetCore.Http.HttpContext; | ||
|
|
||
| public class HeaderWrites | ||
| { | ||
| public void AspNetCoreResponseHeaders(AspNetCoreHttpContext context) | ||
| { | ||
| context.Response.Headers.Append(HeaderNames.XFrameOptions, "DENY"); // $ Alert | ||
| context.Response.Headers.Add("x-frame-options", "SAMEORIGIN"); // $ Alert | ||
| context.Response.Headers.TryAdd( | ||
| HeaderNames.ContentSecurityPolicy, | ||
| "default-src 'self'; FrAmE-AnCeStOrS 'none'"); // $ Alert | ||
|
|
||
| context.Response.Headers["X-Frame-Options"] = "DENY"; // $ Alert | ||
| context.Response.Headers["Content-Security-Policy"] = | ||
| "default-src 'self'; frame-ancestors 'none'"; // $ Alert | ||
|
|
||
| context.Response.Headers.XFrameOptions = "DENY"; // $ Alert | ||
| context.Response.Headers.ContentSecurityPolicy = | ||
| "default-src 'self'; frame-ancestors 'self'"; // $ Alert | ||
| context.Response.Headers["Content-Security-Policy"] = | ||
| "default-src 'self', frame-ancestors 'none'"; // $ Alert | ||
|
|
||
| IHeaderDictionary responseHeaders = context.Response.Headers; | ||
| responseHeaders.Append("X-Frame-Options", "DENY"); // $ Alert | ||
| } | ||
|
|
||
| public void IgnoredHeaderWrites(AspNetCoreHttpContext context) | ||
| { | ||
| context.Request.Headers["X-Frame-Options"] = "DENY"; | ||
|
|
||
| IHeaderDictionary reassignedHeaders = context.Response.Headers; | ||
| reassignedHeaders = context.Request.Headers; | ||
| reassignedHeaders["X-Frame-Options"] = "DENY"; | ||
|
|
||
| var standaloneHeaders = new HeaderDictionary(); | ||
| standaloneHeaders.Append("X-Frame-Options", "DENY"); | ||
| standaloneHeaders["Content-Security-Policy"] = "frame-ancestors 'none'"; | ||
|
|
||
| context.Response.Headers["Content-Security-Policy-Report-Only"] = | ||
| "frame-ancestors 'none'"; | ||
| context.Response.Headers.ContentSecurityPolicyReportOnly = "frame-ancestors 'none'"; | ||
| context.Response.Headers["X-Content-Security-Policy"] = "frame-ancestors 'none'"; | ||
| context.Response.Headers["Content-Security-Policy"] = "default-src 'self'"; | ||
| context.Response.Headers["Content-Security-Policy"] = | ||
| "report-uri https://example.test/frame-ancestors"; | ||
| context.Response.Headers["Content-Security-Policy"] = | ||
| "default-src 'self'; not-frame-ancestors 'none'"; | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
...y-tests/Security Features/CWE-451/MissingXFrameOptions/HeaderWrites/HeaderWrites.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| | HeaderWrites.cs:10:9:10:74 | call to method Append | A clickjacking-related response header is configured here. | | ||
| | HeaderWrites.cs:11:9:11:69 | call to method Add | A clickjacking-related response header is configured here. | | ||
| | HeaderWrites.cs:12:9:14:57 | call to method TryAdd<String,StringValues> | A clickjacking-related response header is configured here. | | ||
| | HeaderWrites.cs:16:9:16:60 | ... = ... | A clickjacking-related response header is configured here. | | ||
| | HeaderWrites.cs:17:9:18:56 | ... = ... | A clickjacking-related response header is configured here. | | ||
| | HeaderWrites.cs:20:9:20:55 | ... = ... | A clickjacking-related response header is configured here. | | ||
| | HeaderWrites.cs:21:9:22:56 | ... = ... | A clickjacking-related response header is configured here. | | ||
| | HeaderWrites.cs:23:9:24:56 | ... = ... | A clickjacking-related response header is configured here. | | ||
| | HeaderWrites.cs:27:9:27:57 | call to method Append | A clickjacking-related response header is configured here. | |
12 changes: 12 additions & 0 deletions
12
...t/query-tests/Security Features/CWE-451/MissingXFrameOptions/HeaderWrites/HeaderWrites.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /** | ||
| * @kind problem | ||
| * @id cs/test/clickjacking-header-write | ||
| * @problem.severity warning | ||
| */ | ||
|
|
||
| import csharp | ||
| import Security_Features.MissingXFrameOptionsLib | ||
|
|
||
| from Expr write | ||
| where write = getAClickjackingHeaderWrite() | ||
| select write, "A clickjacking-related response header is configured here." |
2 changes: 2 additions & 0 deletions
2
...uery-tests/Security Features/CWE-451/MissingXFrameOptions/HeaderWrites/HeaderWrites.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| query: HeaderWrites.ql | ||
| postprocess: utils/test/InlineExpectationsTestQuery.ql |
Empty file.
1 change: 1 addition & 0 deletions
1
...ts/Security Features/CWE-451/MissingXFrameOptions/HeaderWrites/MissingXFrameOptions.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Security Features/CWE-451/MissingXFrameOptions.ql |
2 changes: 2 additions & 0 deletions
2
...l/test/query-tests/Security Features/CWE-451/MissingXFrameOptions/HeaderWrites/Web.config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <?xml version="1.0" encoding="utf-8" ?> | ||
| <configuration /> |
3 changes: 3 additions & 0 deletions
3
...p/ql/test/query-tests/Security Features/CWE-451/MissingXFrameOptions/HeaderWrites/options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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: --load-sources-from-project:${testdir}/../../../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj |
2 changes: 1 addition & 1 deletion
2
...sts/Security Features/CWE-451/MissingXFrameOptions/NoHeader/MissingXFrameOptions.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| | Web.config:0:0:0:0 | Web.config | Configuration file is missing the X-Frame-Options setting. | | ||
| | Web.config:0:0:0:0 | Web.config | Configuration file is missing clickjacking protection. | |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a QL library file, it should be put in the library pack instead.
Perhaps, rename the file to
MissingXFrameOptionsQuery.qlland put it in the `lib/semmle/code/csharp/security folder instead.