Skip to content
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

Fix HTTP3 header decoder buffer allocation #78862

Merged
merged 9 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -243,12 +243,11 @@ private void DecodeInternal(ReadOnlySpan<byte> data, IHttpStreamHeadersHandler h
// will no longer be valid.
if (_headerNameRange != null)
{
EnsureStringCapacity(ref _headerNameOctets, _stringLength, existingLength: 0);
EnsureStringCapacity(ref _headerNameOctets, _headerNameLength, existingLength: 0);
_headerName = _headerNameOctets;

ReadOnlySpan<byte> headerBytes = data.Slice(_headerNameRange.GetValueOrDefault().start, _headerNameRange.GetValueOrDefault().length);
headerBytes.CopyTo(_headerName);
_headerNameLength = headerBytes.Length;
_headerNameRange = null;
}
}
Expand Down Expand Up @@ -294,6 +293,7 @@ private void ParseHeaderName(ReadOnlySpan<byte> data, ref int currentIndex, IHtt
{
// Fast path. Store the range rather than copying.
_headerNameRange = (start: currentIndex, count);
_headerNameLength = _stringLength;
BrunoBlanes marked this conversation as resolved.
Show resolved Hide resolved
currentIndex += count;

_state = State.HeaderValueLength;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Linq;
using System.Net.Http.QPack;
using System.Net.Http.Unit.Tests.HPack;
using System.Text;

using Xunit;

namespace System.Net.Http.Unit.Tests.QPack
{
public class QPackDecoderTest
{
private const int MaxHeaderFieldSize = 8190;

// 4.5.6 - Literal Field Without Name Reference - (literal-header-field)
private static readonly byte[] _literalFieldWithoutNameReference = new byte[] { 0x37, 0x0d, 0x6c, 0x69, 0x74, 0x65, 0x72, 0x61, 0x6c, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x2d, 0x66, 0x69, 0x65, 0x6c, 0x64 };

private const string _headerNameString = "literal-header-field"; // 20 bytes
private const string _headerValueString = "should-not-break"; // 16 bytes

private static readonly byte[] _headerValueBytes = Encoding.ASCII.GetBytes(_headerValueString);

private static readonly byte[] _headerValue = new byte[] { (byte)_headerValueBytes.Length }
.Concat(_headerValueBytes)
.ToArray();

// The key take away here is that the header name length should be
// at least 2^n + 1 and the header value length less than or equal to 2^n.
// This is due to how System.Buffer buckets the arrays to be used by the decoder.
private static readonly byte[] _encodedLiteralField = _literalFieldWithoutNameReference
.Concat(_headerValue)
.ToArray();

private readonly QPackDecoder _decoder;
private readonly TestHttpHeadersHandler _handler = new TestHttpHeadersHandler();

public QPackDecoderTest()
{
_decoder = new QPackDecoder(MaxHeaderFieldSize);
}

[Fact]
public void LiteralFieldWithoutNameReferece_SingleBuffer()
{
_decoder.Decode(new byte[] { 0, 0 }, endHeaders: false, handler: _handler);
_decoder.Decode(_encodedLiteralField, endHeaders: true, handler: _handler);

Assert.Equal(1, _handler.DecodedHeaders.Count);
Assert.Equal(_headerNameString, _handler.DecodedHeaders.Keys.First());
Assert.Equal(_headerValueString, _handler.DecodedHeaders.Values.First());
}

[Fact]
public void LiteralFieldWithoutNameReferece_NameLengthBrokenIntoSeparateBuffers()
{
_decoder.Decode(new byte[] { 0, 0 }, endHeaders: false, handler: _handler);
_decoder.Decode(_encodedLiteralField[..1], endHeaders: false, handler: _handler);
_decoder.Decode(_encodedLiteralField[1..], endHeaders: true, handler: _handler);

Assert.Equal(1, _handler.DecodedHeaders.Count);
Assert.Equal(_headerNameString, _handler.DecodedHeaders.Keys.First());
Assert.Equal(_headerValueString, _handler.DecodedHeaders.Values.First());
}

[Fact]
public void LiteralFieldWithoutNameReferece_NameBrokenIntoSeparateBuffers()
{
_decoder.Decode(new byte[] { 0, 0 }, endHeaders: false, handler: _handler);
_decoder.Decode(_encodedLiteralField[..(_headerNameString.Length / 2)], endHeaders: false, handler: _handler);
_decoder.Decode(_encodedLiteralField[(_headerNameString.Length / 2)..], endHeaders: true, handler: _handler);

Assert.Equal(1, _handler.DecodedHeaders.Count);
Assert.Equal(_headerNameString, _handler.DecodedHeaders.Keys.First());
Assert.Equal(_headerValueString, _handler.DecodedHeaders.Values.First());
}

[Fact]
public void LiteralFieldWithoutNameReferece_NameAndValueBrokenIntoSeparateBuffers()
{
_decoder.Decode(new byte[] { 0, 0 }, endHeaders: false, handler: _handler);
_decoder.Decode(_encodedLiteralField[..^_headerValue.Length], endHeaders: false, handler: _handler);
_decoder.Decode(_encodedLiteralField[^_headerValue.Length..], endHeaders: true, handler: _handler);

Assert.Equal(1, _handler.DecodedHeaders.Count);
Assert.Equal(_headerNameString, _handler.DecodedHeaders.Keys.First());
Assert.Equal(_headerValueString, _handler.DecodedHeaders.Values.First());
}

[Fact]
// Ideally should be ran with a value length of or bigger than 2 bytes
public void LiteralFieldWithoutNameReferece_ValueLengthBrokenIntoSeparateBuffers()
{
_decoder.Decode(new byte[] { 0, 0 }, endHeaders: false, handler: _handler);
_decoder.Decode(_encodedLiteralField[..^(_headerValue.Length - 1)], endHeaders: false, handler: _handler);
_decoder.Decode(_encodedLiteralField[^(_headerValue.Length - 1)..], endHeaders: true, handler: _handler);

Assert.Equal(1, _handler.DecodedHeaders.Count);
Assert.Equal(_headerNameString, _handler.DecodedHeaders.Keys.First());
Assert.Equal(_headerValueString, _handler.DecodedHeaders.Values.First());
}

[Fact]
public void LiteralFieldWithoutNameReferece_ValueBrokenIntoSeparateBuffers()
{
_decoder.Decode(new byte[] { 0, 0 }, endHeaders: false, handler: _handler);
_decoder.Decode(_encodedLiteralField[..^(_headerValueString.Length / 2)], endHeaders: false, handler: _handler);
_decoder.Decode(_encodedLiteralField[^(_headerValueString.Length / 2)..], endHeaders: true, handler: _handler);

Assert.Equal(1, _handler.DecodedHeaders.Count);
Assert.Equal(_headerNameString, _handler.DecodedHeaders.Keys.First());
Assert.Equal(_headerValueString, _handler.DecodedHeaders.Values.First());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@
<Compile Include="HttpContentTest.cs" />
<Compile Include="HttpRuleParserTest.cs" />
<Compile Include="MockContent.cs" />
<Compile Include="QPack\QPackDecoderTest.cs" />
<Compile Include="RuntimeSettingParserTest.cs" />
<Compile Include="StreamToStreamCopyTest.cs" />
<Compile Include="HttpEnvironmentProxyTest.cs" />
Expand Down Expand Up @@ -393,8 +394,12 @@
Link="Common\System\Net\Http\aspnetcore\Http3\QPack\HeaderField.cs" />
<Compile Include="$(CommonPath)System\Net\Http\aspnetcore\Http3\QPack\QPackEncoder.cs"
Link="Common\System\Net\Http\aspnetcore\Http3\QPack\QPackEncoder.cs" />
<Compile Include="$(CommonPath)System\Net\Http\aspnetcore\Http3\QPack\QPackDecoder.cs"
Link="Common\System\Net\Http\aspnetcore\Http3\QPack\QPackDecoder.cs" />
<Compile Include="$(CommonPath)System\Net\Http\aspnetcore\Http3\QPack\QPackEncodingException.cs"
Link="Common\System\Net\Http\aspnetcore\Http3\QPack\QPackEncodingException.cs" />
<Compile Include="$(CommonPath)System\Net\Http\aspnetcore\Http3\QPack\QPackDecodingException.cs"
Link="Common\System\Net\Http\aspnetcore\Http3\QPack\QPackDecodingException.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs"
Link="Common\System\Text\ValueStringBuilder.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.AppendSpanFormattable.cs"
Expand Down