Skip to content

Don't count start line toward header size limit #21272

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
Apr 28, 2020
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions src/Servers/Kestrel/Core/src/Internal/Http/Http1Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal partial class Http1Connection : HttpProtocol, IRequestProcessor
private string _parsedRawTarget = null;
private Uri _parsedAbsoluteRequestTarget;

private int _remainingRequestHeadersBytesAllowed;
private long _remainingRequestHeadersBytesAllowed;

public Http1Connection(HttpConnectionContext context)
{
Expand Down Expand Up @@ -213,6 +213,8 @@ public bool TakeMessageHeaders(ref SequenceReader<byte> reader, bool trailers)
return TrimAndTakeMessageHeaders(ref reader, trailers);
}

var alreadyConsumed = reader.Consumed;

try
{
var result = _parser.ParseHeaders(new Http1ParsingHandler(this, trailers), ref reader);
Expand All @@ -225,7 +227,7 @@ public bool TakeMessageHeaders(ref SequenceReader<byte> reader, bool trailers)
}
finally
{
_remainingRequestHeadersBytesAllowed -= (int)reader.Consumed;
_remainingRequestHeadersBytesAllowed -= reader.Consumed - alreadyConsumed;
}

bool TrimAndTakeMessageHeaders(ref SequenceReader<byte> reader, bool trailers)
Expand All @@ -248,7 +250,7 @@ bool TrimAndTakeMessageHeaders(ref SequenceReader<byte> reader, bool trailers)
}
finally
{
_remainingRequestHeadersBytesAllowed -= (int)reader.Consumed;
_remainingRequestHeadersBytesAllowed -= trimmedReader.Consumed;
}
}
}
Expand Down
95 changes: 60 additions & 35 deletions src/Servers/Kestrel/Core/test/Http1ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ public async Task TakeMessageHeadersThrowsWhenHeadersExceedTotalSizeLimit()
await _application.Output.WriteAsync(Encoding.ASCII.GetBytes($"{headerLine}\r\n"));
var readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

#pragma warning disable CS0618 // Type or member is obsolete
var exception = Assert.Throws<BadHttpRequestException>(() => TakeMessageHeaders(readableBuffer, trailers: false, out _consumed, out _examined));
#pragma warning restore CS0618 // Type or member is obsolete
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please tell me you kept at least some of the old pragmas? We need something that verifies the old exception is still the one thrown.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's still one pragma in this test class because it's checking some internal property. I didn't even touch the other test classes.

var exception = Assert.ThrowsAny<Http.BadHttpRequestException>(() => TakeMessageHeaders(readableBuffer, trailers: false, out _consumed, out _examined));
_transport.Input.AdvanceTo(_consumed, _examined);

Assert.Equal(CoreStrings.BadRequest_HeadersExceedMaxTotalSize, exception.Message);
Expand All @@ -145,15 +143,60 @@ public async Task TakeMessageHeadersThrowsWhenHeadersExceedCountLimit()
await _application.Output.WriteAsync(Encoding.ASCII.GetBytes($"{headerLines}\r\n"));
var readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

#pragma warning disable CS0618 // Type or member is obsolete
var exception = Assert.Throws<BadHttpRequestException>(() => TakeMessageHeaders(readableBuffer, trailers: false, out _consumed, out _examined));
#pragma warning restore CS0618 // Type or member is obsolete
var exception = Assert.ThrowsAny<Http.BadHttpRequestException>(() => TakeMessageHeaders(readableBuffer, trailers: false, out _consumed, out _examined));
_transport.Input.AdvanceTo(_consumed, _examined);

Assert.Equal(CoreStrings.BadRequest_TooManyHeaders, exception.Message);
Assert.Equal(StatusCodes.Status431RequestHeaderFieldsTooLarge, exception.StatusCode);
}

[Fact]
public async Task TakeMessageHeadersDoesNotCountAlreadyConsumedBytesTowardsSizeLimit()
{
const string startLine = "GET / HTTP/1.1\r\n";

// This doesn't actually need to be larger than the start line to cause the regression,
// but doing so gives us a nice HeadersExceedMaxTotalSize error rather than an invalid slice
// when we do see the regression.
const string headerLine = "Header: makethislargerthanthestartline\r\n";

_serviceContext.ServerOptions.Limits.MaxRequestHeadersTotalSize = headerLine.Length;
_http1Connection.Reset();

// Don't send header initially because the regression is only caught if TakeMessageHeaders
// is called multiple times. The first call overcounted the header bytes consumed, and the
// subsequent calls overslice the buffer due to the overcounting.
await _application.Output.WriteAsync(Encoding.ASCII.GetBytes($"{startLine}"));
var readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

SequencePosition TakeStartLineAndMessageHeaders()
{
var reader = new SequenceReader<byte>(readableBuffer);
Assert.True(_http1Connection.TakeStartLine(ref reader));
Assert.False(_http1Connection.TakeMessageHeaders(ref reader, trailers: false));
return reader.Position;
}

_transport.Input.AdvanceTo(TakeStartLineAndMessageHeaders());

Assert.Equal(0, _http1Connection.RequestHeaders.Count);

await _application.Output.WriteAsync(Encoding.ASCII.GetBytes($"{headerLine}\r\n"));
readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

SequencePosition TakeMessageHeaders()
{
var reader = new SequenceReader<byte>(readableBuffer);
Assert.True(_http1Connection.TakeMessageHeaders(ref reader, trailers: false));
return reader.Position;
}

_transport.Input.AdvanceTo(TakeMessageHeaders());

Assert.Equal(1, _http1Connection.RequestHeaders.Count);
Assert.Equal("makethislargerthanthestartline", _http1Connection.RequestHeaders["Header"]);
}

[Fact]
public void ResetResetsScheme()
{
Expand Down Expand Up @@ -442,9 +485,7 @@ public async Task TakeStartLineThrowsWhenTooLong()
await _application.Output.WriteAsync(requestLineBytes);

var readableBuffer = (await _transport.Input.ReadAsync()).Buffer;
#pragma warning disable CS0618 // Type or member is obsolete
var exception = Assert.Throws<BadHttpRequestException>(() => TakeStartLine(readableBuffer, out _consumed, out _examined));
#pragma warning restore CS0618 // Type or member is obsolete
var exception = Assert.ThrowsAny<Http.BadHttpRequestException>(() => TakeStartLine(readableBuffer, out _consumed, out _examined));
_transport.Input.AdvanceTo(_consumed, _examined);

Assert.Equal(CoreStrings.BadRequest_RequestLineTooLong, exception.Message);
Expand All @@ -458,9 +499,7 @@ public async Task TakeStartLineThrowsOnEncodedNullCharInTarget(string target)
await _application.Output.WriteAsync(Encoding.ASCII.GetBytes($"GET {target} HTTP/1.1\r\n"));
var readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

#pragma warning disable CS0618 // Type or member is obsolete
var exception = Assert.Throws<BadHttpRequestException>(() =>
#pragma warning restore CS0618 // Type or member is obsolete
var exception = Assert.ThrowsAny<Http.BadHttpRequestException>(() =>
TakeStartLine(readableBuffer, out _consumed, out _examined));
_transport.Input.AdvanceTo(_consumed, _examined);

Expand All @@ -474,9 +513,7 @@ public async Task TakeStartLineThrowsOnNullCharInTarget(string target)
await _application.Output.WriteAsync(Encoding.ASCII.GetBytes($"GET {target} HTTP/1.1\r\n"));
var readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

#pragma warning disable CS0618 // Type or member is obsolete
var exception = Assert.Throws<BadHttpRequestException>(() =>
#pragma warning restore CS0618 // Type or member is obsolete
var exception = Assert.ThrowsAny<Http.BadHttpRequestException>(() =>
TakeStartLine(readableBuffer, out _consumed, out _examined));
_transport.Input.AdvanceTo(_consumed, _examined);

Expand All @@ -492,9 +529,7 @@ public async Task TakeStartLineThrowsOnNullCharInMethod(string method)
await _application.Output.WriteAsync(Encoding.ASCII.GetBytes(requestLine));
var readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

#pragma warning disable CS0618 // Type or member is obsolete
var exception = Assert.Throws<BadHttpRequestException>(() =>
#pragma warning restore CS0618 // Type or member is obsolete
var exception = Assert.ThrowsAny<Http.BadHttpRequestException>(() =>
TakeStartLine(readableBuffer, out _consumed, out _examined));
_transport.Input.AdvanceTo(_consumed, _examined);

Expand All @@ -510,9 +545,7 @@ public async Task TakeStartLineThrowsOnNullCharInQueryString(string queryString)
await _application.Output.WriteAsync(Encoding.ASCII.GetBytes($"GET {target} HTTP/1.1\r\n"));
var readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

#pragma warning disable CS0618 // Type or member is obsolete
var exception = Assert.Throws<BadHttpRequestException>(() =>
#pragma warning restore CS0618 // Type or member is obsolete
var exception = Assert.ThrowsAny<Http.BadHttpRequestException>(() =>
TakeStartLine(readableBuffer, out _consumed, out _examined));
_transport.Input.AdvanceTo(_consumed, _examined);

Expand All @@ -528,9 +561,7 @@ public async Task TakeStartLineThrowsWhenRequestTargetIsInvalid(string method, s
await _application.Output.WriteAsync(Encoding.ASCII.GetBytes(requestLine));
var readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

#pragma warning disable CS0618 // Type or member is obsolete
var exception = Assert.Throws<BadHttpRequestException>(() =>
#pragma warning restore CS0618 // Type or member is obsolete
var exception = Assert.ThrowsAny<Http.BadHttpRequestException>(() =>
TakeStartLine(readableBuffer, out _consumed, out _examined));
_transport.Input.AdvanceTo(_consumed, _examined);

Expand All @@ -548,7 +579,7 @@ public async Task TakeStartLineThrowsWhenMethodNotAllowed(string requestLine, in
#pragma warning disable CS0618 // Type or member is obsolete
var exception = Assert.Throws<BadHttpRequestException>(() =>
#pragma warning restore CS0618 // Type or member is obsolete
TakeStartLine(readableBuffer, out _consumed, out _examined));
TakeStartLine(readableBuffer, out _consumed, out _examined));
_transport.Input.AdvanceTo(_consumed, _examined);

Assert.Equal(405, exception.StatusCode);
Expand Down Expand Up @@ -792,10 +823,8 @@ public async Task ExceptionDetailNotIncludedWhenLogLevelInformationNotEnabled()
await _application.Output.WriteAsync(Encoding.ASCII.GetBytes($"GET /%00 HTTP/1.1\r\n"));
var readableBuffer = (await _transport.Input.ReadAsync()).Buffer;

#pragma warning disable CS0618 // Type or member is obsolete
var exception = Assert.Throws<BadHttpRequestException>(() =>
#pragma warning restore CS0618 // Type or member is obsolete
TakeStartLine(readableBuffer, out _consumed, out _examined));
var exception = Assert.ThrowsAny<Http.BadHttpRequestException>(() =>
TakeStartLine(readableBuffer, out _consumed, out _examined));
_transport.Input.AdvanceTo(_consumed, _examined);

Assert.Equal(CoreStrings.FormatBadRequest_InvalidRequestTarget_Detail(string.Empty), exception.Message);
Expand Down Expand Up @@ -954,9 +983,7 @@ public void BadRequestFor10BadHostHeaderFormat()
{
_http1Connection.HttpVersion = "HTTP/1.0";
_http1Connection.RequestHeaders[HeaderNames.Host] = "a=b";
#pragma warning disable CS0618 // Type or member is obsolete
var ex = Assert.Throws<BadHttpRequestException>(() => _http1Connection.EnsureHostHeaderExists());
#pragma warning restore CS0618 // Type or member is obsolete
var ex = Assert.ThrowsAny<Http.BadHttpRequestException>(() => _http1Connection.EnsureHostHeaderExists());
Assert.Equal(CoreStrings.FormatBadRequest_InvalidHostHeader_Detail("a=b"), ex.Message);
}

Expand All @@ -965,9 +992,7 @@ public void BadRequestFor11BadHostHeaderFormat()
{
_http1Connection.HttpVersion = "HTTP/1.1";
_http1Connection.RequestHeaders[HeaderNames.Host] = "a=b";
#pragma warning disable CS0618 // Type or member is obsolete
var ex = Assert.Throws<BadHttpRequestException>(() => _http1Connection.EnsureHostHeaderExists());
#pragma warning restore CS0618 // Type or member is obsolete
var ex = Assert.ThrowsAny<Http.BadHttpRequestException>(() => _http1Connection.EnsureHostHeaderExists());
Assert.Equal(CoreStrings.FormatBadRequest_InvalidHostHeader_Detail("a=b"), ex.Message);
}

Expand Down