Skip to content

Commit 1ac6f3d

Browse files
halter73davidfowl
authored andcommitted
Properly update examined when parsing chunked requests (part 2) (#8360)
1 parent bc009fc commit 1ac6f3d

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ private void ParseChunkedPrefix(ReadOnlySequence<byte> buffer, out SequencePosit
323323
return;
324324
}
325325

326-
// Assigned this before calculating the chunk size since that can throw
326+
// Advance examined before possibly throwing, so we don't risk examining less than the previous call to ParseChunkedPrefix.
327327
examined = reader.Position;
328328

329329
var chunkSize = CalculateChunkSize(ch1, 0);
@@ -348,10 +348,12 @@ private void ParseChunkedPrefix(ReadOnlySequence<byte> buffer, out SequencePosit
348348
return;
349349
}
350350

351+
// Advance examined before possibly throwing, so we don't risk examining less than the previous call to ParseChunkedPrefix.
352+
examined = reader.Position;
353+
351354
if (ch1 == '\r' && ch2 == '\n')
352355
{
353356
consumed = reader.Position;
354-
examined = reader.Position;
355357

356358
AddAndCheckConsumedBytes(reader.Consumed);
357359
_inputLength = chunkSize;
@@ -363,9 +365,6 @@ private void ParseChunkedPrefix(ReadOnlySequence<byte> buffer, out SequencePosit
363365
ch1 = ch2;
364366
}
365367

366-
// Set examined so that we capture the progress that way made
367-
examined = reader.Position;
368-
369368
// At this point, 10 bytes have been consumed which is enough to parse the max value "7FFFFFFF\r\n".
370369
BadHttpRequestException.Throw(RequestRejectionReason.BadChunkSizeData);
371370
}
@@ -453,10 +452,13 @@ private void ParseChunkedSuffix(ReadOnlySequence<byte> buffer, out SequencePosit
453452

454453
var suffixBuffer = buffer.Slice(0, 2);
455454
var suffixSpan = suffixBuffer.ToSpan();
455+
456+
// Advance examined before possibly throwing, so we don't risk examining less than the previous call to ParseChunkedSuffix.
457+
examined = suffixBuffer.End;
458+
456459
if (suffixSpan[0] == '\r' && suffixSpan[1] == '\n')
457460
{
458461
consumed = suffixBuffer.End;
459-
examined = suffixBuffer.End;
460462
AddAndCheckConsumedBytes(2);
461463
_mode = Mode.Prefix;
462464
}
@@ -480,10 +482,12 @@ private void ParseChunkedTrailer(ReadOnlySequence<byte> buffer, out SequencePosi
480482
var trailerBuffer = buffer.Slice(0, 2);
481483
var trailerSpan = trailerBuffer.ToSpan();
482484

485+
// Advance examined before possibly throwing, so we don't risk examining less than the previous call to ParseChunkedTrailer.
486+
examined = trailerBuffer.End;
487+
483488
if (trailerSpan[0] == '\r' && trailerSpan[1] == '\n')
484489
{
485490
consumed = trailerBuffer.End;
486-
examined = trailerBuffer.End;
487491
AddAndCheckConsumedBytes(2);
488492
_mode = Mode.Complete;
489493
}

0 commit comments

Comments
 (0)