Skip to content

Lightpanda: the headless browser designed for AI and automation

add CORS metrics

Muki Kiboigoaddba12426b0
diff --git a/src/Metrics.zig b/src/Metrics.zig
index 89a351495..0743d3a9c 100644
--- a/src/Metrics.zig
+++ b/src/Metrics.zig
@@ -91,6 +91,9 @@ http_navigation_delay_ms: Histogram(&.{
}) = .{},
robots_status: CounterEnum("category", @import("network/http.zig").StatusCategory) = .{},
robots_access: CounterEnum("result", enum { allow, deny }) = .{},
+cors_check: CounterEnum("result", enum { same_origin, no_cors, simple, preflight }) = .{},
+cors_preflight: CounterEnum("result", enum { allowed, blocked }) = .{},
+cors_response: CounterEnum("result", enum { allowed, blocked }) = .{},
// Emitted as each metric's "# HELP" line. A field without an entry is a
// compile error.
@@ -123,6 +126,9 @@ const help = .{
.http_navigation_delay_ms = "Time in milliseconds a throttled top-level navigation waited",
.robots_status = "robots.txt response status",
.robots_access = "robots.txt result",
+ .cors_check = "CORS initial classification: same_origin/no_cors need no CORS handling, simple needs response validation only, preflight needs an OPTIONS round-trip first",
+ .cors_preflight = "CORS preflight (OPTIONS) results, one per request that required one",
+ .cors_response = "CORS actual-response validation results",
};
pub fn write(self: *const Metrics, writer: *std.Io.Writer) void {
diff --git a/src/network/CorsGate.zig b/src/network/CorsGate.zig
index bff3060cc..06d519d2a 100644
--- a/src/network/CorsGate.zig
+++ b/src/network/CorsGate.zig
@@ -62,11 +62,13 @@ fn flushPending(self: *CorsGate, key: []const u8, allowed: bool) void {
transfer.unpark();
if (!allowed) {
+ lp.metrics.cors_preflight.incr(.blocked);
log.warn(.cors, "preflight blocked", .{ .url = transfer.req.url });
transfer.failAsync(error.CorsBlocked);
continue;
}
+ lp.metrics.cors_preflight.incr(.allowed);
transfer.client.resumeAfterCors(transfer) catch |e| {
transfer.abortPipelineError(e);
};
@@ -183,6 +185,7 @@ pub fn check(self: *CorsGate, transfer: *Transfer) !Result {
if (req.origin) |origin| {
if (URL.isSameOrigin(req.url, origin)) {
log.debug(.cors, "same origin", .{ .url = req.url, .origin = origin });
+ lp.metrics.cors_check.incr(.same_origin);
return .allowed;
}
}
@@ -198,6 +201,7 @@ pub fn check(self: *CorsGate, transfer: *Transfer) !Result {
.origin = origin,
.mode = "no-cors",
});
+ lp.metrics.cors_check.incr(.no_cors);
return .allowed;
}
@@ -207,6 +211,7 @@ pub fn check(self: *CorsGate, transfer: *Transfer) !Result {
.origin = origin,
.preflight = false,
});
+ lp.metrics.cors_check.incr(.simple);
return .allowed;
}
@@ -215,6 +220,7 @@ pub fn check(self: *CorsGate, transfer: *Transfer) !Result {
.origin = origin,
.preflight = true,
});
+ lp.metrics.cors_check.incr(.preflight);
try self.fetchThenResume(transfer);
return .pending;
@@ -548,6 +554,8 @@ fn fetchThenResume(self: *CorsGate, transfer: *Transfer) !void {
pub fn validateResponse(transfer: *Transfer) !void {
const req = &transfer.req;
+ errdefer lp.metrics.cors_response.incr(.blocked);
+
const allow_origin = HttpClient.findHeader(transfer.res.headers, ACCESS_CONTROL_ALLOW_ORIGIN) orelse {
log.warn(.cors, "blocked", .{ .url = req.url, .reason = "missing acao" });
return error.CorsBlocked;
@@ -589,4 +597,6 @@ pub fn validateResponse(transfer: *Transfer) !void {
return error.CorsBlocked;
}
}
+
+ lp.metrics.cors_response.incr(.allowed);
}