Skip to content

Lightpanda: the headless browser designed for AI and automation

fix(cdp): honor client target filters in setAutoAttach

qidiaid04490d0fa24
diff --git a/src/server/cdp/CDP.zig b/src/server/cdp/CDP.zig
index c8b62145c..98bfc666d 100644
--- a/src/server/cdp/CDP.zig
+++ b/src/server/cdp/CDP.zig
@@ -262,12 +262,9 @@ fn dispatchStartupCommand(command: *Command, method: []const u8) !void {
return dispatchCommand(command, method);
}
- // Navigating the placeholder session has no page behind it: an
- // empty result here would look like success while nothing
- // happened, leaving the client waiting for lifecycle events
- // that never come. Say so, and point at the way to get a real
- // target. (Clients that filter pages out of auto-attach never
- // see this session at all; see Target.setAutoAttach.)
+ // The placeholder session has no page behind it: an empty
+ // result would look like success while nothing happened.
+ // Say so, and point at the way to get a real target.
if (std.mem.eql(u8, method, "Page.navigate")) {
return command.sendError(-32000, "No page on the STARTUP session; create a real target with Target.createTarget", .{});
}
@@ -1657,6 +1654,19 @@ test "cdp: STARTUP sessionId" {
}
}
+test "cdp: Page.navigate on the STARTUP session returns -32000" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ try ctx.processMessage(.{
+ .id = 1,
+ .method = "Page.navigate",
+ .sessionId = "STARTUP",
+ .params = .{ .url = "https://example.com" },
+ });
+ try ctx.expectSentError(-32000, "No page on the STARTUP session; create a real target with Target.createTarget", .{ .id = 1, .session_id = "STARTUP" });
+}
+
test "cdp: disconnect latches so the worker keeps exiting" {
var ctx = try testing.context();
defer ctx.deinit();
diff --git a/src/server/cdp/domains/target.zig b/src/server/cdp/domains/target.zig
index f50fe88ce..655106889 100644
--- a/src/server/cdp/domains/target.zig
+++ b/src/server/cdp/domains/target.zig
@@ -420,28 +420,21 @@ fn setDiscoverTargets(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
-// One entry of Target.setAutoAttach's `filter` (a TargetFilter in
-// the protocol): `type` selects the target type it matches (an
-// absent type matches any type) and `exclude` turns the entry
-// into an exclusion instead of an inclusion.
+/// One entry of Target.setAutoAttach's `filter`: `type`
+/// selects the target type it matches (absent matches any
+/// type), `exclude` turns the entry into an exclusion.
const TargetFilter = struct {
type: ?[]const u8 = null,
exclude: bool = false,
};
-// Does the client's auto-attach filter rule out page targets? An
-// entry with `exclude` set rules out every target matching its
-// `type`; an absent `type` rules out every type.
-fn filterExcludesPage(filter: ?[]const TargetFilter) bool {
- for (filter orelse return false) |f| {
- if (f.exclude) {
- if (f.type) |target_type| {
- if (std.mem.eql(u8, target_type, "page")) return true;
- } else {
- // an untyped exclude rules out every type
- return true;
- }
- }
+/// Does the client's auto-attach filter include page targets?
+/// Entries are checked in order: the first entry matching the
+/// target type decides; an absent type matches any type.
+fn filterIncludesPage(filter: ?[]const TargetFilter) bool {
+ for (filter orelse return true) |f| {
+ const t = f.type orelse return !f.exclude;
+ if (std.mem.eql(u8, t, "page")) return !f.exclude;
}
return false;
}
@@ -494,13 +487,10 @@ fn setAutoAttach(cmd: *CDP.Command) !void {
// This hack requires the main cdp dispatch handler to special case
// messages from this "STARTUP" session.
//
- // A client that excluded page targets from auto-attach (puppeteer
- // does: it only wants the targets it creates itself) must not be
- // told about this placeholder. It would surface as a page, and
- // navigating it lands on the STARTUP session, which has no page
- // behind it (see dispatchStartupCommand), so the navigation would
- // never happen. Stay quiet for those clients.
- if (!filterExcludesPage(params.filter)) {
+ // A client that excluded page targets from auto-attach
+ // (puppeteer does) must not hear about this placeholder:
+ // navigating it would land on the page-less STARTUP session.
+ if (filterIncludesPage(params.filter)) {
try cmd.sendEvent("Target.attachedToTarget", AttachToTarget{
.sessionId = "STARTUP",
.targetInfo = TargetInfo{
@@ -1163,3 +1153,49 @@ test "cdp.target: setAutoAttach false sends detachedFromTarget" {
try testing.expectEqual(null, bc.session_id);
try ctx.expectSentResult(null, .{ .id = 12 });
}
+
+test "cdp.target: setAutoAttach with page-exclude filter sends no attachedToTarget" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ // Puppeteer's filter: exclude page targets, include the rest.
+ try ctx.processMessage(.{
+ .id = 1,
+ .method = "Target.setAutoAttach",
+ .params = .{
+ .autoAttach = true,
+ .waitForDebuggerOnStart = false,
+ .filter = [_]TargetFilter{
+ .{ .type = "page", .exclude = true },
+ .{},
+ },
+ },
+ });
+ try ctx.expectSentResult(null, .{ .id = 1 });
+ // The STARTUP placeholder is a page target: with pages
+ // excluded, only the result may be sent.
+ try ctx.expectSentCount(1);
+}
+
+test "cdp.target: setAutoAttach without filter still sends startup attachedToTarget" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ try ctx.processMessage(.{
+ .id = 1,
+ .method = "Target.setAutoAttach",
+ .params = .{ .autoAttach = true, .waitForDebuggerOnStart = false },
+ });
+ try ctx.expectSentEvent("Target.attachedToTarget", .{
+ .sessionId = "STARTUP",
+ .targetInfo = .{
+ .targetId = "TID-STARTUP",
+ .type = "page",
+ .title = "",
+ .url = "about:blank",
+ .browserContextId = "BID-STARTUP",
+ },
+ }, .{ .index = 0 });
+ try ctx.expectSentResult(null, .{ .id = 1, .index = 1 });
+ try ctx.expectSentCount(2);
+}