Skip to content

Lightpanda: the headless browser designed for AI and automation

`AdBlocker`: run /regex/ filters with PCRE2

Adrià Arrufatc9329d5955cc
diff --git a/build.zig b/build.zig
index be9d47485..42a58eba2 100644
--- a/build.zig
+++ b/build.zig
@@ -125,6 +125,7 @@ pub fn build(b: *Build) !void {
linkZenai(b, lightpanda_module);
linkIsocline(b, lightpanda_module);
linkSqlite(b, lightpanda_module, enable_csan, enable_tsan, orderfile != null);
+ linkPcre2(b, lightpanda_module, enable_csan, enable_tsan, orderfile != null);
// Check compilation
const check = b.step("check", "Check if lightpanda compiles");
@@ -486,6 +487,27 @@ fn linkSqlite(b: *Build, mod: *Build.Module, enable_csan: ?std.zig.SanitizeC, is
mod.addImport("sqlite3", translate_c.createModule());
}
+fn linkPcre2(b: *Build, mod: *Build.Module, enable_csan: ?std.zig.SanitizeC, is_tsan: bool, section: bool) void {
+ const dep = b.dependency("pcre2", .{
+ .target = mod.resolved_target.?,
+ .optimize = mod.optimize.?,
+ .linkage = .static,
+ });
+
+ const lib = sectionize(dep.artifact("pcre2-8"), section);
+ lib.root_module.sanitize_c = enable_csan;
+ lib.root_module.sanitize_thread = is_tsan;
+ mod.linkLibrary(lib);
+
+ const translate_c = b.addTranslateC(.{
+ .root_source_file = lib.getEmittedIncludeTree().path(b, "pcre2.h"),
+ .target = mod.resolved_target.?,
+ .optimize = mod.optimize.?,
+ });
+ translate_c.defineCMacro("PCRE2_CODE_UNIT_WIDTH", "8");
+ mod.addImport("pcre2", translate_c.createModule());
+}
+
fn linkCurl(b: *Build, mod: *Build.Module, is_tsan: bool, section: bool) void {
const target = mod.resolved_target.?;
diff --git a/build.zig.zon b/build.zig.zon
index 1b953147d..d0bf21fcb 100644
--- a/build.zig.zon
+++ b/build.zig.zon
@@ -43,6 +43,10 @@
.url = "git+https://github.com/arrufat/isocline#ec538faf435c616a6b38716f53980b5815c30f8a",
.hash = "N-V-__8AAHhtEwBIqx5nOoiGo_FLAG8gpiVC6XzZn1teMKd0",
},
+ .pcre2 = .{
+ .url = "https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.48/pcre2-10.48.tar.gz",
+ .hash = "pcre2-10.48.0-IZ6r69wnegD6fc08FgRTKVcmyp4Ux2aVefaVvsRndgEl",
+ },
},
.paths = .{""},
}
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index 2ff673156..ce74cc59b 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -4294,12 +4294,31 @@ test "HttpClient: adblock verdicts apply per request" {
\\||typed.example.com^$script
\\||partied.example.com^$third-party
\\||framed.example.com^$subdocument
+ \\/\/[a-z]{4}\.js$/$match-case,script
);
try blocker.parse(&list);
try blocker.build();
client.network.adblocker = blocker;
defer client.network.adblocker = null;
+ // A regex filter reads the URL as requested: case kept, fragment gone.
+ try testing.expect(testIsUrlBlocked(&client, .{
+ .url = "https://cdn.example.com/abcd.js",
+ .resource_type = .script,
+ }));
+ try testing.expect(testIsUrlBlocked(&client, .{
+ .url = "https://cdn.example.com/abcd.js#v2",
+ .resource_type = .script,
+ }));
+ try testing.expect(!testIsUrlBlocked(&client, .{
+ .url = "https://cdn.example.com/ABCD.js",
+ .resource_type = .script,
+ }));
+ try testing.expect(!testIsUrlBlocked(&client, .{
+ .url = "https://cdn.example.com/abcd.js",
+ .resource_type = .xhr,
+ }));
+
try testing.expect(testIsUrlBlocked(&client, .{ .url = "https://ads.example.com/pixel.gif" }));
// Hostnames are matched case-insensitively and without the port.
try testing.expect(testIsUrlBlocked(&client, .{ .url = "https://SUB.ADS.EXAMPLE.COM:8443/x" }));
diff --git a/src/network/adblock/AdBlocker.zig b/src/network/adblock/AdBlocker.zig
index 44ebcb265..005ecc1f0 100644
--- a/src/network/adblock/AdBlocker.zig
+++ b/src/network/adblock/AdBlocker.zig
@@ -29,6 +29,7 @@ const Parser = @import("Parser.zig");
const Engine = @import("Engine.zig");
const HostnameTrie = @import("HostnameTrie.zig");
const NetworkFilter = @import("NetworkFilter.zig");
+const Regex = @import("Regex.zig");
const log = lp.log;
@@ -47,6 +48,11 @@ arena: std.heap.ArenaAllocator,
filters: std.ArrayList(NetworkFilter),
/// Filled while parsing, consumed by `build`.
badfilters: std.AutoHashMapUnmanaged(u64, void),
+/// Every regex compiled for a filter, whether or not `build` kept the
+/// filter; they are freed here, not through `filters`.
+regexes: std.ArrayList(Regex),
+/// What the regexes are compiled and run with; PCRE2 allocates through it.
+regex_context: *Regex.Context,
built: bool,
trie: HostnameTrie,
blocked: u32,
@@ -64,7 +70,7 @@ exceptions: Engine,
/// Rules that reached a trie or an index, across every list parsed so far.
rules_loaded: usize,
/// Rules the lists carry and we do not apply, across those same lists: the
-/// ones the parser could not read, plus the ones no matcher can express.
+/// ones the parser could not read, regex literals PCRE2 rejects included.
rules_skipped: usize,
/// Domain-scoped element-hiding rules and cosmetic-realm exceptions, across
/// those same lists. Not ours to apply, but not rules we failed at either, so
@@ -77,6 +83,8 @@ rules_cosmetic: usize,
const LINE_MAX = 8 * 1024;
pub fn init(allocator: Allocator) Allocator.Error!AdBlocker {
+ const regex_context: *Regex.Context = try .init(allocator);
+ errdefer regex_context.deinit();
var trie: HostnameTrie = try .init(allocator);
errdefer trie.deinit(allocator);
const blocked = try trie.createTrie(allocator);
@@ -89,6 +97,8 @@ pub fn init(allocator: Allocator) Allocator.Error!AdBlocker {
.arena = std.heap.ArenaAllocator.init(allocator),
.filters = .empty,
.badfilters = .empty,
+ .regexes = .empty,
+ .regex_context = regex_context,
.built = false,
.trie = trie,
.blocked = blocked,
@@ -109,6 +119,9 @@ pub fn deinit(self: *AdBlocker) void {
self.blocking_important.deinit(self.allocator);
self.exceptions.deinit(self.allocator);
self.badfilters.deinit(self.allocator);
+ for (self.regexes.items) |regex| regex.deinit();
+ self.regexes.deinit(self.allocator);
+ self.regex_context.deinit();
self.filters.deinit(self.allocator);
self.trie.deinit(self.allocator);
self.arena.deinit();
@@ -180,7 +193,7 @@ pub fn parse(self: *AdBlocker, reader: *Io.Reader) !void {
// both of which the next call reuses, so what we keep is copied out.
defer _ = scratch_instance.reset(.retain_capacity);
- const filter = switch (item) {
+ var filter = switch (item) {
// The parser already counted it; all we want is whether it was an
// exception, in which case its hostname stops being blockable.
.dropped => |dropped| {
@@ -201,11 +214,6 @@ pub fn parse(self: *AdBlocker, reader: *Io.Reader) !void {
self.rules_cosmetic += 1;
continue;
}
- if (!isSupported(&filter)) {
- self.rules_skipped += 1;
- continue;
- }
-
if (filter.badfilter) {
// It cancels a rule that may not have been read yet, so it can
// only be resolved once every list is in. It is not a rule we
@@ -215,6 +223,20 @@ pub fn parse(self: *AdBlocker, reader: *Io.Reader) !void {
continue;
}
+ if (filter.kind == .regex) {
+ const body = filter.pattern[1 .. filter.pattern.len - 1];
+ const regex = Regex.compile(self.regex_context, body, !filter.match_case) catch |err| switch (err) {
+ error.OutOfMemory => return error.OutOfMemory,
+ error.InvalidRegex => {
+ self.rules_skipped += 1;
+ continue;
+ },
+ };
+ errdefer regex.deinit();
+ try self.regexes.append(self.allocator, regex);
+ filter.regex = regex;
+ }
+
try self.filters.append(self.allocator, try dupe(arena, &filter));
self.rules_loaded += 1;
}
@@ -361,12 +383,6 @@ pub fn match(self: *const AdBlocker, request: *const Request) Verdict {
return .blocked;
}
-/// Whether we can evaluate this filter at all.
-fn isSupported(filter: *const NetworkFilter) bool {
- // No regex engine to run them with.
- return filter.kind != .regex;
-}
-
/// Whether the filter's whole effect is "every request to this hostname and
/// its subdomains", which is all a trie can express.
fn isWholeHostname(filter: *const NetworkFilter) bool {
@@ -451,11 +467,12 @@ fn expectVerdict(
source: []const u8,
kind: ResourceTypes,
) !void {
- // `match` takes a lowercased URL, as the real caller hands it; patterns
- // are lowercased too, so a rule spelled "/embed/C-iDzdvIg1Y" still lands.
+ // `match` takes the URL both raw and lowercased, as the real caller hands
+ // it; patterns are lowercased too, so a rule spelled "/embed/C-iDzdvIg1Y"
+ // still lands.
var buf: [512]u8 = undefined;
const lowered = std.ascii.lowerString(buf[0..url.len], url);
- const request: Request = .init(lowered, source, kind);
+ const request: Request = .init(url, lowered, source, kind);
try testing.expectEqual(expected, blocker.match(&request));
}
@@ -591,7 +608,7 @@ test "adblock.AdBlocker: tokens past the request buffer still match" {
// 140 tokens of query noise push the filter's token ("utm", its rarest)
// past what the request holds; the engine walks the rest of the URL.
const noise = "https://example.com/?" ++ "a=1&" ** 70;
- const overflowing: Request = .init(noise ++ "utm_tracker=1", "a.com", script);
+ const overflowing: Request = .init(noise ++ "utm_tracker=1", noise ++ "utm_tracker=1", "a.com", script);
try testing.expect(overflowing.tail.len != 0);
try testing.expectEqual(.blocked, blocker.match(&overflowing));
// A token past the buffer finds its bucket, but the pattern does not fit.
@@ -608,7 +625,7 @@ test "adblock.AdBlocker: cosmetic-realm rules are not skipped rules" {
\\@@||example.com^$generichide
\\@@||example.com^$elemhide
\\example.com##.ad-banner
- \\/regex-we-cannot-run/
+ \\/regex-we-cannot-run(/
);
try testing.expectEqual(1, blocker.rules_loaded);
@@ -616,6 +633,49 @@ test "adblock.AdBlocker: cosmetic-realm rules are not skipped rules" {
try testing.expectEqual(3, blocker.rules_cosmetic);
}
+test "adblock.AdBlocker: the regex rules from EasyList" {
+ var blocker: AdBlocker = try .init(testing.allocator);
+ defer blocker.deinit();
+
+ try testLoad(&blocker,
+ \\/\/[0-9a-f]{32}\/invoke\.js/$script,third-party
+ \\/^https?:\/\/[0-9a-z]{5,}\.com\/.*/$script,third-party,xmlhttprequest,domain=dood.to
+ \\@@/\/invoke\.js/$domain=trusted.com
+ \\/\/[a-z]{4}\.js$/$match-case,script
+ );
+ try testing.expectEqual(4, blocker.rules_loaded);
+ try testing.expectEqual(0, blocker.rules_skipped);
+
+ const invoke = "https://host.com/0123456789abcdef0123456789abcdef/invoke.js";
+ try expectVerdict(&blocker, .blocked, invoke, "site.com", script);
+ try expectVerdict(&blocker, .blocked, "https://HOST.com/0123456789ABCDEF0123456789abcdef/invoke.js", "site.com", script);
+ try expectVerdict(&blocker, .none, invoke, "host.com", script);
+ try expectVerdict(&blocker, .none, invoke, "site.com", image);
+ try expectVerdict(&blocker, .none, "https://host.com/0123456789abcdef0123456789abcde/invoke.js", "site.com", script);
+ try expectVerdict(&blocker, .allowed, invoke, "trusted.com", script);
+
+ try expectVerdict(&blocker, .blocked, "https://abcde.com/x", "dood.to", xhr);
+ try expectVerdict(&blocker, .none, "https://abcde.com/x", "other.to", xhr);
+ try expectVerdict(&blocker, .none, "https://abcd.com/x", "dood.to", xhr);
+
+ // `$match-case` reads the URL as requested, not the lowercased copy.
+ try expectVerdict(&blocker, .blocked, "https://x.com/abcd.js", "x.com", script);
+ try expectVerdict(&blocker, .none, "https://x.com/ABCD.js", "x.com", script);
+}
+
+test "adblock.AdBlocker: $badfilter removes a regex rule" {
+ var blocker: AdBlocker = try .init(testing.allocator);
+ defer blocker.deinit();
+
+ try testLoad(&blocker,
+ \\/\/invoke\.js/$script
+ \\/\/invoke\.js/$script,badfilter
+ );
+ try testing.expectEqual(0, blocker.rules_loaded);
+ try testing.expectEqual(2, blocker.rules_skipped);
+ try expectVerdict(&blocker, .none, "https://host.com/invoke.js", "site.com", script);
+}
+
test "adblock.AdBlocker: verdict precedence" {
var blocker: AdBlocker = try .init(testing.allocator);
defer blocker.deinit();
diff --git a/src/network/adblock/Engine.zig b/src/network/adblock/Engine.zig
index 8a6159070..49bd3031d 100644
--- a/src/network/adblock/Engine.zig
+++ b/src/network/adblock/Engine.zig
@@ -66,6 +66,9 @@ pub fn deinit(self: *Engine, allocator: Allocator) void {
/// request and shared by all three engines.
pub const Request = struct {
url: pattern.Url,
+ /// The URL as requested, fragment stripped but case kept: what a regex
+ /// filter reads, since `$match-case` is only meaningful on the original.
+ raw: []const u8,
/// The hostname of the document the request belongs to. Falls back to the
/// request's own hostname, which is what uBO does for top-level loads.
source_hostname: []const u8,
@@ -95,9 +98,11 @@ pub const Request = struct {
source: [SOURCE_MAX]u8,
};
- /// `url` must already be lowercased and fragment-free; `source_hostname`
- /// may be empty when there is no document context.
+ /// `raw` and `url` are the same fragment-free URL, the second one
+ /// lowercased; `source_hostname` may be empty when there is no document
+ /// context.
pub fn init(
+ raw: []const u8,
url: []const u8,
source_hostname: []const u8,
kind: NetworkFilter.ResourceTypes,
@@ -106,6 +111,7 @@ pub const Request = struct {
const source = if (source_hostname.len == 0) parsed.hostname() else source_hostname;
var request: Request = .{
.url = parsed,
+ .raw = raw,
.source_hostname = source,
.kind = kind,
.third_party = domain.isThirdParty(parsed.hostname(), source),
@@ -125,7 +131,10 @@ pub const Request = struct {
pub fn fromHttp(transfer: *const HttpClient.Transfer, buffers: *Buffers) ?Request {
const req = &transfer.req;
- const url = normalizeUrl(req.url, &buffers.url) orelse return null;
+ // No request URL carries a fragment onto the wire.
+ const fragment = std.mem.indexOfScalar(u8, req.url, '#') orelse req.url.len;
+ const raw = req.url[0..fragment];
+ const url = normalizeUrl(raw, &buffers.url) orelse return null;
var owner: ?*const HttpClient.Owner = transfer.owner;
var subframe = false;
@@ -148,27 +157,23 @@ pub const Request = struct {
.worker => .{ .script = true },
};
- return .init(url, source, resource_type);
+ return .init(raw, url, source, resource_type);
}
inline fn tokens(self: *const Request) []const u32 {
return self.tokens_buf[0..self.tokens_len];
}
- /// Lowercases `url` into `buf` with its fragment stripped; patterns are
- /// stored lowercased, and no request URL carries a fragment onto the wire.
+ /// Lowercases `url` into `buf`, as patterns are stored lowercased.
fn normalizeUrl(url: []const u8, buf: []u8) ?[]const u8 {
- const end = std.mem.indexOfScalar(u8, url, '#') orelse url.len;
- const trimmed = url[0..end];
-
- const upper = for (trimmed, 0..) |c, i| {
+ const upper = for (url, 0..) |c, i| {
if (std.ascii.isUpper(c)) break i;
- } else return trimmed;
+ } else return url;
- if (trimmed.len > buf.len) return null;
- const out = buf[0..trimmed.len];
- @memcpy(out[0..upper], trimmed[0..upper]);
- _ = std.ascii.lowerString(out[upper..], trimmed[upper..]);
+ if (url.len > buf.len) return null;
+ const out = buf[0..url.len];
+ @memcpy(out[0..upper], url[0..upper]);
+ _ = std.ascii.lowerString(out[upper..], url[upper..]);
return out;
}
};
@@ -219,6 +224,8 @@ fn matchesFilter(filter: *const NetworkFilter, request: *const Request) bool {
return false;
}
if (!filter.domains.matches(request.source_hostname)) return false;
+ // uBO tests the raw URL; the case-insensitive flag is on the pattern.
+ if (filter.kind == .regex) return filter.regex.?.matches(request.raw);
return pattern.matches(filter, request.url);
}
@@ -326,8 +333,8 @@ fn collectTokens(filter: *const NetworkFilter, buf: []u32) []u32 {
}
}
- // A /regex/ literal is never indexed (nothing can run it), and `.any`
- // has no pattern at all.
+ // A /regex/ literal is never indexed (no token is pulled out of one),
+ // and `.any` has no pattern at all.
if (filter.kind == .regex or filter.pattern.len == 0) return buf[0..n];
const text = filter.pattern;
@@ -376,12 +383,12 @@ test "adblock.Engine: a request keeps its first tokens, the rest as tail" {
// Exactly as many tokens as the buffer holds: nothing is left to walk...
const full = "x/" ** (max - 1) ++ "x";
- var request: Request = .init(full, "", kind);
+ var request: Request = .init(full, full, "", kind);
try testing.expectEqual(max, request.tokens_len);
try testing.expectEqual(0, request.tail.len);
// ...one more, and only that one is in the tail.
- request = .init(full ++ "/y", "", kind);
+ request = .init(full ++ "/y", full ++ "/y", "", kind);
try testing.expectEqual(max, request.tokens_len);
try testing.expectString("/y", request.tail);
var it: Tokens = .{ .text = request.tail };
@@ -389,7 +396,7 @@ test "adblock.Engine: a request keeps its first tokens, the rest as tail" {
try testing.expect(it.next() == null);
// Fewer than the buffer holds: the tail is empty.
- request = .init("https://example.com/a", "", kind);
+ request = .init("https://example.com/a", "https://example.com/a", "", kind);
try testing.expectEqual(4, request.tokens_len);
try testing.expectEqual(0, request.tail.len);
}
diff --git a/src/network/adblock/NetworkFilter.zig b/src/network/adblock/NetworkFilter.zig
index 0cae510b6..e38053606 100644
--- a/src/network/adblock/NetworkFilter.zig
+++ b/src/network/adblock/NetworkFilter.zig
@@ -18,6 +18,7 @@
const std = @import("std");
const domain = @import("domain.zig");
+const Regex = @import("Regex.zig");
const NetworkFilter = @This();
@@ -38,6 +39,9 @@ exception: bool = false,
important: bool = false,
badfilter: bool = false,
match_case: bool = false,
+/// The compiled `.regex` pattern, set by the blocker once it has read the
+/// literal; the parser never runs one.
+regex: ?Regex = null,
first_party: bool = true,
third_party: bool = true,
hostname_anchor: bool = false,
diff --git a/src/network/adblock/Regex.zig b/src/network/adblock/Regex.zig
new file mode 100644
index 000000000..66dee0b73
--- /dev/null
+++ b/src/network/adblock/Regex.zig
@@ -0,0 +1,215 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// 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 <https://www.gnu.org/licenses/>.
+
+//! A compiled `/regex/` filter body. Filter lists write them in JavaScript
+//! `RegExp` syntax and uBO runs them with `new RegExp(src, 'i')` against the
+//! raw request URL (no flag under `$match-case`); PCRE2 reads that syntax
+//! as-is, escapes like `\/` included.
+//!
+//! A compiled pattern and its `Context` are never modified after `compile`,
+//! so one `Regex` can be shared by every HTTP client thread; the per-call
+//! match data is what PCRE2 requires to be private.
+
+const std = @import("std");
+const lp = @import("lightpanda");
+const pcre2 = @import("pcre2");
+
+const Allocator = std.mem.Allocator;
+
+const log = lp.log;
+
+const Regex = @This();
+
+code: *pcre2.pcre2_code_8,
+context: *const Context,
+
+pub const Error = error{ InvalidRegex, OutOfMemory };
+
+/// What every `Regex` compiled through it shares: the allocator PCRE2 draws
+/// from, and the compile and match settings. Outlives the regexes.
+pub const Context = struct {
+ allocator: Allocator,
+ general: *pcre2.pcre2_general_context_8,
+ compile_context: *pcre2.pcre2_compile_context_8,
+ match_context: *pcre2.pcre2_match_context_8,
+
+ // A pattern from a list that backtracks this much on one URL is broken,
+ // not slow; giving up costs a false negative on that request, nothing
+ // more.
+ const MATCH_LIMIT = 100_000;
+ const DEPTH_LIMIT = 10_000;
+
+ pub fn init(allocator: Allocator) Allocator.Error!*Context {
+ const self = try allocator.create(Context);
+ errdefer allocator.destroy(self);
+ self.allocator = allocator;
+
+ // PCRE2 hands `self` back to the callbacks, so the context has to be
+ // at its final address before anything is allocated through it.
+ const general = pcre2.pcre2_general_context_create_8(cMalloc, cFree, self) orelse return error.OutOfMemory;
+ errdefer pcre2.pcre2_general_context_free_8(general);
+
+ const compile_context = pcre2.pcre2_compile_context_create_8(general) orelse return error.OutOfMemory;
+ errdefer pcre2.pcre2_compile_context_free_8(compile_context);
+ // JavaScript without the `u` flag reads an unknown escape as the
+ // literal character, and that is the mode uBO compiles filters in.
+ _ = pcre2.pcre2_set_compile_extra_options_8(compile_context, pcre2.PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL);
+
+ const match_context = pcre2.pcre2_match_context_create_8(general) orelse return error.OutOfMemory;
+ _ = pcre2.pcre2_set_match_limit_8(match_context, MATCH_LIMIT);
+ _ = pcre2.pcre2_set_depth_limit_8(match_context, DEPTH_LIMIT);
+
+ self.general = general;
+ self.compile_context = compile_context;
+ self.match_context = match_context;
+ return self;
+ }
+
+ pub fn deinit(self: *Context) void {
+ pcre2.pcre2_match_context_free_8(self.match_context);
+ pcre2.pcre2_compile_context_free_8(self.compile_context);
+ pcre2.pcre2_general_context_free_8(self.general);
+ self.allocator.destroy(self);
+ }
+
+ // PCRE2 frees without a size, so every block carries its own in a
+ // header that keeps the payload at malloc's alignment.
+ const HEADER = 16;
+ const alignment: std.mem.Alignment = .fromByteUnits(HEADER);
+
+ fn cMalloc(size: usize, data: ?*anyopaque) callconv(.c) ?*anyopaque {
+ const self: *const Context = @ptrCast(@alignCast(data.?));
+ const total = std.math.add(usize, size, HEADER) catch return null;
+ const block = self.allocator.alignedAlloc(u8, alignment, total) catch return null;
+ std.mem.writeInt(usize, block[0..@sizeOf(usize)], total, .little);
+ return block.ptr + HEADER;
+ }
+
+ fn cFree(ptr: ?*anyopaque, data: ?*anyopaque) callconv(.c) void {
+ const payload = ptr orelse return;
+ const self: *const Context = @ptrCast(@alignCast(data.?));
+ const base: [*]align(HEADER) u8 = @ptrCast(@alignCast(@as([*]u8, @ptrCast(payload)) - HEADER));
+ const total = std.mem.readInt(usize, base[0..@sizeOf(usize)], .little);
+ self.allocator.free(base[0..total]);
+ }
+};
+
+pub fn compile(context: *const Context, pattern: []const u8, case_insensitive: bool) Error!Regex {
+ const options: u32 = if (case_insensitive) pcre2.PCRE2_CASELESS else 0;
+ var err_code: c_int = 0;
+ var err_offset: usize = 0;
+ const code = pcre2.pcre2_compile_8(
+ pattern.ptr,
+ pattern.len,
+ options,
+ &err_code,
+ &err_offset,
+ context.compile_context,
+ ) orelse {
+ // Compile errors are positive codes; 21 is the one for a failed
+ // allocation, and that is ours, not the pattern's.
+ if (err_code == 21) return error.OutOfMemory;
+ var buf: [256]u8 = undefined;
+ const len = pcre2.pcre2_get_error_message_8(err_code, &buf, buf.len);
+ const message: []const u8 = if (len < 0) "unknown error" else buf[0..@intCast(len)];
+ log.debug(.app, "adblock regex rejected", .{
+ .pattern = pattern,
+ .err = message,
+ .offset = err_offset,
+ });
+ return error.InvalidRegex;
+ };
+ return .{ .code = code, .context = context };
+}
+
+pub fn deinit(self: Regex) void {
+ pcre2.pcre2_code_free_8(self.code);
+}
+
+/// Whether the pattern matches anywhere in `text`, as `RegExp.test` would
+/// answer. A match that hits the backtracking limits counts as no match.
+pub fn matches(self: Regex, text: []const u8) bool {
+ // One pair is the whole-match span, all a test needs; capture groups in
+ // the pattern are simply not recorded.
+ const match_data = pcre2.pcre2_match_data_create_8(1, self.context.general) orelse return false;
+ defer pcre2.pcre2_match_data_free_8(match_data);
+
+ const rc = pcre2.pcre2_match_8(self.code, text.ptr, text.len, 0, 0, match_data, self.context.match_context);
+ return rc >= 0;
+}
+
+const testing = @import("../../testing.zig");
+
+test "adblock.Regex: JavaScript escapes and unanchored search" {
+ const context: *Context = try .init(testing.allocator);
+ defer context.deinit();
+
+ const regex = try Regex.compile(context, "^https?:\\/\\/[0-9a-z]{5,}\\.com\\/.*", true);
+ defer regex.deinit();
+
+ try testing.expect(regex.matches("https://abcde.com/x"));
+ try testing.expect(regex.matches("HTTPS://ABCDE.COM/X"));
+ try testing.expect(!regex.matches("https://abcd.com/x"));
+ try testing.expect(!regex.matches("https://abcde.org/x"));
+
+ const invoke = try Regex.compile(context, "\\/[0-9a-f]{32}\\/invoke\\.js", true);
+ defer invoke.deinit();
+ try testing.expect(invoke.matches("https://host.com/0123456789abcdef0123456789abcdef/invoke.js"));
+ try testing.expect(!invoke.matches("https://host.com/0123456789abcdef0123456789abcde/invoke.js"));
+
+ const dash = try Regex.compile(context, "[a-z\\-]+\\?s=", true);
+ defer dash.deinit();
+ try testing.expect(dash.matches("https://x.com/a-b?s=1"));
+ try testing.expect(!dash.matches("https://x.com/?s=1"));
+}
+
+test "adblock.Regex: case sensitivity follows the flag" {
+ const context: *Context = try .init(testing.allocator);
+ defer context.deinit();
+
+ const caseless = try Regex.compile(context, "\\/Ads\\/", true);
+ defer caseless.deinit();
+ try testing.expect(caseless.matches("https://x.com/ads/1.js"));
+ try testing.expect(caseless.matches("https://x.com/ADS/1.js"));
+
+ const exact = try Regex.compile(context, "\\/[a-z0-9]{12}\\/[a-zA-Z0-9]{20,}$", false);
+ defer exact.deinit();
+ try testing.expect(exact.matches("https://x.com/abcdef123456/aBcDeFgHiJkLmNoPqRsTuV"));
+ try testing.expect(!exact.matches("https://x.com/ABCDEF123456/aBcDeFgHiJkLmNoPqRsTuV"));
+}
+
+test "adblock.Regex: invalid patterns are errors, runaway ones no match" {
+ const context: *Context = try .init(testing.allocator);
+ defer context.deinit();
+
+ try testing.expectError(error.InvalidRegex, Regex.compile(context, "(", true));
+ try testing.expectError(error.InvalidRegex, Regex.compile(context, "a{2,1}", true));
+
+ // An unknown alphanumeric escape is the literal, as in JavaScript.
+ const literal = try Regex.compile(context, "\\q", true);
+ defer literal.deinit();
+ try testing.expect(literal.matches("https://x.com/q"));
+
+ // Exponential backtracking stops at the match limit instead of stalling
+ // the request.
+ const runaway = try Regex.compile(context, "^(a+)+$", true);
+ defer runaway.deinit();
+ const subject = "a" ** 64 ++ "b";
+ try testing.expect(!runaway.matches(subject));
+ try testing.expect(runaway.matches("a" ** 64));
+}
diff --git a/src/network/adblock/pattern.zig b/src/network/adblock/pattern.zig
index 6bd2d085d..3406c5572 100644
--- a/src/network/adblock/pattern.zig
+++ b/src/network/adblock/pattern.zig
@@ -86,8 +86,8 @@ pub fn matches(filter: *const NetworkFilter, url: Url) bool {
switch (filter.kind) {
// Option-only filters ("$script,domain=x") match any URL.
.any => return true,
- // Never indexed: there is no regex engine to run them with.
- .regex => return false,
+ // The engine runs these against the raw URL before it gets here.
+ .regex => unreachable,
.hostname, .plain, .wildcard => {},
}