Skip to content

Commit a547aa8

Browse files
authored
Fix form key accumulator (#37272)
Fixes #36987
1 parent 2168a8b commit a547aa8

File tree

8 files changed

+23
-15
lines changed

8 files changed

+23
-15
lines changed

src/Http/Headers/src/HeaderUtilities.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public static bool TryParseSeconds(StringValues headerValues, string targetValue
241241

242242
for (var i = 0; i < headerValues.Count; i++)
243243
{
244-
var segment = headerValues[i]!;
244+
var segment = headerValues[i] ?? string.Empty;
245245

246246
// Trim leading white space
247247
var current = HttpRuleParser.GetWhitespaceLength(segment, 0);
@@ -297,7 +297,7 @@ public static bool ContainsCacheDirective(StringValues cacheControlDirectives, s
297297

298298
for (var i = 0; i < cacheControlDirectives.Count; i++)
299299
{
300-
var segment = cacheControlDirectives[i]!;
300+
var segment = cacheControlDirectives[i] ?? string.Empty;
301301

302302
// Trim leading white space
303303
var current = HttpRuleParser.GetWhitespaceLength(segment, 0);

src/Http/Http/src/Internal/ResponseCookies.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public void Delete(string key, CookieOptions options)
192192

193193
for (var i = 0; i < values.Length; i++)
194194
{
195-
var value = values[i]!;
195+
var value = values[i] ?? string.Empty;
196196
if (!rejectPredicate(value, encodedKeyPlusEquals, options))
197197
{
198198
newValues.Add(value);

src/Http/WebUtilities/src/KeyValueAccumulator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void Append(string key, string value)
3030
StringValues values;
3131
if (_accumulator.TryGetValue(key, out values))
3232
{
33-
if (StringValues.IsNullOrEmpty(values))
33+
if (values.Count == 0)
3434
{
3535
// Marker entry for this key to indicate entry already in expanding list dictionary
3636
_expandingAccumulator[key].Add(value);

src/Http/WebUtilities/test/FormPipeReaderTests.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Buffers;
6-
using System.Collections.Generic;
7-
using System.IO;
85
using System.IO.Pipelines;
96
using System.Text;
10-
using System.Threading.Tasks;
117
using Microsoft.Extensions.Primitives;
12-
using Xunit;
138

149
namespace Microsoft.AspNetCore.WebUtilities
1510
{
@@ -567,6 +562,16 @@ public void ParseFormWithIncompleteValueWhenIsFinalBlockSucceeds(ReadOnlySequenc
567562
Assert.Equal("", values["b"]);
568563
}
569564

565+
[Fact]
566+
public async Task ReadFormAsync_AccumulatesEmptyKeys()
567+
{
568+
var bodyPipe = await MakePipeReader("&&&");
569+
570+
var formCollection = await ReadFormAsync(new FormPipeReader(bodyPipe));
571+
572+
Assert.Single(formCollection);
573+
}
574+
570575
public static TheoryData<ReadOnlySequence<byte>> IncompleteFormKeys =>
571576
new TheoryData<ReadOnlySequence<byte>>
572577
{

src/Middleware/ResponseCaching/src/ResponseCachingKeyProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public string CreateStorageVaryByKey(ResponseCachingContext context)
120120
var requestHeaders = context.HttpContext.Request.Headers;
121121
for (var i = 0; i < headersCount; i++)
122122
{
123-
var header = varyByRules!.Headers[i]!;
123+
var header = varyByRules!.Headers[i] ?? string.Empty;
124124
var headerValues = requestHeaders[header];
125125
builder.Append(KeyDelimiter)
126126
.Append(header)
@@ -174,7 +174,7 @@ public string CreateStorageVaryByKey(ResponseCachingContext context)
174174
{
175175
for (var i = 0; i < varyByRules.QueryKeys.Count; i++)
176176
{
177-
var queryKey = varyByRules.QueryKeys[i]!;
177+
var queryKey = varyByRules.QueryKeys[i] ?? string.Empty;
178178
var queryKeyValues = context.HttpContext.Request.Query[queryKey];
179179
builder.Append(KeyDelimiter)
180180
.Append(queryKey)

src/Servers/IIS/IIS/src/Core/IISHttpContext.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,16 @@ public unsafe void SetResponseHeaders()
431431
var knownHeaderIndex = HttpApiTypes.HTTP_RESPONSE_HEADER_ID.IndexOfKnownHeader(headerPair.Key);
432432
for (var i = 0; i < headerValues.Count; i++)
433433
{
434-
if (string.IsNullOrEmpty(headerValues[i]))
434+
var headerValue = headerValues[i];
435+
436+
if (string.IsNullOrEmpty(headerValue))
435437
{
436438
continue;
437439
}
438440

439441
var isFirst = i == 0;
440-
var headerValueBytes = Encoding.UTF8.GetBytes(headerValues[i]!);
442+
var headerValueBytes = Encoding.UTF8.GetBytes(headerValue);
443+
441444
fixed (byte* pHeaderValue = headerValueBytes)
442445
{
443446
if (knownHeaderIndex == -1)

src/Servers/Kestrel/Core/src/Internal/Http/HttpHeaders.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public static void ValidateHeaderValueCharacters(string headerName, StringValues
268268
var count = headerValues.Count;
269269
for (var i = 0; i < count; i++)
270270
{
271-
ValidateHeaderValueCharacters(headerValues[i]!, requireAscii);
271+
ValidateHeaderValueCharacters(headerValues[i] ?? string.Empty, requireAscii);
272272
}
273273
}
274274

src/Shared/RangeHelper/RangeHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static (bool isRangeRequest, RangeItemHeaderValue? range) ParseRange(
4545
}
4646

4747
// Perf: Check for a single entry before parsing it
48-
if (rawRangeHeader.Count > 1 || rawRangeHeader[0]!.IndexOf(',') >= 0)
48+
if (rawRangeHeader.Count > 1 || (rawRangeHeader[0] ?? string.Empty).IndexOf(',') >= 0)
4949
{
5050
logger.LogDebug("Multiple ranges are not supported.");
5151

0 commit comments

Comments
 (0)