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:
Meta Vens 2026-09-30 15:49:30 +02:00
commit 7de12e84be
3 changed files with 96 additions and 4 deletions

View file

@ -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,
};
}

View file

@ -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();
}