Relayed provider errors are printable, bounded and inert in the page
Self's ruling: the provider_error relay stays, because it is the protocol, but its error and error_description are query parameters anyone can write. Only the characters RFC 6749 4.1.2.1 allows in them survive (%x20-21, %x23-5B, %x5D-7E), and each is cut to a bound (64 and 256). The JSON answer, the browser page and the backend handoff all take the relayed values. The page has no template engine. Every value in it goes through serializeForScript, which escapes `<`, apart from the fallback URL in the link's href, which the URL serializer percent-encodes. A test puts `<script>` in error_description and checks the page holds only its own script tag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
cad12165a9
commit
7de12e84be
3 changed files with 96 additions and 4 deletions
21
src/app.ts
21
src/app.ts
|
|
@ -238,6 +238,17 @@ export function verifyRefreshToken(
|
|||
return payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* A provider's `error` and `error_description` as Heimdall relays them. They
|
||||
* arrive as query parameters anyone can write, so only the characters RFC 6749
|
||||
* (4.1.2.1) allows in them survive, and each is cut to a bound. The browser
|
||||
* page escapes what is left for its own contexts.
|
||||
*/
|
||||
function relayedProviderText(value: string | undefined, bound: number): string | undefined {
|
||||
const printable = value?.replace(/[^\x20-\x21\x23-\x5b\x5d-\x7e]/g, "").slice(0, bound);
|
||||
return printable ? printable : undefined;
|
||||
}
|
||||
|
||||
function prefersHtml(acceptHeader: string | undefined): boolean {
|
||||
if (!acceptHeader) {
|
||||
return false;
|
||||
|
|
@ -959,6 +970,8 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
|
|||
};
|
||||
|
||||
if (request.query.error) {
|
||||
const providerError = relayedProviderText(request.query.error, 64);
|
||||
const providerErrorDescription = relayedProviderText(request.query.error_description, 256);
|
||||
await denyBrowserAttempt("provider_error");
|
||||
if (handoff.kind === "backend_callback") {
|
||||
await maybeDeliverBackendHandoff(handoff, {
|
||||
|
|
@ -973,7 +986,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
|
|||
returnTo: statePayload.return_to,
|
||||
connection: statePayload.connection,
|
||||
error: "provider_error",
|
||||
errorDescription: request.query.error_description ?? request.query.error,
|
||||
errorDescription: providerErrorDescription ?? providerError,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -989,7 +1002,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
|
|||
returnTo: statePayload.return_to,
|
||||
...(handoff.kind === "backend_callback" ? { attemptId: handoff.attemptId } : {}),
|
||||
error: "provider_error",
|
||||
errorDescription: request.query.error_description ?? request.query.error,
|
||||
errorDescription: providerErrorDescription ?? providerError,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
@ -998,8 +1011,8 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
|
|||
return {
|
||||
error: "provider_error",
|
||||
provider: request.params.provider,
|
||||
providerError: request.query.error,
|
||||
providerErrorDescription: request.query.error_description,
|
||||
providerError,
|
||||
providerErrorDescription,
|
||||
returnTo: statePayload.return_to,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,6 +56,9 @@ function buildBrowserFallbackUrl(payload: BrowserHandoffPayload): string {
|
|||
}
|
||||
}
|
||||
|
||||
// URLSearchParams and the URL serializer percent-encode `"`, `<` and `>`,
|
||||
// so the page can place this URL in an attribute as it is. Every other value
|
||||
// in the page goes through serializeForScript; there is no template engine.
|
||||
url.hash = fragment.toString();
|
||||
return url.toString();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1403,3 +1403,79 @@ describe("error answers never echo an input", () => {
|
|||
expect(deliveries[0]).not.toContain(CANARY);
|
||||
});
|
||||
});
|
||||
|
||||
// The provider's error and error_description are relayed, but they arrive as
|
||||
// query parameters anyone can write: only RFC 6749's characters survive, each
|
||||
// is bounded, and the page cannot be made to run what they carry.
|
||||
describe("the relayed provider error", () => {
|
||||
const injected = `<script>alert(1)</script>"\\\u0007é${"x".repeat(300)}`;
|
||||
const allowed = /^[\x20-\x21\x23-\x5b\x5d-\x7e]*$/;
|
||||
|
||||
function providerErrorUrl(stateToken: string): string {
|
||||
const query = new URLSearchParams({ state: stateToken, error: `${"e".repeat(100)}\u0000`, error_description: injected });
|
||||
return `/v1/oauth/discord/callback?${query.toString()}`;
|
||||
}
|
||||
|
||||
it("is printable and bounded in the JSON answer", async () => {
|
||||
const app = await buildApp({ config: createTestConfig() });
|
||||
apps.push(app);
|
||||
|
||||
const response = await app.inject({ method: "GET", url: providerErrorUrl(await startDiscordSignIn(app)) });
|
||||
|
||||
expect(response.statusCode).toBe(400);
|
||||
const { providerError, providerErrorDescription } = response.json() as Record<string, string>;
|
||||
expect(providerError).toBe("e".repeat(64));
|
||||
expect(providerErrorDescription).toHaveLength(256);
|
||||
expect(providerErrorDescription).toMatch(allowed);
|
||||
expect(providerErrorDescription.startsWith("<script>alert(1)</script>xxx")).toBe(true);
|
||||
});
|
||||
|
||||
it("cannot inject markup into the browser page, and is bounded there", async () => {
|
||||
const app = await buildApp({ config: createTestConfig() });
|
||||
apps.push(app);
|
||||
|
||||
const response = await app.inject({
|
||||
method: "GET",
|
||||
url: providerErrorUrl(await startDiscordSignIn(app)),
|
||||
headers: { accept: "text/html" },
|
||||
});
|
||||
|
||||
expect(response.headers["content-type"]).toContain("text/html");
|
||||
expect(response.body.match(/<script/gi)).toHaveLength(1);
|
||||
expect(response.body).not.toContain("<script>alert");
|
||||
expect(response.body).not.toContain("x".repeat(257));
|
||||
});
|
||||
|
||||
it("is printable and bounded in the backend handoff", async () => {
|
||||
const deliveries: Array<Record<string, unknown>> = [];
|
||||
globalThis.fetch = async (_input, init) => {
|
||||
deliveries.push(JSON.parse(String(init?.body)) as Record<string, unknown>);
|
||||
return new Response(null, { status: 204 });
|
||||
};
|
||||
const app = await buildApp({ config: createTestConfig() });
|
||||
apps.push(app);
|
||||
const start = await app.inject({
|
||||
method: "POST",
|
||||
url: "/v1/oauth/discord/start",
|
||||
payload: {
|
||||
appSlug: "repixelizer",
|
||||
mode: "sign_in",
|
||||
returnTo: "https://repixelizer.gamecult.org/app/",
|
||||
handoff: {
|
||||
kind: "backend_callback",
|
||||
attemptId: "attempt-123",
|
||||
callbackUrl: "https://repixelizer.gamecult.org/api/auth/heimdall/callback",
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(start.statusCode).toBe(201);
|
||||
|
||||
await app.inject({ method: "GET", url: providerErrorUrl(start.json().stateToken as string) });
|
||||
|
||||
expect(deliveries).toHaveLength(1);
|
||||
const description = String(deliveries[0]!.errorDescription);
|
||||
expect(description).toHaveLength(256);
|
||||
expect(description).toMatch(allowed);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue