-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Added a null check in ResolveHasInvalidAntiforgeryValidationFeature() #53208
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
754bf00
Added a null check in ResolveHasInvalidAntiforgeryValidationFeature()
johatuni 69d6914
Added braces to the if statement
johatuni e99d719
Changed the null annotation for the _request field and some logic to …
johatuni 01ba77c
Fix bug in ContentType property getter
johatuni f2ac33f
Further tuning of the ContentType property getter and added a check i…
johatuni 65e735f
Improved the _request null check
johatuni 59f4fb3
Improved handling of content type and also changed the logic a bit
johatuni 7ebc49c
Made some smaller changes based on comments received. _formContentTyp…
johatuni bb25336
Update src/Http/Http/src/Features/FormFeature.cs
johatuni 64e6a40
Apply suggestions from code review
johatuni 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 |
---|---|---|
|
@@ -16,11 +16,12 @@ namespace Microsoft.AspNetCore.Http.Features; | |
/// </summary> | ||
public class FormFeature : IFormFeature | ||
{ | ||
private readonly HttpRequest _request; | ||
private readonly HttpRequest? _request; | ||
private readonly Endpoint? _endpoint; | ||
private FormOptions _options; | ||
private Task<IFormCollection>? _parsedFormTask; | ||
private IFormCollection? _form; | ||
private MediaTypeHeaderValue? _formContentType; // null iff _form is null | ||
|
||
/// <summary> | ||
/// Initializes a new instance of <see cref="FormFeature"/>. | ||
|
@@ -31,7 +32,7 @@ public FormFeature(IFormCollection form) | |
ArgumentNullException.ThrowIfNull(form); | ||
|
||
Form = form; | ||
_request = default!; | ||
_formContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); | ||
_options = FormOptions.Default; | ||
} | ||
|
||
|
@@ -71,8 +72,19 @@ private MediaTypeHeaderValue? ContentType | |
{ | ||
get | ||
{ | ||
_ = MediaTypeHeaderValue.TryParse(_request.ContentType, out var mt); | ||
return mt; | ||
MediaTypeHeaderValue? mt = null; | ||
|
||
if (_request is not null) | ||
{ | ||
_ = MediaTypeHeaderValue.TryParse(_request.ContentType, out mt); | ||
} | ||
|
||
if (_form is not null && mt is null) | ||
{ | ||
mt = _formContentType; | ||
} | ||
|
||
return mt; | ||
} | ||
} | ||
|
||
|
@@ -87,6 +99,11 @@ public bool HasFormContentType | |
return true; | ||
} | ||
|
||
if (_request is null) | ||
{ | ||
return false; | ||
} | ||
|
||
var contentType = ContentType; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I think it would be clearer to check whether |
||
return HasApplicationFormContentType(contentType) || HasMultipartFormContentType(contentType); | ||
} | ||
|
@@ -106,6 +123,14 @@ public IFormCollection? Form | |
{ | ||
_parsedFormTask = null; | ||
_form = value; | ||
if (_form is null) | ||
{ | ||
_formContentType = null; | ||
} | ||
else | ||
{ | ||
_formContentType ??= new MediaTypeHeaderValue("application/x-www-form-urlencoded"); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -151,6 +176,11 @@ public Task<IFormCollection> ReadFormAsync(CancellationToken cancellationToken) | |
|
||
private async Task<IFormCollection> InnerReadFormAsync(CancellationToken cancellationToken) | ||
{ | ||
if (_request is null) | ||
{ | ||
throw new InvalidOperationException("Cannot read form from this request. Request is 'null'."); | ||
} | ||
|
||
HandleUncheckedAntiforgeryValidationFeature(); | ||
_options = _endpoint is null ? _options : GetFormOptionsFromMetadata(_options, _endpoint); | ||
|
||
|
@@ -326,6 +356,10 @@ private static bool HasMultipartFormContentType([NotNullWhen(true)] MediaTypeHea | |
|
||
private bool ResolveHasInvalidAntiforgeryValidationFeature() | ||
{ | ||
if (_request is null) | ||
johatuni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return false; | ||
} | ||
var hasInvokedMiddleware = _request.HttpContext.Items.ContainsKey("__AntiforgeryMiddlewareWithEndpointInvoked"); | ||
var hasInvalidToken = _request.HttpContext.Features.Get<IAntiforgeryValidationFeature>() is { IsValid: false }; | ||
return hasInvokedMiddleware && hasInvalidToken; | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.