Skip to content

Lightpanda: the headless browser designed for AI and automation

silence test log

Karl Seguin792d08c7af7c
diff --git a/src/server/Server.zig b/src/server/Server.zig
index 6b36c92ae..69d095263 100644
--- a/src/server/Server.zig
+++ b/src/server/Server.zig
@@ -69,6 +69,8 @@ listener_paused: bool,
// keepalive and hits to /json/version, /metrics, etc.
max_connections: usize,
+advertise_host: ?[]const u8,
+
// the protocols (cdp/bidi) we support
protocols: Protocols,
@@ -224,6 +226,7 @@ pub fn init(app: *App, address: sys_net.IpAddress) !*Server {
.worker_drain = worker_drain,
.shutdown_begun = false,
.worker_wg = .{},
+ .advertise_host = config.explicitAdvertiseHost(),
};
return self;
}
diff --git a/src/server/http.zig b/src/server/http.zig
index 0195d811b..97d783b57 100644
--- a/src/server/http.zig
+++ b/src/server/http.zig
@@ -1069,7 +1069,7 @@ const Upgrade = union(enum) {
// hand the fd to its worker (spawning one for a new connection).
fn upgrade(server: *Server, conn: *Connection, req: *Connection.Request, target: Upgrade) !Served {
var accept_buf: [28]u8 = undefined;
- const accept_key = webSocketAccept(req.head, &accept_buf, server.app.config.explicitAdvertiseHost()) catch |err| {
+ const accept_key = webSocketAccept(req.head, &accept_buf, server.advertise_host) catch |err| {
const response: []const u8 = switch (err) {
error.ForbiddenOrigin => forbidden_origin_response,
error.ForbiddenHost => forbidden_host_response,
@@ -1136,8 +1136,8 @@ fn webSocketAccept(head: []const u8, out: *[28]u8, advertise_host: ?[]const u8)
log.warn(.serve, "rejected websocket origin", .{ .origin = h.value[0..@min(h.value.len, 64)] });
return error.ForbiddenOrigin;
} else if (std.ascii.eqlIgnoreCase(h.key, "host")) {
- if (!isAllowedHost(h.value, advertise_host)) {
- log.warn(.serve, "rejected websocket host", .{ .host = h.value[0..@min(h.value.len, 64)] });
+ if (isAllowedHost(h.value, advertise_host) == false) {
+ log.warn(.serve, "rejected websocket host", .{ .host = h.value[0..@min(h.value.len, 64)], .note = "See `--advertise-host <host>` if this is a legitimate host" });
return error.ForbiddenHost;
}
}
@@ -1178,25 +1178,26 @@ fn isAllowedHost(value: []const u8, advertise_host: ?[]const u8) bool {
const testing = @import("../testing.zig");
test "http: isAllowedHost accepts IP literals and localhost, rejects other names" {
- try testing.expect(isAllowedHost("127.0.0.1:9222", null));
- try testing.expect(isAllowedHost("[::1]:9222", null));
- try testing.expect(isAllowedHost("localhost:9222", null));
- try testing.expect(!isAllowedHost("lightpanda:9222", null));
- try testing.expect(!isAllowedHost("attacker.example:9222", null));
+ try testing.expectEqual(true, isAllowedHost("127.0.0.1:9222", null));
+ try testing.expectEqual(true, isAllowedHost("[::1]:9222", null));
+ try testing.expectEqual(true, isAllowedHost("localhost:9222", null));
+ try testing.expectEqual(false, isAllowedHost("lightpanda:9222", null));
+ try testing.expectEqual(false, isAllowedHost("attacker.example:9222", null));
}
test "http: isAllowedHost accepts the explicitly advertised host" {
- try testing.expect(isAllowedHost("lightpanda:9222", "lightpanda"));
- try testing.expect(isAllowedHost("LightPanda:9222", "lightpanda"));
- try testing.expect(isAllowedHost("lightpanda", "lightpanda"));
- try testing.expect(!isAllowedHost("lightpanda.attacker.example:9222", "lightpanda"));
- try testing.expect(!isAllowedHost("attacker.example:9222", "lightpanda"));
- try testing.expect(!isAllowedHost(":9222", "lightpanda"));
+ try testing.expectEqual(true, isAllowedHost("lightpanda:9222", "lightpanda"));
+ try testing.expectEqual(true, isAllowedHost("LightPanda:9222", "lightpanda"));
+ try testing.expectEqual(true, isAllowedHost("lightpanda", "lightpanda"));
+ try testing.expectEqual(false, isAllowedHost("lightpanda.attacker.example:9222", "lightpanda"));
+ try testing.expectEqual(false, isAllowedHost("attacker.example:9222", "lightpanda"));
+ try testing.expectEqual(false, isAllowedHost(":9222", "lightpanda"));
// The explicit host never weakens the original rules.
- try testing.expect(isAllowedHost("10.0.0.5:9222", "lightpanda"));
+ try testing.expectEqual(true, isAllowedHost("10.0.0.5:9222", "lightpanda"));
}
test "http: webSocketAccept honours the advertised host" {
+ testing.expectLog(&.{.serve});
const head = "GET / HTTP/1.1\r\n" ++
"Host: lightpanda:9222\r\n" ++
"Upgrade: websocket\r\n" ++