fix(website): validate URLs and paths taken from request input in image, asset and CSV redirect loaders

Merged
GU
13d ago
+667-49

What is this Contribution About?

Three website loaders, all reachable through /live/invoke, took a URL or path from request input and fetched or read it without validation.

website/loaders/image.ts

src went straight to fetch(). With the default pass-through engine in the Deno runtime, that meant:

  • Local file read: fetch() in Deno supports file://. A src such as file:///etc/... returned the file's contents with a 200 and an immutable public cache header.
  • SSRF: loopback, private, link-local and cloud metadata addresses could be reached, either directly or through a redirect from a public URL.
  • Header forwarding: the remote engine forwarded every request header, including cookie and authorization, to whichever host src named.
  • Cache before checks: the loader read the Cache API before any validation, so a response cached for a bad src would keep being served after disableProxy or whilelistURLs was set.

website/loaders/asset.ts

It already accepted only http(s), but fetched any host, internal ones included, and followed redirects to them. Non-2xx responses were rethrown with the upstream body in the error message.

website/loaders/redirectsFromCsv.ts

  • Local file read: a local from was joined onto the working directory without containment, so ../ escaped the site. Every line of the target file that had a , or ; came back as a route, which exposed its first two fields.
  • SSRF: a from starting with http was fetched without validation.
  • Unbounded cache: each distinct from was cached forever.

This loader does not consult disableProxy, so that setting was no mitigation here.

Changes

  • New website/utils/publicUrl.ts:
    • parsePublicUrl accepts only http:/https: and rejects localhost, single-label hosts, .local/.internal/.svc/.cluster.local, and private, loopback, link-local and reserved IPv4/IPv6 literals. IPv4-mapped IPv6 and the alternate IPv4 forms are covered because the URL parser normalizes them.
    • fetchPublicUrl resolves the host's A/AAAA records before each request and rejects internal addresses, so a public name pointing at an internal IP (such as 127.0.0.1.nip.io) is refused. It follows redirects manually (up to 5), validates every hop, and returns 502 for a malformed Location. It takes an optional fetcher, so asset.ts keeps Deco's cached fetch with the STALE policy and its retry.
    • readTextWithLimit reads a body up to a byte cap.
  • image.ts:
    • The remote (pass-through and deco) and wasm engines fetch through fetchPublicUrl.
    • Only accept, user-agent and referer are forwarded to the origin. referer is kept for origins with hotlink protection.
    • The wasm engine keeps the upstream 4xx/5xx status, without Deno.inspect(response).
    • src is validated before the cache lookup.
    • Error responses no longer include Deno.inspect(error).
  • asset.ts: validates src with parsePublicUrl and fetches through fetchPublicUrl. A rejected URL or host returns a 400/403 response. Upstream errors are rethrown with the status only, not the body.
  • redirectsFromCsv.ts:
    • A local path must stay inside the working directory once normalized and end in .csv.
    • A remote from goes through fetchPublicUrl and is read up to 20 MB. A non-2xx response is treated as a failure.
    • Failed reads are no longer cached, so a transient error does not drop the redirects until restart. The cache holds at most 50 sources.
    • The .csv rule only applies to local paths; remote URLs such as *.csv.css uploads keep working. Local from values found in existing site configurations (redirects.csv, /static/redirects.csv, static/redirects-*.csv, utils/redirects.csv) are all .csv.

Sites that cannot upgrade right away can turn on disableProxy, or set whilelistURLs, in the Website app. That covers image.ts and asset.ts, but not redirectsFromCsv.ts. Because of the cache ordering in image.ts, they also need to restart the runtime and purge their CDN cache for that route.

Not covered: DNS rebinding. The pre-fetch lookup and the connection resolve separately, so a host that changes its answer between them can still reach an internal address. Closing that needs resolution pinned at connect time or egress filtering at the network layer; it is left for a follow-up.

Verification

  • Local reproductions against main, with synthetic files:
    • image.ts returned a file's contents with a 200.
    • redirectsFromCsv.ts with a ../ path returned the file's lines as routes.
  • With this change:
    • image.ts: file:// → 400; http://169.254.169.254/... → 403; a public PNG → 200.
    • asset.ts: a public woff2 → 200 font/woff2; a public icon → 200.
    • With real DNS: http://127.0.0.1.nip.io/... → 403 in both image.ts and asset.ts.
  • website/utils/publicUrl_test.ts (13 tests) covers:
    • rejected schemes and hosts, and accepted public hosts;
    • hosts that resolve to internal addresses (IPv4, IPv6, IPv4-mapped, mixed records) and unresolvable hosts;
    • a public → public → internal redirect chain, the redirect limit and a malformed Location;
    • the body size cap;
    • header filtering;
    • image.ts refusing file:// without reading the file;
    • asset.ts refusing local and internal sources without fetching;
    • redirectsFromCsv.ts reading a .csv inside the site but returning nothing for a ../ path, an absolute path outside the site, a non-CSV file and an internal URL;
    • redirectsFromCsv.ts retrying a remote file after a failed read.
  • With Deno 2.4.5 (the CI version): deno fmt --check, deno lint, deno check website/mod.ts and the tests pass. The full deno task check over every mod.ts was not run locally.

Issue Link

  • Issue: none (reported privately)

Loom Video

N/A (no UI change)

Demonstration Link

N/A

🤖 Generated with Claude Code