Lightpanda: the headless browser designed for AI and automation
cdp: fall back to loopback when /json/version bind host is a wildcard
guyua98be30d56f803
diff --git a/src/Config.zig b/src/Config.zigindex fc74e4b23..58abd4cf4 100644--- a/src/Config.zig+++ b/src/Config.zig@@ -663,12 +663,37 @@ pub fn port(self: *const Config) u16 {pub fn advertiseHost(self: *const Config) []const u8 {return switch (self.mode) {- .serve => |opts| opts.advertise_host orelse opts.host,+ .serve => |opts| opts.advertise_host orelse advertiseHostFallback(opts.host),.mcp => "127.0.0.1",else => unreachable,};}+// Wildcard bind addresses (0.0.0.0, ::) are not routable for clients+// resolving /json/version. Fall back to a loopback address so the+// advertised webSocketDebuggerUrl is at least connectable from the same+// host (covers the official Docker image, which exposes 9222 via the+// container's published port, and the WSL localhost bridge).+// For remote hosts, users can still pin the URL with --advertise-host.+// See issue #1922.+fn advertiseHostFallback(host: []const u8) []const u8 {+ if (std.mem.eql(u8, host, "0.0.0.0") or std.mem.eql(u8, host, "::")) {+ return "127.0.0.1";+ }+ return host;+}++// True when serve is binding a wildcard address (e.g. Docker --host+// 0.0.0.0) without an explicit --advertise-host. /json/version replaces+// the wildcard with 127.0.0.1 in advertiseHostFallback so the URL stays+// resolvable, but the caller still benefits from emitting a guidance log.+pub fn bindIsWildcard(self: *const Config) bool {+ return switch (self.mode) {+ .serve => |opts| std.mem.eql(u8, opts.host, "0.0.0.0") or std.mem.eql(u8, opts.host, "::"),+ else => false,+ };+}+pub fn webBotAuth(self: *const Config) ?WebBotAuthConfig {return switch (self.mode) {inline .serve, .fetch, .mcp, .agent => |opts| WebBotAuthConfig{@@ -980,6 +1005,47 @@ test "Config: blockedUrlPatterns splits comma-separated patterns" {try std.testing.expectEqual(null, patterns.next());}+// /json/version must never advertise a wildcard bind address because+// clients (Chromedp, Playwright MCP, etc.) cannot dial 0.0.0.0/::. See+// issue #1922.+test "Config: advertiseHost falls back to loopback for wildcard binds" {+ {+ var config = try Config.init(std.testing.allocator, "test", .{ .serve = .{+ .host = "0.0.0.0",+ } });+ defer config.deinit(std.testing.allocator);+ try std.testing.expect(config.bindIsWildcard());+ try std.testing.expectEqualStrings("127.0.0.1", config.advertiseHost());+ }+ {+ var config = try Config.init(std.testing.allocator, "test", .{ .serve = .{+ .host = "::",+ } });+ defer config.deinit(std.testing.allocator);+ try std.testing.expect(config.bindIsWildcard());+ try std.testing.expectEqualStrings("127.0.0.1", config.advertiseHost());+ }+}++test "Config: advertiseHost honors explicit --advertise-host override" {+ var config = try Config.init(std.testing.allocator, "test", .{ .serve = .{+ .host = "0.0.0.0",+ .advertise_host = "192.168.0.5",+ } });+ defer config.deinit(std.testing.allocator);+ try std.testing.expect(config.bindIsWildcard());+ try std.testing.expectEqualStrings("192.168.0.5", config.advertiseHost());+}++test "Config: advertiseHost preserves concrete host when not a wildcard" {+ var config = try Config.init(std.testing.allocator, "test", .{ .serve = .{+ .host = "127.0.0.1",+ } });+ defer config.deinit(std.testing.allocator);+ try std.testing.expect(!config.bindIsWildcard());+ try std.testing.expectEqualStrings("127.0.0.1", config.advertiseHost());+}+pub fn validateUserAgent(ua: []const u8) !void {for (ua) |c| {if (!std.ascii.isPrint(c)) {diff --git a/src/Server.zig b/src/Server.zigindex bba89d8ca..9b32ac141 100644--- a/src/Server.zig+++ b/src/Server.zig@@ -282,9 +282,13 @@ fn handleConnection(self: *Server, socket: posix.socket_t) void {fn buildJSONVersionResponse(app: *const App, port: u16) ![]const u8 {const host = app.config.advertiseHost();- if (std.mem.eql(u8, host, "0.0.0.0")) {- log.info(.cdp, "unreachable advertised host", .{- .message = "when --host is set to 0.0.0.0 consider setting --advertise-host to a reachable address",+ if (app.config.bindIsWildcard()) {+ // Serve is bound to INADDR_ANY but no --advertise-host was given;+ // advertiseHost() falls back to 127.0.0.1 so clients can still+ // connect locally. Surface the trade-off so users running+ // outside the same host know they have to opt in.+ log.note(.cdp, "advertising loopback for wildcard bind", .{+ .message = "--host is a wildcard (0.0.0.0 / ::) without --advertise-host; clients on other hosts will need --advertise-host to reach the CDP endpoint",});}const body_format =