Skip to content

Lightpanda: the headless browser designed for AI and automation

origin is tainted on cross origin redirects

Muki Kiboigo34b743fbd6fc
diff --git a/src/network/CorsGate.zig b/src/network/CorsGate.zig
index f76185d47..b28b9970f 100644
--- a/src/network/CorsGate.zig
+++ b/src/network/CorsGate.zig
@@ -182,16 +182,17 @@ const Result = enum { allowed, pending };
pub fn check(self: *CorsGate, transfer: *Transfer) !Result {
const req = &transfer.req;
- if (req.origin) |origin| {
- if (URL.isSameOrigin(req.url, origin)) {
- log.debug(.cors, "same origin", .{ .url = req.url, .origin = origin });
- lp.metrics.cors_check.incr(.same_origin);
- return .allowed;
+ if (!transfer._cors_origin_tainted) {
+ if (req.origin) |origin| {
+ if (URL.isSameOrigin(req.url, origin)) {
+ log.debug(.cors, "same origin", .{ .url = req.url, .origin = origin });
+ lp.metrics.cors_check.incr(.same_origin);
+ return .allowed;
+ }
}
}
- const origin = req.origin orelse "null";
-
+ const origin = transfer.effectiveOrigin();
transfer._cors_cross_origin = true;
// https://fetch.spec.whatwg.org/#append-a-request-origin-header
@@ -579,11 +580,7 @@ pub fn validateResponse(transfer: *Transfer) !void {
}
if (!is_wildcard_origin) {
- const origin = req.origin orelse {
- log.warn(.cors, "blocked", .{ .url = req.url, .reason = "opaque origin" });
- return error.CorsBlocked;
- };
-
+ const origin = transfer.effectiveOrigin();
if (!std.mem.eql(u8, allow_origin, origin)) {
log.warn(.cors, "blocked", .{
.url = req.url,
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index c2c1510eb..145aa75d9 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -2247,6 +2247,9 @@ pub const Transfer = struct {
_retired: bool = false,
_cors_cross_origin: bool = false,
+ // Set once a redirect target origin differs from origin of the URL
+ // that redirected to it.
+ _cors_origin_tainted: bool = false,
pub const State = union(enum) {
// Pre-commit. Only valid inside the request flow (Client.request
@@ -2597,6 +2600,11 @@ pub const Transfer = struct {
self.failAsync(err);
}
+ pub fn effectiveOrigin(transfer: *const Transfer) []const u8 {
+ if (transfer._cors_origin_tainted) return "null";
+ return transfer.req.origin orelse "null";
+ }
+
pub fn getCookieString(self: *Transfer, arena: Allocator) !?[:0]const u8 {
const req = &self.req;
if (!req.credentialsAllowed()) return null;
@@ -3291,6 +3299,14 @@ pub const Transfer = struct {
}
transfer.redirectTaint(url);
+
+ if (transfer.req.request_mode == .cors and !transfer._cors_origin_tainted) {
+ const already_cross_origin = if (req.origin) |o| !URL.isSameOrigin(base, o) else true;
+ if (already_cross_origin and !URL.isSameOrigin(url, base)) {
+ transfer._cors_origin_tainted = true;
+ }
+ }
+
try transfer.updateURL(url);
// 301, 302, 303 → change to GET, drop body.
// 307, 308 → keep method and body.