-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[C#] Recognize RequireAntiforgeryToken attributes #22322
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
Merged
michaelnebel
merged 2 commits into
github:main
from
theinfosecguy:csharp-require-antiforgery-token
Aug 14, 2026
+344
−13
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions
4
csharp/ql/src/change-notes/2026-08-09-require-antiforgery-token.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,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * The `cs/web/missing-token-validation` query now recognizes enabled ASP.NET Core `RequireAntiforgeryToken` attributes when antiforgery middleware is used. |
27 changes: 27 additions & 0 deletions
27
...curity Features/CWE-352/disabled-require-antiforgery/MissingAntiForgeryTokenValidation.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,27 @@ | ||
| using Microsoft.AspNetCore.Antiforgery; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| public class HomeController : Controller | ||
| { | ||
| [HttpPost] | ||
| [RequireAntiforgeryToken(false)] | ||
| public ActionResult DisabledValidation() | ||
| { | ||
| return View(); | ||
| } | ||
|
|
||
| [HttpPost] | ||
| public ActionResult MissingValidation() | ||
| { | ||
| return View(); | ||
| } | ||
| } | ||
|
|
||
| public class Startup | ||
| { | ||
| public void Configure(IApplicationBuilder app) | ||
| { | ||
| app.UseAntiforgery(); | ||
| } | ||
| } |
Empty file.
1 change: 1 addition & 0 deletions
1
...ity Features/CWE-352/disabled-require-antiforgery/MissingAntiForgeryTokenValidation.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 @@ | ||
| query: Security Features/CWE-352/MissingAntiForgeryTokenValidation.ql |
2 changes: 2 additions & 0 deletions
2
csharp/ql/test/query-tests/Security Features/CWE-352/disabled-require-antiforgery/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,2 @@ | ||
| semmle-extractor-options: /nostdlib /noconfig | ||
| semmle-extractor-options: --load-sources-from-project:${testdir}/../../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj |
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
10 changes: 8 additions & 2 deletions
10
...s/Security Features/CWE-352/missing-aspnetcore/MissingAntiForgeryTokenValidation.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,2 +1,8 @@ | ||
| | MissingAntiForgeryTokenValidation.cs:7:25:7:29 | Login | Method 'Login' handles a POST request without performing CSRF token validation. | | ||
| | MissingAntiForgeryTokenValidation.cs:58:25:58:45 | NoInheritedValidation | Method 'NoInheritedValidation' handles a POST request without performing CSRF token validation. | | ||
| | MissingAntiForgeryTokenValidation.cs:12:25:12:29 | Login | Method 'Login' handles a POST request without performing CSRF token validation. | | ||
| | MissingAntiForgeryTokenValidation.cs:52:25:52:42 | DisabledValidation | Method 'DisabledValidation' handles a POST request without performing CSRF token validation. | | ||
| | MissingAntiForgeryTokenValidation.cs:60:25:60:50 | ConstantDisabledValidation | Method 'ConstantDisabledValidation' handles a POST request without performing CSRF token validation. | | ||
| | MissingAntiForgeryTokenValidation.cs:126:25:126:38 | DisabledAction | Method 'DisabledAction' handles a POST request without performing CSRF token validation. | | ||
| | MissingAntiForgeryTokenValidation.cs:137:25:137:48 | DisabledControllerAction | Method 'DisabledControllerAction' handles a POST request without performing CSRF token validation. | | ||
| | MissingAntiForgeryTokenValidation.cs:161:25:161:47 | DisabledInheritedAction | Method 'DisabledInheritedAction' handles a POST request without performing CSRF token validation. | | ||
| | MissingAntiForgeryTokenValidation.cs:207:25:207:45 | NoInheritedValidation | Method 'NoInheritedValidation' handles a POST request without performing CSRF token validation. | | ||
| | MissingAntiForgeryTokenValidation.cs:224:29:224:46 | LookalikeAttribute | Method 'LookalikeAttribute' handles a POST request without performing CSRF token validation. | |
19 changes: 19 additions & 0 deletions
19
...tures/CWE-352/require-antiforgery-without-middleware/MissingAntiForgeryTokenValidation.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,19 @@ | ||
| using Microsoft.AspNetCore.Antiforgery; | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| public class HomeController : Controller | ||
| { | ||
| [HttpPost] | ||
| [ValidateAntiForgeryToken] | ||
| public ActionResult FilterValidated() | ||
| { | ||
| return View(); | ||
| } | ||
|
|
||
| [HttpPost] | ||
| [RequireAntiforgeryToken] | ||
| public ActionResult MetadataWithoutMiddleware() // $ Alert | ||
| { | ||
| return View(); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
...CWE-352/require-antiforgery-without-middleware/MissingAntiForgeryTokenValidation.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 @@ | ||
| | MissingAntiForgeryTokenValidation.cs:15:25:15:49 | MetadataWithoutMiddleware | Method 'MetadataWithoutMiddleware' handles a POST request without performing CSRF token validation. | |
1 change: 1 addition & 0 deletions
1
...es/CWE-352/require-antiforgery-without-middleware/MissingAntiForgeryTokenValidation.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 @@ | ||
| query: Security Features/CWE-352/MissingAntiForgeryTokenValidation.ql |
2 changes: 2 additions & 0 deletions
2
...test/query-tests/Security Features/CWE-352/require-antiforgery-without-middleware/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,2 @@ | ||
| semmle-extractor-options: /nostdlib /noconfig | ||
| semmle-extractor-options: --load-sources-from-project:${testdir}/../../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Perhaps, we can refactor/encapsulate some of the logic and avoid using the
bindingsetpragmas.Maybe the remaining part of the query can look something like:
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.
Note that then it is also possible to remove the
bindingsetpragmas on theget*like predicates.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.
Thanks for the suggestion. I’ve applied the refactor and removed the bindingset pragmas.