Skip to content

Properly update examined when parsing chunked requests (part 2) #8360

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 1 commit into from
Mar 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ private void ParseChunkedPrefix(ReadOnlySequence<byte> buffer, out SequencePosit
return;
}

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

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

// Advance examined before possibly throwing, so we don't risk examining less than the previous call to ParseChunkedPrefix.
examined = reader.Position;

if (ch1 == '\r' && ch2 == '\n')
{
consumed = reader.Position;
examined = reader.Position;

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

// Set examined so that we capture the progress that way made
examined = reader.Position;

// At this point, 10 bytes have been consumed which is enough to parse the max value "7FFFFFFF\r\n".
BadHttpRequestException.Throw(RequestRejectionReason.BadChunkSizeData);
}
Expand Down Expand Up @@ -453,10 +452,13 @@ private void ParseChunkedSuffix(ReadOnlySequence<byte> buffer, out SequencePosit

var suffixBuffer = buffer.Slice(0, 2);
var suffixSpan = suffixBuffer.ToSpan();

// Advance examined before possibly throwing, so we don't risk examining less than the previous call to ParseChunkedSuffix.
examined = suffixBuffer.End;

if (suffixSpan[0] == '\r' && suffixSpan[1] == '\n')
{
consumed = suffixBuffer.End;
examined = suffixBuffer.End;
AddAndCheckConsumedBytes(2);
_mode = Mode.Prefix;
}
Expand All @@ -480,10 +482,12 @@ private void ParseChunkedTrailer(ReadOnlySequence<byte> buffer, out SequencePosi
var trailerBuffer = buffer.Slice(0, 2);
var trailerSpan = trailerBuffer.ToSpan();

// Advance examined before possibly throwing, so we don't risk examining less than the previous call to ParseChunkedTrailer.
examined = trailerBuffer.End;

if (trailerSpan[0] == '\r' && trailerSpan[1] == '\n')
{
consumed = trailerBuffer.End;
examined = trailerBuffer.End;
AddAndCheckConsumedBytes(2);
_mode = Mode.Complete;
}
Expand Down