Skip to content

feat(security): enforce SSRF policy inside Chromium with pinned-DNS egress guard - #91

Merged
gildesmarais merged 13 commits into
mainfrom
brain/t2-chrome-egress
Nov 30, 2026
Merged

gildesmarais merged 13 commits into
mainfrom
brain/t2-chrome-egress

Conversation

@gildesmarais

@gildesmarais gildesmarais commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

What changed

  • Every scrape now sends traffic through its own proxy listener on 127.0.0.1, added in app/security/egress_proxy.py.
    • The listener resolves each destination once and refuses blocked address classes.
    • It then connects to the exact IP it checked.
  • Chromium is started with these flags:
    • --proxy-server pointing at that listener
    • --proxy-bypass-list=<-loopback>
    • --disable-quic
  • Each Chromium profile is seeded with the WebRTC preference disable_non_proxied_udp before launch. The matching --force-webrtc-ip-handling-policy flag is ignored by the bundled Chromium: pages still gathered srflx candidates, sending UDP to page-chosen STUN addresses (private ones included) around the proxy and exposing the host IP. With the preference, WebRTC gathers no candidates.
  • Navigations, redirects, sub-resources, iframes, XHR/fetch and websockets now all pass the SSRF policy.
  • Warm spare drivers get the same guard, because they are built by the same factory.
  • The request tier (tls-client) goes through its own listener as well, so each redirect hop is checked.
  • UrlGuard.resolve_host is now the only place the rule lives. validate() and the listener both call it.
  • is_blocked_ip changes:
    • IPv4-mapped addresses are unwrapped explicitly.
    • 100.64.0.0/10 (CGNAT), 192.88.99.0/24, 2001:20::/28 and fec0::/10 are now blocked. The stdlib flags miss them; the gem already denies them.
  • metadata.google.internal is refused by name, like localhost.
  • A blocked top-level navigation or redirect returns 403 with error_category=validation (ScrapeService).
  • Blocked sub-requests and iframes are only logged as egress_blocked, and the page still succeeds.
    • egress_blocked carries the scrape's request_id and host. It is logged once per host per attempt, and repeats are only counted.
    • Each listener logs one egress_summary on close when anything was refused or failed. It counts blocked connections, DNS timeouts and failures, connect failures and upstream proxy refusals, and records the upstream's status (for example 407 or 402). It logs at WARNING when the upstream refused or DNS timed out.
    • Main-frame hops are found by CDP frame_id, compared with the top-level frame (XhrCollector).
  • An upstream proxy must be http://. It becomes the listener's next hop. Each destination is still resolved and checked locally first. The upstream then receives the hostname (CONNECT host:port), because it resolves on its own network, which cannot reach ours. TLS-inspecting and geo-routed upstreams, such as residential gateways, need the name.
  • The block ledger:
    • is scoped to one navigation attempt
    • keeps one entry per host
    • is capped at 1024 hosts, and entries are never evicted
  • DNS resolution runs on a dedicated pool of 32 workers with a 5 s timeout. The request head must arrive within 10 s.
    • Each listener holds at most 4 lookups at once. A slot is freed when the lookup thread returns, not when the caller times out, so a page that floods slow DNS names stalls only its own scrape.
    • A checked answer is reused for the listener's lifetime (one scrape), and concurrent connections to the same host share one lookup. Refusals are not kept, so each attempt still records its blocks.

Why

Before this change, only the top-level URL was checked, once, before the scrape. After that, Chromium resolved and fetched whatever the page asked for. That included redirects and sub-requests to private, link-local and metadata addresses. A hostname could also resolve to a public address at check time and a private one at connect time.

With a fixed proxy, Chromium resolves no names itself. The guard resolves, checks, and connects by IP, so rebinding has nothing to race.

Filtering egress at the host level was rejected: it needs host firewall rules that are not kept in git.

Risk

  • Hot path: each connection now costs one extra getaddrinfo plus a loopback hop. Request-mode bench p50 was 62.5 ms before and 50.6–74.4 ms after. Network noise is larger than any difference. Browser-mode p50 (8 runs, Docker) before → after: Wikipedia article 11.7 s → 11.7 s, Guardian front page 12.1 s → 9.6 s, which is within noise.
  • Upstream proxies:
    • https:// and socks upstream proxies now get a 400. Only http:// is supported.
    • Destinations must resolve locally even when a proxy is set.
    • With an upstream, the address the upstream resolves can differ from the one we checked. That is accepted, because it resolves from the provider's network, not ours.
  • Residuals:
    • A getaddrinfo call that hangs keeps its DNS worker until the resolver gives up. It counts against that listener's 4 slots, so other scrapes keep resolving.
    • Established tunnels have no idle timeout. The listener closes when the request ends.
    • ScrapeService picks 403 from error_category == validation, not from a typed signal. Today only the egress block emits that category.
  • Pre-existing, out of scope: XhrCollector._on_response still matches the main document by request id == loader id. An iframe document response can therefore overwrite the status and headers it captured.

Review map

  1. tests/security/test_egress_proxy.py: rebinding refused, IP pinning, blocked classes over CONNECT and plain HTTP, upstream hostname forwarding and auth, timeouts, ledger dedupe.
  2. tests/engine/test_egress_tiers.py: request-tier redirect and 403-response paths, browser main-frame block, sub-request and iframe not escalated, per-attempt reset.
  3. tests/api/test_http_contract.py: blocked navigation returns 403.
  4. app/security/egress_proxy.py: the listener, the event loop, the resolver pool.
  5. app/security/url_guard.py: resolve_host as the single owner of the policy.
  6. app/engine/browser_tier.py, app/engine/request_tier.py, app/engine/warm_pool.py: how each tier is wired to the listener.
  7. app/infra/xhr_collector.py: capture of the main-frame navigation chain.
  8. scripts/smoke.sh: an in-Chromium egress probe against real CDP.

Validation

  • make check: 251 tests, pyright 0 errors, openapi-verify ok.
  • make smoke: both contract-prewarm-off and prewarm-on-warm-handoff pass, including the new egress probe. It checks that an img to 169.254.169.254 and an iframe to 10.0.0.1 are blocked while the page renders, and that a main-frame navigation to 127.0.0.1 is refused and recorded. It also checks that WebRTC gathers no ICE candidates, given STUN servers at 10.0.0.1 and a public host. Without the seeded preference, the same check returned a host and a srflx candidate. A second check hands Chromium a remote candidate on a local UDP port and requires 0 packets to arrive. Without the preference, 129 arrived.
  • A separate reviewer re-checked the branch; the blocking findings are fixed.

Follow-ups (merge after this)

🤖 Generated with Claude Code

gildesmarais and others added 7 commits November 30, 2026 14:30
…ress proxy

Every Chromium connection (navigation, redirects, sub-requests, iframes,
websockets) and every request-tier hop now goes through a per-driver /
per-call loopback listener. It resolves each destination once through
UrlGuard.resolve_host, refuses blocked addresses, and connects to the
exact IP it checked, so DNS rebinding cannot swap the address. The user
proxy becomes the listener's pinned, authenticated upstream.

A refused main-frame navigation maps to the SSRF validation error;
refused sub-requests only log egress_blocked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The egress proxy chains to plain-HTTP upstreams only; validate_proxy now
returns 400 for other schemes instead of failing at driver/listener
construction. Request-tier cleanup no longer leaks the listener if
Request() fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… per attempt

The domain maps the engine's only validation error (egress guard refusing
the page's own navigation) to the 403 SSRF contract. The listener's block
ledger resets per navigation attempt so an earlier refused sub-request
cannot reclassify a later failure. Proxy field docs describe the http://
upstream chaining; openapi.yaml regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Request tier: a proxy 403 response for a refused plain-http redirect hop
  maps to the SSRF error instead of challenge_block/escalation.
- Upstream proxy mode sends the checked IP (CONNECT ip:port, IP authority
  with original Host), so the upstream cannot re-resolve.
- Block 100.64.0.0/10 (CGNAT shared space).
- DNS runs on a bounded egress-dns executor with a resolve timeout; client
  and upstream heads have a read timeout; the block ledger keeps the last 64.
- Browser SSRF mapping only counts hosts of the main-frame navigation chain
  (CDP RequestWillBeSent, loader-id match), never unrelated sub-requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ledger

- XhrCollector reads the top-level frame id (Page.getFrameTree) at install
  and records navigation hosts only for that frame; iframe Documents also
  have request id == loader id and no longer 403 the whole scrape.
- The block ledger keeps one entry per host (first reason) up to 1024 hosts
  and never evicts, so repeated or flooding sub-request blocks cannot push
  out a main-frame hit.
- Smoke probe asserts the iframe/sub-request hosts are not main-frame hosts
  in real Chromium.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gildesmarais
gildesmarais marked this pull request as ready for review November 30, 2026 13:52
gildesmarais and others added 2 commits November 30, 2026 16:24
Python's ipaddress flags miss 192.88.99.0/24, 2001:20::/28 and fec0::/10;
the gem's NetworkGuard already denies them. metadata.google.internal is
now refused by name before resolution.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…crape

A hung getaddrinfo keeps its thread after the timeout, so one page could
fill the shared DNS pool and stall every concurrent scrape. Each listener
now holds at most 4 lookups, released only when the thread returns, on a
32-worker pool. A checked answer is reused for the listener's lifetime and
concurrent connections share one lookup; refusals are not kept, so each
attempt still records its blocks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gildesmarais and others added 4 commits November 30, 2026 16:57
The destination still passes the local resolve-and-check, but the
upstream now gets host:port instead of the pinned IP. It resolves on its
own network, which cannot reach ours, and TLS-inspecting or geo-routed
upstreams (residential gateways) need the name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
--force-webrtc-ip-handling-policy is ignored by the bundled Chromium:
pages still gathered srflx candidates, sending UDP to page-chosen STUN
addresses (including private ones) around the egress proxy and exposing
the host IP next to an upstream proxy. Each profile is now seeded with
webrtc.ip_handling_policy=disable_non_proxied_udp before launch, so
WebRTC gathers no candidates. Smoke asserts it in Chromium.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…idate

ICE connectivity checks to a candidate passed via addIceCandidate are a
second UDP path around the egress proxy. Without the seeded preference
Chromium sent 129 packets to such an address; the probe now requires 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
egress_blocked now carries request_id/host (the listener logs from its own
threads, which never saw the request context; warm spares re-attach on
adoption) and is logged once per host per attempt instead of per
connection. Each listener emits one egress_summary on close when anything
was refused or failed: blocked connections, DNS timeouts/failures,
connect failures and upstream proxy refusals with their status (WARNING
when the upstream refused or DNS timed out).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gildesmarais
gildesmarais added this pull request to stack #93 November 30, 2026 17:14
@gildesmarais
gildesmarais merged commit e04fa14 into main Nov 30, 2026
13 checks passed
@gildesmarais
gildesmarais deleted the brain/t2-chrome-egress branch November 30, 2026 17:15
gildesmarais added a commit to html2rss/html2rss that referenced this pull request Nov 30, 2026
…anchors (#723)

## What changed

- `request.botasaurus.proxy` must now start with `http://`, and
credentials are still allowed. `https://` and `socks` URLs fail config
validation with `is in invalid format`.
  - The rule lives in `BotasaurusContract::PROXY_FORMAT`.
  - `Config::Validator` applies it.
- `Config::Schema::Builder` now maps Ruby's whole-string anchors
`\A`/`\z` to ECMA-262 `^`/`$` in every exported `pattern`. Example and
default values are left untouched.
- JSON Schema validators read `pattern` as ECMA-262, which has neither
anchor. Until now `channel.language` (`\A…\z`) rejected every valid
value in schema-aware editors.
  - Ruby validators keep the strict anchors.
- `schema/html2rss-config.schema.json` is regenerated:
`channel.language` and `request.botasaurus.proxy` now export `^…$` and
`^http:\/\/`.
- `spec/fixtures/botasaurus/openapi.yaml` is vendored from the scrape
API with the new `proxy` description.

## Why

html2rss/botasaurus-scrape-api#91 sends all scrape traffic through an
egress guard. The request `proxy` becomes that guard's upstream, and the
guard talks only plain HTTP to it. Without this change, an `https://` or
`socks` proxy in a feed config would pass validation here and then fail
at scrape time with a 400 from the API. Rejecting it at config load
points the failure at the config.

## Risk

- Existing configs with a `socks` or `https://` proxy stop validating.
With #91 deployed they could not scrape anyway.
- Merge after html2rss/botasaurus-scrape-api#91. The sibling-OpenAPI
sync spec expects that PR's `openapi.yaml`.

Review map: small. Start at
`spec/lib/html2rss/config/validator_spec.rb`, then
`spec/lib/html2rss/config/schema_spec.rb`.

## Validation

- `make ready`: 8717 examples and 0 failures, RuboCop clean, the schema
check passes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
gildesmarais added a commit to html2rss/html2rss that referenced this pull request Nov 30, 2026
## What changed

- `request.botasaurus.timezone`, an IANA id such as `Europe/Berlin`, is
a new accepted option. It is forwarded on `POST /scrape` through
`BotasaurusContract::REQUEST_OPTION_KEYS`. `Config::Validator` accepts
it as a non-empty string.
- `spec/fixtures/botasaurus/openapi.yaml` is vendored from
html2rss/botasaurus-scrape-api#92. `schema/html2rss-config.schema.json`
is regenerated.
- The six boolean Botasaurus options are declared in one loop, so the
schema block stays within `Metrics/BlockLength`. The exported schema is
unchanged by this.

## Why

The scrape API now lets the caller set the timezone the browser reports
to pages, for example to match a residential proxy's exit country (#92).
Without the key here, feed configs could not set it.

## Risk

- Low. The scrape API owns the IANA check and answers unknown ids with
`422`, so the gem does not duplicate the tz database.
- Merge order:
  1. html2rss/botasaurus-scrape-api#91, then #92
  2. #723
  3. this PR, retargeted to `master`

Review map: small. Start at
`spec/lib/html2rss/request_service/botasaurus_contract_spec.rb`.

## Validation

- `make ready`: 8717 examples and 0 failures, RuboCop clean, the schema
check passes. The sibling-OpenAPI sync spec ran against #92's
`openapi.yaml`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant