Skip to content

Lightpanda: the headless browser designed for AI and automation

http: strip Authorization on cross-origin redirects

Pierre Tachoire467167d6e69f
diff --git a/src/browser/tests/net/fetch.html b/src/browser/tests/net/fetch.html
index c559092e1..f96eb0640 100644
--- a/src/browser/tests/net/fetch.html
+++ b/src/browser/tests/net/fetch.html
@@ -484,6 +484,27 @@
}
</script>
+<script id=fetch_redirect_authorization type=module>
+ {
+ // Authorization must not follow a cross-origin redirect. Custom headers
+ // are not stripped by the redirect itself (that is a CORS job).
+ const state = await testing.async();
+ const headers = { Authorization: 'Bearer secret', 'X-Hop': 'keep' };
+ const same = await (await fetch('/redirect_same_echo_headers', { headers })).text();
+ const cross = await (await fetch('/redirect_cross_echo_headers', { headers })).text();
+ state.resolve();
+
+ await state.done(() => {
+ const s = same.toLowerCase();
+ const c = cross.toLowerCase();
+ testing.expectEqual(true, s.includes('authorization: bearer secret'));
+ testing.expectEqual(true, s.includes('x-hop: keep'));
+ testing.expectEqual(false, c.includes('authorization'));
+ testing.expectEqual(true, c.includes('x-hop: keep'));
+ });
+ }
+</script>
+
<script id=fetch_redirect_referer type=module>
{
const state = await testing.async();
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index f1a50d102..a5ed1f979 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -2858,6 +2858,14 @@ pub const Transfer = struct {
break :blk resolved;
};
+ // When the redirect target is not same-origin with the current URL,
+ // the Authorization header must not follow the request to the new
+ // origin.
+ // _auth_challenge is always cleared thanks to detectAuthChallenge.
+ if (!URL.isSameOrigin(url, req.url)) {
+ transfer.removeHeader("Authorization");
+ }
+
try transfer.updateURL(url);
// 301, 302, 303 → change to GET, drop body.
// 307, 308 → keep method and body.
diff --git a/src/testing.zig b/src/testing.zig
index d3e0d8aff..b47456466 100644
--- a/src/testing.zig
+++ b/src/testing.zig
@@ -1121,6 +1121,27 @@ fn testHTTPHandler(req: *std.http.Server.Request) !void {
});
}
+ if (std.mem.eql(u8, path, "/redirect_same_echo_headers")) {
+ // Same-origin 302 to /echo_headers: Authorization must survive the hop.
+ return req.respond("", .{
+ .status = .found,
+ .extra_headers = &.{
+ .{ .name = "Location", .value = "/echo_headers" },
+ },
+ });
+ }
+
+ if (std.mem.eql(u8, path, "/redirect_cross_echo_headers")) {
+ // 302 to /echo_headers on the localhost alias — a cross-origin hop, so
+ // Authorization must be stripped before the request is re-sent.
+ return req.respond("", .{
+ .status = .found,
+ .extra_headers = &.{
+ .{ .name = "Location", .value = "http://localhost:9582/echo_headers" },
+ },
+ });
+ }
+
if (std.mem.eql(u8, path, "/redirect_same_echo_referer")) {
// Same-origin 302 to /echo_referer: the full Referer must survive the hop.
return req.respond("", .{