Skip to content

Commit 2c0ee4c

Browse files
johatuniamcasey
andauthored
Added a null check in ResolveHasInvalidAntiforgeryValidationFeature() (#53208)
* Added a null check in ResolveHasInvalidAntiforgeryValidationFeature() * Added braces to the if statement * Changed the null annotation for the _request field and some logic to handle it * Fix bug in ContentType property getter * Further tuning of the ContentType property getter and added a check in ReadFormAsync() to guard against _request equal to null * Improved the _request null check * Improved handling of content type and also changed the logic a bit * Made some smaller changes based on comments received. _formContentType will now be set to null if Form is set to null. * Update src/Http/Http/src/Features/FormFeature.cs Co-authored-by: Andrew Casey <[email protected]> * Apply suggestions from code review Co-authored-by: Andrew Casey <[email protected]> --------- Co-authored-by: Andrew Casey <[email protected]>
1 parent 4cceeb1 commit 2c0ee4c

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

src/Http/Http/src/Features/FormFeature.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ namespace Microsoft.AspNetCore.Http.Features;
1616
/// </summary>
1717
public class FormFeature : IFormFeature
1818
{
19-
private readonly HttpRequest _request;
19+
private readonly HttpRequest? _request;
2020
private readonly Endpoint? _endpoint;
2121
private FormOptions _options;
2222
private Task<IFormCollection>? _parsedFormTask;
2323
private IFormCollection? _form;
24+
private MediaTypeHeaderValue? _formContentType; // null iff _form is null
2425

2526
/// <summary>
2627
/// Initializes a new instance of <see cref="FormFeature"/>.
@@ -31,7 +32,7 @@ public FormFeature(IFormCollection form)
3132
ArgumentNullException.ThrowIfNull(form);
3233

3334
Form = form;
34-
_request = default!;
35+
_formContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
3536
_options = FormOptions.Default;
3637
}
3738

@@ -71,8 +72,19 @@ private MediaTypeHeaderValue? ContentType
7172
{
7273
get
7374
{
74-
_ = MediaTypeHeaderValue.TryParse(_request.ContentType, out var mt);
75-
return mt;
75+
MediaTypeHeaderValue? mt = null;
76+
77+
if (_request is not null)
78+
{
79+
_ = MediaTypeHeaderValue.TryParse(_request.ContentType, out mt);
80+
}
81+
82+
if (_form is not null && mt is null)
83+
{
84+
mt = _formContentType;
85+
}
86+
87+
return mt;
7688
}
7789
}
7890

@@ -87,6 +99,11 @@ public bool HasFormContentType
8799
return true;
88100
}
89101

102+
if (_request is null)
103+
{
104+
return false;
105+
}
106+
90107
var contentType = ContentType;
91108
return HasApplicationFormContentType(contentType) || HasMultipartFormContentType(contentType);
92109
}
@@ -106,6 +123,14 @@ public IFormCollection? Form
106123
{
107124
_parsedFormTask = null;
108125
_form = value;
126+
if (_form is null)
127+
{
128+
_formContentType = null;
129+
}
130+
else
131+
{
132+
_formContentType ??= new MediaTypeHeaderValue("application/x-www-form-urlencoded");
133+
}
109134
}
110135
}
111136

@@ -151,6 +176,11 @@ public Task<IFormCollection> ReadFormAsync(CancellationToken cancellationToken)
151176

152177
private async Task<IFormCollection> InnerReadFormAsync(CancellationToken cancellationToken)
153178
{
179+
if (_request is null)
180+
{
181+
throw new InvalidOperationException("Cannot read form from this request. Request is 'null'.");
182+
}
183+
154184
HandleUncheckedAntiforgeryValidationFeature();
155185
_options = _endpoint is null ? _options : GetFormOptionsFromMetadata(_options, _endpoint);
156186

@@ -326,6 +356,10 @@ private static bool HasMultipartFormContentType([NotNullWhen(true)] MediaTypeHea
326356

327357
private bool ResolveHasInvalidAntiforgeryValidationFeature()
328358
{
359+
if (_request is null)
360+
{
361+
return false;
362+
}
329363
var hasInvokedMiddleware = _request.HttpContext.Items.ContainsKey("__AntiforgeryMiddlewareWithEndpointInvoked");
330364
var hasInvalidToken = _request.HttpContext.Features.Get<IAntiforgeryValidationFeature>() is { IsValid: false };
331365
return hasInvokedMiddleware && hasInvalidToken;

0 commit comments

Comments
 (0)