-
Notifications
You must be signed in to change notification settings - Fork 1
fix: close redirect-based SSRF bypass in local provider fetches (sec) #536
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7ab40ab
fix(ai,llamacpp-server,stable-diffusion-server): re-validate redirect…
sroussey 7bacc65
fix(ai): make localOnlyFetch tolerant of non-spec Response mocks
sroussey 9e00aa2
test: fix localOnlyFetch redirect case using link-local metadata IP
sroussey 80b3c33
fix(ai): enforce loopback-only policy in localOnlyFetch + validate in…
sroussey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Steven Roussey <sroussey@gmail.com> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { isLoopbackHostname } from "./localUrl"; | ||
|
|
||
| const MAX_REDIRECTS = 5; | ||
|
|
||
| /** | ||
| * Standard HTTP redirect status codes per the Fetch/HTTP specs. A 3xx that is | ||
| * NOT one of these (e.g. `300 Multiple Choices`, `304 Not Modified`, | ||
| * `306 (unused)`) is NOT a redirect even if it carries a `Location` header — | ||
| * such responses are returned to the caller unchanged. | ||
| */ | ||
| const REDIRECT_STATUS_CODES = new Set([301, 302, 303, 307, 308]); | ||
|
|
||
| /** | ||
| * Returns true only when `res` is a spec-shaped standard redirect: a numeric | ||
| * `status` in {301,302,303,307,308} AND a `headers.get` method we can read | ||
| * `location` from. Real `fetch` responses satisfy both; minimal test doubles | ||
| * (e.g. `{ ok: true, json }`) have `undefined` status/headers and are | ||
| * therefore treated as terminal responses rather than misclassified as | ||
| * redirects (which would throw on `res.headers.get`). This narrows the | ||
| * redirect path without weakening validation of genuine redirects. | ||
| */ | ||
| function isRedirectResponse(res: Response): boolean { | ||
| const status = res.status; | ||
| if (typeof status !== "number" || !REDIRECT_STATUS_CODES.has(status)) return false; | ||
| return typeof res.headers?.get === "function"; | ||
| } | ||
|
|
||
| /** | ||
| * Validate a request target against the strict LOOPBACK-ONLY policy: it must | ||
| * be a valid http(s) URL, carry no credentials, and resolve to a loopback | ||
| * host (`localhost`, `127.0.0.0/8`, `::1`, or IPv4-mapped loopback). Throws a | ||
| * generic, label-prefixed Error otherwise. `context` distinguishes the | ||
| * initial URL from a redirect in the message. | ||
| */ | ||
| function assertLoopbackTarget(url: URL, label: string, context: "initial URL" | "redirect"): void { | ||
| if (url.protocol !== "http:" && url.protocol !== "https:") { | ||
| throw new Error(`${label}: refusing ${context} to non-HTTP(S) URL.`); | ||
| } | ||
| if (url.username || url.password) { | ||
| throw new Error(`${label}: refusing ${context} with credentials.`); | ||
| } | ||
| const host = url.hostname.replace(/^\[|\]$/g, ""); | ||
| if (!isLoopbackHostname(host)) { | ||
| throw new Error(`${label}: refusing ${context} to non-loopback host (${url.href}).`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * fetch() restricted to STRICTLY-LOOPBACK hosts on EVERY hop. These AI server | ||
| * clients only ever talk to a backend on the same host, so a broad "local" | ||
| * allow-list (which includes RFC 1918 and the `169.254.169.254` cloud-metadata | ||
| * address) is wider than needed and is itself an SSRF vector. This wrapper | ||
| * therefore enforces loopback-only on: | ||
| * - the initial `input` URL, validated defensively BEFORE any network call | ||
| * (a bad initial URL throws with zero fetches issued); and | ||
| * - every redirect `Location`, resolved against the current URL and | ||
| * re-validated, closing the redirect-based SSRF bypass left by | ||
| * base-URL-only validation. | ||
| * | ||
| * Only standard redirect codes (301/302/303/307/308) are followed; other 3xx | ||
| * responses are returned unchanged. The final Response is returned untouched | ||
| * so streaming consumers are unaffected. | ||
| */ | ||
| export async function localOnlyFetch( | ||
| input: string, | ||
| init?: RequestInit, | ||
| label = "localOnlyFetch", | ||
| ): Promise<Response> { | ||
| // Defensively validate the initial URL BEFORE issuing any request. A bad | ||
| // initial URL must throw before fetch is ever called (zero network calls). | ||
| let initialUrl: URL; | ||
| try { | ||
| initialUrl = new URL(input); | ||
| } catch { | ||
| throw new Error(`${label}: invalid initial URL.`); | ||
| } | ||
| assertLoopbackTarget(initialUrl, label, "initial URL"); | ||
|
|
||
| let current = initialUrl.href; | ||
| for (let hop = 0; hop <= MAX_REDIRECTS; hop++) { | ||
| const res = await fetch(current, { ...init, redirect: "manual" }); | ||
| if (!isRedirectResponse(res)) return res; | ||
| const location = res.headers.get("location"); | ||
| if (!location) return res; | ||
| const next = new URL(location, current); | ||
| assertLoopbackTarget(next, label, "redirect"); | ||
| current = next.href; | ||
| } | ||
| throw new Error(`${label}: too many redirects (> ${MAX_REDIRECTS}).`); | ||
| } | ||
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
Oops, something went wrong.
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.
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.
Fixed in 80b3c33. Redirect following is now restricted to the standard redirect codes 301/302/303/307/308 (a
REDIRECT_STATUS_CODESSet). Other 3xx responses (e.g. 300, 304, 306) are returned unchanged even if they carry aLocation. The numeric-status +headers.getguard is retained so non-spec test doubles still pass through as terminal.Generated by Claude Code