Skip to content
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

Add antiforgery (anti-csrf) support to minimal endpoints #38630

Closed
halter73 opened this issue Nov 24, 2021 · 5 comments · Fixed by #42586
Closed

Add antiforgery (anti-csrf) support to minimal endpoints #38630

halter73 opened this issue Nov 24, 2021 · 5 comments · Fixed by #42586
Assignees
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc Docs This issue tracks updating documentation feature-minimal-actions Controller-like actions for endpoint routing Needs: Design This issue requires design work before implementating. old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Priority:0 Work that we can't release without triage-focus Add this label to flag the issue for focus at triage
Milestone

Comments

@halter73
Copy link
Member

halter73 commented Nov 24, 2021

Today, Antiforgery is implemented as a filter in MVC. There's an attribute that also acts as an auth filter. It reads the form and performs a validation. Razor Pages add an implicit antiforgery filter that is active for all non-idempotent (POST / PUT / DELETE etc) requests. We'd like to enable Antiforgery to become available outside of MVC. My goal was to continue relying on these attributes, but as metadata that indicated if the user intended to perform antiforgery validation for that endpoint.

In minimal endpoints, this might look like:

app.MapPost("/post", (IFormFile formFile) => { ... })
    .ValidateAntiforgery(); // Maybe we can infer this based on the presence of things that come from a HTTP form.

Generating the antiforgery token that goes in to the form is still tied to the app framework. MVC will generate if you're using the form tag helper / Html.BeginForm(). For a form post using minimal, a user can use the IAntiforgery service to produce it. This PR doesn't attempt to make that experience better.

#38314 (comment)

Thanks @pranavkm for the Antiforgery middleware PR and the nice description in the comments. And extra thanks to @martincostello for adding support for form file parameters to minimal endpoints with #35158!

Now that it is possible to accept IFormFile and IFormFileCollection parameters in minimal actions, we will need to require an antiforgery token for all authenticated endpoints taking these parameter types. Right now, because we don't check for an antiforgery token on these endpoints, we reject any request with a cookie, cert or auth header.

I don't think we'll end up needing the .ValidateAntiforgery() extension method, but maybe that would be useful for endpoints manually consuming a form from the HttpRequest.

Since minimal endpoints are not opinionated about the client app language or framework, having a single solution for generating and sending the antiforgery token is difficult. The current IAntiforgery service interface is built around idea that the client will be performing a request before posting form data and will be able to send a cookie set during that prior request during the actual POST. This makes sense for web clients, but we want to make uploading files easier for non-web clients as well because "multipart/form-data" is one of the most widely adopted ways to upload files over HTTP.

My understanding is that the cookie is there to prevent an attacker from laundering an antiforgery token meant for a different user. It would be more convenient if this could be done without a cookie and instead with a single token tied to the IPrincipal in some other way.

@blowdart

@halter73 halter73 added feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Nov 24, 2021
@blowdart
Copy link
Contributor

The whole point of our CSRF is a dual token approach, and it's a well understood approach. The cookie is there both for user checks, and also for things coming from other sites, which can happen with shared domain suffixes such as azurewebsites.net

However if you want to propose something different @GrabYourPitchforks and I will listen.

@davidfowl
Copy link
Member

This isn't just for minimal APIs right? It's also for ApiControllers as well?

@rafikiassumani-msft rafikiassumani-msft added this to the .NET 7 Planning milestone Nov 30, 2021
@ghost
Copy link

ghost commented Nov 30, 2021

Thanks for contacting us.

We're moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@rafikiassumani-msft rafikiassumani-msft added the Needs: Design This issue requires design work before implementating. label Dec 1, 2021
@GrabYourPitchforks
Copy link
Member

As a reminder, CSRF is only a concern for endpoints that use "ambient" authentication. By "ambient" I mean something that flows automatically as part of the request without active consent by the user: cookies, NTLM auth, client certs, etc.

For things like standard API calls, where authentication flows as part of a manually added Authorize request header, CSRF is not a concern and no checks are needed.

@ghost
Copy link

ghost commented Jun 14, 2022

Thanks for contacting us.

We're moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@brunolins16 brunolins16 added the Docs This issue tracks updating documentation label Jul 5, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 11, 2022
@amcasey amcasey added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc Docs This issue tracks updating documentation feature-minimal-actions Controller-like actions for endpoint routing Needs: Design This issue requires design work before implementating. old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Priority:0 Work that we can't release without triage-focus Add this label to flag the issue for focus at triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants