Lightpanda: the headless browser designed for AI and automation
Only answer auth pauses with continueWithAuth, fail declined CONNECT refusals
a.dmitryuk232b8aa18f63
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zigindex b226faad7..5d83104b8 100644--- a/src/network/HttpClient.zig+++ b/src/network/HttpClient.zig@@ -1841,16 +1841,26 @@ fn processOneMessage(self: *Client, msg: http.Handles.MultiMessage, transfer: *T// In case of auth challenge// TODO give a way to configure the number of auth retries.if (transfer._auth_challenge != null and transfer._tries < 10) {+ // The conn is released if CDP parks the transfer: keep what+ // cancelAuthChallenge will deliver. Done before notifying, so a+ // failure here never leaves CDP holding a transfer that isn't parked.+ if (try msg.conn.getResponseCode() == 0) {+ // A proxy refused the CONNECT: there is no response from the+ // origin. Like Chrome (ERR_TUNNEL_CONNECTION_FAILED), declining+ // the challenge fails the request instead of showing the proxy's+ // 407 as the origin's response.+ transfer._auth_declined_err = effective_err orelse error.CouldntConnect;+ } else {+ transfer._auth_declined_err = null;+ try transfer.materializeResponse(msg.conn, .{ .check_content_length = false });+ }+var wait_for_interception = false;transfer.notify(.http_request_auth_required,&.{ .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 });@@ -2275,7 +2285,7 @@ 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.+// transfer was parked. A refused CONNECT has no such response and fails.pub fn cancelAuthChallenge(self: *Client, transfer: *Transfer) !void {if (comptime lp.IS_DEBUG) {log.debug(.http, "cancel auth challenge", .{ .intercepted = self.intercepted });@@ -2284,6 +2294,19 @@ pub fn cancelAuthChallenge(self: *Client, transfer: *Transfer) !void {transfer.unpark();transfer._auth_challenge = null;+ if (transfer._auth_declined_err) |err| {+ if (self.interceptResponse(transfer, .{ .failed = .{ .err = err, .header_first = false } })) {+ return;+ }+ return transfer.failAsync(err);+ }++ if (transfer.getContentLength()) |cl| {+ if (cl > self.max_response_size) {+ return transfer.failAsync(error.ResponseTooLarge);+ }+ }+if (transfer._cors_cross_origin and transfer.req.request_mode == .cors) {CorsGate.validateResponse(transfer) catch |err| {return transfer.failAsync(err);@@ -2596,6 +2619,10 @@ pub const Transfer = struct {_auth_challenge: ?http.AuthChallenge = null,+ // What declining _auth_challenge fails with (a refused CONNECT), instead+ // of delivering the response. Set when the challenge is offered to CDP.+ _auth_declined_err: ?anyerror = null,+// number of times the transfer has been tried.// incremented by reset func._tries: u8 = 0,@@ -3625,6 +3652,28 @@ pub const Transfer = struct {return;}+ // libcurl returns -1 if the connection wasn't used. We want to avoid+ // negative, so -1 -> 0 and everything else += 1;+ // An auth challenge materializes the response before it is offered+ // to CDP; when it isn't parked, the regular path gets here again.+ if (!self.res.materialized) {+ try self.materializeFromConn(conn);+ }++ // A partial fetch is exempt: the cap exists to bound how much body we+ // buffer, and this transfer buffers at most PARTIAL_DRAIN_MAX of it. Failing a 4 MB image+ // we were never going to read would turn the size limit into a+ // spurious `error` event on a perfectly good response.+ if (opts.check_content_length and self.req.partial == null) {+ if (self.getContentLength()) |cl| {+ if (cl > self.client.max_response_size) {+ return error.ResponseTooLarge;+ }+ }+ }+ }++ fn materializeFromConn(self: *Transfer, conn: *http.Connection) !void {// libcurl returns -1 if the connection wasn't used. We want to avoid// negative, so -1 -> 0 and everything else += 1;const conn_id = conn.getConnId() catch -1;@@ -3655,18 +3704,7 @@ pub const Transfer = struct {}}}-- // A partial fetch is exempt: the cap exists to bound how much body we- // buffer, and this transfer buffers at most PARTIAL_DRAIN_MAX of it. Failing a 4 MB image- // we were never going to read would turn the size limit into a- // spurious `error` event on a perfectly good response.- if (opts.check_content_length and self.req.partial == null) {- if (self.getContentLength()) |cl| {- if (cl > self.client.max_response_size) {- return error.ResponseTooLarge;- }- }- }+ self.res.materialized = true;}fn configureConn(self: *Transfer, conn: *http.Connection) anyerror!void {@@ -4616,6 +4654,10 @@ const Response = struct {// The final (non-1xx) response's header block has been fully received.headers_complete: bool = false,+ // materializeResponse has copied the status, headers and cookies out of+ // the conn.+ materialized: bool = false,+// Set when dataCallback deliberately killed the transfer to satisfy// `Request.partial`. processOneMessage uses it to tell our own abort// apart from a real CURLE_WRITE_ERROR and deliver the response (headers,diff --git a/src/server/cdp/domains/fetch.zig b/src/server/cdp/domains/fetch.zigindex 644bf73f6..a1020e914 100644--- a/src/server/cdp/domains/fetch.zig+++ b/src/server/cdp/domains/fetch.zig@@ -361,11 +361,18 @@ fn continueWithAuth(cmd: *CDP.Command) !void {var intercept_state = &bc.intercept_state;const intercept_id = try idFromRequestId(params.requestId);- const transfer_id = intercept_state.remove(intercept_id) orelse return error.RequestNotFound;+ const transfer_id = intercept_state.get(intercept_id) orelse return error.RequestNotFound;const transfer = client.findTransfer(transfer_id) orelse {+ _ = intercept_state.remove(intercept_id);log.debug(.cdp, "intercept lookup miss", .{ .id = transfer_id, .op = "auth" });return cmd.sendResult(null, .{});};+ // Only an authRequired pause can be answered with continueWithAuth: a+ // request- or response-stage pause stays paused, as in Chrome.+ if (transfer.state != .parked or transfer.state.parked != .intercept_auth) {+ return cmd.sendError(-32000, "Invalid InterceptionId.", .{});+ }+ _ = intercept_state.remove(intercept_id);log.debug(.cdp, "request intercept", .{.state = "continue with auth",@@ -1365,11 +1372,50 @@ test "cdp.Fetch: continueWithAuth ProvideCredentials retries, a declined retry dtry 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 on a request-stage pause is rejected and keeps the pause" {+ 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);++ for ([_][]const u8{ "CancelAuth", "ProvideCredentials" }, 2..) |response, cmd_id| {+ try ctx.processMessage(.{ .id = cmd_id, .method = "Fetch.continueWithAuth", .params = .{+ .requestId = "INT-0000000001",+ .authChallengeResponse = .{ .response = response },+ } });+ try ctx.expectSentError(-32000, "Invalid InterceptionId.", .{ .id = cmd_id });+ }+ try testing.expectEqual(false, probe.done);+ try testing.expectEqual(null, probe.err);+ try testing.expectEqual(1, bc.cdp.browser.http_client.intercepted);++ // Still paused at the request stage: continueRequest resumes it.+ try ctx.processMessage(.{ .id = 4, .method = "Fetch.continueRequest", .params = .{ .requestId = "INT-0000000001" } });+ try ctx.expectSentResult(null, .{ .id = 4 });+ try ResponseStageProbe.tickUntilPaused(bc, 1);+ try ctx.expectSentEvent("Fetch.authRequired", .{ .requestId = "INT-0000000002" }, .{ .session_id = "SID-AU" });++ try ctx.processMessage(.{ .id = 5, .method = "Fetch.continueWithAuth", .params = .{+ .requestId = "INT-0000000002",+ .authChallengeResponse = .{ .response = "Default" },+ } });+ try ctx.expectSentResult(null, .{ .id = 5 });+ try probe.tickUntilSettled(bc);+ try testing.expect(probe.done);+ try testing.expectEqual(401, probe.status);+}+test "cdp.Fetch: cdpErrorReason" {try testing.expectEqual(network.ErrorReason.TimedOut, cdpErrorReason(error.OperationTimedout));try testing.expectEqual(network.ErrorReason.NameNotResolved, cdpErrorReason(error.CouldntResolveHost));