feat(cdp): add Network.setBlockedURLs and --block-urls request blocking#3004
Closed
mvanhorn wants to merge 2 commits into
Closed
feat(cdp): add Network.setBlockedURLs and --block-urls request blocking#3004mvanhorn wants to merge 2 commits into
mvanhorn wants to merge 2 commits into
Conversation
Reimplement request blocking on the non-deprecated urlPatterns shape: each pattern carries an explicit block/allow flag (first match wins), UrlBlocklist owns the compiled patterns plus their block flags, and the legacy setBlockedUrls path stays for back-compat. Tests updated.
Contributor
Author
|
Reworked onto urlPatterns in b573eb1: each pattern carries its block/allow flag (first match wins), the blocklist owns the compiled patterns plus flags, and the legacy urls path stays for back-compat. The three standalone UrlBlocklist tests pass; the full Zig 0.15.2 build runs in CI since my local toolchain is 0.16. |
Member
|
merged via #3040 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Add request-time URL blocking from two entry points that share one blocklist:
--block-urls <LIST>— comma-separated*-wildcard patterns, matched case-insensitively against the full URL, onserve/fetch/mcp, next to the existing--block-cidrs.Network.setBlockedURLs— the CDP method, matching Chrome's semantics.Why
Lightpanda's
NetworkCDP domain implemented no request-blocking method (src/cdp/domains/network.zigdispatchedenable,disable,setCacheDisabled, header/cookie methods, andgetResponseBody, nothing else), and thefetchCLI had no URL blocklist. So a Puppeteer/Playwright script that blocks ad/tracker requests against Chrome viapage.setRequestInterception/page.route(..., r => r.abort())— backed by CDPNetwork.setBlockedURLs— silently lost that behavior when pointed at Lightpanda. For a high-volume scraper those dropped requests are the point: they cut connections, latency, and bandwidth an agent never reads (Browserless markets the same request/ad blocking as a scraping speedup).Change
src/network/UrlBlocklist.zig, modeled onIpFilter.zig: compiles and owns a set of wildcard patterns once, and answersisBlocked(url)with an allocation-free two-pointer glob match.HttpClientholds an optionalUrlBlocklistbuilt fromconfig.blockedUrlPatterns()at init. Enforcement sits at the existing request-start gate that already handles robots and IP/CIDR blocking, and also on the redirect target, so a blocked URL never opens a socket and fails via the same async path. Internal transfers (e.g.robots.txt) are exempt, matching the robots / IP-filter exemption.setBlockedUrls()replaces the live list at runtime for the CDP path (owning its own copies).error.UrlBlocked, which maps throughNotification.RequestFail.blocked_reasontoNetwork.loadingFailedwithblockedReason: "inspector"— the reason Chrome reports forsetBlockedURLshits — so CDP clients see the expected event.Demo
Simulated demo (illustrative, not a live capture):
--block-urls "*doubleclick*,*analytics*,*://*/*.png"drops the matching ad/tracker/image requests whileindex.htmland the non-matchingpixel.gifstill load.Test
src/network/UrlBlocklist.zig: wildcard-matcher unit tests — case-insensitivity, backtracking, consecutive/trailing*, empty and*patterns, and pattern ownership (mutating the source afterinitleaves matches intact). These run standalone under the leak-detecting test allocator and pass.src/network/HttpClient.zig:setBlockedUrlsowns/replaces/clears, and the internal-transfer exemption.src/cdp/domains/network.zig: a blocked request emitsNetwork.loadingFailedwithblockedReason: "inspector", for both the initial request and a redirect target.zig fmt --checkclean;zig ast-checkclean on all touched files. The fullmake testneeds the V8 toolchain build.