Skip to content

Commit e637272

Browse files
fix: http(s) scheme floor for redirect navigation targets (#3175)
maskRedirect resolves Location with new URL(target, requestUrl), where an absolute scheme wins over the base — throw redirect(next) with user data emitted "302 javascript:alert(document.cookie)" as the header's documented "resolved absolute target". Same-origin script execution in any integration that navigates to the decoded value. - The transport (enforceComposedHeaderInvariants, the widened #3158 hook) refuses non-http(s) targets on REDIRECT_HEADER and Location with a sanitized 500. Relative and cross-origin http(s) targets flow: the floor kills the script-execution class; same-origin-vs-allowlist is the separate policy ruling still pending. - decodeRedirectHeaderValue enforces the resolved-absolute-http(s) contract it documents (and the masked status set), so a hostile peer cannot re-open the class against location.href integrations. Co-authored-by: Cursor <[email protected]>
1 parent 1ac14eb commit e637272

4 files changed

Lines changed: 157 additions & 29 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/web": patch
3+
---
4+
5+
Navigation targets now carry an http(s) scheme floor on both legs of the redirect header (#3175). `maskRedirect` resolves targets with `new URL(target, requestUrl)` where an absolute scheme wins over the base, so `throw redirect(next)` with user data emitted `javascript:alert(document.cookie)` as the header's "resolved absolute target" — same-origin script execution in any integration that navigates to the decoded value. The transport now refuses non-http(s) schemes on `X-Server-Function-Redirect` and `Location` with a sanitized 500 (relative targets and cross-origin http(s) still flow — the same-origin-vs-allowlist policy is a separate, pending decision), and `decodeRedirectHeaderValue` enforces the resolved-absolute-http(s) contract it documents, so a hostile peer cannot re-open the class against `location.href = decoded.url` integrations.

‎packages/web/server-functions/src/server.ts‎

Lines changed: 83 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,42 +1441,96 @@ function maskRedirect(headers, response, requestUrl) {
14411441
// a silently stale cache. Runs ahead of the stub fold so integration
14421442
// cookies still ride the refusal (#3159).
14431443
const BOUNDED_COMPOSED_HEADERS = [REDIRECT_HEADER, "Location", REVALIDATE_HEADER];
1444-
function enforceComposedHeaderBounds(response) {
1444+
1445+
// Scheme floor for outgoing navigation targets (#3175): only http(s) — or a
1446+
// scheme-less relative form, which resolves against an http(s) request url —
1447+
// may leave on a navigation header. maskRedirect resolves `Location` with
1448+
// `new URL(target, requestUrl)`, where an absolute scheme WINS over the
1449+
// base, so the classic open-redirect shape (`throw redirect(next)` with
1450+
// `next` from a query param) emitted `javascript:alert(document.cookie)` as
1451+
// the header's "resolved absolute target" — same-origin script execution in
1452+
// any integration that navigates to the decoded value. This is the floor,
1453+
// not the policy: cross-origin http(s) (OAuth hand-offs) still flows — the
1454+
// same-origin-vs-allowlist ruling is a separate, pending decision — and a
1455+
// custom app scheme (deep links) is refused by default until that ruling
1456+
// gives it an opt-in. The decoder enforces the same floor independently
1457+
// (decodeRedirectHeaderValue), so a hostile peer cannot re-open the class
1458+
// against integrations either.
1459+
function refusedTargetScheme(target) {
1460+
// explicit scheme present and not http(s) → refused
1461+
const match = /^[a-zA-Z][a-zA-Z0-9+.-]*:/.exec(target);
1462+
return match !== null && !/^https?:$/i.test(match[0]);
1463+
}
1464+
1465+
// The transport half of redirect()'s and initWithRevalidate's invariants:
1466+
// composed-header BOUNDS (#3158 — an over-long header dies at a proxy
1467+
// AFTER the mutation committed) and the navigation-target scheme floor
1468+
// (#3175 — see above). The invariants live HERE, at the edge where the
1469+
// composed headers leave, one check for every producer — returned or
1470+
// thrown, raw or envelope-carried, scripted mask or plain passthrough —
1471+
// and the helpers' own throws (inside the function body, with the legible
1472+
// authoring-time message) become the fast path rather than the only guard.
1473+
// Refused, never trimmed or stripped, for the helpers' own reasons: a cut
1474+
// or rewritten target is a DIFFERENT address, a dropped revalidate key is
1475+
// a silently stale cache. Runs ahead of the stub fold so integration
1476+
// cookies still ride the refusal (#3159).
1477+
function enforceComposedHeaderInvariants(response) {
14451478
for (const name of BOUNDED_COMPOSED_HEADERS) {
14461479
const value = response.headers.get(name);
1447-
if (value === null || value.length <= RESPONSE_HEADER_VALUE_LIMIT) continue;
1448-
// the replaced body's producers may be demand-parked — release them
1449-
if (response.body) {
1450-
try {
1451-
const cancelled = response.body.cancel();
1452-
if (cancelled && typeof cancelled.then === "function") cancelled.then(undefined, () => {});
1453-
} catch {}
1480+
if (value === null) continue;
1481+
if (value.length > RESPONSE_HEADER_VALUE_LIMIT) {
1482+
return refuseComposedHeader(
1483+
response,
1484+
name,
1485+
`${name} response header refused at ${value.length} characters`,
1486+
DEV
1487+
? `The ${name} response header is ${value.length} characters; past ` +
1488+
`${RESPONSE_HEADER_VALUE_LIMIT} it would overflow receivers (an 8 KiB proxy ` +
1489+
`buffer holds the whole header block) and the response dies at the socket after ` +
1490+
`the mutation committed. Refused rather than trimmed: a cut redirect target is a ` +
1491+
`different address, a dropped revalidate key is a silently stale cache. The ` +
1492+
`redirect()/reload() helpers enforce this bound with the full reasoning at the ` +
1493+
`call site.`
1494+
: null
1495+
);
14541496
}
1455-
const headers = new Headers();
1456-
headers.set(
1457-
ERROR_HEADER,
1458-
boundedErrorHeaderValue(
1497+
if (name === REVALIDATE_HEADER) continue;
1498+
// REDIRECT_HEADER rides as "<status> <target>"; Location is the target
1499+
const target = name === REDIRECT_HEADER ? value.slice(value.indexOf(" ") + 1) : value;
1500+
if (refusedTargetScheme(target)) {
1501+
return refuseComposedHeader(
1502+
response,
1503+
name,
1504+
`${name} response header refused: non-http(s) navigation target`,
14591505
DEV
1460-
? `${name} response header refused at ${value.length} characters`
1461-
: GENERIC_SERVER_ERROR_MESSAGE
1462-
)
1463-
);
1464-
return new Response(
1465-
DEV
1466-
? `The ${name} response header is ${value.length} characters; past ` +
1467-
`${RESPONSE_HEADER_VALUE_LIMIT} it would overflow receivers (an 8 KiB proxy ` +
1468-
`buffer holds the whole header block) and the response dies at the socket after ` +
1469-
`the mutation committed. Refused rather than trimmed: a cut redirect target is a ` +
1470-
`different address, a dropped revalidate key is a silently stale cache. The ` +
1471-
`redirect()/reload() helpers enforce this bound with the full reasoning at the ` +
1472-
`call site.`
1473-
: null,
1474-
{ status: 500, headers }
1475-
);
1506+
? `The ${name} response header carries a navigation target with a non-http(s) ` +
1507+
`scheme ("${target.slice(0, 64)}"). A javascript: target is same-origin script ` +
1508+
`execution in any integration that navigates to it, so only http(s) and ` +
1509+
`relative targets leave this transport. If the target came from request data ` +
1510+
`(?next= and friends), validate it against your own origin before redirecting.`
1511+
: null
1512+
);
1513+
}
14761514
}
14771515
return response;
14781516
}
14791517

1518+
function refuseComposedHeader(response, name, headerMessage, body) {
1519+
// the replaced body's producers may be demand-parked — release them
1520+
if (response.body) {
1521+
try {
1522+
const cancelled = response.body.cancel();
1523+
if (cancelled && typeof cancelled.then === "function") cancelled.then(undefined, () => {});
1524+
} catch {}
1525+
}
1526+
const headers = new Headers();
1527+
headers.set(
1528+
ERROR_HEADER,
1529+
boundedErrorHeaderValue(DEV ? headerMessage : GENERIC_SERVER_ERROR_MESSAGE)
1530+
);
1531+
return new Response(body, { status: 500, headers });
1532+
}
1533+
14801534
// Dev-only (#3101): a 304 forwards transparently — it is not a redirect,
14811535
// and unscripted callers need the real status — but the scripted transport
14821536
// sends no conditional headers (no If-None-Match), so a hand-rolled 304
@@ -3065,7 +3119,7 @@ export async function handleServerFunctionRequest(request, options = {}) {
30653119
// foreign-response path at once (raw passthrough, unscripted returns and
30663120
// throws, custom handleNoJS results, envelope-carried responses).
30673121
const response = commitEventResponse(
3068-
enforceComposedHeaderBounds(ownResponse(await dispatch())),
3122+
enforceComposedHeaderInvariants(ownResponse(await dispatch())),
30693123
event
30703124
);
30713125
return finalizeTransportResponse(protectsRequest ? withCSRFVary(response) : response, method);

‎packages/web/server-functions/src/shared.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,14 @@ export const REDIRECT_HEADER = "X-Server-Function-Redirect";
646646
* resolved absolute target. Integration plumbing for readers of the header
647647
* (routers); the wire format is the runtime's own, not a contract to parse
648648
* by hand.
649+
*
650+
* The documented output — a resolved ABSOLUTE http(s) target and a redirect
651+
* status — is enforced, not assumed (#3175): the absoluteness used to be a
652+
* property of the server having resolved it, and a hostile or buggy peer
653+
* could ride a `javascript:` target straight into the `location.href =
654+
* decoded.url` an integration reasonably writes. A value that does not
655+
* parse as an absolute http(s) url, or whose status is not one the runtime
656+
* masks, decodes to `undefined` — the same answer a missing header gives.
649657
*/
650658
export function decodeRedirectHeaderValue(
651659
value: string | null | undefined
@@ -656,6 +664,15 @@ export function decodeRedirectHeaderValue(
656664
const status = Number(value.slice(0, at));
657665
const url = value.slice(at + 1);
658666
if (!Number.isInteger(status) || !url) return undefined;
667+
if (status !== 301 && status !== 302 && status !== 303 && status !== 307 && status !== 308)
668+
return undefined;
669+
let parsed: URL;
670+
try {
671+
parsed = new URL(url);
672+
} catch {
673+
return undefined;
674+
}
675+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return undefined;
659676
return { status, url };
660677
}
661678

‎packages/web/test/server/server-functions-open-gaps.spec.tsx‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,58 @@ describe("the decode depth cap", () => {
681681
});
682682
});
683683

684+
describe("navigation targets carry an http(s) scheme floor (#3175)", () => {
685+
const REDIRECT_HEADER = "X-Server-Function-Redirect";
686+
687+
// The classic open-redirect shape: user data flows into redirect() and an
688+
// absolute scheme wins the URL resolution — the header carried
689+
// `302 javascript:alert(document.cookie)` as its "resolved absolute
690+
// target" for any integration that navigates to the decoded value.
691+
test("a javascript: redirect target is refused at the transport", async () => {
692+
registerServerFunction("target-scheme-js", async () => {
693+
const { redirect } = await import("@solidjs/web");
694+
throw redirect("javascript:alert(document.cookie)" as string);
695+
});
696+
const response = await handleServerFunctionRequest(scriptedPost("target-scheme-js"));
697+
expect(response.status).toBe(500);
698+
expect(response.headers.get(REDIRECT_HEADER)).toBeNull();
699+
expect(await response.text()).not.toContain("alert(document.cookie)");
700+
});
701+
702+
test("controls: relative and cross-origin http(s) targets still flow", async () => {
703+
const { redirect } = await import("@solidjs/web");
704+
registerServerFunction("target-scheme-relative", async () => {
705+
throw redirect("/dashboard");
706+
});
707+
registerServerFunction("target-scheme-oauth", async () => {
708+
// cross-origin http(s) is the pending POLICY question, not the floor —
709+
// OAuth hand-offs must keep working until that ruling
710+
throw redirect("https://accounts.example/oauth");
711+
});
712+
const relative = await handleServerFunctionRequest(scriptedPost("target-scheme-relative"));
713+
expect(relative.status).toBe(200);
714+
expect(relative.headers.get(REDIRECT_HEADER)).toBe("302 https://app.example/dashboard");
715+
const oauth = await handleServerFunctionRequest(scriptedPost("target-scheme-oauth"));
716+
expect(oauth.status).toBe(200);
717+
expect(oauth.headers.get(REDIRECT_HEADER)).toBe("302 https://accounts.example/oauth");
718+
});
719+
720+
// The decoder enforces the contract it documents — a hostile peer cannot
721+
// re-open the class against `location.href = decoded.url` integrations.
722+
test("decodeRedirectHeaderValue refuses what the contract never promised", async () => {
723+
const { decodeRedirectHeaderValue } = await import("@solidjs/web/server-functions/server");
724+
// frenzzy's table from the report, plus the well-formed control
725+
expect(decodeRedirectHeaderValue("302 javascript:alert(document.cookie)")).toBeUndefined();
726+
expect(decodeRedirectHeaderValue("302 not-even-a-url")).toBeUndefined();
727+
expect(decodeRedirectHeaderValue("999 //evil.example")).toBeUndefined();
728+
expect(decodeRedirectHeaderValue("302 data:text/html,<script>1</script>")).toBeUndefined();
729+
expect(decodeRedirectHeaderValue("302 https://evil.example/steal")).toEqual({
730+
status: 302,
731+
url: "https://evil.example/steal"
732+
});
733+
});
734+
});
735+
684736
describe("channels behind a getter or as a Map key (#3176)", () => {
685737
const SECRET = "user=svc_billing password=hunter2 host=10.0.0.7";
686738
const mkRejection = () => {

0 commit comments

Comments
 (0)