-
Notifications
You must be signed in to change notification settings - Fork 10.3k
[Blazor] Add support for antiforgery #49108
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a6f775a
Add support for antiforgery
javiercn 6ded8b8
Support disabling antiforgery
javiercn 3db4c14
Cleanups
javiercn 89adae6
Feedback
javiercn 6b946dd
Add couple of missing tests
javiercn 8a95a04
Update src/Components/Web/src/Forms/AntiforgeryStateProvider.cs
javiercn 0ed7919
Fix empty response handling
SteveSandersonMS d89743b
Make DefaultAntiforgeryStateProvider shared source
javiercn bf09a17
Fix build
javiercn 2581403
Fix incorrect dependency
javiercn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Antiforgery.Infrastructure; | ||
|
||
/// <summary> | ||
/// A marker interface which can be used to identify antiforgery metadata. | ||
/// </summary> | ||
public interface IAntiforgeryMetadata | ||
{ | ||
/// <summary> | ||
/// Gets a value indicating whether the antiforgery token should be validated. | ||
/// </summary> | ||
bool Required { get; } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Antiforgery.Infrastructure.IAntiforgeryMetadata | ||
Microsoft.AspNetCore.Antiforgery.Infrastructure.IAntiforgeryMetadata.Required.get -> bool | ||
Microsoft.AspNetCore.Antiforgery.RequireAntiforgeryTokenAttribute | ||
Microsoft.AspNetCore.Antiforgery.RequireAntiforgeryTokenAttribute.RequireAntiforgeryTokenAttribute(bool required = true) -> void | ||
Microsoft.AspNetCore.Antiforgery.RequireAntiforgeryTokenAttribute.Required.get -> bool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Antiforgery.Infrastructure; | ||
|
||
namespace Microsoft.AspNetCore.Antiforgery; | ||
|
||
/// <summary> | ||
/// An attribute that can be used to indicate whether the antiforgery token must be validated. | ||
/// </summary> | ||
/// <param name="required">A value indicating whether the antiforgery token should be validated.</param> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] | ||
public class RequireAntiforgeryTokenAttribute(bool required = true) : Attribute, IAntiforgeryMetadata | ||
{ | ||
/// <summary> | ||
/// Gets or sets a value indicating whether the antiforgery token should be validated. | ||
/// </summary> | ||
/// <remarks> | ||
/// Defaults to <see langword="true"/>; <see langword="false"/> indicates that | ||
/// the validation check for the antiforgery token can be avoided. | ||
/// </remarks> | ||
public bool Required { get; } = required; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/Components/Endpoints/src/Forms/EndpointAntiforgeryStateProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Antiforgery; | ||
using Microsoft.AspNetCore.Components.Forms; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.Components.Endpoints.Forms; | ||
|
||
internal class EndpointAntiforgeryStateProvider(IAntiforgery antiforgery, PersistentComponentState state) : DefaultAntiforgeryStateProvider(state) | ||
{ | ||
private HttpContext? _context; | ||
|
||
internal void SetRequestContext(HttpContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public override AntiforgeryRequestToken? GetAntiforgeryToken() | ||
{ | ||
if (_context == null) | ||
{ | ||
return null; | ||
} | ||
|
||
// We already have a callback setup to generate the token when the response starts if needed. | ||
// If we need the tokens before we start streaming the response, we'll generate and store them; | ||
// otherwise we'll just retrieve them. | ||
// In case there are no tokens available, we are going to return null and no-op. | ||
var tokens = !_context.Response.HasStarted ? antiforgery.GetAndStoreTokens(_context) : antiforgery.GetTokens(_context); | ||
if (tokens.RequestToken is null) | ||
{ | ||
return null; | ||
} | ||
|
||
return new AntiforgeryRequestToken(tokens.RequestToken, tokens.FormFieldName); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the idea that people would write
<... @onsubmit:name>
with no value?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do no value, but more commonly you would do
@onsubmit:name=""
which gets translated totrue
, which is what this code avoids. Once the directive for the razor compiler is in, this will just beSetEventHandlerName