Skip to content

Lightpanda: the headless browser designed for AI and automation

add missing user-agent validator for CLI

Pierre Tachoire859543ed3ea1
diff --git a/src/Config.zig b/src/Config.zig
index 699402da4..1eb09a480 100644
--- a/src/Config.zig
+++ b/src/Config.zig
@@ -195,7 +195,7 @@ const CommonOptions = .{
.{ .name = "web_bot_auth_key_file", .type = ?[]const u8 },
.{ .name = "web_bot_auth_keyid", .type = ?[]const u8 },
.{ .name = "web_bot_auth_domain", .type = ?[]const u8 },
- .{ .name = "user_agent", .type = ?[]const u8 },
+ .{ .name = "user_agent", .type = ?[]const u8, .validator = userAgentValidator },
.{ .name = "block_private_networks", .type = bool },
.{ .name = "block_cidrs", .type = ?[]const u8 },
.{ .name = "block_urls", .type = ?[]const u8 },
@@ -1051,6 +1051,16 @@ test "Config: advertiseHost preserves concrete host when not a wildcard" {
try std.testing.expectEqualStrings("127.0.0.1", config.advertiseHost());
}
+fn userAgentValidator(allocator: Allocator, args: *std.process.Args.Iterator, ua: *?[]const u8) !void {
+ const str = args.next() orelse return error.MissingArgument;
+ validateUserAgent(str) catch {
+ log.fatal(.app, "invalid user-agent", .{ .hint = "User agent can't contain Mozilla" });
+ return error.InvalidArgument;
+ };
+
+ ua.* = try allocator.dupe(u8, str);
+}
+
pub fn validateUserAgent(ua: []const u8) !void {
for (ua) |c| {
if (!std.ascii.isPrint(c)) {