Skip to content

Commit d2065f7

Browse files
committed
cdp: implement Security.setIgnoreCertificateErrors
1 parent fe37770 commit d2065f7

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/cdp/domains/security.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,29 @@ const std = @import("std");
2121
pub fn processMessage(cmd: anytype) !void {
2222
const action = std.meta.stringToEnum(enum {
2323
enable,
24+
setIgnoreCertificateErrors,
2425
}, cmd.input.action) orelse return error.UnknownMethod;
2526

2627
switch (action) {
2728
.enable => return cmd.sendResult(null, .{}),
29+
.setIgnoreCertificateErrors => return setIgnoreCertificateErrors(cmd),
2830
}
2931
}
32+
33+
fn setIgnoreCertificateErrors(cmd: anytype) !void {
34+
const params = (try cmd.params(struct {
35+
ignore: bool,
36+
})) orelse return error.InvalidParams;
37+
38+
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
39+
40+
if (params.ignore) {
41+
try cmd.cdp.browser.http_client.disableTlsVerify();
42+
} else {
43+
try cmd.cdp.browser.http_client.enableTlsVerify();
44+
}
45+
46+
return cmd.sendResult(.{
47+
.browserContextId = bc.id,
48+
}, .{});
49+
}

src/http/Client.zig

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ notification: ?*Notification = null,
9393
// restoring, this originally-configured value is what it goes to.
9494
http_proxy: ?[:0]const u8 = null,
9595

96+
// track if the client use a proxy for connections.
97+
// We can't use http_proxy because we want also to track proxy configured via
98+
// CDP.
99+
use_proxy: bool,
100+
96101
// The complete user-agent header line
97102
user_agent: [:0]const u8,
98103

@@ -126,6 +131,7 @@ pub fn init(allocator: Allocator, ca_blob: ?c.curl_blob, opts: Http.Opts) !*Clie
126131
.handles = handles,
127132
.allocator = allocator,
128133
.http_proxy = opts.http_proxy,
134+
.use_proxy = opts.http_proxy != null,
129135
.user_agent = opts.user_agent,
130136
.transfer_pool = transfer_pool,
131137
};
@@ -315,6 +321,7 @@ pub fn changeProxy(self: *Client, proxy: [:0]const u8) !void {
315321
for (self.handles.handles) |*h| {
316322
try errorCheck(c.curl_easy_setopt(h.conn.easy, c.CURLOPT_PROXY, proxy.ptr));
317323
}
324+
self.use_proxy = true;
318325
}
319326

320327
// Same restriction as changeProxy. Should be ok since this is only called on
@@ -326,6 +333,37 @@ pub fn restoreOriginalProxy(self: *Client) !void {
326333
for (self.handles.handles) |*h| {
327334
try errorCheck(c.curl_easy_setopt(h.conn.easy, c.CURLOPT_PROXY, proxy));
328335
}
336+
self.use_proxy = proxy != null;
337+
}
338+
339+
// Enable TLS verification on all connections.
340+
pub fn enableTlsVerify(self: *const Client) !void {
341+
for (self.handles.handles) |*h| {
342+
const easy = h.conn.easy;
343+
344+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_SSL_VERIFYHOST, @as(c_long, 1)));
345+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_SSL_VERIFYPEER, @as(c_long, 1)));
346+
347+
if (self.use_proxy) {
348+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_PROXY_SSL_VERIFYHOST, @as(c_long, 1)));
349+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_PROXY_SSL_VERIFYPEER, @as(c_long, 1)));
350+
}
351+
}
352+
}
353+
354+
// Disable TLS verification on all connections.
355+
pub fn disableTlsVerify(self: *const Client) !void {
356+
for (self.handles.handles) |*h| {
357+
const easy = h.conn.easy;
358+
359+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_SSL_VERIFYHOST, @as(c_long, 0)));
360+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_SSL_VERIFYPEER, @as(c_long, 0)));
361+
362+
if (self.use_proxy) {
363+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_PROXY_SSL_VERIFYHOST, @as(c_long, 0)));
364+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_PROXY_SSL_VERIFYPEER, @as(c_long, 0)));
365+
}
366+
}
329367
}
330368

331369
fn makeRequest(self: *Client, handle: *Handle, transfer: *Transfer) !void {

0 commit comments

Comments
 (0)