Skip to content

Lightpanda: the headless browser designed for AI and automation

Deliver 401/407 responses that carry no auth challenge or decline it

a.dmitryuk310a9a6daacd
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index 34f2541ba..b226faad7 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -1847,6 +1847,10 @@ fn processOneMessage(self: *Client, msg: http.Handles.MultiMessage, transfer: *T
&.{ .transfer = transfer, .wait_for_interception = &wait_for_interception },
);
if (wait_for_interception) {
+ // The conn is released below: keep the response so that
+ // cancelAuthChallenge can still deliver it.
+ try transfer.materializeResponse(msg.conn, .{});
+
self.intercepted += 1;
if (comptime lp.IS_DEBUG) {
log.debug(.http, "wait for auth interception", .{ .intercepted = self.intercepted });
@@ -2269,6 +2273,33 @@ pub fn continueTransfer(self: *Client, transfer: *Transfer) !void {
};
}
+// CDP Fetch.continueWithAuth with Default or CancelAuth: drop the challenge
+// and deliver the 401/407 response that raised it, materialized before the
+// transfer was parked.
+pub fn cancelAuthChallenge(self: *Client, transfer: *Transfer) !void {
+ if (comptime lp.IS_DEBUG) {
+ log.debug(.http, "cancel auth challenge", .{ .intercepted = self.intercepted });
+ }
+
+ transfer.unpark();
+ transfer._auth_challenge = null;
+
+ if (transfer._cors_cross_origin and transfer.req.request_mode == .cors) {
+ CorsGate.validateResponse(transfer) catch |err| {
+ return transfer.failAsync(err);
+ };
+ }
+
+ if (self.interceptResponse(transfer, .final)) {
+ return;
+ }
+
+ self.deliverResponse(transfer) catch |err| {
+ transfer.abortPipelineError(err);
+ return err;
+ };
+}
+
// CDP Fetch.continueResponse (or continueRequest on a response-stage pause):
// release what the transfer is holding, unchanged — deliver the response,
// follow the redirect, or report the network failure.
@@ -3737,9 +3768,13 @@ pub const Transfer = struct {
const url = try conn.getEffectiveUrl();
var status = try conn.getResponseCode();
- if (status == 0 and self._auth_challenge != null) {
- // A proxy that refuses the CONNECT gives us no status code
- status = try conn.getConnectCode();
+ if (status == 0) {
+ // A proxy that refuses the CONNECT gives us no status code. Not
+ // tied to _auth_challenge: a 407 may come without Proxy-Authenticate.
+ const connect_status = try conn.getConnectCode();
+ if (connect_status >= 300) {
+ status = connect_status;
+ }
}
self.res.header = .{
@@ -3910,7 +3945,9 @@ pub const Transfer = struct {
} else if (conn.getConnectHeader("Proxy-Authenticate", 0)) |hdr| {
transfer._auth_challenge = http.AuthChallenge.parse(status, .proxy, hdr.value) catch null;
} else {
- transfer._auth_challenge = .{ .status = status, .source = null, .scheme = null, .realm = null };
+ // A 401/407 without a challenge header is a plain response, not
+ // an auth challenge (e.g. a JSON API with session auth).
+ transfer._auth_challenge = null;
}
}
diff --git a/src/server/cdp/domains/fetch.zig b/src/server/cdp/domains/fetch.zig
index e6dd5cb25..644bf73f6 100644
--- a/src/server/cdp/domains/fetch.zig
+++ b/src/server/cdp/domains/fetch.zig
@@ -374,7 +374,9 @@ fn continueWithAuth(cmd: *CDP.Command) !void {
});
if (params.authChallengeResponse.response != .ProvideCredentials) {
- transfer.abortAuthChallenge();
+ // Like Chrome: give up on the challenge and let the page see the
+ // 401/407 that raised it.
+ try client.cancelAuthChallenge(transfer);
return cmd.sendResult(null, .{});
}
@@ -885,6 +887,7 @@ test "cdp.Fetch: enable compiles patterns, disable clears them" {
// the outcome through the request's own callbacks.
const ResponseStageProbe = struct {
streaming: bool = false,
+ status: u16 = 0,
body: std.ArrayList(u8) = .empty,
done: bool = false,
err: ?anyerror = null,
@@ -893,6 +896,12 @@ const ResponseStageProbe = struct {
self.body.deinit(testing.allocator);
}
+ fn headerCallback(transfer: *HttpClient.Transfer) !HttpClient.Transfer.HeaderResult {
+ const self: *ResponseStageProbe = @ptrCast(@alignCast(transfer.req.ctx));
+ self.status = transfer.responseStatus() orelse 0;
+ return .proceed;
+ }
+
fn dataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
const self: *ResponseStageProbe = @ptrCast(@alignCast(transfer.req.ctx));
try self.body.appendSlice(testing.allocator, data);
@@ -922,6 +931,7 @@ const ResponseStageProbe = struct {
.streaming = self.streaming,
.notification = bc.session.notification,
.ctx = self,
+ .header_callback = headerCallback,
.data_callback = dataCallback,
.done_callback = doneCallback,
.error_callback = errorCallback,
@@ -1238,6 +1248,128 @@ test "cdp.Fetch: closing the browser context releases a response-stage pause" {
try testing.expectEqual(false, probe.done);
}
+fn enableAuthHandling(ctx: anytype) !void {
+ try ctx.processMessage(.{
+ .id = 1,
+ .method = "Fetch.enable",
+ .params = .{ .patterns = .{.{ .urlPattern = "*" }}, .handleAuthRequests = true },
+ });
+ try ctx.expectSentResult(null, .{ .id = 1 });
+}
+
+fn sentEventCount(ctx: anytype, method: []const u8) usize {
+ var count: usize = 0;
+ for (ctx.received.items) |message| {
+ const m = message.object.get("method") orelse continue;
+ if (std.mem.eql(u8, m.string, method)) count += 1;
+ }
+ return count;
+}
+
+test "cdp.Fetch: a 401 without WWW-Authenticate is delivered, not an auth challenge" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+ const bc = try ctx.loadBrowserContext(.{ .id = "BID-AU", .session_id = "SID-AU" });
+ try enableAuthHandling(&ctx);
+
+ var probe: ResponseStageProbe = .{};
+ defer probe.deinit();
+ try probe.start(bc, "http://127.0.0.1:9582/status/401");
+ try ResponseStageProbe.tickUntilPaused(bc, 1);
+
+ try ctx.processMessage(.{ .id = 2, .method = "Fetch.continueRequest", .params = .{ .requestId = "INT-0000000001" } });
+ try ctx.expectSentResult(null, .{ .id = 2 });
+
+ try probe.tickUntilSettled(bc);
+ try testing.expectEqual(null, probe.err);
+ try testing.expect(probe.done);
+ try testing.expectEqual(401, probe.status);
+ try testing.expectEqual(0, sentEventCount(&ctx, "Fetch.authRequired"));
+ try testing.expectEqual(0, bc.cdp.browser.http_client.intercepted);
+}
+
+fn expectAuthDeclinedDeliversResponse(response: []const u8) !void {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+ const bc = try ctx.loadBrowserContext(.{ .id = "BID-AU", .session_id = "SID-AU" });
+ try enableAuthHandling(&ctx);
+
+ var probe: ResponseStageProbe = .{};
+ defer probe.deinit();
+ try probe.start(bc, "http://127.0.0.1:9582/xhr/401");
+ try ResponseStageProbe.tickUntilPaused(bc, 1);
+
+ try ctx.processMessage(.{ .id = 2, .method = "Fetch.continueRequest", .params = .{ .requestId = "INT-0000000001" } });
+ try ctx.expectSentResult(null, .{ .id = 2 });
+ try ResponseStageProbe.tickUntilPaused(bc, 1);
+
+ try ctx.expectSentEvent("Fetch.authRequired", .{
+ .requestId = "INT-0000000002",
+ .request = .{ .url = "http://127.0.0.1:9582/xhr/401" },
+ .authChallenge = .{ .source = "Server", .scheme = "basic" },
+ }, .{ .session_id = "SID-AU" });
+ try testing.expectEqual(false, probe.done);
+
+ try ctx.processMessage(.{ .id = 3, .method = "Fetch.continueWithAuth", .params = .{
+ .requestId = "INT-0000000002",
+ .authChallengeResponse = .{ .response = response },
+ } });
+ try ctx.expectSentResult(null, .{ .id = 3 });
+
+ // Like Chrome, declining the challenge delivers the original response.
+ try probe.tickUntilSettled(bc);
+ try testing.expectEqual(null, probe.err);
+ try testing.expect(probe.done);
+ try testing.expectEqual(401, probe.status);
+ try testing.expectEqualSlices(u8, "No", probe.body.items);
+ try testing.expectEqual(0, bc.cdp.browser.http_client.intercepted);
+}
+
+test "cdp.Fetch: continueWithAuth Default delivers the 401" {
+ try expectAuthDeclinedDeliversResponse("Default");
+}
+
+test "cdp.Fetch: continueWithAuth CancelAuth delivers the 401" {
+ try expectAuthDeclinedDeliversResponse("CancelAuth");
+}
+
+test "cdp.Fetch: continueWithAuth ProvideCredentials retries, a declined retry delivers the 401" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+ const bc = try ctx.loadBrowserContext(.{ .id = "BID-AU", .session_id = "SID-AU" });
+ try enableAuthHandling(&ctx);
+
+ var probe: ResponseStageProbe = .{};
+ defer probe.deinit();
+ try probe.start(bc, "http://127.0.0.1:9582/xhr/401");
+ try ResponseStageProbe.tickUntilPaused(bc, 1);
+
+ try ctx.processMessage(.{ .id = 2, .method = "Fetch.continueRequest", .params = .{ .requestId = "INT-0000000001" } });
+ try ctx.expectSentResult(null, .{ .id = 2 });
+ try ResponseStageProbe.tickUntilPaused(bc, 1);
+
+ // The test server rejects any credentials: the retry is challenged again.
+ try ctx.processMessage(.{ .id = 3, .method = "Fetch.continueWithAuth", .params = .{
+ .requestId = "INT-0000000002",
+ .authChallengeResponse = .{ .response = "ProvideCredentials", .username = "u", .password = "p" },
+ } });
+ try ctx.expectSentResult(null, .{ .id = 3 });
+ try ResponseStageProbe.tickUntilPaused(bc, 1);
+ try ctx.expectSentEvent("Fetch.authRequired", .{ .requestId = "INT-0000000003" }, .{ .session_id = "SID-AU" });
+
+ try ctx.processMessage(.{ .id = 4, .method = "Fetch.continueWithAuth", .params = .{
+ .requestId = "INT-0000000003",
+ .authChallengeResponse = .{ .response = "CancelAuth" },
+ } });
+ try ctx.expectSentResult(null, .{ .id = 4 });
+
+ try probe.tickUntilSettled(bc);
+ try testing.expectEqual(null, probe.err);
+ try testing.expectEqual(401, probe.status);
+ try testing.expectEqualSlices(u8, "No", probe.body.items);
+ try testing.expectEqual(0, bc.cdp.browser.http_client.intercepted);
+}
+
test "cdp.Fetch: cdpErrorReason" {
try testing.expectEqual(network.ErrorReason.TimedOut, cdpErrorReason(error.OperationTimedout));
try testing.expectEqual(network.ErrorReason.NameNotResolved, cdpErrorReason(error.CouldntResolveHost));