Skip to content

Add option to disable form field as CSRF token source from AntiforgeryOptions #51912

@MaceWindu

Description

@MaceWindu

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Related to one of issues discussed here #18087

We use CSRF token from headers and have custom file upload middleware.

For it we don't want to have current behavior when call to IAntiforgery.IsRequestValidAsync drains request body stream if there is no token header, which makes stream unusable for custom form parser and consume a lot of resources on big uploads for nothing. For similar reason we don't want to move CSRF check to form parse code - it complicates parse logic and results in a lot of discarded work (especially if you already saved received file body to file system/database) if validation fails (perfect example where fail fast behavior is a must)

Describe the solution you'd like

// AntiforgeryOptions.cs
public bool SuppressTokenFromForm { get; set; } // or whatever name you like

Additional context

related code?

if (requestToken.Count == 0 && httpContext.Request.HasFormContentType)

// proposed change
if (requestToken.Count == 0 && httpContext.Request.HasFormContentType && !_options.SuppressTokenFromForm)

Summary Comment : #51912 (comment)

Metadata

Metadata

Assignees

Labels

Complexity: MediumHandline this issue may not be as simple and may involve some non-trivial effortarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-mvc-antiforgerygood first issueGood for newcomers.help wantedUp for grabs. We would accept a PR to help resolve this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions