C#: The cs/web/missing-token-validation query now recognizes an ASP.NET Core AutoValidateAntiforgeryTokenAttribute. - #22431
Draft
michaelnebel wants to merge 6 commits into
Draft
C#: The cs/web/missing-token-validation query now recognizes an ASP.NET Core AutoValidateAntiforgeryTokenAttribute.#22431michaelnebel wants to merge 6 commits into
cs/web/missing-token-validation query now recognizes an ASP.NET Core AutoValidateAntiforgeryTokenAttribute.#22431michaelnebel wants to merge 6 commits into
Conversation
michaelnebel
force-pushed
the
csharp/csrf
branch
from
August 28, 2026 07:22
35e8b9b to
ae42d08
Compare
michaelnebel
force-pushed
the
csharp/csrf
branch
from
August 28, 2026 07:30
ae42d08 to
9177cce
Compare
cs/web/missing-token-validation query now recognizes an ASP.NET Core AutoValidateAntiforgeryTokenAttribute.
michaelnebel
force-pushed
the
csharp/csrf
branch
from
August 28, 2026 11:36
2107cd2 to
c272bf9
Compare
Contributor
There was a problem hiding this comment.
Copilot review overview
Review tier: Balanced
Findings: 1
New issues introduced by this change (1)
| Severity | Finding |
|---|---|
csharp/ql/src/Security Features/CWE-352/MissingAntiForgeryTokenValidation.ql — FilterCollection.Add(Type) is a supported overload (and is present in the loaded stub), but… |
What changed in this PR
Adds ASP.NET Core global antiforgery-filter recognition to reduce false positives in the C# CSRF query.
Changes:
- Models MVC registrations and global
AutoValidateAntiforgeryTokenAttributefilters. - Scopes global filters and middleware to the controller compilation.
- Adds regression coverage and a change note.
| File | Description |
|---|---|
MissingAntiForgeryTokenValidation.ql |
Detects compilation-scoped global filters. |
AspNetCore.qll |
Models filter registrations and generic Add. |
global-aspnetcore/options |
Loads ASP.NET Core stubs. |
global-aspnetcore/MissingAntiForgeryTokenValidation.qlref |
Selects the tested query. |
global-aspnetcore/MissingAntiForgeryTokenValidation.expected |
Expects no alerts. |
global-aspnetcore/MissingAntiForgeryTokenValidation.cs |
Adds a global-filter regression case. |
2026-08-27-csrf-autovalidate.md |
Documents the analysis improvement. |
Suppressed comments (2)
csharp/ql/src/Security Features/CWE-352/MissingAntiForgeryTokenValidation.ql:146
- A global
AutoValidateAntiforgeryTokenAttributecan be overridden on a controller or action with[IgnoreAntiforgeryToken], but this condition removes every POST action in the compilation before action metadata is considered. Consequently, explicitly unprotected actions are never reported. Model the effective ignore attribute and exempt those actions from the global-filter suppression.
not hasGlobalAspNetMvcAntiForgeryFilter(compilation)
csharp/ql/src/Security Features/CWE-352/MissingAntiForgeryTokenValidation.ql:59
- The test only exercises
Add(new AutoValidateAntiforgeryTokenAttribute()), while the new generic-target branch forFilters.Add<AutoValidateAntiforgeryTokenAttribute>()has no existing coverage. Add an isolated generic-registration case so the object registration cannot mask whether generic method extraction works.
addGlobalFilter.getTarget().(ConstructedGeneric).getTypeArgument(0) instanceof
AspNetCore::AutoValidateAntiforgeryTokenAttribute
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
michaelnebel
force-pushed
the
csharp/csrf
branch
from
August 28, 2026 12:45
14fec96 to
57395f3
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

The content of this PR addresses the comment seen here. There exists a "global" auto validate for ASP.NET as explained here.
The compilation is now also taken into account when auto "global" validation is enabled.