fix(auth): move passkeys to SimpleWebAuthn 14, which no longer fetches a CRL a client names

@simplewebauthn/server 13.3.3 fetched the CRL address in an android-key
attestation certificate before it checked the chain or the signature, with
no timeout or size cap, so anyone signed in could make the server request
any URL by registering a passkey. 14.1.0 only checks revocation for a chain
it validated against Google's pinned roots.

14.x refuses an expected origin with a path, a trailing slash or an
uppercase host and an RP ID that is not lower case, where 13.x only failed
to match them. The configured origins are now reduced to the origin the
browser reports and the RP ID is lower-cased, so such settings keep
working; an origins list in which nothing parses leaves passkeys
unconfigured instead of falling back to APP_URL. Verification gets the
algorithm list the options offer, and stored transports are read as plain
strings.

Stored credentials keep their format. typescript-eslint moves to 8.73.0.
This commit is contained in:
Maurice
2026-11-24 23:58:39 +01:00
parent 3754518d37
commit c65e9bf76e
9 changed files with 802 additions and 171 deletions
+5 -4
View File
@@ -20,7 +20,6 @@ import {
verifyRegistrationResponse,
generateAuthenticationOptions,
verifyAuthenticationResponse,
type AuthenticatorTransportFuture,
} from '@simplewebauthn/server';
import bcrypt from 'bcryptjs';
@@ -72,11 +71,11 @@ function originFromResponse(resp: unknown): string | null {
return typeof origin === 'string' ? origin : null;
}
function parseTransports(raw: string | null): AuthenticatorTransportFuture[] | undefined {
function parseTransports(raw: string | null): string[] | undefined {
if (!raw) return undefined;
try {
const parsed = JSON.parse(raw);
return Array.isArray(parsed) ? (parsed as AuthenticatorTransportFuture[]) : undefined;
const parsed: unknown = JSON.parse(raw);
return Array.isArray(parsed) ? parsed.filter((t): t is string => typeof t === 'string') : undefined;
} catch {
return undefined;
}
@@ -308,6 +307,8 @@ export class PasskeyService {
expectedOrigin,
expectedRPID: cfg.rpID,
requireUserVerification: true,
// The same list the options offered, so the verifier never falls back to its own default.
supportedAlgorithmIDs: SUPPORTED_ALGORITHM_IDS,
});
} catch (err) {
// Body stays the generic 400 (parity), but the operator gets to see WHY —
+50 -10
View File
@@ -2,7 +2,7 @@ import { getAppUrl, readEnv } from '../../app-config';
import { AppSettings } from '../../db/entities/AppSettings.entity';
import type { AppSettingsRepository } from '../../db/repositories/AppSettings.repository';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
function hostOf(url: string): string | null {
try {
@@ -19,6 +19,23 @@ function isIpHost(host: string): boolean {
return false;
}
/**
* An http(s) origin reduced to the scheme, host and port the browser reports
* (`new URL(o).origin`), so a configured path, trailing slash or uppercase host
* still matches. @simplewebauthn/server refuses such an expected origin
* outright, where older releases only failed to match it. Any other scheme (an
* app package origin) is kept as written. Null when the value does not parse.
*/
function normalizeOrigin(origin: string): string | null {
let url: URL;
try {
url = new URL(origin);
} catch {
return null;
}
return url.protocol === 'http:' || url.protocol === 'https:' ? url.origin : origin;
}
/**
* The scope rule WebAuthn itself applies when the browser runs a ceremony: the
* origin's host must equal the RP ID or be a subdomain of it, over https
@@ -70,8 +87,25 @@ export interface WebauthnConfig {
*/
@Injectable()
export class WebauthnConfigService {
private readonly logger = new Logger(WebauthnConfigService.name);
constructor(@InjectRepository(AppSettings) private readonly appSettings: AppSettingsRepository) {}
/** Normalizes every origin, dropping (and naming) the ones that do not parse. */
private normalizeOrigins(origins: string[]): string[] {
const normalized: string[] = [];
const unparseable: string[] = [];
for (const origin of origins) {
const value = normalizeOrigin(origin);
if (value === null) unparseable.push(origin);
else normalized.push(value);
}
if (unparseable.length > 0) {
this.logger.warn(`Ignoring passkey origins that are not valid URLs: ${unparseable.join(', ')}`);
}
return Array.from(new Set(normalized));
}
/** `SELECT value FROM app_settings WHERE key = ?` (WC1), through `AppSettingsRepository.getValue` (Plan 3a). */
private async setting(key: string): Promise<string | null> {
const raw = (await this.appSettings.getValue(key)) ?? undefined;
@@ -85,14 +119,18 @@ export class WebauthnConfigService {
* disabled so nobody can enrol a credential bound to the wrong origin.
*/
async resolve(): Promise<WebauthnConfig | null> {
// 1. Explicit operator config always wins.
const explicitRpId = (readEnv().webauthn.rpId || (await this.setting('webauthn_rp_id')))?.trim() || null;
const explicitOrigins = (readEnv().webauthn.origins || (await this.setting('webauthn_origins')) || '')
// 1. Explicit operator config always wins. Host names compare without case,
// and the verifier refuses an RP ID that is not in its lower-case form.
const explicitRpId =
(readEnv().webauthn.rpId || (await this.setting('webauthn_rp_id')))?.trim().toLowerCase() || null;
const configuredOrigins = (readEnv().webauthn.origins || (await this.setting('webauthn_origins')) || '')
.split(',')
// The lookbehind matches only the first slash of the trailing run. Without it the
// engine retries from every slash, which is quadratic on a slash-heavy value.
.map((o) => o.trim().replace(/(?<!\/)\/+$/, ''))
.filter(Boolean);
const explicitOrigins = this.normalizeOrigins(configuredOrigins);
const hasExplicitOrigins = configuredOrigins.length > 0;
const appUrl = getAppUrl();
const appHost = hostOf(appUrl);
@@ -104,12 +142,14 @@ export class WebauthnConfigService {
}
if (!rpID) return null; // bare IP / unresolved → WebAuthn cannot be used here
// 3. Resolve the allowed origins. Explicit list wins verbatim (operator's
// responsibility). Otherwise derive a SINGLE origin from APP_URL — we never
// silently union dev localhost origins into a production allow-list.
// 3. Resolve the allowed origins. Explicit list wins (operator's
// responsibility); a list whose every entry failed to parse stays explicit
// and leaves the feature unconfigured rather than falling back to APP_URL.
// Otherwise derive a SINGLE origin from APP_URL; we never silently union
// dev localhost origins into a production allow-list.
let origins = explicitOrigins;
if (origins.length === 0) {
if (appHost) origins = [appUrl.replace(/(?<!\/)\/+$/, '')];
if (!hasExplicitOrigins) {
if (appHost) origins = this.normalizeOrigins([appUrl.replace(/(?<!\/)\/+$/, '')]);
if (rpID === 'localhost') {
// Dev: the browser origin is the Vite dev server (:5173), not the API port.
origins = Array.from(new Set([...origins, 'http://localhost:5173', 'http://localhost:3001']));
@@ -117,7 +157,7 @@ export class WebauthnConfigService {
}
if (origins.length === 0) return null;
return { rpID, rpName: 'TREK', origins, explicitOrigins: explicitOrigins.length > 0 };
return { rpID, rpName: 'TREK', origins, explicitOrigins: hasExplicitOrigins };
}
/** True when a usable RP ID resolves for this deployment (exposed as a pure boolean on app-config). */