// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What this server must do with input nobody wrote. //! //! Everything here is a property rather than an example — the unit tests have //! the examples. The properties are the ones that would be a vulnerability if //! they were false: //! //! * `paths` is a security boundary, so it gets the strongest one: no input //! may produce a path that leaves the root, no decoded segment may contain a //! separator, and the answer must be a fixed point, or a backend that //! resolves twice gets a different file the second time. //! * `exchange` runs whole requests through the driver and then *parses what //! came back*, insisting that the output is a whole number of well-framed //! HTTP responses with nothing left over. That is the property request //! smuggling breaks: a server that can be made to emit a response whose //! declared length does not match the octets after it has desynchronised //! the stream, and anything in front of it will read the difference as a //! reply to a request that was never made. //! * `head` insists that a head the parser accepted is one it would write out //! the same way — every name a token, every value free of the octets that //! would re-frame the message around it. //! //! Each target is an ordinary test as well as a fuzz target: without `--fuzz` //! it runs the seeds beside it, so `zig build test` exercises the same //! properties on inputs that have already been interesting once. //! //! `zig build fuzz --fuzz` searches them with Zig's own fuzzer, steered by //! coverage because the test binary is built with LLVM; `tools/fuzz.zig` is a //! loop of our own that runs them from a corpus instead. const builtin = @import("builtin"); const std = @import("std"); const Io = std.Io; const Allocator = std.mem.Allocator; const testing = std.testing; const Smith = std.testing.Smith; const http = @import("http"); const protocol = http.protocol; /// The allocator the targets run against. /// /// Under `zig build test` that is the testing allocator, which reports a leak /// as a failure. `tools/fuzz.zig` cannot name it — it is not a test build — /// so it sets this to a checked allocator of its own instead. pub var backing: Allocator = if (builtin.is_test) testing.allocator else undefined; /// The `Io` the targets that need one run against. /// /// Only `h2-exchanges` does. The HTTP/1.x driver is fuzzed on `Io.failing`, /// which is the stronger test — a driver that reached for I/O would fail /// loudly — but the HTTP/2 driver gives each stream a task, so an `Io` with /// no concurrency would make it refuse every stream and the target would /// fuzz nothing but the refusal. `std.testing.io` cannot be named outside a /// test build, so `tools/fuzz.zig` stands up an `Io.Threaded` and sets this. pub var io: Io = if (builtin.is_test) testing.io else undefined; // -- request heads ---------------------------------------------------------- const head_seeds = [_][]const u8{ "GET / HTTP/1.1\r\nHost: a\r\n\r\n", "POST /x?y=1 HTTP/1.1\r\nHost: a\r\nContent-Length: 3\r\n\r\n", "GET http://a/b HTTP/1.0\r\n\r\n", "OPTIONS * HTTP/1.1\r\nHost: a\r\n\r\n", "CONNECT a:443 HTTP/1.1\r\nHost: a:443\r\n\r\n", "GET / HTTP/1.1\r\nHost: a\r\nX: 1\r\nX: 2\r\nCookie: a=b; c=d\r\n\r\n", // Shapes that must be refused. "GET / HTTP/1.1\r\nX: a\r\n b\r\n\r\n", "GET / HTTP/1.1\r\nContent-Length : 5\r\n\r\n", "GET / HTTP/1.1\r\n\r\n", "GET / HTTP/9.9\r\n\r\n", "\r\n\r\n", "", }; /// A head the parser accepted is one whose pieces are all well formed, which /// is the same as saying that writing it back out cannot change the framing. fn headProperty(input: []const u8) !void { var fields: std.ArrayList(protocol.Field) = .empty; defer fields.deinit(backing); const parsed = http.http1.head.parse(input, backing, &fields, 64) catch return; if (!protocol.token.isToken(parsed.method.name)) return error.MethodIsNotAToken; if (parsed.target.raw.len == 0) return error.EmptyTarget; switch (parsed.target.form) { .origin, .absolute => { if (parsed.target.path.len == 0 or parsed.target.path[0] != '/') { return error.PathIsNotRooted; } }, .authority, .asterisk => {}, } for (fields.items) |field| { if (!protocol.token.isToken(field.name)) return error.FieldNameIsNotAToken; if (!protocol.token.isFieldValue(field.value)) return error.FieldValueWouldReframe; // Everything a field points at must be inside the head it came from, // or the handler is reading somebody else's memory. if (@intFromPtr(field.name.ptr) < @intFromPtr(input.ptr)) return error.FieldEscapedTheHead; if (@intFromPtr(field.value.ptr) + field.value.len > @intFromPtr(input.ptr) + input.len) return error.FieldEscapedTheHead; } } test "fuzz request heads" { for (head_seeds) |seed| try headProperty(seed); try testing.fuzz({}, fuzzHead, .{}); } fn fuzzHead(_: void, smith: *Smith) !void { var buffer: [2048]u8 = undefined; const len = smith.slice(&buffer); try headProperty(buffer[0..len]); } // -- response heads, which a client reads and must not be fooled by --------- const response_seeds = [_][]const u8{ "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\n", "HTTP/1.1 204 \r\n\r\n", "HTTP/1.0 200 OK\r\n\r\n", "HTTP/1.1 301 Moved\r\nLocation: /x\r\n\r\n", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n", // The framing disagreement §6.3 calls unrecoverable, which must come out // as `invalid` rather than as either framing. "HTTP/1.1 200 OK\r\nContent-Length: 5\r\nTransfer-Encoding: chunked\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 1, 1\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 1, 2\r\n\r\n", "HTTP/1.1 999 \r\n\r\n", "HTTP/1.1 200\r\n\r\n", }; fn responseProperty(input: []const u8) !void { var fields: std.ArrayList(protocol.Field) = .empty; defer fields.deinit(backing); const parsed = http.http1.head.parseResponse(input, backing, &fields, 64) catch return; // §4 of RFC 9112: three digits, and a class that exists. if (parsed.status < 100 or parsed.status > 999) return error.StatusOutsideRange; if (!protocol.token.isFieldValue(parsed.reason)) return error.ReasonWouldReframe; for (fields.items) |field| { if (!protocol.token.isToken(field.name)) return error.FieldNameIsNotAToken; if (!protocol.token.isFieldValue(field.value)) return error.FieldValueWouldReframe; if (@intFromPtr(field.name.ptr) < @intFromPtr(input.ptr)) return error.FieldEscapedTheHead; if (@intFromPtr(field.value.ptr) + field.value.len > @intFromPtr(input.ptr) + input.len) return error.FieldEscapedTheHead; } // The framing has to be decidable, and a decision that is not one of the // five is a client that does not know where the body ends -- which is // where response splitting lives. var headers: protocol.Headers = .{ .fields = fields }; const framing = protocol.responseFraming(parsed.status, false, false, &headers); switch (framing) { .none, .chunked, .until_close, .invalid => {}, .exact => |n| if (n > std.math.maxInt(u64) / 2) return error.AbsurdLength, } // A response that declares both framings must never come out as either // of them: §6.3 rule 3 is the one an attacker asks for by name. const has_length = headers.get("content-length") != null; const has_encoding = headers.get("transfer-encoding") != null; if (has_length and has_encoding and framing != .invalid and framing != .none) { return error.TwoFramingsAccepted; } } fn fuzzResponse(_: void, smith: *Smith) !void { var buffer: [2048]u8 = undefined; const len = smith.slice(&buffer); try responseProperty(buffer[0..len]); } // -- the clients, fed by an adversarial server ------------------------------ const client_seeds = [_][]const u8{ "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nabc", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nabc\r\n0\r\n\r\n", "HTTP/1.0 200 OK\r\n\r\nno framing at all", "HTTP/1.1 204 \r\n\r\n", // The framing disagreement §6.3 calls unrecoverable. "HTTP/1.1 200 OK\r\nContent-Length: 5\r\nTransfer-Encoding: chunked\r\n\r\nabcde", // A chunk size that is not a size, and a chunked body that never ends. "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\nzz\r\n", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nab", // A length that promises more than arrives. "HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\nshort", // Two responses, which is what a kept-alive connection carries. "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\naHTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\nb", // Interim responses ahead of the final one: a `100`, two Early Hints, one // that frames itself as though it had a body, and a `101`, which is // final. "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok", "HTTP/1.1 103 Early Hints\r\nLink: ; rel=preload\r\n\r\n" ++ "HTTP/1.1 103 Early Hints\r\n\r\nHTTP/1.1 204 No Content\r\n\r\n", "HTTP/1.1 103 Early Hints\r\nContent-Length: 3\r\n\r\nHTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na", "HTTP/1.1 101 Switching Protocols\r\nUpgrade: x\r\n\r\nnot http", // Answers to a 100-continue expectation: refused outright, and refused // after a hint. "HTTP/1.1 417 Expectation Failed\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 103 Early Hints\r\n\r\nHTTP/1.1 401 Unauthorized\r\nContent-Length: 1\r\n\r\nx", }; /// An `InterimHook` that checks what it is told: an interim response, which /// is a 1xx and never a `101`. const InterimCheck = struct { told: usize = 0, wrong: bool = false, fn hook(i: *InterimCheck) http.client.Request.InterimHook { return .{ .context = i, .call = record }; } fn record(context: ?*anyopaque, head: http.client.Request.InterimHead) void { const i: *InterimCheck = @ptrCast(@alignCast(context.?)); i.told += 1; if (head.status < 100 or head.status > 199 or head.status == 101) i.wrong = true; } /// What a driver returned, judged together with what the hook was told. /// `final_101` is whether a `101` may be the final response, which it /// may only be over HTTP/1.1. fn check(i: *const InterimCheck, status: u16, final_101: bool, max: u8) !void { if (i.wrong) return error.HookToldOfFinalResponse; if (i.told > max) return error.MoreInterimResponsesThanAllowed; // §15.2 of RFC 9110: an interim response is never the response. if (status >= 100 and status <= 199 and !(final_101 and status == 101)) { return error.InterimResponseReturnedAsFinal; } } }; /// Everything a server can say to the HTTP/1.1 client, including things no /// server would. /// /// The properties are about not being fooled rather than about succeeding: /// almost every input is a failure, and what matters is that the failures are /// reported rather than papered over, and that a connection whose framing was /// uncertain is never offered for reuse -- because what is left on the wire /// would then be read as the next response, which is response splitting /// performed on oneself. fn clientProperty(input: []const u8, chunk: usize) !void { var conn: http.transport.memory.Connection = try .init(backing, input, .{ .chunk = chunk, .read_buffer_len = 256, }); defer conn.deinit(); var c = try http.http1.client.Connection.init(io, backing, conn.connection(), .{ .max_head_len = 1024, .max_fields = 32, .body_buffer_len = 256, }); // The transport connection is this function's, not the client's: the // memory transport frees its own buffers. _ = c.release(); defer c.deinit(); var interim: InterimCheck = .{}; // Content to send, and in half the runs a 100-continue expectation that // holds it back until the server asks. const response = c.send(.{ .method = protocol.Method.post, .host = "example.com", .target = "/", .body = .{ .exact = 2 }, .content = "hi", .expect_continue = chunk & 1 == 1, .interim = interim.hook(), }) catch { if (interim.wrong) return error.HookToldOfFinalResponse; return; }; try interim.check(response.status, true, 16); // §10.1.1 of RFC 9110: content never sent leaves the server waiting for // it or about to close, and the connection is neither's to reuse. if (!response.content_sent and c.canReuse()) return error.ReusableAfterUnsentContent; // §4 of RFC 9112: three digits, which is 100 through 999 -- not 100 // through 599. A code outside the five classes §15 of RFC 9110 defines is // carried rather than refused, because refusing it would make this // library the thing that broke rather than the server that sent it, and // `Response.class` reports `.unknown` for exactly that. The first version // of this property asserted 100..599 and the fuzzer found `HTTP/1.1 804` // in seconds -- the property was wrong, not the parser. if (response.status < 100 or response.status > 999) return error.StatusOutsideRange; const known = response.status >= 100 and response.status < 600; if (!known and response.class() != .unknown) return error.UnknownClassMisreported; if (known and response.class() == .unknown) return error.KnownClassMisreported; const has_length = response.headers.get("content-length") != null; const has_encoding = response.headers.get("transfer-encoding") != null; // A response that declared both framings and carried a body must never // have been accepted: §6.3 rule 3 calls that unrecoverable, and a client // that picked one would be guessing where the next response starts. if (has_length and has_encoding and response.body.present) { return error.TwoFramingsAccepted; } // Rule 1 outranks rule 3, so a `204` saying both is framed as bodiless // and that is right -- but whatever follows it on the wire is then not // reliably the next response, so the connection must not be offered for // one. The fuzzer found this shape before the client handled it. if (!response.body.present and (has_length or has_encoding)) { if (c.canReuse()) return error.ReusableAfterAmbiguousFraming; } const body = c.readAll(response, 1 << 16) catch { // A body that failed to arrive leaves the rest of a message on the // wire, so the connection must not be offered for another exchange. if (c.canReuse()) return error.ReusableAfterFailedBody; return; }; defer backing.free(body); // §6.3 rule 6: a declared length is the length, and a client that handed // back more or fewer octets than it promised has mis-framed the message. if (response.body.length) |declared| { if (declared != body.len) return error.BodyDisagreesWithLength; } } fn fuzzClient(_: void, smith: *Smith) !void { var buffer: [4096]u8 = undefined; const len = smith.slice(&buffer); const chunk: usize = smith.value(u2); try clientProperty(buffer[0..len], chunk); } /// An empty SETTINGS frame, which §3.4 of RFC 9113 requires a server to send /// first and the client refuses to proceed without. const h2_settings = [_]u8{ 0, 0, 0, 0x04, 0, 0, 0, 0, 0 }; const h2_client_seeds = [_][]const u8{ // A HEADERS frame with an indexed `:status: 200` and END_STREAM. &.{ 0, 0, 1, 0x01, 0x05, 0, 0, 0, 1, 0x88 }, // The same without END_STREAM, then a DATA frame that ends it. &.{ 0, 0, 1, 0x01, 0x04, 0, 0, 0, 1, 0x88, 0, 0, 3, 0x00, 0x01, 0, 0, 0, 1, 'a', 'b', 'c' }, // A RST_STREAM, which is a stream this server would not answer. &.{ 0, 0, 4, 0x03, 0, 0, 0, 0, 1, 0, 0, 0, 8 }, // A GOAWAY naming no streams. &.{ 0, 0, 8, 0x07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // A WINDOW_UPDATE of zero, which §6.9 makes an error. &.{ 0, 0, 4, 0x08, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // A frame whose type nothing defines, which §4.1 says to discard. &.{ 0, 0, 2, 0x63, 0, 0, 0, 0, 0, 0xde, 0xad }, // A `103` and then the final response; a `103` that ends the stream, // which §8.1 makes malformed; and a `101`, which §8.6 does. &.{ 0, 0, 5, 0x01, 0x04, 0, 0, 0, 1, 0x08, 0x03, '1', '0', '3', 0, 0, 1, 0x01, 0x05, 0, 0, 0, 1, 0x88 }, &.{ 0, 0, 5, 0x01, 0x05, 0, 0, 0, 1, 0x08, 0x03, '1', '0', '3' }, &.{ 0, 0, 5, 0x01, 0x04, 0, 0, 0, 1, 0x08, 0x03, '1', '0', '1', 0, 0, 1, 0x01, 0x05, 0, 0, 0, 1, 0x88 }, // A trailer section, after the body. &.{ 0, 0, 1, 0x01, 0x04, 0, 0, 0, 1, 0x88, 0, 0, 1, 0x00, 0x00, 0, 0, 0, 1, 'a', 0, 0, 7, 0x01, 0x05, 0, 0, 0, 1, 0x00, 0x01, 'x', 0x03, 'y', 'e', 's' }, }; /// Everything a server can put on an HTTP/2 connection after its SETTINGS. /// /// The prefix is fixed because the client refuses to go on without it, and a /// fuzzer that had to guess nine exact octets would spend its whole run in /// `handshake` -- the same reason the corpus seeds carry plausible frame /// headers rather than noise. fn h2ClientProperty(input: []const u8, chunk: usize) !void { var source: std.ArrayList(u8) = .empty; defer source.deinit(backing); try source.appendSlice(backing, &h2_settings); try source.appendSlice(backing, input); var conn: http.transport.memory.Connection = try .init(backing, source.items, .{ .chunk = chunk, .read_buffer_len = 512, }); defer conn.deinit(); var c = try http.http2.client.Connection.init(io, backing, conn.connection(), .{ .max_concurrent_streams = 2, .stream_receive_window = 1 << 16, .connection_receive_window = 1 << 17, }); _ = c.releaseConnection(); defer c.deinit(); c.handshake() catch return; var interim: InterimCheck = .{}; const handle = c.begin(.{ .method = protocol.Method.post, .host = "example.com", .target = "/", .body = .{ .exact = 2 }, .content = "hi", .expect_continue = chunk & 1 == 1, .interim = interim.hook(), }) catch return; const response = c.await(handle) catch { if (interim.wrong) return error.HookToldOfFinalResponse; return; }; try interim.check(response.status, false, 16); if (response.status < 100 or response.status > 999) return error.StatusOutsideRange; // §8.2.1: a field name that reached the caller must be usable as one -- // lowercase, a token, and not a pseudo-header, which is control data and // belongs to the message rather than to its fields. for (response.headers.fields.items) |field| { if (field.name.len == 0) return error.EmptyFieldName; if (field.name[0] == ':') return error.PseudoHeaderInFields; if (!protocol.token.isToken(field.name)) return error.FieldNameIsNotAToken; for (field.name) |ch| { if (ch >= 'A' and ch <= 'Z') return error.UppercaseFieldName; } } c.finish(handle); } fn fuzzH2Client(_: void, smith: *Smith) !void { var buffer: [4096]u8 = undefined; const len = smith.slice(&buffer); const chunk: usize = smith.value(u2); try h2ClientProperty(buffer[0..len], chunk); } const h3_client_seeds = [_][]const u8{ // A HEADERS frame whose field section is a single indexed `:status: 200`. &.{ 0x01, 0x03, 0x00, 0x00, 0xd9 }, // The same, followed by a DATA frame. &.{ 0x01, 0x03, 0x00, 0x00, 0xd9, 0x00, 0x03, 'a', 'b', 'c' }, // A DATA frame first, which §4.1 makes a malformed message. &.{ 0x00, 0x03, 'a', 'b', 'c' }, // A frame type §11.2.1 reserves for grease, which must be ignored. &.{ 0x21, 0x02, 0xde, 0xad, 0x01, 0x03, 0x00, 0x00, 0xd9 }, // A length that runs off the end of the stream. &.{ 0x01, 0x40, 0xff }, // A `100` and a `103` before the final response, a `103` and then // nothing, and a `101`, which §4.5 makes malformed. &.{ 0x01, 0x04, 0x00, 0x00, 0xff, 0x00, 0x01, 0x03, 0x00, 0x00, 0xd8, 0x01, 0x03, 0x00, 0x00, 0xd9, 0x00, 0x01, 'a' }, &.{ 0x01, 0x03, 0x00, 0x00, 0xd8 }, &.{ 0x01, 0x08, 0x00, 0x00, 0x5f, 0x09, 0x03, '1', '0', '1', 0x01, 0x03, 0x00, 0x00, 0xd9 }, }; /// Everything a server can put on an HTTP/3 response stream. /// /// Driven through `transport.memory_session`, which is the whole reason this /// target can exist: the driver is written against `transport.Session`, so a /// scripted answer reaches it without a QUIC handshake, a socket or a port. fn h3ClientProperty(input: []const u8) !void { var scripted: http.transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = input, }; defer scripted.deinit(backing); var c: http.http3.client.Connection = .init(backing, scripted.session(backing), .{ .max_fields = 32, .decode_scratch_len = 4096, .max_body_len = 1 << 16, }); c.start() catch return; var interim: InterimCheck = .{}; var exchange = c.send(.{ .method = protocol.Method.post, .host = "example.com", .target = "/", .body = .{ .exact = 2 }, .content = "hi", .expect_continue = input.len & 1 == 1, .interim = interim.hook(), }) catch { if (interim.wrong) return error.HookToldOfFinalResponse; return; }; defer exchange.deinit(backing); const response = exchange.response; try interim.check(response.status, false, 16); if (response.status < 100 or response.status > 999) return error.StatusOutsideRange; for (response.headers.fields.items) |field| { if (field.name.len == 0) return error.EmptyFieldName; // §4.3: control data is pseudo-header fields, and they belong to the // message rather than to its fields. if (field.name[0] == ':') return error.PseudoHeaderInFields; if (!protocol.token.isToken(field.name)) return error.FieldNameIsNotAToken; for (field.name) |ch| { if (ch >= 'A' and ch <= 'Z') return error.UppercaseFieldName; } } // The body is what the DATA frames carried and no more. if (response.body.length) |declared| { if (declared != exchange.body().len) return error.BodyDisagreesWithLength; } } fn fuzzH3Client(_: void, smith: *Smith) !void { var buffer: [4096]u8 = undefined; const len = smith.slice(&buffer); try h3ClientProperty(buffer[0..len]); } // -- request targets and the paths they resolve to -------------------------- const path_seeds = [_][]const u8{ "/", "/a/b/c", "/a/../b", "/a/./b/", "/%2e%2e/etc/passwd", "/a%2fb", "/a%00b", "/a//b///c", "/very/deep/../../../../../root", "/%", "/%zz", "/a?b=../../c", "http://example.com/a/../b", "*", "example.com:443", "", }; /// The resolved path may never leave the root, may never keep a dot segment, /// and resolving it again must give exactly the same answer — because /// anything downstream is entitled to resolve it a second time. fn pathProperty(input: []const u8) !void { const target = protocol.Target.parse(input) catch return; var buffer: [4096]u8 = undefined; const resolved = target.resolvePath(&buffer) catch return; if (resolved.len == 0 or resolved[0] != '/') return error.NotRooted; if (resolved.len > target.path.len) return error.GrewWhileResolving; if (std.mem.indexOfScalar(u8, resolved, 0) != null) return error.KeptANul; var segments = std.mem.splitScalar(u8, resolved[1..], '/'); while (segments.next()) |segment| { if (std.mem.eql(u8, segment, ".")) return error.KeptADotSegment; if (std.mem.eql(u8, segment, "..")) return error.KeptADotDotSegment; } // There is deliberately no "resolve it again and get the same answer" // check here, though it is the obvious one to want. A resolved path is // not a request target: resolving decodes, and the decoded octets mean // something else on the way back in. `/%2541` resolves to `/%41`, which // resolves again to `/A`; `/a%3fb` resolves to `/a?b`, whose `?` starts a // query the second time. Neither is a defect to be fixed here — they are // what percent-decoding *is* — and both are the reason `resolvePath` says // it must be applied exactly once and the decoded path carried from // there. // // The property that would have caught a dot-segment remover leaving // something for a second pass is above instead, and stated directly: the // answer holds no dot segments at all, so removing them again cannot // change it. } test "fuzz paths" { for (path_seeds) |seed| try pathProperty(seed); try testing.fuzz({}, fuzzPath, .{}); } fn fuzzPath(_: void, smith: *Smith) !void { var buffer: [1024]u8 = undefined; const len = smith.slice(&buffer); try pathProperty(buffer[0..len]); } // -- dates ------------------------------------------------------------------ const date_seeds = [_][]const u8{ "Sun, 06 Nov 1994 08:49:37 GMT", "Sunday, 06-Nov-94 08:49:37 GMT", "Sun Nov 6 08:49:37 1994", "Thu, 01 Jan 1970 00:00:00 GMT", "Wed, 31 Dec 1969 23:59:59 GMT", "Sun, 99 Nov 1994 08:49:37 GMT", "Sun, 06 Nov 1994 08:49:60 GMT", "", }; /// A date this understood must survive being written back out and read again, /// which is the only way to catch a formatter and a parser that have drifted /// apart. fn dateProperty(input: []const u8) !void { const seconds = protocol.date.parse(input) orelse return; var buffer: [protocol.date.imf_len]u8 = undefined; const written = protocol.date.formatImf(&buffer, seconds) catch return; const again = protocol.date.parse(written) orelse return error.WroteSomethingUnreadable; if (again != seconds) return error.NotAFixedPoint; } test "fuzz dates" { for (date_seeds) |seed| try dateProperty(seed); try testing.fuzz({}, fuzzDate, .{}); } fn fuzzDate(_: void, smith: *Smith) !void { var buffer: [64]u8 = undefined; const len = smith.slice(&buffer); try dateProperty(buffer[0..len]); } // -- whole exchanges -------------------------------------------------------- const exchange_seeds = [_][]const u8{ "GET / HTTP/1.1\r\nHost: a\r\n\r\n", "GET /a HTTP/1.1\r\nHost: a\r\n\r\nGET /b HTTP/1.1\r\nHost: a\r\n\r\n", "POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 5\r\n\r\nhello", "POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n0\r\n\r\n", "POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\n\r\n5;x=1\r\nhello\r\n0\r\nT: v\r\n\r\n", "POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 5\r\nExpect: 100-continue\r\n\r\nhello", "HEAD / HTTP/1.1\r\nHost: a\r\n\r\n", "GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n", // The shapes a smuggled request has. "POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 6\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\nG", "POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 6\r\nContent-Length: 0\r\n\r\nabcdef", "POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\n\r\nfffffffffffffffff\r\n", // The routes the handler splits on, so that the driver's other framing // paths are reachable by mutation rather than by luck. "GET /chunked HTTP/1.1\r\nHost: a\r\n\r\n", "POST /chunked HTTP/1.1\r\nHost: a\r\nContent-Length: 20\r\n\r\nabcdefghijklmnopqrst", "GET /nobody HTTP/1.1\r\nHost: a\r\n\r\n", "GET /interim HTTP/1.1\r\nHost: a\r\n\r\n", "GET /fail HTTP/1.1\r\nHost: a\r\n\r\n", "GET /upgrade HTTP/1.1\r\nHost: a\r\nConnection: Upgrade\r\nUpgrade: fuzz\r\n\r\nafterwards", "GET /upgrade HTTP/1.1\r\nHost: a\r\n\r\n", "", }; /// Whatever arrives, the server answers with a whole number of well-framed /// HTTP responses and nothing left over. /// /// "Nothing left over" is the part that matters. A trailing fragment means /// the server wrote octets that are not part of any response it declared, /// which is precisely the desynchronisation that request smuggling is built /// out of. fn exchangeProperty(input: []const u8, chunk: usize, limits: u2) !void { // The limits are part of the input, because most of the code that // enforces them is only reached when something hits them: a head that // does not fit is `431`, and a chunk-size line longer than the // connection's read buffer is a malformed chunk. With one fixed // configuration those branches are unreachable however long the fuzzer // runs. The tightest here is `http1.min_read_buffer_len` exactly. const read_buffer_len: usize, const max_head_len: usize = switch (limits) { 0 => .{ 512, 1024 }, 1 => .{ http.http1.min_read_buffer_len, 128 }, 2 => .{ 128, 256 }, 3 => .{ 4096, 8192 }, }; var conn: http.transport.memory.Connection = try .init(backing, input, .{ .chunk = chunk, .read_buffer_len = read_buffer_len, }); defer conn.deinit(); var transport_conn = conn.connection(); try http.http1.serve(Io.failing, backing, &transport_conn, .initStateless(dispatch), .{ .max_head_len = max_head_len, .max_header_count = 32, .max_requests = 8, .send_date = false, .server_name = null, }); // A response to `HEAD` declares the length the matching `GET` would have // had and sends none of it, so the octets the framing promises are // legitimately absent — and an oracle that has not seen the requests // cannot tell that from a server that lost them. Re-parsing the request // stream here to find out would mean the oracle disagreeing with the // parser under test about where requests begin, which is a worse problem // than the one it solves. So an input that mentions the method at all // keeps every other property — no crash, no leak, no hang — and skips // this one; `tests/e2e.zig` pins `HEAD` exactly instead. if (std.mem.indexOf(u8, input, "HEAD") != null) return; try checkResponses(conn.written()); } /// The handler the driver is fuzzed through, which is a small router rather /// than one fixed reply. /// /// It has to be. A handler that only ever calls `res.send` exercises one of /// the driver's framing paths — the one with a `Content-Length` — and leaves /// the chunked writer, the statuses that may not carry a body, interim /// responses and the whole protocol switch unreached. Routing on the path /// means the fuzzer explores them through the thing it already mutates. fn dispatch(req: *http.server.Request, res: *http.server.Response) anyerror!void { const path = req.target.path; if (std.mem.indexOf(u8, path, "upgrade") != null) { const stream = res.upgrade("fuzz") catch |err| switch (err) { // Both halves of this are worth reaching: the switch itself, and // the refusal of one the client never offered. error.NotOffered, error.UpgradeUnavailable, error.BodyNotConsumed => { return res.sendStatus(.upgrade_required); }, else => |e| return e, }; // Past here it is not HTTP any more. Read whatever is left and say // something back, which is what a protocol would do. _ = stream.input.discardRemaining() catch {}; stream.output.writeAll("switched") catch {}; stream.output.flush() catch {}; return; } if (std.mem.indexOf(u8, path, "fail") != null) { // Before anything is written, so the driver owes a clean `500`. return error.HandlerGaveUp; } if (std.mem.indexOf(u8, path, "nobody") != null) { return res.sendStatus(.no_content); } if (std.mem.indexOf(u8, path, "interim") != null) { var hints: protocol.Headers = .empty; defer hints.deinit(req.arena); try hints.append(req.arena, "Link", "; rel=preload"); try res.sendInterim(.early_hints, &hints); } const body = req.body.readAlloc(req.arena, 64 * 1024) catch |err| switch (err) { // A body the client did not finish sending is the client's problem, // not a reason for the handler to fail. error.ReadFailed, error.StreamTooLong => "", error.OutOfMemory => return error.OutOfMemory, }; try res.setHeader("Content-Type", "application/octet-stream"); if (std.mem.indexOf(u8, path, "chunked") != null) { // A length the handler does not know in advance, written in several // goes so that more than one chunk comes out. const w = try res.beginBody(.unknown); var rest = body; while (rest.len != 0) { const take = @min(rest.len, 7); try w.writeAll(rest[0..take]); try w.flush(); rest = rest[take..]; } return res.end(); } try res.send(body); } /// Walks the server's output as a sequence of responses, insisting that every /// octet belongs to one. fn checkResponses(output: []const u8) !void { var rest = output; while (rest.len != 0) { const head_end = std.mem.indexOf(u8, rest, "\r\n\r\n") orelse return error.UnterminatedHead; const head = rest[0 .. head_end + 2]; rest = rest[head_end + 4 ..]; var lines = std.mem.splitSequence(u8, head, "\r\n"); const status_line = lines.next() orelse return error.NoStatusLine; if (!std.mem.startsWith(u8, status_line, "HTTP/1.")) return error.NotAStatusLine; if (status_line.len < "HTTP/1.1 200 ".len) return error.TruncatedStatusLine; const code = std.fmt.parseInt(u16, status_line[9..12], 10) catch return error.NotAStatusCode; if (code < 100 or code > 599) return error.ImpossibleStatusCode; var content_length: ?u64 = null; var chunked = false; while (lines.next()) |line| { if (line.len == 0) continue; const colon = std.mem.indexOfScalar(u8, line, ':') orelse return error.FieldWithNoColon; const name = line[0..colon]; const value = protocol.token.trimOws(line[colon + 1 ..]); if (!protocol.token.isToken(name)) return error.FieldNameIsNotAToken; if (!protocol.token.isFieldValue(value)) return error.FieldValueWouldReframe; if (protocol.token.eqlIgnoreCase(name, "content-length")) { if (content_length != null) return error.TwoContentLengths; content_length = std.fmt.parseInt(u64, value, 10) catch return error.BadContentLength; } if (protocol.token.eqlIgnoreCase(name, "transfer-encoding")) chunked = true; } if (chunked and content_length != null) return error.BothFramingFields; // `101` means the stream stops being HTTP here: whatever follows // belongs to another protocol and is not ours to parse. if (code == 101) return; // An interim response has no body at all, whatever it declared. if (code >= 100 and code < 200) continue; // Nor do these two, and `304` is allowed to declare a length it does // not send -- it is the length the matching `200` would have had. if (code == 204 or code == 304) continue; if (chunked) { rest = try skipChunkedBody(rest); } else if (content_length) |n| { if (n > rest.len) return error.BodyShorterThanDeclared; rest = rest[@intCast(n)..]; } else { // No framing field: the body runs to the end of the connection, // which the driver only ever does on its way to closing. return; } } } fn skipChunkedBody(input: []const u8) ![]const u8 { var rest = input; while (true) { const line_end = std.mem.indexOf(u8, rest, "\r\n") orelse return error.UnterminatedChunk; const digits = rest[0..line_end]; const size = std.fmt.parseInt(u64, digits, 16) catch return error.BadChunkSize; rest = rest[line_end + 2 ..]; if (size == 0) { if (!std.mem.startsWith(u8, rest, "\r\n")) return error.UnterminatedTrailers; return rest[2..]; } if (size + 2 > rest.len) return error.ChunkShorterThanDeclared; rest = rest[@intCast(size)..]; if (!std.mem.startsWith(u8, rest, "\r\n")) return error.ChunkNotTerminated; rest = rest[2..]; } } test "fuzz exchanges" { for (exchange_seeds) |seed| { // Every combination of delivery and limits, so that `zig build test` // covers what the fuzzer would have to stumble on. for ([_]usize{ 0, 1 }) |chunk| { for ([_]u2{ 0, 1, 2, 3 }) |limits| { try exchangeProperty(seed, chunk, limits); } } } try testing.fuzz({}, fuzzExchange, .{}); } fn fuzzExchange(_: void, smith: *Smith) !void { var buffer: [4096]u8 = undefined; const len = smith.slice(&buffer); // `value` answers a read outside the type's range with the minimum, and // almost every random `u64` is outside a `u2`, so this is zero for most // inputs and only occasionally the slow path. That is the right way // round: delivering a whole request at once is what a socket usually // does. const chunk: usize = smith.value(u2); const limits = smith.value(u2); try exchangeProperty(buffer[0..len], chunk, limits); } // -- field values ----------------------------------------------------------- const field_seeds = [_][]const u8{ "gzip, chunked", "keep-alive, Upgrade", "text/html;q=0.9, */*;q=0.8", "one;p=\"a,b\", two", "one;p=\"a\\\",b\", two", ", ,a,,", "\"unterminated", "a=1&b=2&c", "%41%zz%2", "", }; /// The list grammar of RFC 9110 §5.6.1, and the percent-decoding beside it. /// /// The property worth having is that `containsToken` and a walk of the list /// cannot disagree: the fast answer that every framing decision in the driver /// is made from has to be the same answer as the slow one. fn fieldProperty(input: []const u8) !void { var elements: usize = 0; var last: ?[]const u8 = null; var it = protocol.token.list(input); while (it.next()) |element| { elements += 1; if (elements > input.len + 1) return error.ListDoesNotTerminate; // Elements are trimmed and never empty, which is what lets the // driver compare them directly. if (element.len == 0) return error.YieldedAnEmptyElement; if (protocol.token.isOws(element[0])) return error.YieldedUntrimmed; if (protocol.token.isOws(element[element.len - 1])) return error.YieldedUntrimmed; // And each one points inside the value it came from. if (@intFromPtr(element.ptr) < @intFromPtr(input.ptr)) return error.ElementEscaped; if (@intFromPtr(element.ptr) + element.len > @intFromPtr(input.ptr) + input.len) return error.ElementEscaped; last = element; } if (protocol.token.lastElement(input)) |l| { if (last == null or !std.mem.eql(u8, l, last.?)) return error.LastElementDisagrees; } else if (last != null) return error.LastElementDisagrees; // `containsToken` must agree with walking the list, for every element // that is in it and for one that is not. var again = protocol.token.list(input); while (again.next()) |element| { if (!protocol.token.containsToken(input, element)) return error.ContainsTokenDisagrees; } if (protocol.token.containsToken(input, "\x01not-a-token\x01")) { return error.ContainsTokenInventedOne; } // Decoding never writes past the buffer it was given, whatever it is // handed. The buffer is deliberately the same length as the input, which // is the smallest that can always hold the result. const dest = try backing.alloc(u8, input.len); defer backing.free(dest); const decoded = protocol.Target.percentDecodeForm(dest, input) catch return; if (decoded.len > input.len) return error.DecodingGrew; } test "fuzz field values" { for (field_seeds) |seed| try fieldProperty(seed); try testing.fuzz({}, fuzzField, .{}); } fn fuzzField(_: void, smith: *Smith) !void { var buffer: [512]u8 = undefined; const len = smith.slice(&buffer); try fieldProperty(buffer[0..len]); } // -- HPACK ------------------------------------------------------------------ const hpack_seeds = [_][]const u8{ // The Appendix C blocks, which is what stands in for coverage feedback: // random bytes are almost never a field section, and starting from one // that decodes is what gets past the first octet. "\x82\x86\x84\x41\x0fwww.example.com", "\x82\x86\x84\xbe\x58\x08no-cache", "\x40\x0acustom-key\x0dcustom-header", "\x04\x0c/sample/path", "\x10\x08password\x06secret", "\x82", // And the shapes that must be refused. "\x80", // index zero "\x00", // a literal whose name length is missing "\xbf\xff\xff\xff\xff\xff\xff\xff", // an integer that never ends "\x3f\xe1\xff\xff\xff\x07", // a table size update above any limit "\x00\x81\xff", // over-long Huffman padding "\x00\x84\xff\xff\xff\xff", // an explicit EOS "", }; /// A field section nobody wrote must cost what this endpoint said it would, /// and nothing it returns may point at memory that is about to move. /// /// Three properties, and each is a bug that has been shipped by somebody: /// /// * Nothing the decoder returns points into the block it was given or into /// the dynamic table. The block is a reassembly buffer the driver reuses, /// and the table is a ring that evicts — and can evict *during the very /// section that references it*. A `Headers` pointing into either is a /// use-after-free a handler reaches in the ordinary course of its work. /// * The dynamic table's recorded size is the sum of its entries' sizes and /// never exceeds its capacity, however it got there. The peer is keeping /// the same table by the same rules, and the moment the two disagree /// every index means something different to each end. /// /// What is deliberately *not* asserted here is that the fields are well /// formed. HPACK is a compression format: it carries whatever octets it was /// given, and RFC 7541 has nothing to say about an upper-case name or a value /// with a CR in it. Those are RFC 9113 §8.2.1 rules, they make a *request* /// malformed rather than a *connection* broken, and they are checked by the /// `h2-requests` target below — which is the layer that enforces them. The /// fuzzer found this distinction by disagreeing with an earlier version of /// this comment. fn hpackProperty(input: []const u8) !void { var arena_state: std.heap.ArenaAllocator = .init(backing); defer arena_state.deinit(); const arena = arena_state.allocator(); var scratch: [4096]u8 = undefined; var decoder: http.http2.hpack.Decoder = .init(4096, &scratch, .{ .max_header_list_size = 8192, }); defer decoder.deinit(backing); var fields: std.ArrayList(protocol.Field) = .empty; const result = decoder.decode(backing, arena, input, &fields); // Whatever happened, the table must still describe itself. var total: u32 = 0; for (decoder.table_.entries.items) |e| total += e.size(); if (total != decoder.table_.size) return error.TableSizeDisagrees; if (decoder.table_.size > decoder.table_.capacity) return error.TableOverCapacity; result catch return; for (fields.items) |f| { try expectOutsideOf(f.name, input); try expectOutsideOf(f.value, input); try expectOutsideOf(f.name, &scratch); try expectOutsideOf(f.value, &scratch); for (decoder.table_.entries.items) |e| { try expectOutsideOf(f.name, e.name); try expectOutsideOf(f.value, e.value); } } } // -- QPACK, and the HTTP/3 frame layer -------------------------------------- /// RFC 9204 §4.5: a field section this decoder accepted is one whose field /// lines are all either in the caller's own memory or in `.rodata`. /// /// The property is the same one HPACK's target checks and matters for a /// sharper reason here. A QPACK decoder returns a name or a value that is /// either a slice of the section it was given, a slice of the scratch it was /// handed, or a static table entry -- and the first two are the *caller's*, /// which is the contract that lets a driver reuse both for the next request. /// A decoder that returned anything pointing into state of its own would be /// handing out a borrow that the next section invalidates, and the bug would /// appear as one request's headers turning up in another's. fn qpackProperty(input: []const u8) !void { var fields: [64]http.http3.qpack.Field = undefined; var scratch: [4096]u8 = undefined; const n = http.http3.qpack.decode(input, &fields, &scratch) catch return; for (fields[0..n]) |f| { // Somewhere it may legitimately be: the section, the scratch, or the // static table. Anything else is memory the caller does not own. try expectWithinOneOf(f.name, input, &scratch); try expectWithinOneOf(f.value, input, &scratch); // Deliberately *not* checked here: that the name is lower case. // §4.1.1 of RFC 9114 requires a sender to write it that way and §4.2 // makes an upper-case one a malformed message, but neither is this // layer's rule -- QPACK's job is to decompress faithfully, and a // Huffman-coded name that decodes to `X-A` decoded correctly. The // rule belongs where the field section becomes a request, which is // what `h3-requests` below checks. Asserting it here failed within // seconds of the target existing, which was the property being wrong // rather than the decoder. } } /// Whether a slice lies in one of two regions, or is a static table entry. fn expectWithinOneOf(s: []const u8, a: []const u8, b: []const u8) !void { if (s.len == 0) return; if (within(s, a) or within(s, b)) return; // The remaining legitimate home: `.rodata`, which is to say an entry of // the static table, returned by pointer rather than copied because it // outlives everything. for (http.http3.qpack.table.entries) |e| { if (s.ptr == e.name.ptr or s.ptr == e.value.ptr) return; } return error.PointsIntoDecoderState; } fn within(s: []const u8, region: []const u8) bool { if (region.len == 0) return false; const at = @intFromPtr(s.ptr); const start = @intFromPtr(region.ptr); return at >= start and at + s.len <= start + region.len; } /// RFC 9114 §7.1: `parse` is total over the octets it is given. /// /// A frame header is a varint type and a varint length, so the only answers /// are a header or "not all of it has arrived" -- there is no input that is /// *wrong*, because §7.2.8 makes an unknown type something to step over /// rather than refuse. What this checks is that the arithmetic behind that /// never wraps: a length near 2^62 and a header length added to it is exactly /// where an overflow would be, and `end` returning null is how it says so. fn h3FrameProperty(input: []const u8) !void { const frame = http.http3.frame; const header = frame.parse(input) catch return; // The header occupied at least two octets and no more than there were. if (header.header_len < 2) return error.ImpossiblyShortHeader; if (header.header_len > input.len) return error.HeaderPastTheEnd; // §7.1: the length is a varint, so it cannot exceed what one holds -- // and `end` must refuse rather than wrap when the sum does not fit. if (header.length > (@as(u64, 1) << 62) - 1) return error.LengthTooLarge; if (header.end()) |end| { if (end < header.header_len) return error.EndBeforeTheStart; } // Writing the same header back and reading it again yields the same type // and the same length. // // The *widths* are deliberately not compared, and asserting they matched // failed within seconds of this target existing. §16 of RFC 9000 lets a // varint be written in any of four widths, and HTTP/3 puts no // minimal-encoding rule on a frame's length -- §12.4's one applies to // QUIC's frame types, not to these. So a peer may write 47 in two octets // and a writer that chooses one octet has not disagreed with it about // anything. What has to round trip is the value. var out: [32]u8 = undefined; if (frame.write(&out, header.kind, header.length)) |n| { const again = try frame.parse(out[0..n]); if (@backingInt(again.kind) != @backingInt(header.kind)) return error.TypeChanged; if (again.length != header.length) return error.LengthChanged; if (again.header_len != n) return error.WroteMoreThanItReads; } else |_| {} } /// RFC 9114 §4.2: a request this server accepted over HTTP/3 is one no /// downstream HTTP/1.1 recipient could be made to re-frame. /// /// The same property as `h2-requests`, reached through a different /// compressor, and worth checking twice because the path to it is different: /// HPACK's decoder and QPACK's decoder produce the field list that the shared /// `field.request` then judges, and a decoder that let an upper-case name or /// a value carrying CR, LF or NUL through would hand that judgement something /// the judgement was never shown. §4.2 is explicit that "any request or /// response that contains a character not permitted in a field value MUST be /// treated as malformed", and the reason is the gateway in front: one field /// with a CR in it becomes two fields, or a second request. fn h3RequestProperty(input: []const u8) !void { var arena_state: std.heap.ArenaAllocator = .init(backing); defer arena_state.deinit(); const arena = arena_state.allocator(); var decoded: [64]http.http3.qpack.Field = undefined; var scratch: [4096]u8 = undefined; const n = http.http3.qpack.decode(input, &decoded, &scratch) catch return; var fields: std.ArrayList(protocol.Field) = .empty; for (decoded[0..n]) |f| { const name = arena.dupe(u8, f.name) catch return; const value = arena.dupe(u8, f.value) catch return; fields.append(arena, .{ .name = name, .value = value }) catch return; } const req = http.field.request.build( Io.failing, arena, &fields, .{ .secure = true, .alpn = "h3" }, .{ .reader = http.server.Request.Body.empty_reader, .length = .none }, .{ .version = .http_3 }, ) catch return; if (req.version != .http_3) return error.WrongVersion; if (!protocol.token.isToken(req.method.name)) return error.MethodIsNotAToken; if (req.authority() == null) return error.AcceptedARequestWithNoAuthority; for (req.headers.fields.items) |f| { if (f.name.len == 0) return error.EmptyName; for (f.name) |c| { if (c >= 'A' and c <= 'Z') return error.UppercaseName; if (!protocol.token.isTokenChar(c)) return error.NameIsNotAToken; } for (f.value) |c| { if (c == '\r' or c == '\n' or c == 0) return error.ValueWouldReframe; } } } fn fuzzH3Request(_: void, smith: *Smith) !void { var buffer: [4096]u8 = undefined; const len = smith.slice(&buffer); try h3RequestProperty(buffer[0..len]); } fn fuzzQpack(_: void, smith: *Smith) !void { var buffer: [4096]u8 = undefined; const len = smith.slice(&buffer); try qpackProperty(buffer[0..len]); } fn fuzzH3Frame(_: void, smith: *Smith) !void { var buffer: [256]u8 = undefined; const len = smith.slice(&buffer); try h3FrameProperty(buffer[0..len]); } /// Sections that reach the interesting representations quickly: §4.5's two /// zero octets of prefix, then an indexed field line, a literal with a name /// reference, and a literal with a literal name. const qpack_seeds = [_][]const u8{ &.{ 0x00, 0x00 }, &.{ 0x00, 0x00, 0xd1 }, &.{ 0x00, 0x00, 0xd1, 0xd7, 0xc1 }, &.{ 0x00, 0x00, 0x5f, 0x09, 0x03, '4', '1', '8' }, &.{ 0x00, 0x00, 0x27, 'x', '-', 'a', 0x01, 'y' }, // A reference to the dynamic table, which is refused: capacity zero. &.{ 0x00, 0x00, 0x80 }, &.{ 0x01, 0x00 }, }; /// Frame headers: the small types, and a length wide enough to have a varint /// of every width. const h3_frame_seeds = [_][]const u8{ &.{ 0x01, 0x00 }, &.{ 0x00, 0x3c }, &.{ 0x04, 0x00 }, &.{ 0x07, 0x01, 0x00 }, &.{ 0x21, 0x00 }, &.{ 0x00, 0xbf, 0xff }, &.{ 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, }; fn expectOutsideOf(s: []const u8, region: []const u8) !void { if (s.len == 0 or region.len == 0) return; const at = @intFromPtr(s.ptr); const start = @intFromPtr(region.ptr); if (at >= start and at < start + region.len) return error.PointsIntoBorrowedMemory; } test "fuzz hpack" { for (hpack_seeds) |seed| try hpackProperty(seed); try testing.fuzz({}, fuzzHpack, .{}); } fn fuzzHpack(_: void, smith: *Smith) !void { var buffer: [4096]u8 = undefined; const len = smith.slice(&buffer); try hpackProperty(buffer[0..len]); } // -- requests built from a field section ------------------------------------ /// A request this server accepted is one no downstream HTTP/1.1 recipient /// could be made to re-frame. /// /// This is where RFC 9113 §8.2.1 lives, and the property is the h2-to-1.1 /// gateway smuggling class: if an accepted request carried an upper-case /// name, or a value with a CR, an LF or a NUL in it, then anything in front /// of this server that re-serialised to HTTP/1.1 would turn one field into /// two — or into a second request. fn h2RequestProperty(input: []const u8, extended_connect: bool) !void { var arena_state: std.heap.ArenaAllocator = .init(backing); defer arena_state.deinit(); const arena = arena_state.allocator(); var scratch: [4096]u8 = undefined; var decoder: http.http2.hpack.Decoder = .init(4096, &scratch, .{}); defer decoder.deinit(backing); var fields: std.ArrayList(protocol.Field) = .empty; decoder.decode(backing, arena, input, &fields) catch return; const req = http.http2.request.build(Io.failing, arena, &fields, .{}, .{ .reader = http.server.Request.Body.empty_reader, .length = .none, }, .{ .extended_connect = extended_connect }) catch return; // RFC 8441 §4: `:protocol` is defined only on CONNECT, and a request // carrying it must carry `:scheme` and `:path` too -- which is to say an // accepted one looks like every other request apart from its method. A // server that let `:protocol` through on a `GET`, or through at all // without having advertised the setting, would be answering a request // whose meaning no specification defines. if (req.connect_protocol) |name| { if (!extended_connect) return error.ProtocolPseudoHeaderWithoutTheSetting; if (!req.method.eql(protocol.Method.connect)) { return error.ProtocolOnSomethingOtherThanConnect; } if (!protocol.token.isToken(name)) return error.ProtocolIsNotAToken; if (req.target.scheme == null) return error.ExtendedConnectWithoutScheme; } if (!protocol.token.isToken(req.method.name)) return error.MethodIsNotAToken; switch (req.target.form) { .origin, .absolute => if (req.target.path.len == 0 or req.target.path[0] != '/') { return error.PathIsNotRooted; }, .asterisk, .authority => {}, } if (req.authority() == null) return error.AcceptedARequestWithNoAuthority; for (req.headers.fields.items) |f| { if (f.name.len == 0) return error.EmptyName; for (f.name) |c| { if (c >= 'A' and c <= 'Z') return error.UppercaseName; if (!protocol.token.isTokenChar(c)) return error.NameIsNotAToken; } if (!protocol.token.isFieldValue(f.value)) return error.ValueWouldReframe; // The connection-specific fields §8.2.2 forbids outright. for ([_][]const u8{ "connection", "proxy-connection", "keep-alive", "transfer-encoding", "upgrade", }) |forbidden| { if (std.mem.eql(u8, f.name, forbidden)) return error.AcceptedAHopByHopField; } } } test "fuzz h2 requests" { // Both ways round, because the setting changes what is malformed: // `:protocol` is a defined pseudo-header with it and an undefined one // without, and undefined is a stream error. for (hpack_seeds) |seed| try h2RequestProperty(seed, false); for (hpack_seeds) |seed| try h2RequestProperty(seed, true); try testing.fuzz({}, fuzzH2Request, .{}); } fn fuzzH2Request(_: void, smith: *Smith) !void { var buffer: [4096]u8 = undefined; const len = smith.slice(&buffer); const extended_connect = smith.value(bool); try h2RequestProperty(buffer[0..len], extended_connect); } // -- the Huffman code ------------------------------------------------------- const huffman_seeds = [_][]const u8{ "www.example.com", "no-cache", "custom-value", "\x00\x01\x02", "", "a", "Content-Type", "0123456789", }; /// Every byte string survives being coded and read back, and no byte string /// at all makes the decoder read outside its buffers. /// /// Both directions, because the interesting failure is asymmetric: decoding /// arbitrary octets is the attack surface, and `decode(encode(s)) == s` is /// the only thing that catches a table transcribed one row out of place — /// which a decode-only property would call a perfectly good decode of /// something else. fn huffmanProperty(input: []const u8) !void { const huffman = http.http2.hpack.huffman; // Decoding arbitrary octets never overruns, whatever they are. var small: [64]u8 = undefined; _ = huffman.decode(input, &small) catch {}; // And the round trip. const coded = try backing.alloc(u8, huffman.encodedLen(input, false) + 1); defer backing.free(coded); var w: std.Io.Writer = .fixed(coded); try huffman.encode(&w, input, false); if (w.end != huffman.encodedLen(input, false)) return error.LengthDisagrees; const back = try backing.alloc(u8, input.len + 1); defer backing.free(back); const got = huffman.decode(coded[0..w.end], back) catch return error.WroteSomethingUnreadable; if (!std.mem.eql(u8, input, got)) return error.NotAFixedPoint; } test "fuzz huffman" { for (huffman_seeds) |seed| try huffmanProperty(seed); try testing.fuzz({}, fuzzHuffman, .{}); } fn fuzzHuffman(_: void, smith: *Smith) !void { var buffer: [1024]u8 = undefined; const len = smith.slice(&buffer); try huffmanProperty(buffer[0..len]); } // -- frames ----------------------------------------------------------------- const frame_seeds = [_][]const u8{ "\x00\x00\x00\x04\x00\x00\x00\x00\x00", // SETTINGS, empty "\x00\x00\x08\x06\x00\x00\x00\x00\x0012345678", // PING "\x00\x00\x04\x03\x00\x00\x00\x00\x01\x00\x00\x00\x08", // RST_STREAM "\x00\x00\x04\x08\x00\x00\x00\x00\x00\x00\x00\x00\x01", // WINDOW_UPDATE "\x00\x00\x05\x02\x00\x00\x00\x00\x01\x00\x00\x00\x00\x10", // PRIORITY "\x00\x00\x08\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", // GOAWAY "\x00\x40\x00\x00\x01\x00\x00\x00\x01", // a full-size DATA header "\xff\xff\xff\x63\xff\xff\xff\xff\xff", // an unknown type, every bit set "", }; /// Nine octets are always a frame header, and the checks on one never /// disagree with themselves. /// /// `Header.parse` is total: there is no bit pattern it may refuse, because /// §4.1 requires an unknown type to be discarded and the reserved bit to be /// ignored, and a parser refusing either would refuse frames the RFC says to /// accept. So the property is that parse and write are inverse up to that /// reserved bit, that `checkHeader` answers for every header and every /// maximum, and that each payload parser handed an arbitrary slice either /// refuses it or returns values inside their declared ranges. fn frameProperty(input: []const u8) !void { const f = http.http2.frame; var rest = input; while (rest.len >= f.header_len) { const header = f.Header.parse(rest[0..f.header_len]); // Parse and write are inverse, the reserved bit excepted -- which // `parse` drops on purpose and `write` therefore cannot restore. var again: [f.header_len]u8 = undefined; header.write(&again); const reparsed = f.Header.parse(&again); if (reparsed.length != header.length) return error.LengthMoved; if (reparsed.type != header.type) return error.TypeMoved; if (reparsed.flags != header.flags) return error.FlagsMoved; if (reparsed.stream_id != header.stream_id) return error.StreamIdMoved; // `checkHeader` has an answer for everything, and never a stream-scoped // one for a frame that has no stream. for ([_]u24{ 16384, 16777215 }) |max| { if (f.checkHeader(header, max)) |fault| { if (fault.scope == .stream and header.stream_id == 0) { return error.StreamErrorWithoutAStream; } } } const payload_end = @min(rest.len, f.header_len + @as(usize, header.length)); const payload = rest[f.header_len..payload_end]; // Every payload parser, handed whatever is there. _ = f.parseRstStream(payload) catch {}; if (f.parseWindowUpdate(payload)) |increment| { if (increment == 0) return error.AcceptedAZeroIncrement; } else |_| {} if (f.parsePriority(payload)) |priority| { // The reserved bit must never survive into the dependency. if (priority.dependency > std.math.maxInt(u31)) return error.ReservedBitSurvived; } else |_| {} if (f.parseGoaway(payload)) |goaway| { if (goaway.debug.len + 8 != payload.len) return error.DebugDataWrongLength; } else |_| {} if (f.Padding.parse(header.length, if (payload.len > 0) payload[0] else 0)) |padding| { if (@as(usize, padding.data_len) + padding.pad_len + 1 != header.length) { return error.PaddingDoesNotAddUp; } } else |_| {} rest = rest[payload_end..]; if (payload_end == 0) break; } } test "fuzz frames" { for (frame_seeds) |seed| try frameProperty(seed); try testing.fuzz({}, fuzzFrame, .{}); } fn fuzzFrame(_: void, smith: *Smith) !void { var buffer: [2048]u8 = undefined; const len = smith.slice(&buffer); try frameProperty(buffer[0..len]); } // -- the table `tools/fuzz.zig` drives -------------------------------------- // -- the request a client emits, which nothing above this point watches ----- // // Every target above hands a client what a *server* said. Both faults these // drivers actually shipped were in the other direction -- a request that never // said how long its body was, and a FIN that travelled in a frame of its own // carrying nothing -- and no amount of fuzzing the response side could have // reached either, because neither is a parsing decision. They are things the // client writes, and until now nothing looked at what it wrote. // // So this fuzzes a caller instead. One request -- fuzzed target, fuzzed // authority, fuzzed body -- is handed to all three drivers, the octets each of // them put on the wire are read back, and the messages underneath compared. §8.1 of RFC 9113 and §4.1 of RFC 9114 each describe // their framing as carrying the message §3 of RFC 9112 spells out in ASCII, so // where two of these three disagree about what was sent, one of them is wrong. /// An emitted request, reduced to what all three versions say the same way. /// /// The strings are the arena's, not the transport's: each emitter takes its /// connection down before returning, and what is compared has to outlive that. const Emitted = struct { /// Which driver produced it, so that a disagreement names the two. version: []const u8, method: []const u8, authority: []const u8, path: []const u8, /// What `content-length` said, or null for a request that sent none. declared: ?u64, /// The content octets that reached the wire. body: []const u8, /// How many octets travelled *with* the end of the stream. Meaningful /// only over HTTP/3, where a stream is what delimits the body. fin_carried: usize = 0, }; /// Targets, authorities and bodies worth mutating, all from one corpus /// because a `Target` carries one -- and no worse for it: a mutated path /// makes a fine body, and an authority in the target's place is one of the /// shapes worth refusing. const emitted_request_seeds = [_][]const u8{ "/", "/index.html", "/a/b?c=d", "/dns-query", "/%20space", "hello, world", "{\"query\":\"a\"}", "example.com", "example.com:8443", "[::1]:443", }; const emitted_methods = [_]protocol.Method{ protocol.Method.get, protocol.Method.post, protocol.Method.put, protocol.Method.delete, protocol.Method.head, protocol.Method.options, protocol.Method.patch, protocol.Method.trace, }; /// The canned answer each driver needs before it will consider its work done. /// None of it is what is being tested; it is what stops `send` reporting a /// failure before the request it already wrote can be read back. const h1_answer = "HTTP/1.1 204 No Content\r\n\r\n"; const h2_answer = [_]u8{ 0, 0, 1, 0x01, 0x05, 0, 0, 0, 1, 0x88 }; const h3_answer = [_]u8{ 0x01, 0x03, 0x00, 0x00, 0xd9 }; fn emitOverHttp1(arena: Allocator, request: http.client.Request) !?Emitted { var conn: http.transport.memory.Connection = try .init(backing, h1_answer, .{ .chunk = 0, .read_buffer_len = 512, }); defer conn.deinit(); var c = try http.http1.client.Connection.init(io, backing, conn.connection(), .{ .max_head_len = 2048, .max_fields = 64, .body_buffer_len = 256, }); _ = c.release(); defer c.deinit(); // A refusal is an answer, not a fault: §6.1 of RFC 9112 has no chunked // over HTTP/1.0 and `declaredContentLength` refuses a length that // disagrees with the content, so a driver that declines to send is doing // its job. What is being checked is what happens when one does send. _ = c.send(request) catch return null; const wire = conn.written(); // §2.1: the head ends at the first empty line, and everything after it is // content. Finding it by search rather than by trusting the driver is the // point -- a head that ends somewhere else is the fault being looked for. const break_at = std.mem.indexOf(u8, wire, "\r\n\r\n") orelse return error.EmittedRequestHasNoHeadTerminator; const head = wire[0 .. break_at + 4]; const content = wire[break_at + 4 ..]; var fields: std.ArrayList(protocol.Field) = .empty; defer fields.deinit(backing); // Our own parser, reading our own output. A request this library emits // that this library cannot read back is malformed however it got that way, // and a target or an authority carrying a CR or an LF is how it gets that // way -- §2.2 of RFC 9112 makes that a request splitting attack written // by the caller and posted by us. const parsed = http.http1.head.parse(head, backing, &fields, 64) catch return error.EmittedRequestDoesNotParse; if (!std.mem.eql(u8, parsed.method.name, request.method.name)) { return error.MethodChangedOnTheWire; } if (!std.mem.eql(u8, parsed.target.raw, request.target)) { return error.TargetChangedOnTheWire; } var headers: protocol.Headers = .{ .fields = fields }; const authority = headers.get("host") orelse ""; if (!std.mem.eql(u8, authority, request.host)) return error.AuthorityChangedOnTheWire; // One field per field asked for, plus `Host`, plus at most one framing // field the driver added. Anything beyond that was injected rather than // sent, which is the same fault as the two above seen by counting. const framing_fields: usize = @intFromBool(request.body != .none); if (fields.items.len != request.headers.fields.items.len + 1 + framing_fields) { return error.EmittedRequestGrewFields; } // §6.1: chunked is a coding applied to the content, so what a comparison // wants is the content and not the chunks around it. const body = switch (request.body) { .unknown => request.content orelse "", else => content, }; return .{ .version = "HTTP/1.1", .method = try arena.dupe(u8, parsed.method.name), .authority = try arena.dupe(u8, authority), .path = try arena.dupe(u8, parsed.target.raw), .declared = if (headers.get("content-length")) |text| std.fmt.parseInt(u64, text, 10) catch return error.EmittedAnUnparseableContentLength else null, .body = try arena.dupe(u8, body), }; } /// Walks the field block and the content out of what an HTTP/2 client wrote. /// /// §4.1 gives every frame the same nine-octet header, so this needs to know /// nothing about the types it is not interested in -- which is the property /// that makes a client's output readable at all without a second HTTP/2. fn emitOverHttp2(arena: Allocator, request: http.client.Request) !?Emitted { var source: std.ArrayList(u8) = .empty; defer source.deinit(backing); try source.appendSlice(backing, &h2_settings); try source.appendSlice(backing, &h2_answer); var conn: http.transport.memory.Connection = try .init(backing, source.items, .{ .chunk = 0, .read_buffer_len = 4096, }); defer conn.deinit(); var c = try http.http2.client.Connection.init(io, backing, conn.connection(), .{ .max_concurrent_streams = 2, .stream_receive_window = 1 << 16, .connection_receive_window = 1 << 17, }); _ = c.releaseConnection(); defer c.deinit(); c.handshake() catch return null; const handle = c.begin(request) catch return null; _ = c.await(handle) catch {}; c.finish(handle); const wire = conn.written(); // §3.4: the connection preface comes first and is these exact octets. if (!std.mem.startsWith(u8, wire, http.http2.preface)) return error.EmittedNoPreface; var block: std.ArrayList(u8) = .empty; defer block.deinit(backing); var content: std.ArrayList(u8) = .empty; defer content.deinit(backing); var at: usize = http.http2.preface.len; var ended = false; var headers_done = false; while (at < wire.len) { if (at + 9 > wire.len) return error.EmittedATruncatedFrameHeader; const length = (@as(usize, wire[at]) << 16) | (@as(usize, wire[at + 1]) << 8) | @as(usize, wire[at + 2]); const kind = wire[at + 3]; const flags = wire[at + 4]; const payload = at + 9; if (payload + length > wire.len) return error.EmittedAFrameRunningOffTheEnd; switch (kind) { // HEADERS and CONTINUATION. §6.2: a block is contiguous, so a // CONTINUATION arriving after the block was closed is the // interleaving §4.3 forbids. 0x01, 0x09 => { if (headers_done) return error.EmittedAFieldBlockInTwoPieces; try block.appendSlice(backing, wire[payload..][0..length]); if (flags & 0x04 != 0) headers_done = true; if (flags & 0x01 != 0) ended = true; }, 0x00 => { try content.appendSlice(backing, wire[payload..][0..length]); if (flags & 0x01 != 0) ended = true; }, else => {}, } at = payload + length; } if (!headers_done) return error.EmittedAnUnterminatedFieldBlock; // §8.1: the message is the field block and the content, and the stream's // end is what says there is no more of it. A request that never ended its // half is a request the server is still waiting for. if (!ended) return error.EmittedNoEndOfStream; var scratch: [8192]u8 = undefined; var decoder: http.http2.hpack.Decoder = .init(4096, &scratch, .{}); defer decoder.deinit(backing); var fields: std.ArrayList(protocol.Field) = .empty; decoder.decode(backing, arena, block.items, &fields) catch return error.EmittedAFieldBlockThatDoesNotDecode; return try fromFields(arena, "HTTP/2", fields.items, content.items, 0); } fn emitOverHttp3(arena: Allocator, request: http.client.Request) !?Emitted { var scripted: http.transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = &h3_answer, }; defer scripted.deinit(backing); var c: http.http3.client.Connection = .init(backing, scripted.session(backing), .{ .max_fields = 64, .decode_scratch_len = 8192, .max_body_len = 1 << 16, }); c.start() catch return null; var exchange = c.send(request) catch return null; defer exchange.deinit(backing); // §2.1 of RFC 9000 numbers a client's bidirectional streams 0, 4, 8; the // scripted session hands out the first of them, and the request is on it. const state = scripted.find(.{ .value = 0 }) orelse return error.EmittedNoRequestStream; const wire = state.written.items; var block: []const u8 = &.{}; var content: std.ArrayList(u8) = .empty; defer content.deinit(backing); var at: usize = 0; var seen_headers = false; while (at < wire.len) { const header = http.http3.frame.parse(wire[at..]) catch return error.EmittedATruncatedFrameHeader; const end = header.end() orelse return error.EmittedAFrameLengthThatDoesNotFit; if (at + end > wire.len) return error.EmittedAFrameRunningOffTheEnd; const payload = wire[at + header.header_len ..][0..@intCast(header.length)]; switch (header.kind) { .headers => { // §4.1: one field section, and it comes first. if (seen_headers) return error.EmittedTwoFieldSections; if (content.items.len != 0) return error.EmittedDataBeforeItsFieldSection; block = payload; seen_headers = true; }, .data => { if (!seen_headers) return error.EmittedDataBeforeItsFieldSection; try content.appendSlice(backing, payload); }, else => {}, } at += end; } if (!seen_headers) return error.EmittedNoFieldSection; // §4.1 again: the stream's end is the end of the message, so a request // that did not end its half is one no server will answer. if (!state.finished) return error.EmittedNoEndOfStream; var decoded: [128]http.http3.qpack.Field = undefined; var scratch: [8192]u8 = undefined; const n = http.http3.qpack.decode(block, &decoded, &scratch) catch return error.EmittedAFieldSectionThatDoesNotDecode; var fields: std.ArrayList(protocol.Field) = .empty; for (decoded[0..n]) |f| { try fields.append(arena, .{ .name = try arena.dupe(u8, f.name), .value = try arena.dupe(u8, f.value), }); } return try fromFields(arena, "HTTP/3", fields.items, content.items, state.fin_carried); } /// The two versions that carry control data in pseudo-header fields, read the /// same way, because §4.3 of RFC 9114 defines them as HTTP/2's. fn fromFields( arena: Allocator, version: []const u8, fields: []const protocol.Field, content: []const u8, fin_carried: usize, ) !Emitted { var method: ?[]const u8 = null; var authority: ?[]const u8 = null; var path: ?[]const u8 = null; var declared: ?u64 = null; var ordinary_seen = false; for (fields) |field| { if (field.name.len == 0) return error.EmittedAnEmptyFieldName; // §8.3 of RFC 9113 and §4.3 of RFC 9114: every pseudo-header comes // before every ordinary field, and a duplicate of one is malformed. if (field.name[0] == ':') { if (ordinary_seen) return error.EmittedAPseudoHeaderAfterAnOrdinaryField; // §8.2.1 of RFC 9113: no CR, LF or NUL in a field value, which // §4.3 of RFC 9114 carries over and which applies to control // data as much as to a field. These two versions could carry // such an octet safely -- nothing here is framed by lines -- but // a gateway converting the message to HTTP/1.1 could not, and // then the splitting happens one hop further on. if (!protocol.token.isFieldValue(field.value)) { return error.EmittedAPseudoHeaderValueThatWouldReframe; } if (std.mem.eql(u8, field.name, ":method")) { if (method != null) return error.EmittedADuplicatePseudoHeader; method = field.value; } else if (std.mem.eql(u8, field.name, ":authority")) { if (authority != null) return error.EmittedADuplicatePseudoHeader; authority = field.value; } else if (std.mem.eql(u8, field.name, ":path")) { if (path != null) return error.EmittedADuplicatePseudoHeader; path = field.value; } continue; } ordinary_seen = true; // §8.2.1: "field names MUST be converted to lowercase", and a // recipient treats an uppercase one as malformed -- so emitting one // is emitting a message the peer must refuse. for (field.name) |ch| { if (ch >= 'A' and ch <= 'Z') return error.EmittedAnUppercaseFieldName; } if (!protocol.token.isToken(field.name)) return error.EmittedAFieldNameThatIsNotAToken; if (!protocol.token.isFieldValue(field.value)) return error.EmittedAFieldValueThatWouldReframe; // §8.2.2: these have no meaning here and make the message malformed. if (protocol.token.eqlIgnoreCase(field.name, "connection") or protocol.token.eqlIgnoreCase(field.name, "transfer-encoding") or protocol.token.eqlIgnoreCase(field.name, "keep-alive") or protocol.token.eqlIgnoreCase(field.name, "upgrade")) { return error.EmittedAConnectionSpecificField; } if (protocol.token.eqlIgnoreCase(field.name, "content-length")) { // Two of them is how a message is framed one way by its sender // and another by a gateway converting it to HTTP/1.1, which is // request smuggling arranged by the thing that sent it. if (declared != null) return error.EmittedTwoContentLengths; declared = std.fmt.parseInt(u64, field.value, 10) catch return error.EmittedAnUnparseableContentLength; } } return .{ .version = version, .method = try arena.dupe(u8, method orelse return error.EmittedNoMethod), .authority = try arena.dupe(u8, authority orelse return error.EmittedNoAuthority), .path = try arena.dupe(u8, path orelse return error.EmittedNoPath), .declared = declared, .body = try arena.dupe(u8, content), .fin_carried = fin_carried, }; } /// The property: the same request, on every version, says the same thing. fn emittedRequestProperty( target: []const u8, content: []const u8, host: []const u8, method_index: usize, body_kind: usize, with_content: bool, ) !void { var arena_state: std.heap.ArenaAllocator = .init(backing); defer arena_state.deinit(); const arena = arena_state.allocator(); const request: http.client.Request = .{ .method = emitted_methods[method_index % emitted_methods.len], .target = target, .host = host, .body = switch (body_kind % 3) { 0 => .none, 1 => .{ .exact = content.len }, else => .unknown, }, .content = if (with_content) content else null, }; var emitted: [3]?Emitted = .{ null, null, null }; emitted[0] = try emitOverHttp1(arena, request); emitted[1] = try emitOverHttp2(arena, request); emitted[2] = try emitOverHttp3(arena, request); // §4.1 of RFC 9114: an HTTP/3 request ends when its stream does, and // §19.8 of RFC 9000 permits the FIN to travel in a frame carrying no // data -- but a peer is within its rights to want the last octets on the // frame that ends the stream, and one that does answers a request framed // the other way with silence. So a body that was written must have gone // out with the end of the stream rather than ahead of it. if (emitted[2]) |h3| { if (h3.body.len > 0 and h3.fin_carried == 0) return error.TheFinTravelledAlone; } for (emitted, 0..) |maybe_one, i| { const one = maybe_one orelse continue; for (emitted[i + 1 ..]) |maybe_other| { const other = maybe_other orelse continue; if (!std.mem.eql(u8, one.method, other.method)) return error.VersionsDisagreeOnTheMethod; if (!std.mem.eql(u8, one.path, other.path)) return error.VersionsDisagreeOnThePath; if (!std.mem.eql(u8, one.authority, other.authority)) { return error.VersionsDisagreeOnTheAuthority; } if (!std.mem.eql(u8, one.body, other.body)) return error.VersionsDisagreeOnTheBody; // §8.6 of RFC 9110 asks a user agent to send `Content-Length` on // a request with content, and for a long time HTTP/2 and HTTP/3 // left it out on the ground that §8.1 and §4.1 made it optional. // A server is within its rights to insist, and one does: this is // the disagreement that cost a working DNS client its answers. const one_says = one.declared orelse std.math.maxInt(u64); const other_says = other.declared orelse std.math.maxInt(u64); if (one_says != other_says) return error.VersionsDisagreeOnTheContentLength; } } // And whatever was declared, it was the truth: §8.1.1 of RFC 9113 and // §4.1.2 of RFC 9114 both make a length that disagrees with the content // malformed, and a sender that does it has framed one message two ways. for (emitted) |maybe_one| { const one = maybe_one orelse continue; if (one.declared) |declared| { if (declared != one.body.len) return error.DeclaredALengthItDidNotSend; } } } fn fuzzEmittedRequest(_: void, smith: *Smith) !void { var target_buf: [512]u8 = undefined; var content_buf: [512]u8 = undefined; var host_buf: [512]u8 = undefined; const target_len = smith.slice(&target_buf); const content_len = smith.slice(&content_buf); // The authority is fuzzed too, and has to be: it is written into a `Host` // line of its own and splits exactly as a target does, so a target that // varied only the path would cover half of what `Request.validate` // refuses and none of the half that was found second. const host_len = smith.slice(&host_buf); const method_index: usize = smith.value(u3); const body_kind: usize = smith.value(u2); const with_content = smith.value(u1) == 1; try emittedRequestProperty( target_buf[0..target_len], content_buf[0..content_len], if (host_len == 0) "example.com" else host_buf[0..host_len], method_index, body_kind, with_content, ); } test "fuzz the request a client emits" { for (emitted_request_seeds) |seed| { try emittedRequestProperty(seed, "body", "example.com", 0, 0, false); try emittedRequestProperty("/", seed, "example.com", 1, 1, true); try emittedRequestProperty("/", seed, "example.com", 2, 2, true); // And the authority in the position that splits a `Host` line. try emittedRequestProperty("/", "body", seed, 0, 0, false); } try testing.fuzz({}, fuzzEmittedRequest, .{}); } // -- what a client says it will take --------------------------------------- const coding_seeds = [_][]const u8{ "gzip, deflate, br", "gzip;q=1.0, identity;q=0.5, *;q=0", "*", "*;q=0", "identity;q=0", "gzip;q=0.001", " GZIP ; Q=1.0 ", "x-gzip, zstd;q=0.9", "", ";q=", "q=1", "gzip;;q=0.5;q=1", }; /// §12.5.3 of RFC 9110 is a preference list a peer writes, which makes every /// octet of it hostile in the ordinary way -- and the answer decides what a /// server puts on the wire, so a wrong one is a body the client cannot read. fn codingProperty(input: []const u8) !void { const offers = [_][]const protocol.coding.Coding{ &.{ .gzip, .identity }, &.{ .identity, .gzip }, &.{.gzip}, &.{.identity}, &.{}, }; for (offers) |offered| { const chosen = protocol.coding.selectEncoding(input, offered) orelse continue; // Whatever comes back is something this caller said it could do. // Anything else is a coding applied by a server that cannot apply it. var on_offer = false; for (offered) |c| { if (c == chosen) on_offer = true; } if (!on_offer) return error.ChoseACodingNobodyOffered; // And something the client did not refuse. §12.5.3's `q=0` is a // refusal rather than a low preference, which is the rule that makes // this a parse rather than a membership test -- so it is checked here // by looking for the refusal directly rather than by trusting the // same parser that produced the answer. var it = protocol.token.list(input); while (it.next()) |element| { const semi = std.mem.indexOfScalar(u8, element, ';') orelse continue; const name = protocol.token.trimOws(element[0..semi]); const names_it = std.mem.eql(u8, name, "*") or (protocol.coding.Coding.parse(name) orelse continue) == chosen; if (!names_it) continue; // A `q=0` on the element that names it, with nothing else naming // it more specifically, would be a refusal being ignored. The // wildcard is skipped here because an explicit element overrides // it and this walk does not track which won. if (std.mem.eql(u8, name, "*")) continue; const rest = element[semi + 1 ..]; const eq = std.mem.indexOfScalar(u8, rest, '=') orelse continue; if (!protocol.token.eqlIgnoreCase(protocol.token.trimOws(rest[0..eq]), "q")) continue; const value = protocol.token.trimOws(rest[eq + 1 ..]); if (std.mem.eql(u8, value, "0") or std.mem.eql(u8, value, "0.0") or std.mem.eql(u8, value, "0.00") or std.mem.eql(u8, value, "0.000")) { return error.ChoseACodingTheClientRefused; } } } // `applied` reads a field a *sender* wrote, and has to be total for the // same reason: what it cannot undo it must say so about rather than // guess at. switch (protocol.coding.applied(input)) { .identity, .unsupported => {}, .one => |c| if (!c.canDecode()) return error.PromisedToUndoACodingItCannotDo, } } fn fuzzCoding(_: void, smith: *Smith) !void { var buffer: [512]u8 = undefined; const len = smith.slice(&buffer); try codingProperty(buffer[0..len]); } test "fuzz the content coding a client asks for" { for (coding_seeds) |seed| try codingProperty(seed); try testing.fuzz({}, fuzzCoding, .{}); } // -- a coded body, chosen by whoever sent it ------------------------------- /// A gzip member and a zstd frame, both valid, to mutate away from. /// /// The zstd one came from libzstd 1.5.7 and the gzip one from this library's /// own compressor, which is fine here: what is being fuzzed is what happens to /// octets a *peer* chose, and a valid frame is only the starting point the /// mutations walk away from. const coded_seeds = [_][]const u8{ // gzip: the shortest possible member, holding nothing. &.{ 0x1f, 0x8b, 0x08, 0x00, 0, 0, 0, 0, 0x00, 0x03, 0x03, 0x00, 0, 0, 0, 0, 0, 0, 0, 0 }, // zstd: a frame carrying repeated text. &.{ 0x28, 0xb5, 0x2f, 0xfd, 0x64, 0x20, 0x03, 0xfd, 0x01, 0x00, 0x22, 0x44, 0x0d, 0x11, 0xb0, 0xeb, 0x20, 0x4b, 0x84, 0x2d, 0x02, 0x96, 0x34, 0xc5, 0x98, 0x24, 0xe0, 0xa2, 0xc9, 0x42, 0x04, 0x0e, 0x4f, 0xe2, 0xae, 0xbb, 0x9c, 0x9b, 0xc7, 0x46, 0xa1, 0xd3, 0x54, 0x79, 0xb5, 0x91, 0x7c, 0x16, 0x4e, 0xba, 0xc5, 0xe0, 0x8d, 0x19, 0x0e, 0x3f, 0xaf, 0x24, 0xef, 0x4c, 0x62, 0x8b, 0xdf, 0x58, 0xb4, 0x08, 0x01, 0x00, 0xc2, 0xf6, 0xa2, 0x0a, 0x51, 0xa6, 0x6a, 0x4c, 0xe4, }, // The magic numbers alone, which is a header and then nothing. &.{ 0x1f, 0x8b }, &.{ 0x28, 0xb5, 0x2f, 0xfd }, }; /// The largest window any coding this library can undo asks for, which is /// zstd's: §3.1.1.1.2 of RFC 8878 lets a sender choose it and this is the /// ceiling on what will be accepted. const max_decode_window = protocol.coding.decodeBufferLen(.zstd); var window_one: [max_decode_window]u8 = undefined; var window_two: [max_decode_window]u8 = undefined; /// The front of a static buffer, cut to what this coding actually asks for -- /// handing a decoder more than it wants would not test the size it asserts. fn windowFor(what: protocol.coding.Coding, buffer: *[max_decode_window]u8) []u8 { return buffer[0..protocol.coding.decodeBufferLen(what)]; } /// What a decoder does with octets somebody else chose. /// /// This is the surface content codings added, and it is the worst kind: a /// format parser reading attacker-controlled input, where the *output* size is /// also the attacker's to choose. §8.4 of RFC 9110 puts no bound on what a /// coding expands to, so the bound is the caller's and is checked here. /// /// The property with teeth is the second one. A decoder is handed the same /// octets twice — once whole, once a few at a time through the memory /// transport, which is how they actually arrive over a socket — and has to /// produce the same answer. A parser that only works when its input arrives /// all at once is not a parser, which is the same thing `tests/e2e.zig` says /// about request heads; a decoder has more state to get that wrong with. /// /// # What it found /// /// A panic in Zig 0.16.0's flate decoder, within a quarter of an hour of /// existing. These twenty-five octets are a gzip header whose `FLG` is `0xfe` /// — nearly every optional-field flag set — followed by data that ends /// mid-bit-stream, while the decoder is reading a dynamic block's code /// lengths: /// /// 1f 8b 08 fe 64 00 00 d2 00 03 03 00 00 00 00 00 /// c6 00 00 00 0d 73 32 62 63 /// /// `tossBitsShort` added `consumed_bits` where it had to subtract them and so /// reported bits that were not there; the underflow surfaced later in /// `peekBitsEnding`, on a subtraction that is correct. Twenty-four octets do /// not reach it and neither do twenty-six: it is bit alignment, which is what /// a fuzzer is for and what nobody finds by reading. /// /// Fixed upstream by ziglang/zig#35815 for 0.17.0, which this library now /// requires. The octets are a test in `protocol/coding.zig` that has to come /// back `Malformed`, which is where a regression would show as a failure /// rather than as the suite gone. fn codedBodyProperty(input: []const u8, which: usize, chunk: usize) !void { const what: protocol.coding.Coding = if (which % 2 == 0) .gzip else .zstd; const limit: usize = 1 << 20; // Static, and not taken from the checked allocator, because the window is // scratch the decoder never reads before writing -- and because zstd's is // eight megabytes. Allocating and freeing that per input cost two orders // of magnitude of throughput: 32 thousand inputs a run where every other // target does millions, which is a fuzzer that is mostly an allocator // benchmark. const window = windowFor(what, &window_one); // Whole. const whole: ?[]u8 = protocol.coding.undo(backing, input, what, window, limit) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, // Each is a legitimate answer to octets a peer chose: not that // coding, or more of it than the caller allowed. error.Malformed, error.TooLarge, error.Unsupported => null, }; defer if (whole) |w| backing.free(w); // §8.4 gives a coding no ceiling, so the caller's is the only one there // is, and a decoder that went past it has spent memory nobody agreed to. if (whole) |w| { if (w.len > limit) return error.DecodedPastTheLimit; } // A few octets at a time, through the same transport a socket uses. var conn: http.transport.memory.Connection = try .init(backing, input, .{ .chunk = chunk, .read_buffer_len = 4096, }); defer conn.deinit(); const c = conn.connection(); var d = protocol.coding.Decoder.init(what, c.input, windowFor(what, &window_two)) catch return; const piecemeal: ?[]u8 = d.reader().allocRemaining(backing, .limited(limit)) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, else => null, }; defer if (piecemeal) |pm| backing.free(pm); // The same octets, so the same answer. if (whole) |a| { const b = piecemeal orelse return error.ChunkedDeliveryLostABodyThatDecoded; if (!std.mem.eql(u8, a, b)) return error.ChunkedDeliveryDecodedSomethingElse; } else if (piecemeal) |b| { // The other direction matters just as much: a decoder that accepts // piecemeal what it refuses whole is one whose answer depends on the // network rather than on the message. if (b.len > 0) return error.ChunkedDeliveryAcceptedWhatWholeRefused; } } fn fuzzCodedBody(_: void, smith: *Smith) !void { var buffer: [4096]u8 = undefined; const len = smith.slice(&buffer); const which: usize = smith.value(u1); const chunk: usize = smith.value(u4); try codedBodyProperty(buffer[0..len], which, chunk); } test "fuzz what a decoder does with a body somebody else chose" { for (coded_seeds, 0..) |seed, i| { try codedBodyProperty(seed, i, 0); try codedBodyProperty(seed, i, 3); try codedBodyProperty(seed, i + 1, 1); } try testing.fuzz({}, fuzzCodedBody, .{}); } pub const Target = struct { name: []const u8, run: *const fn (input: []const u8) anyerror!void, /// Inputs worth mutating: the same seeds the tests above run, which are /// what stands in for the coverage feedback a real fuzzer would have. corpus: []const []const u8, /// The buffer this target reads its input into. /// /// `Smith.slice` yields an *empty* slice for a length larger than the /// buffer rather than a truncated one, so a generator that does not know /// this number hands the target nothing at all most of the time. content_max: usize, /// How many `Smith.slice` calls this target makes before it starts /// asking for values. /// /// The generator writes exactly this many length-prefixed chunks and then /// the numbers, so that a `Smith.value` lands on a number rather than on /// the next chunk's length header — which is what it was doing, and which /// is out of range for any small type, so every such knob was reading /// its minimum and one branch was being fuzzed. slices: usize = 1, }; /// Wraps one of the `fuzz*` functions above so that it takes raw bytes. fn Driven(comptime one: fn (void, *Smith) anyerror!void) type { return struct { fn run(input: []const u8) anyerror!void { var smith: Smith = .{ .in = input }; return one({}, &smith); } }; } // -- whole HTTP/2 connections ----------------------------------------------- const h2_oracle = @import("h2/oracle.zig"); const h2_frames = @import("h2/frames.zig"); /// One HTTP/2 frame, at comptime, so that a seed reads as what it is. fn h2f( comptime kind: u8, comptime flags: u8, comptime id: u32, comptime payload: []const u8, ) []const u8 { const head = [_]u8{ @intCast((payload.len >> 16) & 0xff), @intCast((payload.len >> 8) & 0xff), @intCast(payload.len & 0xff), kind, flags, @intCast((id >> 24) & 0xff), @intCast((id >> 16) & 0xff), @intCast((id >> 8) & 0xff), @intCast(id & 0xff), }; return &(head ++ payload[0..payload.len].*); } /// A field, as the one HPACK representation that needs no table: §6.2.2's /// literal without indexing, with a new name and no Huffman coding. fn h2field(comptime name: []const u8, comptime value: []const u8) []const u8 { return &([_]u8{ 0x00, name.len } ++ name[0..name.len].* ++ [_]u8{value.len} ++ value[0..value.len].*); } const h2_get = h2field(":method", "GET") ++ h2field(":scheme", "http") ++ h2field(":authority", "a") ++ h2field(":path", "/"); const h2_exchange_seeds = [_][]const u8{ // The preface and a SETTINGS frame, which is the least a client may send. h2_frames.preface ++ h2f(0x4, 0, 0, ""), // One request, whole. h2_frames.preface ++ h2f(0x4, 0, 0, "") ++ h2f(0x1, 0x5, 1, h2_get), // A request with a body. h2_frames.preface ++ h2f(0x4, 0, 0, "") ++ h2f(0x1, 0x4, 1, h2field(":method", "POST") ++ h2field(":scheme", "http") ++ h2field(":authority", "a") ++ h2field(":path", "/echo")) ++ h2f(0x0, 0x1, 1, "a body"), // Two streams at once. h2_frames.preface ++ h2f(0x4, 0, 0, "") ++ h2f(0x1, 0x5, 1, h2_get) ++ h2f(0x1, 0x5, 3, h2_get), // A field block split, with an empty fragment in the middle. h2_frames.preface ++ h2f(0x4, 0, 0, "") ++ h2f(0x1, 0x1, 1, h2_get[0..4]) ++ h2f(0x9, 0, 1, "") ++ h2f(0x9, 0x4, 1, h2_get[4..]), // The control frames. h2_frames.preface ++ h2f(0x4, 0, 0, "") ++ h2f(0x6, 0, 0, "12345678") ++ h2f(0x8, 0, 0, &.{ 0, 0, 0x10, 0 }) ++ h2f(0x2, 0, 1, &.{ 0, 0, 0, 0, 16 }), // A stream opened and immediately cancelled. h2_frames.preface ++ h2f(0x4, 0, 0, "") ++ h2f(0x1, 0x5, 1, h2_get) ++ h2f(0x3, 0, 1, &.{ 0, 0, 0, 8 }), // A preface that is not the preface. "PRI * HTTP/2.0\r\n\r\nXX\r\n\r\n", "", }; /// A whole HTTP/2 connection, and then the thing the octets must be. /// /// The oracle here is strictly stronger than the HTTP/1.1 one next door, /// which has to carve out exceptions because HTTP/1.1 framing is not /// self-describing. Every octet on an HTTP/2 connection belongs to a frame /// whose length is written on the front of it, so the walker in /// `tests/h2/oracle.zig` can insist on the thing that matters most: every /// field block contiguous and complete. HPACK state is shared by every stream /// on the connection, so an interrupted block leaves the peer's decoder /// permanently out of step and *every subsequent* block decodes to something /// the server never sent. That is HTTP/2's request smuggling. fn h2ExchangeProperty(input: []const u8, chunk: usize, limits: u2) !void { // As with the HTTP/1.1 target, the limits are part of the input: most of // the code that enforces one is only reached by hitting it. const config: http.http2.Config = switch (limits) { 0 => .{}, 1 => .{ .max_frame_size = 16384, .stream_receive_window = 64, .max_field_block_len = 256 }, 2 => .{ .max_concurrent_streams = 1, .reset_budget = 2, .max_noise = 8 }, 3 => .{ .max_header_list_size = 128, .max_field_len = 32, .max_continuation_frames = 2 }, }; var used = config; used.send_date = false; used.server_name = null; // A task per stream is an operating system thread under the `Io` the // driver below is given, and the fuzzer runs millions of inputs. Four is // enough to reach multiplexing and cheap enough to do it a million times. used.max_concurrent_streams = @min(used.max_concurrent_streams, 4); var conn: http.transport.memory.Connection = try .init(backing, input, .{ .chunk = chunk, .read_buffer_len = @max(http.http2.min_read_buffer_len, 512), }); defer conn.deinit(); var transport_conn = conn.connection(); try http.http2.serve(io, backing, &transport_conn, .initStateless(dispatch), used); const list = try h2_oracle.parse(backing, conn.written()); defer backing.free(list); if (h2_oracle.check(list, .{ .max_frame_size = used.max_frame_size })) |violation| { var buffer: [4096]u8 = undefined; const locked = std.debug.lockStderr(&buffer); defer std.debug.unlockStderr(); const w = &locked.file_writer.interface; w.print("\n-- {s} --\n", .{@errorName(violation)}) catch {}; h2_oracle.dump(list, w) catch {}; w.flush() catch {}; return violation; } } fn fuzzH2Exchange(_: void, smith: *Smith) !void { var buffer: [8192]u8 = undefined; const len = smith.slice(&buffer); // The same trick as `fuzzExchange`: `value` answers a read outside the // type's range with the minimum, so a `u2` is zero for most inputs and // whole-frame delivery is the common case, as it is on a socket. const chunk: usize = smith.value(u2); const limits = smith.value(u2); try h2ExchangeProperty(buffer[0..len], chunk, limits); } test "fuzz h2 exchanges" { for (h2_exchange_seeds) |seed| try h2ExchangeProperty(seed, 0, 0); for (h2_exchange_seeds) |seed| try h2ExchangeProperty(seed, 1, 1); for (h2_exchange_seeds) |seed| try h2ExchangeProperty(seed, 7, 2); for (h2_exchange_seeds) |seed| try h2ExchangeProperty(seed, 0, 3); } pub const all = [_]Target{ .{ .name = "heads", .run = Driven(fuzzHead).run, .corpus = &head_seeds, .content_max = 2048 }, .{ .name = "responses", .run = Driven(fuzzResponse).run, .corpus = &response_seeds, .content_max = 2048 }, .{ .name = "client", .run = Driven(fuzzClient).run, .corpus = &client_seeds, .content_max = 4096 }, .{ .name = "h2-client", .run = Driven(fuzzH2Client).run, .corpus = &h2_client_seeds, .content_max = 4096 }, .{ .name = "h3-client", .run = Driven(fuzzH3Client).run, .corpus = &h3_client_seeds, .content_max = 4096 }, .{ .name = "paths", .run = Driven(fuzzPath).run, .corpus = &path_seeds, .content_max = 1024 }, .{ .name = "dates", .run = Driven(fuzzDate).run, .corpus = &date_seeds, .content_max = 64 }, .{ .name = "exchanges", .run = Driven(fuzzExchange).run, .corpus = &exchange_seeds, .content_max = 4096 }, .{ .name = "fields", .run = Driven(fuzzField).run, .corpus = &field_seeds, .content_max = 512 }, .{ .name = "hpack", .run = Driven(fuzzHpack).run, .corpus = &hpack_seeds, .content_max = 4096 }, .{ .name = "h2-requests", .run = Driven(fuzzH2Request).run, .corpus = &hpack_seeds, .content_max = 4096 }, .{ .name = "huffman", .run = Driven(fuzzHuffman).run, .corpus = &huffman_seeds, .content_max = 1024 }, .{ .name = "frames", .run = Driven(fuzzFrame).run, .corpus = &frame_seeds, .content_max = 2048 }, .{ .name = "h2-exchanges", .run = Driven(fuzzH2Exchange).run, .corpus = &h2_exchange_seeds, .content_max = 8192 }, .{ .name = "qpack", .run = Driven(fuzzQpack).run, .corpus = &qpack_seeds, .content_max = 4096 }, .{ .name = "h3-frames", .run = Driven(fuzzH3Frame).run, .corpus = &h3_frame_seeds, .content_max = 256 }, .{ .name = "h3-requests", .run = Driven(fuzzH3Request).run, .corpus = &qpack_seeds, .content_max = 4096 }, .{ .name = "requests", .run = Driven(fuzzEmittedRequest).run, .corpus = &emitted_request_seeds, .content_max = 512, .slices = 3 }, .{ .name = "codings", .run = Driven(fuzzCoding).run, .corpus = &coding_seeds, .content_max = 512 }, .{ .name = "coded-bodies", .run = Driven(fuzzCodedBody).run, .corpus = &coded_seeds, .content_max = 4096 }, };