fix(website): validate URLs and paths taken from request input in image, asset and CSV redirect loaders
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 supportsfile://. Asrcsuch asfile:///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
cookieandauthorization, to whichever hostsrcnamed. - Cache before checks: the loader read the Cache API before any validation, so a response cached for a bad
srcwould keep being served afterdisableProxyorwhilelistURLswas 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
fromwas 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
fromstarting withhttpwas fetched without validation. - Unbounded cache: each distinct
fromwas cached forever.
This loader does not consult disableProxy, so that setting was no mitigation here.
Changes
- New
website/utils/publicUrl.ts:parsePublicUrlaccepts onlyhttp:/https:and rejectslocalhost, 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.fetchPublicUrlresolves the host's A/AAAA records before each request and rejects internal addresses, so a public name pointing at an internal IP (such as127.0.0.1.nip.io) is refused. It follows redirects manually (up to 5), validates every hop, and returns 502 for a malformedLocation. It takes an optional fetcher, soasset.tskeeps Deco's cachedfetchwith theSTALEpolicy and its retry.readTextWithLimitreads a body up to a byte cap.
image.ts:- The
remote(pass-through and deco) andwasmengines fetch throughfetchPublicUrl. - Only
accept,user-agentandrefererare forwarded to the origin.refereris kept for origins with hotlink protection. - The wasm engine keeps the upstream 4xx/5xx status, without
Deno.inspect(response). srcis validated before the cache lookup.- Error responses no longer include
Deno.inspect(error).
- The
asset.ts: validatessrcwithparsePublicUrland fetches throughfetchPublicUrl. 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
fromgoes throughfetchPublicUrland 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
.csvrule only applies to local paths; remote URLs such as*.csv.cssuploads keep working. Localfromvalues found in existing site configurations (redirects.csv,/static/redirects.csv,static/redirects-*.csv,utils/redirects.csv) are all.csv.
- A local path must stay inside the working directory once normalized and end in
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.tsreturned a file's contents with a 200.redirectsFromCsv.tswith 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 → 200font/woff2; a public icon → 200.- With real DNS:
http://127.0.0.1.nip.io/...→ 403 in bothimage.tsandasset.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.tsrefusingfile://without reading the file;asset.tsrefusing local and internal sources without fetching;redirectsFromCsv.tsreading a.csvinside the site but returning nothing for a../path, an absolute path outside the site, a non-CSV file and an internal URL;redirectsFromCsv.tsretrying a remote file after a failed read.
- With Deno 2.4.5 (the CI version):
deno fmt --check,deno lint,deno check website/mod.tsand the tests pass. The fulldeno task checkover everymod.tswas not run locally.
Issue Link
- Issue: none (reported privately)
Loom Video
N/A (no UI change)
Demonstration Link
N/A
🤖 Generated with Claude Code