From 03e87155ca0563c655f8b0425d574a43c4b25719 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 8 Jan 2025 16:15:54 +0100 Subject: [PATCH 1/3] cdp: add security.enable --- src/cdp/cdp.zig | 3 +++ src/cdp/security.zig | 59 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/cdp/security.zig diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index 9ce311a4..95174a2d 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -35,6 +35,7 @@ const Input = @import("msg.zig").Input; const inspector = @import("inspector.zig").inspector; const dom = @import("dom.zig").dom; const css = @import("css.zig").css; +const security = @import("security.zig").security; const log_cdp = std.log.scoped(.cdp); @@ -69,6 +70,7 @@ const Domains = enum { Emulation, Fetch, Performance, + Security, }; // The caller is responsible for calling `free` on the returned slice. @@ -112,6 +114,7 @@ pub fn dispatch( .Emulation => emulation(alloc, msg, action, ctx), .Fetch => fetch(alloc, msg, action, ctx), .Performance => performance(alloc, msg, action, ctx), + .Security => security(alloc, msg, action, ctx), }; } diff --git a/src/cdp/security.zig b/src/cdp/security.zig new file mode 100644 index 00000000..42912544 --- /dev/null +++ b/src/cdp/security.zig @@ -0,0 +1,59 @@ +// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) +// +// Francis Bouvier +// Pierre Tachoire +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +const std = @import("std"); + +const server = @import("../server.zig"); +const Ctx = server.Ctx; +const cdp = @import("cdp.zig"); +const result = cdp.result; +const IncomingMessage = @import("msg.zig").IncomingMessage; +const Input = @import("msg.zig").Input; + +const log = std.log.scoped(.cdp); + +const Methods = enum { + enable, +}; + +pub fn security( + alloc: std.mem.Allocator, + msg: *IncomingMessage, + action: []const u8, + ctx: *Ctx, +) ![]const u8 { + const method = std.meta.stringToEnum(Methods, action) orelse + return error.UnknownMethod; + + return switch (method) { + .enable => enable(alloc, msg, ctx), + }; +} + +fn enable( + alloc: std.mem.Allocator, + msg: *IncomingMessage, + _: *Ctx, +) ![]const u8 { + // input + const input = try Input(void).get(alloc, msg); + defer input.deinit(); + log.debug("Req > id {d}, method {s}", .{ input.id, "security.enable" }); + + return result(alloc, input.id, null, null, input.sessionId); +} From c761cd059b5b3cc3b78dce072f4435fbfe7437e8 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 8 Jan 2025 16:16:27 +0100 Subject: [PATCH 2/3] cdp: log errors on sendMessageToTarget --- src/cdp/target.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/cdp/target.zig b/src/cdp/target.zig index c4b17365..d21b4edd 100644 --- a/src/cdp/target.zig +++ b/src/cdp/target.zig @@ -453,13 +453,17 @@ fn sendMessageToTarget( }; const input = try Input(Params).get(alloc, msg); defer input.deinit(); - log.debug("Req > id {d}, method {s}", .{ input.id, "target.sendMessageToTarget" }); + log.debug("Req > id {d}, method {s} ({s})", .{ input.id, "target.sendMessageToTarget", input.params.message }); // get the wrapped message. var wmsg = IncomingMessage.init(alloc, input.params.message); defer wmsg.deinit(); - const res = try cdp.dispatch(alloc, &wmsg, ctx); + const res = cdp.dispatch(alloc, &wmsg, ctx) catch |e| { + log.err("send message {d} ({s}): {any}", .{ input.id, input.params.message, e }); + // TODO dispatch error correctly. + return e; + }; // receivedMessageFromTarget event const ReceivedMessageFromTarget = struct { From 82bbe78e95cadb347e3a2cebf05f798068e116e0 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 8 Jan 2025 16:16:51 +0100 Subject: [PATCH 3/3] cdp: return correct result on Runtime --- src/cdp/runtime.zig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cdp/runtime.zig b/src/cdp/runtime.zig index cab25b90..3eef3f6c 100644 --- a/src/cdp/runtime.zig +++ b/src/cdp/runtime.zig @@ -129,7 +129,10 @@ fn sendInspector( } ctx.sendInspector(msg.json); - return ""; + + if (msg.id == null) return ""; + + return result(alloc, msg.id.?, null, null, msg.sessionId); } pub const AuxData = struct {