Skip to content
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
16 changes: 6 additions & 10 deletions src/clients/ioxide.httpclient/Http1/HttpClientConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,11 @@ private unsafe int AppendAvailable(HttpClientResponse response, long limit)
/// </summary>
/// <remarks>
/// Structure and hardening are Glyph11's: bare LF, obs-fold, whitespace before the colon,
/// token and field-value charsets, duplicate and malformed Content-Length, and the
/// token and field-value charsets, duplicate and malformed Content-Length, the
/// Transfer-Encoding + Content-Length pair that lets a proxy and a client disagree about where
/// this response ends. What stays here is what Glyph11 has no opinion on - copying the fields
/// the caller keeps, and connection reuse.
/// this response ends, and Transfer-Encoding from an HTTP/1.0 sender, which RFC 9112 6.1 calls
/// faulty framing. What stays here is what Glyph11 has no opinion on - copying the fields the
/// caller keeps, and connection reuse.
/// </remarks>
private unsafe int TryParseHead(HttpClientResponse response, HttpClientRequest request)
{
Expand All @@ -488,11 +489,10 @@ private unsafe int TryParseHead(HttpClientResponse response, HttpClientRequest r
ReadOnlyMemory<byte> buffered = ReceiveBufferAsMemory();

bool complete;
int bytesRead;
int headSize;
try
{
complete = UltraHardenedParser.TryExtractFullResponseHeaderROM(
ref buffered, _head, in _limits, out bytesRead);
complete = UltraHardenedParser.TryParseResponseHeader(ref buffered, _head, in _limits, out headSize);
}
catch (HttpParseException e)
{
Expand All @@ -506,10 +506,6 @@ private unsafe int TryParseHead(HttpClientResponse response, HttpClientRequest r
return 0;
}

// Glyph11 reports one less than the block's real size - see the note in its diff harness,
// "C# returns total-1; the C ABI returns the clean total".
int headSize = bytesRead + 1;

response.Status = _head.Status;

for (int i = 0; i < _head.Headers.Count; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/clients/ioxide.httpclient/ioxide.httpclient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<!-- Response parsing. Hardened HTTP/1.1 is a solved problem and a bad one to solve twice:
obs-fold, bare LF, duplicate Content-Length and the Transfer-Encoding smuggling checks
are where the CVEs live. Pure managed, no native asset, no transitive dependency. -->
<PackageReference Include="Glyph11" Version="0.3.7" />
<PackageReference Include="Glyph11" Version="0.4.1" />
</ItemGroup>

<ItemGroup>
Expand Down
103 changes: 103 additions & 0 deletions tests/Ioxide.Tests.Http/HttpClientHttp10Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Text;
using ioxide;
using ioxide.httpclient;

namespace Ioxide.Tests;

/// <summary>
/// RFC 9112 6.1: a recipient of an HTTP/1.0 message with a Transfer-Encoding "MUST treat the message as if the framing
/// is faulty, even if a Content-Length is present, and close the connection after processing the message".
/// </summary>
internal static class HttpClientHttp10Tests
{
private static int _connections;

public static void Register(Runner runner)
{
(string Path, string What)[] refused =
[
("/chunked", "an HTTP/1.0 response with Transfer-Encoding"),
("/notmodified", "an HTTP/1.0 304 with Transfer-Encoding, though it has no body to frame"),
];

foreach ((string path, string what) in refused)
{
runner.Test($"httpclient h1: refuses {what}", () =>
{
int origin = TestServer.Start(OriginHandler);
int proxy = HttpClientTests.StartProxy(origin, poolSize: 1);

(_, string body) = Client.Get(proxy, path);
Assert.True(body.StartsWith("599|") && body.Contains("HTTP/1.0"),
$"{what} should have been refused, got: {body}");
});
}

runner.Test("httpclient h1: control: an HTTP/1.0 response with Content-Length is read", () =>
{
int origin = TestServer.Start(OriginHandler);
int proxy = HttpClientTests.StartProxy(origin, poolSize: 1);

(_, string body) = Client.Get(proxy, "/length");
Assert.Equal("200|hello", body);
});

runner.Test("httpclient h1: an HTTP/1.0 response with Transfer-Encoding closes its connection, though it asked for keep-alive", () =>
{
int origin = TestServer.Start(OriginHandler);
// Its handler skips TestServer's request-less probe, whose stray request could close the connection mid-test.
int proxy = HttpClientDnsTests.StartProxy(new HttpClientOptions { Host = "127.0.0.1", Port = (ushort)origin, PoolSize = 1 });

// Control: keep-alive holds an HTTP/1.0 connection, so a reuse would show.
(_, string first) = Client.Get(proxy, "/conn");
Assert.True(first.StartsWith("200|connection "), $"expected a numbered connection, got: {first}");
Assert.Equal(first, Client.Get(proxy, "/conn").Body);

(_, string body) = Client.Get(proxy, "/keepalive");
Assert.True(body.StartsWith("599|"), $"its framing is faulty, and it was read as chunked: {body}");

(_, string next) = Client.Get(proxy, "/conn");
Assert.True(next.StartsWith("200|connection ") && next != first, $"its connection was reused: {next}");
});
}

private static string KeptAlive(string body)
=> $"HTTP/1.0 200 OK\r\nConnection: keep-alive\r\nContent-Length: {body.Length}\r\n\r\n{body}";

private static async Task OriginHandler(Reactor reactor, TcpConnection connection)
{
int number = 0;
try
{
while (true)
{
RecvSnapshot snapshot = await connection.ReadAsync();
string path = Wire.ReadPath(connection, snapshot);
if (path == "/conn" && number == 0)
{
number = Interlocked.Increment(ref _connections);
}

connection.Write(Encoding.ASCII.GetBytes(path switch
{
"/chunked" => "HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n0\r\n\r\n",
"/notmodified" => "HTTP/1.0 304 Not Modified\r\nTransfer-Encoding: chunked\r\n\r\n",
"/keepalive" => "HTTP/1.0 200 OK\r\nConnection: keep-alive\r\nTransfer-Encoding: chunked\r\n\r\n"
+ "5\r\nhello\r\n0\r\n\r\n",
"/conn" => KeptAlive($"connection {number}"),
_ => "HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\nhello",
}));

await connection.FlushAsync();
if (snapshot.IsClosed)
{
return;
}
}
}
finally
{
connection.DecRef();
}
}
}
1 change: 1 addition & 0 deletions tests/Ioxide.Tests.Http/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private static int Main()
RingHttpClientTests.Register(runner);
HttpClientChunkedTests.Register(runner);
HttpClientResponseLimitTests.Register(runner);
HttpClientHttp10Tests.Register(runner);
HttpClientThreadTests.Register(runner);
TlsClientTests.Register(runner);

Expand Down
Loading