diff --git a/src/app.ts b/src/app.ts index f6a72e9..a1d9a2f 100644 --- a/src/app.ts +++ b/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`, + // 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(); } diff --git a/tests/app.test.ts b/tests/app.test.ts index de2a896..dbb3208 100644 --- a/tests/app.test.ts +++ b/tests/app.test.ts @@ -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 = `"\\\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; + expect(providerError).toBe("e".repeat(64)); + expect(providerErrorDescription).toHaveLength(256); + expect(providerErrorDescription).toMatch(allowed); + expect(providerErrorDescription.startsWith("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(/