Skip to content

Lightpanda: the headless browser designed for AI and automation

`Network`: support custom CA install

Halil Durakb7f61e57661b
diff --git a/src/Config.zig b/src/Config.zig
index 2ec4b82a2..8576a987d 100644
--- a/src/Config.zig
+++ b/src/Config.zig
@@ -118,6 +118,9 @@ const CommonOptions = .{
.{ .name = "v8_flags_unsafe", .type = ?[]const u8 },
.{ .name = "v8_max_heap_mb", .type = ?u32 },
.{ .name = "watchdog_ms", .type = ?u32 },
+ .{ .name = "ca_cert", .type = [:0]const u8, .multiple = true },
+ .{ .name = "ca_path", .type = [:0]const u8, .multiple = true },
+ .{ .name = "disable_root_certificates", .type = bool },
};
fn dumpValidator(_: Allocator, args: *std.process.Args.Iterator) !?DumpFormat {
@@ -388,6 +391,14 @@ pub fn v8MaxHeapMb(self: *const Config) ?u32 {
};
}
+pub fn disableRootCertificates(self: *const Config) bool {
+ return switch (self.mode) {
+ inline .serve, .fetch, .mcp, .agent => |opts| opts.disable_root_certificates,
+ .version => false,
+ else => unreachable,
+ };
+}
+
pub fn httpProxy(self: *const Config) ?[:0]const u8 {
return switch (self.mode) {
inline .serve, .fetch, .mcp, .agent => |opts| opts.http_proxy,
diff --git a/src/network/Network.zig b/src/network/Network.zig
index 868e82680..5cf2c8fd3 100644
--- a/src/network/Network.zig
+++ b/src/network/Network.zig
@@ -183,7 +183,7 @@ pub fn init(allocator: Allocator, app: *App, config: *const Config) !Network {
const x509_store = blk: {
if (config.tlsVerifyHost()) {
- break :blk try createX509Store(allocator);
+ break :blk try createX509Store(allocator, config);
}
break :blk crypto.X509_STORE_new() orelse {
return error.FailedToCreateX509Store;
@@ -734,10 +734,38 @@ const CreateX509StoreError = std.crypto.Certificate.Bundle.RescanError || error{
/// NEVER give full ownership of store to `SSL_CTX`, always rely on ref counting.
/// Allocations made through passed `allocator` are freed before this function returns.
-pub fn createX509Store(allocator: Allocator) CreateX509StoreError!*crypto.X509_STORE {
+fn createX509Store(allocator: Allocator, config: *const Config) CreateX509StoreError!*crypto.X509_STORE {
const store = crypto.X509_STORE_new() orelse return error.FailedToCreateX509Store;
errdefer crypto.X509_STORE_free(store);
+ switch (config.mode) {
+ // Load custom CA if provided.
+ inline .serve, .fetch, .mcp, .agent => |opts| {
+ const files = opts.ca_cert.items;
+ const directories = opts.ca_path.items;
+
+ for (directories) |ca_path| {
+ if (crypto.X509_STORE_load_locations(store, null, ca_path) != 1) {
+ log.warn(.app, "Invalid CA path", .{ .ca_path = ca_path });
+ }
+ }
+
+ for (files) |ca_cert| {
+ if (crypto.X509_STORE_load_locations(store, ca_cert, null) != 1) {
+ log.warn(.app, "Invalid CA cert", .{ .ca_cert = ca_cert });
+ }
+ }
+ },
+ .help => unreachable,
+ .version => {}, // Don't load custom CA for version command.
+ }
+
+ // Early return if root certificates are unwanted.
+ if (config.disableRootCertificates()) {
+ log.warn(.app, "Root certificates are disabled", .{});
+ return store;
+ }
+
switch (comptime builtin.os.tag) {
.linux, .openbsd, .netbsd, .freebsd => blk: {
// Iterate over known directories.