-
Notifications
You must be signed in to change notification settings - Fork 146
fix: validate each redirect target in the image loader #1338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,11 @@ export async function handleImageRequest( | |
| status: 504, | ||
| }); | ||
| } | ||
| if (fetchImageResult.error === "invalid_redirect") { | ||
| return new Response('"url" parameter is valid but upstream response is invalid', { | ||
| status: 400, | ||
| }); | ||
| } | ||
|
Comment on lines
+65
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Missing changeset for a user-visible behaviour change The image loader now rejects redirects it previously followed ( Repository rule requiring a changeset for behavioural changes
Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @arpitjain099 could you please address this issue by running
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added: |
||
| if (fetchImageResult.error === "too_many_redirects") { | ||
| return new Response('"url" parameter is valid but upstream response is invalid', { | ||
| status: 508, | ||
|
|
@@ -357,6 +362,18 @@ async function fetchWithRedirects( | |
| } else { | ||
| redirectTarget = locationHeader; | ||
| } | ||
| // The allow list is applied to the original URL only, so each hop is | ||
| // re-validated here. Scheme and literal address are all that can be checked: | ||
| // the Workers runtime has no DNS resolution API. | ||
| let parsedTarget: URL; | ||
| try { | ||
| parsedTarget = new URL(redirectTarget); | ||
| } catch { | ||
| return { ok: false, error: "invalid_redirect" } satisfies FetchWithRedirectsErrorResult; | ||
| } | ||
| if (!["http:", "https:"].includes(parsedTarget.protocol) || isNonRoutableHost(parsedTarget.hostname)) { | ||
| return { ok: false, error: "invalid_redirect" } satisfies FetchWithRedirectsErrorResult; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Image redirects that use a relative path are now rejected with an error page A redirect whose target is written as a path relative to the current image URL is parsed without a base ( Mechanism: only absolute and root-relative Location values are resolved
(Refers to lines 354-376) Was this helpful? React with 👍 or 👎 to provide feedback.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, and your suggested fix is the correct one. Taken.
Worth being precise about what changed and when, since this is not purely something my patch broke: before this PR the unresolved value went straight to The whole block now resolves once against the URL of the hop that sent it: let parsedTarget: URL;
try {
parsedTarget = new URL(locationHeader, url);
} catch { ... }and the recursion takes No unit test for this one: |
||
| const result = await fetchWithRedirects(redirectTarget, timeoutMS, maxRedirectCount - 1); | ||
| return result; | ||
| } | ||
|
|
@@ -380,7 +397,7 @@ type FetchWithRedirectsErrorResult = { | |
| error: FetchImageError; | ||
| }; | ||
|
|
||
| type FetchImageError = "timed_out" | "too_many_redirects"; | ||
| type FetchImageError = "timed_out" | "too_many_redirects" | "invalid_redirect"; | ||
|
|
||
| const redirectResponseStatuses = [301, 302, 303, 307, 308]; | ||
|
|
||
|
|
@@ -703,6 +720,51 @@ type ParseRelativeURLResult = { | |
| search: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Checks whether a hostname is a literal address that should never be reached by | ||
| * following a redirect. | ||
| * | ||
| * Only literal addresses are considered. The Workers runtime has no DNS resolution | ||
| * API, so a hostname cannot be resolved to find out where it points, and this is a | ||
| * coarse filter rather than a substitute for the `remotePatterns` allow list that is | ||
| * applied to the original URL. | ||
| */ | ||
| export function isNonRoutableHost(hostname: string): boolean { | ||
| const host = hostname.toLowerCase(); | ||
|
|
||
| if (host === "localhost" || host.endsWith(".localhost")) { | ||
| return true; | ||
| } | ||
|
|
||
| // IPv6 arrives from URL.hostname without its surrounding brackets. | ||
| if (host.includes(":")) { | ||
| const v6 = host.startsWith("[") ? host.slice(1, -1) : host; | ||
| if (v6 === "::" || v6 === "::1") { | ||
| return true; | ||
| } | ||
| // fc00::/7 unique local, fe80::/10 link local. | ||
| return /^f[cd][0-9a-f]{2}:/.test(v6) || /^fe[89ab][0-9a-f]:/.test(v6); | ||
| } | ||
|
|
||
| const octets = host.split("."); | ||
| if (octets.length !== 4) { | ||
| return false; | ||
| } | ||
| const parsed = octets.map((octet) => (/^\d{1,3}$/.test(octet) ? Number(octet) : NaN)); | ||
| if (parsed.some((octet) => Number.isNaN(octet) || octet > 255)) { | ||
| return false; | ||
| } | ||
| const [a, b] = parsed as [number, number, number, number]; | ||
| return ( | ||
| a === 0 || // 0.0.0.0/8 | ||
| a === 127 || // loopback | ||
| a === 10 || // RFC1918 | ||
| (a === 172 && b >= 16 && b <= 31) || // RFC1918 | ||
| (a === 192 && b === 168) || // RFC1918 | ||
| (a === 169 && b === 254) // link local, includes the metadata address | ||
| ); | ||
| } | ||
|
Comment on lines
+728
to
+839
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟨 Redirect address filter misses IPv4-mapped/embedded IPv6 loopback and private addresses The new per-hop guard rejects literal loopback/private hosts, but only in plain IPv4 dotted form or in the Was this helpful? React with 👍 or 👎 to provide feedback.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed and fixed. This one was a real hole in the guard I added.
Rather than add more string patterns, the address is now expanded to its eight 16 bit groups and matched numerically. An IPv4 mapped address ( On the other ranges: added carrier grade NAT ( 11 new cases in |
||
|
|
||
| export function matchLocalPattern(pattern: LocalPattern, url: { pathname: string; search: string }): boolean { | ||
| if (pattern.search !== undefined && pattern.search !== url.search) { | ||
| return false; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could this test be folded into the 2 previous ones?