Skip to content

Commit ffc45dc

Browse files
authored
Clarifications on the antiforgery secure policy (#36191)
1 parent 9937eb1 commit ffc45dc

File tree

1 file changed

+91
-14
lines changed

1 file changed

+91
-14
lines changed

aspnetcore/security/anti-request-forgery.md

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
title: Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ASP.NET Core
3+
ai-usage: ai-assisted
34
author: tdykstra
4-
description: Discover how to prevent attacks against web apps where a malicious website can influence the interaction between a client browser and the app.
5-
ms.author: tdykstra
65
content_well_notification: AI-contribution
6+
description: Discover how to prevent attacks against web apps where a malicious website can influence the interaction between a client browser and the app.
77
monikerRange: '>= aspnetcore-3.1'
8+
ms.author: tdykstra
89
ms.custom: mvc
910
ms.date: 11/16/2023
1011
uid: security/anti-request-forgery
11-
ai-usage: ai-assisted
1212
---
1313
# Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ASP.NET Core
1414

@@ -161,14 +161,13 @@ Calling <xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExten
161161

162162
Multiple tabs logged in as different users, or one logged in as anonymous, are not supported.
163163

164-
165164
## Configure antiforgery with `AntiforgeryOptions`
166165

167-
Customize <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions> in `Program.cs`:
166+
Customize <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions> in the app's `Program` file:
168167

169168
:::code language="csharp" source="anti-request-forgery/samples/6.x/AntiRequestForgerySample/Snippets/Program.cs" id="snippet_AddAntiforgeryOptions":::
170169

171-
Set the antiforgery `Cookie` properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
170+
Set the antiforgery cookie properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
172171

173172
| Option | Description |
174173
| --- | --- |
@@ -177,6 +176,20 @@ Set the antiforgery `Cookie` properties using the properties of the <xref:Micros
177176
| <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.HeaderName%2A> | The name of the header used by the antiforgery system. If `null`, the system considers only form data. |
178177
| <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.SuppressXFrameOptionsHeader%2A> | Specifies whether to suppress generation of the `X-Frame-Options` header. By default, the header is generated with a value of "SAMEORIGIN". Defaults to `false`. |
179178

179+
Some browsers don't allow insecure endpoints to set cookies with a 'secure' flag or overwrite cookies whose 'secure' flag is set (for more information, see [Deprecate modification of 'secure' cookies from non-secure origins](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-alone-01)). Since mixing secure and insecure endpoints is a common scenario in apps, ASP.NET Core relaxes the restriction on the secure policy on some cookies, such as the antiforgery cookie, by setting the cookie's <xref:Microsoft.AspNetCore.Http.CookieBuilder.SecurePolicy%2A> to [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy). Even if a malicious user steals an antiforgery cookie, they also must steal the antiforgery token that's typically sent via a form field (more common) or a separate request header (less common) plus the authentication cookie. Cookies related to authentication or authorization use a stronger policy than [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy).
180+
181+
Optionally, you can secure the antiforgery cookie in non-Development environments using Secure Sockets Layer (SSL), over HTTPS only, with the following <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.Cookie%2A?displayProperty=nameWithType> property setting in the app's `Program` file:
182+
183+
```csharp
184+
if (!builder.Environment.IsDevelopment())
185+
{
186+
builder.Services.AddAntiforgery(o =>
187+
{
188+
o.Cookie.SecurePolicy = CookieSecurePolicy.Always;
189+
});
190+
}
191+
```
192+
180193
For more information, see <xref:Microsoft.AspNetCore.Builder.CookieAuthenticationOptions>.
181194

182195
## Generate antiforgery tokens with `IAntiforgery`
@@ -481,15 +494,15 @@ With the Synchronizer Token Pattern, only the most recently loaded page contains
481494
* Only the most recently loaded tab contains a valid antiforgery token.
482495
* Requests made from previously loaded tabs fail with an error: `Antiforgery token validation failed. The antiforgery cookie token and request token do not match`
483496

484-
Consider alternative CSRF protection patterns if this poses an issue.
497+
Consider alternative CSRF protection patterns if this poses an issue.
485498

486499
## Configure antiforgery with `AntiforgeryOptions`
487500

488-
Customize <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions> in `Program.cs`:
501+
Customize <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions> in the app's `Program` file:
489502

490503
:::code language="csharp" source="anti-request-forgery/samples/6.x/AntiRequestForgerySample/Snippets/Program.cs" id="snippet_AddAntiforgeryOptions":::
491504

492-
Set the antiforgery `Cookie` properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
505+
Set the antiforgery cookie properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
493506

494507
| Option | Description |
495508
| --- | --- |
@@ -498,6 +511,20 @@ Set the antiforgery `Cookie` properties using the properties of the <xref:Micros
498511
| <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.HeaderName%2A> | The name of the header used by the antiforgery system. If `null`, the system considers only form data. |
499512
| <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.SuppressXFrameOptionsHeader%2A> | Specifies whether to suppress generation of the `X-Frame-Options` header. By default, the header is generated with a value of "SAMEORIGIN". Defaults to `false`. |
500513

514+
Some browsers don't allow insecure endpoints to set cookies with a 'secure' flag or overwrite cookies whose 'secure' flag is set (for more information, see [Deprecate modification of 'secure' cookies from non-secure origins](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-alone-01)). Since mixing secure and insecure endpoints is a common scenario in apps, ASP.NET Core relaxes the restriction on the secure policy on some cookies, such as the antiforgery cookie, by setting the cookie's <xref:Microsoft.AspNetCore.Http.CookieBuilder.SecurePolicy%2A> to [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy). Even if a malicious user steals an antiforgery cookie, they also must steal the antiforgery token that's typically sent via a form field (more common) or a separate request header (less common) plus the authentication cookie. Cookies related to authentication or authorization use a stronger policy than [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy).
515+
516+
Optionally, you can secure the antiforgery cookie in non-Development environments using Secure Sockets Layer (SSL), over HTTPS only, with the following <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.Cookie%2A?displayProperty=nameWithType> property setting in the app's `Program` file:
517+
518+
```csharp
519+
if (!builder.Environment.IsDevelopment())
520+
{
521+
builder.Services.AddAntiforgery(o =>
522+
{
523+
o.Cookie.SecurePolicy = CookieSecurePolicy.Always;
524+
});
525+
}
526+
```
527+
501528
For more information, see <xref:Microsoft.AspNetCore.Builder.CookieAuthenticationOptions>.
502529

503530
## Generate antiforgery tokens with `IAntiforgery`
@@ -742,7 +769,7 @@ ASP.NET Core includes three [filters](xref:mvc/controllers/filters) for working
742769
* [AutoValidateAntiforgeryToken](xref:Microsoft.AspNetCore.Mvc.AutoValidateAntiforgeryTokenAttribute)
743770
* [IgnoreAntiforgeryToken](xref:Microsoft.AspNetCore.Mvc.IgnoreAntiforgeryTokenAttribute)
744771

745-
### Antiforgery with AddControllers
772+
## Antiforgery with `AddControllers`
746773

747774
Calling <xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllers%2A> does ***not*** enable antiforgery tokens. <xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersWithViews%2A> must be called to have built-in antiforgery token support.
748775

@@ -753,15 +780,15 @@ With the Synchronizer Token Pattern, only the most recently loaded page contains
753780
* Only the most recently loaded tab contains a valid antiforgery token.
754781
* Requests made from previously loaded tabs fail with an error: `Antiforgery token validation failed. The antiforgery cookie token and request token do not match`
755782

756-
Consider alternative CSRF protection patterns if this poses an issue.
783+
Consider alternative CSRF protection patterns if this poses an issue.
757784

758785
## Configure antiforgery with `AntiforgeryOptions`
759786

760-
Customize <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions> in `Program.cs`:
787+
Customize <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions> in the app's `Program` file:
761788

762789
:::code language="csharp" source="anti-request-forgery/samples/6.x/AntiRequestForgerySample/Snippets/Program.cs" id="snippet_AddAntiforgeryOptions":::
763790

764-
Set the antiforgery `Cookie` properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
791+
Set the antiforgery cookie properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
765792

766793
| Option | Description |
767794
| --- | --- |
@@ -770,6 +797,20 @@ Set the antiforgery `Cookie` properties using the properties of the <xref:Micros
770797
| <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.HeaderName%2A> | The name of the header used by the antiforgery system. If `null`, the system considers only form data. |
771798
| <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.SuppressXFrameOptionsHeader%2A> | Specifies whether to suppress generation of the `X-Frame-Options` header. By default, the header is generated with a value of "SAMEORIGIN". Defaults to `false`. |
772799

800+
Some browsers don't allow insecure endpoints to set cookies with a 'secure' flag or overwrite cookies whose 'secure' flag is set (for more information, see [Deprecate modification of 'secure' cookies from non-secure origins](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-alone-01)). Since mixing secure and insecure endpoints is a common scenario in apps, ASP.NET Core relaxes the restriction on the secure policy on some cookies, such as the antiforgery cookie, by setting the cookie's <xref:Microsoft.AspNetCore.Http.CookieBuilder.SecurePolicy%2A> to [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy). Even if a malicious user steals an antiforgery cookie, they also must steal the antiforgery token that's typically sent via a form field (more common) or a separate request header (less common) plus the authentication cookie. Cookies related to authentication or authorization use a stronger policy than [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy).
801+
802+
Optionally, you can secure the antiforgery cookie in non-Development environments using Secure Sockets Layer (SSL), over HTTPS only, with the following <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.Cookie%2A?displayProperty=nameWithType> property setting in the app's `Program` file:
803+
804+
```csharp
805+
if (!builder.Environment.IsDevelopment())
806+
{
807+
builder.Services.AddAntiforgery(o =>
808+
{
809+
o.Cookie.SecurePolicy = CookieSecurePolicy.Always;
810+
});
811+
}
812+
```
813+
773814
For more information, see <xref:Microsoft.AspNetCore.Builder.CookieAuthenticationOptions>.
774815

775816
## Generate antiforgery tokens with `IAntiforgery`
@@ -1034,7 +1075,7 @@ services.AddAntiforgery(options =>
10341075
});
10351076
```
10361077

1037-
Set the antiforgery `Cookie` properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
1078+
Set the antiforgery cookie properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
10381079

10391080
| Option | Description |
10401081
| --- | --- |
@@ -1043,6 +1084,42 @@ Set the antiforgery `Cookie` properties using the properties of the <xref:Micros
10431084
| <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.HeaderName%2A> | The name of the header used by the antiforgery system. If `null`, the system considers only form data. |
10441085
| <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.SuppressXFrameOptionsHeader%2A> | Specifies whether to suppress generation of the `X-Frame-Options` header. By default, the header is generated with a value of "SAMEORIGIN". Defaults to `false`. |
10451086

1087+
Some browsers don't allow insecure endpoints to set cookies with a 'secure' flag or overwrite cookies whose 'secure' flag is set (for more information, see [Deprecate modification of 'secure' cookies from non-secure origins](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-alone-01)). Since mixing secure and insecure endpoints is a common scenario in apps, ASP.NET Core relaxes the restriction on the secure policy on some cookies, such as the antiforgery cookie, by setting the cookie's <xref:Microsoft.AspNetCore.Http.CookieBuilder.SecurePolicy%2A> to [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy). Even if a malicious user steals an antiforgery cookie, they also must steal the antiforgery token that's typically sent via a form field (more common) or a separate request header (less common) plus the authentication cookie. Cookies related to authentication or authorization use a stronger policy than [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy).
1088+
1089+
Optionally, you can secure the antiforgery cookie in non-Development environments using Secure Sockets Layer (SSL), over HTTPS only, with the following <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.Cookie%2A?displayProperty=nameWithType> property setting in the app's `Startup` class:
1090+
1091+
```csharp
1092+
public class Startup
1093+
{
1094+
public Startup(IConfiguration configuration, IHostEnvironment environment)
1095+
{
1096+
Configuration = configuration;
1097+
Environment = environment;
1098+
}
1099+
1100+
public IConfiguration Configuration { get; }
1101+
public IHostEnvironment Environment { get; }
1102+
1103+
public void ConfigureServices(IServiceCollection services)
1104+
{
1105+
// Other services are registered here
1106+
1107+
if (!Environment.IsDevelopment())
1108+
{
1109+
services.AddAntiforgery(o =>
1110+
{
1111+
o.Cookie.SecurePolicy = CookieSecurePolicy.Always;
1112+
});
1113+
}
1114+
}
1115+
1116+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
1117+
{
1118+
// Request processing pipeline
1119+
}
1120+
}
1121+
```
1122+
10461123
For more information, see <xref:Microsoft.AspNetCore.Builder.CookieAuthenticationOptions>.
10471124

10481125
## Configure antiforgery features with IAntiforgery

0 commit comments

Comments
 (0)