Three Low/Info hardening items from the 2026-06-10 security review.
OAuth/credential log redaction:
- oauth-flow.server.ts logged the raw exception on code-exchange
failure; a provider error can embed the auth code / token response,
which would land in logs + Sentry. Log only e.message now.
- manager.markNeedsRelink bounded the provider-supplied reason string
to 200 chars before logging.
Magic-link account enumeration:
- createMagicToken now returns null (instead of throwing "No account
found for this email") when no account matches; the login route
always responds { step: "magic-link-sent" }, minting a token and
sending mail only for a real account. The public login form can no
longer be used to probe which emails are registered. Registration's
email/username "already in use/taken" messages are intentionally
unchanged — standard signup UX, and the passkey ceremony can't be
made to fake-succeed.
Federation remote-document size cap:
- assertRemoteDocSize rejects an actor/outbox document over 4 MB once
serialized, applied in the ingest fetchJson seam. Fedify owns the
transfer (with its own SSRF + redirect limits) and our poll uses the
authenticated loader for secure-mode instances, so this is a
downstream guard on iteration/persistence, complementing the existing
per-poll item cap.
Tests: assertRemoteDocSize unit cases; a gated (FEDERATION_INTEGRATION=1)
integration test asserting an unsigned POST to /users/:u/inbox is
rejected with 401 — a regression guard for Fedify's signature
verification.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
128 lines
4.5 KiB
TypeScript
128 lines
4.5 KiB
TypeScript
// The OAuth connect → callback lifecycle as one module. Routes stay
|
|
// thin adapters: they look up the manifest, call initiate/complete,
|
|
// and map the result to redirects. Everything protocol-shaped —
|
|
// state encoding, PKCE pair generation, the verifier cookie's
|
|
// lifecycle, redirect-URI construction, code exchange, linking the
|
|
// connection — lives here, so the next OAuth provider (Coros, Strava)
|
|
// reuses the flow instead of the pattern.
|
|
|
|
import { redirect } from "react-router";
|
|
import { getOrigin } from "../config.server.ts";
|
|
import { link } from "./manager.ts";
|
|
import type { ProviderManifest } from "./registry.ts";
|
|
import {
|
|
clearPkceCookieHeader,
|
|
decodeOAuthState,
|
|
encodeOAuthState,
|
|
generatePkcePair,
|
|
pkceCookieHeader,
|
|
readPkceVerifier,
|
|
type PushOAuthState,
|
|
} from "./oauth-state.server.ts";
|
|
|
|
function callbackUri(manifest: ProviderManifest): string {
|
|
return `${getOrigin()}/api/sync/callback/${manifest.id}`;
|
|
}
|
|
|
|
/**
|
|
* Build the provider authorization redirect. Encodes post-callback
|
|
* intent into the state param and, for PKCE providers, sets the
|
|
* httpOnly verifier cookie — including when the flow is initiated
|
|
* from a push-resume (the old inline code only handled PKCE on the
|
|
* plain connect path).
|
|
*
|
|
* Caller must have verified `manifest.buildAuthUrl` exists.
|
|
*/
|
|
export function initiateOAuthFlow(
|
|
manifest: ProviderManifest,
|
|
intent: PushOAuthState,
|
|
): Response {
|
|
if (!manifest.buildAuthUrl) {
|
|
throw new Error(`Provider ${manifest.id} has no buildAuthUrl`);
|
|
}
|
|
const state = encodeOAuthState(intent);
|
|
const redirectUri = callbackUri(manifest);
|
|
|
|
if (manifest.pkce) {
|
|
const { verifier, challenge } = generatePkcePair();
|
|
return redirect(manifest.buildAuthUrl(redirectUri, state, { codeChallenge: challenge }), {
|
|
headers: { "Set-Cookie": pkceCookieHeader(verifier) },
|
|
});
|
|
}
|
|
return redirect(manifest.buildAuthUrl(redirectUri, state));
|
|
}
|
|
|
|
export type OAuthCompletion =
|
|
/** Provider sent the user back with access_denied. */
|
|
| { status: "denied"; state: PushOAuthState }
|
|
/** No authorization code in the callback URL. */
|
|
| { status: "missing_code"; state: PushOAuthState }
|
|
/** PKCE provider but the verifier cookie is gone (expired / cleared). */
|
|
| { status: "missing_verifier"; state: PushOAuthState }
|
|
/** Code exchange or linking failed; `code` is provider-specific or "sync_failed". */
|
|
| { status: "error"; code: string; state: PushOAuthState }
|
|
/** Connection linked. */
|
|
| { status: "linked"; state: PushOAuthState };
|
|
|
|
/**
|
|
* Consume the provider callback: decode state, recover the PKCE
|
|
* verifier, exchange the code, and link the connection. Never throws —
|
|
* every outcome is a variant the route maps to a redirect. The spent
|
|
* verifier cookie should be cleared on whatever response the route
|
|
* returns (`clearPkceCookieHeader`).
|
|
*/
|
|
export async function completeOAuthFlow(
|
|
manifest: ProviderManifest,
|
|
request: Request,
|
|
userId: string,
|
|
): Promise<OAuthCompletion> {
|
|
if (!manifest.exchangeCode) {
|
|
throw new Error(`Provider ${manifest.id} has no exchangeCode`);
|
|
}
|
|
const url = new URL(request.url);
|
|
const state = decodeOAuthState(url.searchParams.get("state"));
|
|
|
|
if (url.searchParams.get("error") === "access_denied") {
|
|
return { status: "denied", state };
|
|
}
|
|
|
|
const code = url.searchParams.get("code");
|
|
if (!code) return { status: "missing_code", state };
|
|
|
|
const codeVerifier = manifest.pkce ? readPkceVerifier(request) : null;
|
|
if (manifest.pkce && !codeVerifier) {
|
|
return { status: "missing_verifier", state };
|
|
}
|
|
|
|
try {
|
|
const exchange = await manifest.exchangeCode(
|
|
code,
|
|
callbackUri(manifest),
|
|
codeVerifier ? { codeVerifier } : undefined,
|
|
);
|
|
await link({
|
|
userId,
|
|
provider: manifest.id,
|
|
credentialKind: manifest.credentialKind,
|
|
credentials: exchange.credentials as Record<string, unknown>,
|
|
providerUserId: exchange.providerUserId,
|
|
grantedScopes: exchange.grantedScopes,
|
|
});
|
|
return { status: "linked", state };
|
|
} catch (e) {
|
|
// Log only the error message, never the raw exception object — a
|
|
// provider's thrown error can embed the authorization code or token
|
|
// exchange response, which would then land in logs / Sentry.
|
|
console.error(
|
|
`OAuth callback failed for ${manifest.id}:`,
|
|
e instanceof Error ? e.message : String(e),
|
|
);
|
|
const code =
|
|
typeof (e as { code?: string }).code === "string"
|
|
? (e as { code: string }).code
|
|
: "sync_failed";
|
|
return { status: "error", code, state };
|
|
}
|
|
}
|
|
|
|
export { clearPkceCookieHeader };
|