Skip to content

Commit 2723a56

Browse files
committed
fix(router): strip reserved redirect marker from stub-merge primitives (#572)
Review follow-up: the reserved x-rango-redirect-external header could still leak on a non-3xx response when set through the stub (e.g. a stray ctx.header("x-rango-redirect-external", "1") in middleware or a response route). mergeResponse() strips the marker from the base response, but the stub/header- merge primitives re-introduced it: createResponseWithMergedHeaders -> applyStubHeaders copies ctx.res.headers, and middleware mergeResponse() merges mergeStubHeaders/mergeReqCtxStub back in after the delete. This did not reopen the redirect bypass (3xx still hits the guard, which strips), but it contradicted the guarantee that the reserved header is stripped at every browser-facing exit. Fix: applyStubHeaders, mergeStubHeaders, and mergeReqCtxStub now refuse to copy the reserved marker (same treatment Set-Cookie already gets). The opt-in remains the out-of-band brand; the header is never a trust signal and never leaves. Tests (red-before-green, via the dispatch userland primitive): a marker set via ctx.header() on a non-3xx middleware short-circuit (mergeStubHeaders), on a response-route 200 (applyStubHeaders), and on the request-context stub through the middleware chain (mergeReqCtxStub) are all stripped. Docs updated. Full gate green: typecheck, test:unit:all, lint, format; e2e redirect-guard + semantic-matrix (dev + production).
1 parent 5dbe63b commit 2723a56

4 files changed

Lines changed: 79 additions & 7 deletions

File tree

‎packages/rangojs-router/docs/internal/security-checklist.md‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,16 @@ small set of internal redirect-rebuild paths transfer the brand
133133
(`mergeResponse` in `middleware.ts`, `carryOverRedirectHeaders` and the response-
134134
route `rewrapResponse`); the guard and the SPA intercept read it. A proxied
135135
upstream Response is never branded, so its forged header is inert — and the
136-
reserved header name is stripped defensively at every browser-facing exit so a
137-
forged value never reaches the browser. The brand is set by app code at the call
138-
site only; the attacker controls the URL value, never whether the app wrote
139-
`{ external: true }` — so `redirect(userInput)` stays safe (Rails
140-
`allow_other_host: true` model). Brand transfer is **fail-closed**: if a rebuild
141-
path ever drops it, the redirect is neutralized to root, never opened off-host.
136+
reserved header name is stripped defensively so a forged value (or a stray
137+
`ctx.header("x-rango-redirect-external", …)`) never reaches the browser: the guard
138+
deletes it on 3xx, and the stub/header-merge primitives that build every
139+
browser-facing response refuse to copy it (`applyStubHeaders` in `helpers.ts`,
140+
`mergeStubHeaders` + `mergeReqCtxStub` in `middleware.ts`, plus the redirect-
141+
rebuild copiers). The brand is set by app code at the call site only; the attacker
142+
controls the URL value, never whether the app wrote `{ external: true }` — so
143+
`redirect(userInput)` stays safe (Rails `allow_other_host: true` model). Brand
144+
transfer is **fail-closed**: if a rebuild path ever drops it, the redirect is
145+
neutralized to root, never opened off-host.
142146

143147
Soft SPA/Flight redirects are `200`/`204` responses (`X-RSC-Redirect` header /
144148
`metadata.redirect` payload), not 3xx, so they never reach the server guard —

‎packages/rangojs-router/src/router/middleware.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ function mergeStubHeaders(
300300
stubOverridesNonCookie: boolean,
301301
): void {
302302
stub.forEach((value, name) => {
303+
// The reserved external-redirect marker is internal and never a trust
304+
// signal; never copy a stub value (e.g. a stray ctx.header() call) onto a
305+
// browser-facing response. The opt-in is the out-of-band brand.
306+
if (name.toLowerCase() === EXTERNAL_REDIRECT_MARKER) return;
303307
if (name.toLowerCase() === "set-cookie") {
304308
target.append(name, value);
305309
} else if (stubOverridesNonCookie || !target.has(name)) {
@@ -325,6 +329,8 @@ function mergeReqCtxStub(
325329
}
326330
}
327331
reqCtx.res.headers.forEach((value, name) => {
332+
// Never propagate the reserved external-redirect marker (see mergeStubHeaders).
333+
if (name.toLowerCase() === EXTERNAL_REDIRECT_MARKER) return;
328334
if (name !== "set-cookie" && !target.has(name)) {
329335
target.set(name, value);
330336
}

‎packages/rangojs-router/src/rsc/helpers.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ function applyCacheSignalHeader(target: Headers, ctx: RequestContext): void {
4646
function applyStubHeaders(target: Headers, stub: Headers): void {
4747
stub.forEach((value, name) => {
4848
try {
49+
// The reserved external-redirect marker is internal and never a trust
50+
// signal; never copy a stub value (e.g. a stray ctx.header() call) onto a
51+
// browser-facing response. The opt-in is the out-of-band brand.
52+
if (name.toLowerCase() === EXTERNAL_REDIRECT_MARKER) return;
4953
if (name.toLowerCase() === "set-cookie") {
5054
target.append(name, value);
5155
} else if (!target.has(name)) {

‎packages/rangojs-router/src/testing/__tests__/dispatch.test.ts‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ describe("dispatch", () => {
460460

461461
// Header-leak regression (defense-in-depth): the reserved marker must never
462462
// reach the browser, even on a non-3xx response the 3xx-only guard does not
463-
// touch. mergeResponse strips it on the middleware path.
463+
// touch. mergeResponse strips it from the base response on the middleware path.
464464
it("strips a forged external marker header from a non-3xx middleware response", async () => {
465465
const mw: MiddlewareFn = () =>
466466
new Response("ok", {
@@ -476,6 +476,64 @@ describe("dispatch", () => {
476476
expect(res.status).toBe(200);
477477
expect(res.headers.get("x-rango-redirect-external")).toBeNull();
478478
});
479+
480+
// Header-leak via the STUB (ctx.header), not the base response. Stripping the
481+
// base in mergeResponse is not enough -- the stub-merge primitives re-add it.
482+
// mergeStubHeaders must refuse to copy the reserved marker.
483+
it("strips a reserved marker set via ctx.header() on a non-3xx middleware short-circuit", async () => {
484+
const mw: MiddlewareFn = (ctx) => {
485+
ctx.header("x-rango-redirect-external", "1");
486+
return new Response("ok", { status: 200 });
487+
};
488+
const res = await dispatch(routerWithMw(mw), {
489+
request: "http://localhost/api/data",
490+
});
491+
expect(res.status).toBe(200);
492+
expect(res.headers.get("x-rango-redirect-external")).toBeNull();
493+
});
494+
495+
// Same leak via a response route's ctx.header() on a 200: the serialized
496+
// result flows through createResponseWithMergedHeaders -> applyStubHeaders,
497+
// which must refuse to copy the reserved marker.
498+
it("strips a reserved marker set via ctx.header() on a response-route 200", async () => {
499+
const router = createRouter<{}>({}).routes(
500+
urls(({ path }) => [
501+
path.json(
502+
"/h",
503+
() => {
504+
getRequestContext().header("x-rango-redirect-external", "1");
505+
return { ok: true };
506+
},
507+
{ name: "h" },
508+
),
509+
]),
510+
) as any;
511+
const res = await dispatch(router, { request: "http://localhost/h" });
512+
expect(res.status).toBe(200);
513+
expect(res.headers.get("x-rango-redirect-external")).toBeNull();
514+
expect(await res.json()).toEqual({ ok: true });
515+
});
516+
517+
// Same leak via the request-context stub merged through the middleware chain
518+
// (mergeReqCtxStub) on a 200 downstream response.
519+
it("strips a reserved marker set on the request-context stub through the middleware chain", async () => {
520+
const mw: MiddlewareFn = (_ctx, next) => {
521+
getRequestContext().header("x-rango-redirect-external", "1");
522+
return next();
523+
};
524+
const router = createRouter<{}>({})
525+
.use(mw)
526+
.routes(
527+
urls(({ path }) => [
528+
path.json("/api/data", () => ({ ok: true }), { name: "api.data" }),
529+
]),
530+
) as any;
531+
const res = await dispatch(router, {
532+
request: "http://localhost/api/data",
533+
});
534+
expect(res.status).toBe(200);
535+
expect(res.headers.get("x-rango-redirect-external")).toBeNull();
536+
});
479537
});
480538

481539
it("lets global middleware pass through to the response route", async () => {

0 commit comments

Comments
 (0)