Skip to content

C#: The cs/web/missing-token-validation query now recognizes an ASP.NET Core AutoValidateAntiforgeryTokenAttribute. - #22431

Open
michaelnebel wants to merge 6 commits into
github:mainfrom
michaelnebel:csharp/csrf
Open

C#: The cs/web/missing-token-validation query now recognizes an ASP.NET Core AutoValidateAntiforgeryTokenAttribute.#22431
michaelnebel wants to merge 6 commits into
github:mainfrom
michaelnebel:csharp/csrf

Conversation

@michaelnebel

@michaelnebel michaelnebel commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

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. However, it is worth noting that only works fully for traced extraction (as we only create one compilation in build-mode: none extracted databases).

It appears that the OrchardCMS/OrchardCore has some global configuration enabled, which removes all results. This can be seen here.

@michaelnebel michaelnebel changed the title C#: Detect possible use of global Auto validate. C#: The cs/web/missing-token-validation query now recognizes an ASP.NET Core AutoValidateAntiforgeryTokenAttribute. Aug 28, 2026
@michaelnebel
michaelnebel requested a balanced review from Copilot August 28, 2026 11:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

Review tier: Balanced
Findings: 1 Medium severity

New issues introduced by this change (1)
Severity Finding
Medium severity csharp/​ql/​src/​Security Features/​CWE-352/​MissingAntiForgeryTokenValidation.qlFilterCollection.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 AutoValidateAntiforgeryTokenAttribute filters.
  • 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 AutoValidateAntiforgeryTokenAttribute can 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 for Filters.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.

Comment thread csharp/ql/src/Security Features/CWE-352/MissingAntiForgeryTokenValidation.ql Outdated
@michaelnebel
michaelnebel requested a review from hvitved August 31, 2026 07:43
@michaelnebel
michaelnebel marked this pull request as ready for review August 31, 2026 07:43
@michaelnebel
michaelnebel requested a review from a team as a code owner August 31, 2026 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants