Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Anti-Forgery Protection in a view-less Web API

I'm implementing a REST API using ASP.NET Core. It is stateless except for the fact that is uses cookies for authentication and therefore is vulnerable to cross-site request forgery (CSRF) attacks.

Luckily, ASP.NET Core provides means as a protection against that: Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ASP.NET Core.

As my application does not have any views or pages, I'm only configuring my controllers using services.AddControllers() in my Startup.

When hitting a REST endpoint that is attributed with [ValidateAntiForgeryToken], I get the following exception:

System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.Filters.ValidateAntiforgeryTokenAuthorizationFilter' has been registered.

Registering my controllers using services.AddControllersWithViews() makes this error go away as it internally registers the appropriate service.

According to the docs:

Antiforgery middleware is added to the Dependency injection container when one of the following APIs is called in Startup.ConfigureServices:

AddMvc

MapRazorPages

MapControllerRoute

MapBlazorHub

All of these method seem to me to be view-centric (except MapControllerRoute which I'm doing in the Configure method in my Startup but it doesn't help) and part of the namespace of the missing service is ViewFeatures. This confuses me because in my understanding, and need to take care of CSRF although I'm developing a pure Web API without views.

Is my understanding wrong? How is CSRF protection configured when no views are involved?

like image 492
Dejan Avatar asked Jun 12 '20 15:06

Dejan


People also ask

How do I stop a cross site request forgery in Web API?

To prevent CSRF attacks, use anti-forgery tokens with any authentication protocol where the browser silently sends credentials after the user logs in. This includes cookie-based authentication protocols, such as forms authentication, as well as protocols such as Basic and Digest authentication.

What is AntiForgeryToken in Web API?

Adding an AntiForgeryToken generates a Cryptographically valid hash at the server end which is split and a part is added as a hidden field, whereas the rest goes into a cookie. When data is posted, the Cookie and the Hidden Field are both sent back and if they are missing or they don't match, the POST is rejected.


1 Answers

I will suggest move away from the default ValidateAntiForgeryToken attribute

All the harder work is done by services.AddAntiforgery(), and the ValidateAntiForgeryToken just calls antiforgery.ValidateRequestAsync()

You can create your own filter for it and register it etc. but take a look at this neat implementation, you can simply inject an instance of IAntiforgery in all the POST api methods

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/security/anti-request-forgery/sample/AngularSample/Startup.cs

like image 123
LarryX Avatar answered Oct 10 '22 02:10

LarryX