Lightpanda: the headless browser designed for AI and automation
webapi: enhance header to block forbidden response headers
Karl Seguin96de22fc0a7d
diff --git a/src/browser/tests/net/fetch.html b/src/browser/tests/net/fetch.htmlindex f96eb0640..a644937e0 100644--- a/src/browser/tests/net/fetch.html+++ b/src/browser/tests/net/fetch.html@@ -518,3 +518,18 @@});}</script>++<script id=fetch_forbidden_response_header type=module>+ {+ const state = await testing.async();+ const response = await fetch('http://127.0.0.1:9582/set_cookie');+ state.resolve();++ await state.done(() => {+ testing.expectEqual('yes', response.headers.get('x-visible'));+ testing.expectEqual(null, response.headers.get('set-cookie'));+ testing.expectEqual(0, response.headers.getSetCookie().length);+ testing.expectEqual(false, [...response.headers.keys()].includes('set-cookie'));+ });+ }+</script>diff --git a/src/browser/tests/net/headers.html b/src/browser/tests/net/headers.htmlindex 37279c349..e51f061e9 100644--- a/src/browser/tests/net/headers.html+++ b/src/browser/tests/net/headers.html@@ -380,3 +380,47 @@testing.expectEqual('b=2; HttpOnly', cookies[1]);}</script>++<script id=guard>+{+ // Set-Cookie is a forbidden response header: a response-guarded Headers+ // ignores it however it arrives.+ const res = new Response();+ res.headers.append('Set-Cookie', 'a=1');+ res.headers.set('sEt-cOokIe', 'b=2');+ testing.expectEqual(0, res.headers.getSetCookie().length);+ testing.expectEqual(null, res.headers.get('set-cookie'));++ const from_init = new Response('', {headers: {'Set-Cookie': 'a=1', 'X-Keep': 'yes'}});+ testing.expectEqual(0, from_init.headers.getSetCookie().length);+ testing.expectEqual('yes', from_init.headers.get('x-keep'));++ // A standalone Headers is unguarded and keeps them, but filling a Response+ // from one still drops them.+ const plain = new Headers({'Set-Cookie': 'a=1'});+ testing.expectEqual(1, plain.getSetCookie().length);+ testing.expectEqual(0, new Response('', {headers: plain}).headers.getSetCookie().length);++ // clone() carries the guard over.+ const cloned = from_init.clone();+ cloned.headers.append('Set-Cookie', 'a=1');+ testing.expectEqual(0, cloned.headers.getSetCookie().length);+ testing.expectEqual('yes', cloned.headers.get('x-keep'));+}+</script>++<script id=guard_immutable>+{+ const redirect = Response.redirect('http://127.0.0.1:9582/hi.html', 302);+ testing.expectEqual('http://127.0.0.1:9582/hi.html', redirect.headers.get('location'));++ for (const headers of [Response.error().headers, redirect.headers]) {+ let threw = 0;+ try { headers.append('x-a', '1'); } catch (e) { if (e instanceof TypeError) threw += 1; }+ try { headers.set('x-a', '1'); } catch (e) { if (e instanceof TypeError) threw += 1; }+ try { headers.delete('x-a'); } catch (e) { if (e instanceof TypeError) threw += 1; }+ testing.expectEqual(3, threw);+ testing.expectEqual(null, headers.get('x-a'));+ }+}+</script>diff --git a/src/browser/tests/net/xhr.html b/src/browser/tests/net/xhr.htmlindex 929aff89f..9051f7a39 100644--- a/src/browser/tests/net/xhr.html+++ b/src/browser/tests/net/xhr.html@@ -754,3 +754,19 @@ handler runs, abort() cancels the re-send and no load is delivered.testing.expectEqual('9000!!!', first.over);}</script>++<script id=xhr_forbidden_response_header type=module>+ {+ const state = await testing.async();+ const req = new XMLHttpRequest();+ req.onload = (event) => state.resolve(event);+ req.open('GET', 'http://127.0.0.1:9582/set_cookie');+ req.send();++ await state.done(() => {+ testing.expectEqual('yes', req.getResponseHeader('X-Visible'));+ testing.expectEqual(null, req.getResponseHeader('Set-Cookie'));+ testing.expectEqual(false, req.getAllResponseHeaders().toLowerCase().includes('set-cookie'));+ });+ }+</script>diff --git a/src/browser/webapi/net/Headers.zig b/src/browser/webapi/net/Headers.zigindex d628e96e5..c94216599 100644--- a/src/browser/webapi/net/Headers.zig+++ b/src/browser/webapi/net/Headers.zig@@ -12,6 +12,14 @@ const Execution = js.Execution;const Headers = @This();_list: KeyValueList,+_guard: Guard = .none,++// What mutation JS can make+pub const Guard = enum {+ none, // don't block anything+ response, // block forbidden response headers+ immutable, // block everythig+};pub const InitOpts = union(enum) {obj: *Headers,@@ -20,7 +28,11 @@ pub const InitOpts = union(enum) {};pub fn init(opts_: ?InitOpts, exec: *const Execution) !*Headers {- const list = blk: {+ return initGuarded(opts_, .none, exec);+}++pub fn initGuarded(opts_: ?InitOpts, guard: Guard, exec: *const Execution) !*Headers {+ var list = blk: {const opts = opts_ orelse break :blk KeyValueList.init();switch (opts) {.obj => |obj| break :blk try KeyValueList.copy(exec.arena, obj._list),@@ -37,18 +49,56 @@ pub fn init(opts_: ?InitOpts, exec: *const Execution) !*Headers {}};+ if (guard == .response) {+ // easier to use the KVL's creation upfront and then strip these out+ list.delete("set-cookie", null);+ list.delete("set-cookie2", null);+ }+return exec._factory.create(Headers{._list = list,+ ._guard = guard,});}+pub fn isForbiddenResponseHeaderName(name: []const u8) bool {+ if (std.ascii.eqlIgnoreCase(name, "set-cookie")) {+ return true;+ }++ if (std.ascii.eqlIgnoreCase(name, "set-cookie2")) {+ // yup, this is a real, never used / deprecated, header+ return true;+ }++ return false;+}++const Mutation = enum { proceed, ignore };++fn checkGuard(self: *const Headers, name: []const u8) !Mutation {+ return switch (self._guard) {+ .none => .proceed,+ .immutable => error.TypeError,+ .response => if (isForbiddenResponseHeaderName(name)) .ignore else .proceed,+ };+}+pub fn append(self: *Headers, name: []const u8, value: []const u8, exec: *const Execution) !void {const normalized_name = normalizeHeaderName(name, exec.buf);+ const mutation = try self.checkGuard(normalized_name);+ if (mutation == .ignore) {+ return;+ }try self._list.append(exec.arena, normalized_name, value);}-pub fn delete(self: *Headers, name: []const u8, exec: *const Execution) void {+pub fn delete(self: *Headers, name: []const u8, exec: *const Execution) !void {const normalized_name = normalizeHeaderName(name, exec.buf);+ const mutation = try self.checkGuard(normalized_name);+ if (mutation == .ignore) {+ return;+ }self._list.delete(normalized_name, null);}@@ -76,6 +126,10 @@ pub fn has(self: *const Headers, name: []const u8, exec: *const Execution) boolpub fn set(self: *Headers, name: []const u8, value: []const u8, exec: *const Execution) !void {const normalized_name = normalizeHeaderName(name, exec.buf);+ const mutation = try self.checkGuard(normalized_name);+ if (mutation == .ignore) {+ return;+ }try self._list.set(exec.arena, normalized_name, value);}diff --git a/src/browser/webapi/net/Response.zig b/src/browser/webapi/net/Response.zigindex 79ff5f936..daf658eef 100644--- a/src/browser/webapi/net/Response.zig+++ b/src/browser/webapi/net/Response.zig@@ -115,7 +115,7 @@ fn initWithArena(arena: *lp.Arena, body_: ?BodyInit, opts_: ?InitOpts, exec: *co}};- const headers = try Headers.init(opts.headers, exec);+ const headers = try Headers.initGuarded(opts.headers, .response, exec);if (content_type) |ct| {if (!headers.has("content-type", exec)) {try headers.append("content-type", ct, exec);@@ -151,7 +151,7 @@ pub fn createError(exec: *const Execution) !*Response {._body = .empty,._type = .@"error",._is_redirected = false,- ._headers = try Headers.init(null, exec),+ ._headers = try .initGuarded(null, .immutable, exec),};arena.report();return self;@@ -171,7 +171,9 @@ pub fn createRedirect(url_: []const u8, status_: ?u16, exec: *const Execution) !const location = try URL.resolve(arena.allocator(), exec.base(), url_, .{ .encoding = exec.charset.* });const headers = try Headers.init(null, exec);+ // append location directly, then lock the headerstry headers.set("location", location, exec);+ headers._guard = .immutable;const self = try arena.create(Response);self.* = .{@@ -204,7 +206,7 @@ pub fn createJson(data: js.Value, opts_: ?InitOpts, exec: *const Execution) !*Reconst opts = opts_ orelse InitOpts{};const status_text = if (opts.statusText) |st| try arena.dupe(u8, st) else "";- const headers = try Headers.init(opts.headers, exec);+ const headers = try Headers.initGuarded(opts.headers, .response, exec);if (!headers.has("content-type", exec)) {try headers.append("content-type", "application/json", exec);}@@ -553,7 +555,7 @@ pub fn clone(self: *const Response, exec: *const Execution) !*Response {._body = body,._type = self._type,._is_redirected = self._is_redirected,- ._headers = try Headers.init(.{ .obj = self._headers }, exec),+ ._headers = try .initGuarded(.{ .obj = self._headers }, self._headers._guard, exec),._http_transfer = null,};arena.report();diff --git a/src/browser/webapi/net/XMLHttpRequest.zig b/src/browser/webapi/net/XMLHttpRequest.zigindex d503ac2bb..dc88a5e57 100644--- a/src/browser/webapi/net/XMLHttpRequest.zig+++ b/src/browser/webapi/net/XMLHttpRequest.zig@@ -634,6 +634,9 @@ fn httpHeaderDoneCallback(transfer: *Transfer) !Transfer.HeaderResult {var it = transfer.responseHeaderIterator();while (it.next()) |hdr| {+ if (Headers.isForbiddenResponseHeaderName(hdr.name)) {+ continue;+ }const joined = try std.fmt.allocPrint(self._arena.allocator(), "{s}: {s}", .{ hdr.name, hdr.value });try self._response_headers.append(self._arena.allocator(), joined);}diff --git a/src/testing.zig b/src/testing.zigindex c1706cb36..71976ac37 100644--- a/src/testing.zig+++ b/src/testing.zig@@ -1140,6 +1140,15 @@ fn testHTTPHandler(req: *std.http.Server.Request) !void {});}+ if (std.mem.eql(u8, path, "/set_cookie")) {+ return req.respond("", .{+ .extra_headers = &.{+ .{ .name = "Set-Cookie", .value = "lp_hidden=1; Path=/set_cookie_scope" },+ .{ .name = "X-Visible", .value = "yes" },+ },+ });+ }+if (std.mem.eql(u8, path, "/redirect_same_echo_headers")) {// Same-origin 302 to /echo_headers: Authorization must survive the hop.return req.respond("", .{