Lightpanda: the headless browser designed for AI and automation
Centralize header validation
Karl Seguina9b96fe5f804
diff --git a/src/Config.zig b/src/Config.zigindex 94d797e5d..74647342d 100644--- a/src/Config.zig+++ b/src/Config.zig@@ -22,6 +22,7 @@ const lp = @import("lightpanda");const cli = @import("cli.zig");const dump = @import("browser/dump.zig");+const Mime = @import("browser/Mime.zig");const WebBotAuthConfig = @import("network/WebBotAuth.zig").Config;const HttpHeader = @import("network/http.zig").Header;@@ -93,13 +94,25 @@ fn httpHeaderValidator(allocator: Allocator, args: *std.process.Args.Iterator, lreturn error.InvalidArgument;};+ // The same shape script-set headers go through in `Headers`: a name that+ // is an HTTP token, a value that is a CR/LF-free byte string.+ if (Mime.isHttpToken(header.name) == false) {+ log.fatal(.app, "invalid option value", .{ .arg = "--http-header", .value = str, .hint = "name must be a non-empty HTTP token" });+ return error.InvalidArgument;+ }++ if (Mime.isHttpHeaderValue(header.value) == false) {+ log.fatal(.app, "invalid option value", .{ .arg = "--http-header", .value = str, .hint = "value must be Latin-1 text without CR, LF or NUL" });+ return error.InvalidArgument;+ }+if (std.ascii.eqlIgnoreCase(header.name, "User-Agent")) {log.fatal(.app, "invalid option value", .{ .arg = "--http-header", .value = str, .hint = "Use --user-agent instead" });return error.InvalidArgument;}if (std.ascii.eqlIgnoreCase(header.name, "Sec-Ch-Ua")) {- log.fatal(.app, "invalid option value", .{ .arg = "--http-header", .value = str, .hint = "Sec-Ch-Ua is not oevrridable" });+ log.fatal(.app, "invalid option value", .{ .arg = "--http-header", .value = str, .hint = "Sec-Ch-Ua is not overridable" });return error.InvalidArgument;}@@ -1097,6 +1110,55 @@ test "Config: validateUserAgent" {try std.testing.expectError(error.NonPrintable, validateUserAgent("bad\x01ua"));}+test "Config: parseArgs refuses an invalid --http-header" {+ const invalid = [_][*:0]const u8{+ // no colon to split on+ "not-a-header",+ // empty name+ ": value",+ // names that aren't HTTP tokens+ "Foo Bar: value",+ "X(Foo)=a: value",+ // CR/LF in the value would smuggle a second header onto the wire+ "X-A: b\r\nX-B: c",+ "X-A: b\n",+ // reserved names have their own flag, or none at all+ "User-Agent: Custom/1.0",+ "Sec-Ch-Ua: \"Chromium\";v=\"140\"",+ };++ for (invalid) |header| {+ log.expectLog(&.{.app});+ const argv = [_][*:0]const u8{ "lightpanda", "fetch", "--http-header", header };+ const proc_args: std.process.Args = .{ .vector = &argv };+ try std.testing.expectError(error.InvalidArgument, parseArgs(std.testing.allocator, proc_args));+ }+}++test "Config: parseArgs collects --http-header" {+ // The parsed headers are owned by the allocator for the process lifetime;+ // an arena stands in for main's.+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();++ const argv = [_][*:0]const u8{+ "lightpanda", "fetch",+ "--http-header", "Accept-Language: fr-FR,fr;q=0.9",+ "--http-header", " \tX-Empty \t: \t",+ "http://example.com",+ };+ const proc_args: std.process.Args = .{ .vector = &argv };+ const config = try parseArgs(arena.allocator(), proc_args);++ const headers = config.httpHeaders();+ try std.testing.expectEqual(2, headers.len);+ try std.testing.expectEqualStrings("Accept-Language", headers[0].name);+ try std.testing.expectEqualStrings("fr-FR,fr;q=0.9", headers[0].value);+ // Name and value are trimmed; an empty value is legal.+ try std.testing.expectEqualStrings("X-Empty", headers[1].name);+ try std.testing.expectEqualStrings("", headers[1].value);+}+test "Config: httpHeaders accessor" {{var config = try Config.init(std.testing.allocator, "test", .{ .serve = .{} });diff --git a/src/browser/Mime.zig b/src/browser/Mime.zigindex 9e7e0197b..d2e10fbca 100644--- a/src/browser/Mime.zig+++ b/src/browser/Mime.zig@@ -648,6 +648,24 @@ pub fn isHttpToken(s: []const u8) bool {return true;}+pub fn isHttpHeaderValue(value: []const u8) bool {+ var i: usize = 0;+ while (i < value.len) {+ const n = std.unicode.utf8ByteSequenceLength(value[i]) catch return false;+ if (i + n > value.len) {+ return false;+ }++ const cp = std.unicode.utf8Decode(value[i..][0..n]) catch return false;++ if (cp > 0xFF or cp == 0x00 or cp == 0x0A or cp == 0x0D) {+ return false;+ }+ i += n;+ }+ return true;+}+/// Whether every code point in `value` is an "HTTP quoted-string token code/// point" (HT, 0x20-0x7E, or 0x80-0xFF). Decodes UTF-8 so that a code point/// above U+00FF (e.g. U+FFFD) is rejected even though its bytes are each >=diff --git a/src/browser/webapi/net/Headers.zig b/src/browser/webapi/net/Headers.zigindex 5436aac2d..b806642bf 100644--- a/src/browser/webapi/net/Headers.zig+++ b/src/browser/webapi/net/Headers.zig@@ -121,30 +121,12 @@ fn validateAndNormalize(list: *KeyValueList) !void {if (Mime.isHttpToken(entry.name.str()) == false) {return error.TypeError;}+ // A valid header value is a byte string without NUL, LF or CR.const trimmed = entry.value.trim(&Mime.HTTP_WHITESPACE);- try validateHeaderValue(trimmed.str());- entry.value = trimmed;- }-}--/// Validate an already-normalized header value. Returns `error.TypeError` —-/// surfaced to script as a JS TypeError — when it contains a code point above-/// U+00FF (not a valid byte string) or a 0x00/0x0A/0x0D byte.-/// https://fetch.spec.whatwg.org/#headers-class-fn validateHeaderValue(value: []const u8) error{TypeError}!void {- var i: usize = 0;- while (i < value.len) {- const n = std.unicode.utf8ByteSequenceLength(value[i]) catch return error.TypeError;- if (i + n > value.len) {+ if (Mime.isHttpHeaderValue(trimmed.str()) == false) {return error.TypeError;}-- const cp = std.unicode.utf8Decode(value[i..][0..n]) catch return error.TypeError;-- if (cp > 0xFF or cp == 0x00 or cp == 0x0A or cp == 0x0D) {- return error.TypeError;- }- i += n;+ entry.value = trimmed;}}diff --git a/src/cdp/domains/network.zig b/src/cdp/domains/network.zigindex d27686f64..99bf6cb37 100644--- a/src/cdp/domains/network.zig+++ b/src/cdp/domains/network.zig@@ -132,16 +132,13 @@ fn setExtraHTTPHeaders(cmd: *CDP.Command) !void {const key = header.key_ptr.*;const value = header.value_ptr.*;- if (std.mem.indexOfAny(u8, key, "\r\n") != null or std.mem.indexOfAny(u8, value, "\r\n") != null) {- log.warn(.cdp, "network.setExtraHTTPHeaders", .{ .param = "header", .value = key, .info = "header name/value must not contain CR or LF" });+ if (Mime.isHttpToken(key) == false) {+ log.warn(.cdp, "network.setExtraHTTPHeaders", .{ .param = "header", .value = key, .info = "header name must be a non-empty HTTP token" });continue;}- // A colon in the name would smuggle a different header name onto the- // wire once the pair is joined ("User-Agent:Mozilla/5.0 (X" + "Y)"),- // bypassing the User-Agent validation below.- if (std.mem.indexOfScalar(u8, key, ':') != null) {- log.warn(.cdp, "network.setExtraHTTPHeaders", .{ .param = "header", .value = key, .info = "header name must not contain a colon" });+ if (Mime.isHttpHeaderValue(value) == false) {+ log.warn(.cdp, "network.setExtraHTTPHeaders", .{ .param = "header", .value = key, .info = "header value must be Latin-1 text without CR, LF or NUL" });continue;}@@ -804,6 +801,30 @@ test "cdp.network setExtraHTTPHeaders rejects a header that smuggles CRLF" {try testing.expectEqual("ok", bc.extra_headers.items[0].value);}+test "cdp.network setExtraHTTPHeaders rejects a name that isn't an HTTP token" {+ testing.silenceLog(&.{.cdp});++ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{ .id = "NID-UA6", .session_id = "NESI-UA6" });++ // A space — or any other non-token byte — in the name would land+ // malformed on the wire.+ try ctx.processMessage(.{+ .id = 3,+ .method = "Network.setExtraHTTPHeaders",+ .params = .{ .headers = .{+ .@"X Custom" = "bar",+ .@"x-keep" = "ok",+ } },+ });++ try testing.expectEqual(bc.extra_headers.items.len, 1);+ try testing.expectEqual("x-keep", bc.extra_headers.items[0].name);+ try testing.expectEqual("ok", bc.extra_headers.items[0].value);+}+test "cdp.Network: cookies" {const ResCookie = CdpStorage.ResCookie;const CdpCookie = CdpStorage.CdpCookie;diff --git a/src/network/http.zig b/src/network/http.zigindex 926b47784..11c601bbe 100644--- a/src/network/http.zig+++ b/src/network/http.zig@@ -62,25 +62,11 @@ pub const Header = struct {};pub fn parse(header_str: []const u8) ?Header {- // ignore headers containing "\r\n".- if (std.mem.indexOfAny(u8, header_str, "\r\n") != null) {- return null;- }- // ignore headers w/o colon.const colon_pos = std.mem.indexOfScalar(u8, header_str, ':') orelse return null;const name = std.mem.trim(u8, header_str[0..colon_pos], " \t");const value = std.mem.trim(u8, header_str[colon_pos + 1 ..], " \t");- // no empty name.- if (name.len == 0) {- return null;- }- // no name incl. space.- if (std.mem.indexOfAny(u8, name, " ") != null) {- return null;- }-return .{ .name = name, .value = value };}@@ -945,17 +931,17 @@ test "Header.parse" {try testing.expectEqualSlices(u8, "", h.value);}- // empty name- try testing.expect(Header.parse(": value") == null);- // bad name- try testing.expect(Header.parse("Foo Bar: value") == null);- // no colon+ {+ // Splitting is all `parse` does; a name that isn't an HTTP token still+ // parses. Callers validate what comes back.+ const h = Header.parse("Foo Bar: value").?;+ try testing.expectEqualSlices(u8, "Foo Bar", h.name);+ try testing.expectEqualSlices(u8, "value", h.value);+ }++ // no colon, no headertry testing.expect(Header.parse("not-a-header") == null);try testing.expect(Header.parse("") == null);- // CR or LF anywhere rejects the header (injection guard)- try testing.expect(Header.parse("X-A: b\r\nX-B: c") == null);- try testing.expect(Header.parse("X-A: b\n") == null);- try testing.expect(Header.parse("\rX-A: b") == null);}test "Header.firstValue" {