mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
fix(auth): bind derived passkey origins to the configured host
This commit is contained in:
@@ -12,7 +12,12 @@ import { UnitOfWork } from '../database/unit-of-work';
|
||||
import type { SessionClient } from '../sessions/sessions.service';
|
||||
import { stripUserForClient } from './auth.helpers';
|
||||
import { AuthService } from './auth.service';
|
||||
import { WebauthnConfigService, originWithinRpScope, type WebauthnConfig } from './webauthn-config.service';
|
||||
import {
|
||||
WebauthnConfigService,
|
||||
originWithinRpScope,
|
||||
passkeyVerificationOrigins,
|
||||
type WebauthnConfig,
|
||||
} from './webauthn-config.service';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import {
|
||||
@@ -182,31 +187,6 @@ export class PasskeyService {
|
||||
// Origin handling (#2147)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The origins handed to the verifier for this ceremony. An explicit operator
|
||||
* list is honored verbatim. A derived config (APP_URL fallback) often carries
|
||||
* the wrong scheme or port (TLS-terminating proxy, first ALLOWED_ORIGINS
|
||||
* entry), so the browser-asserted origin is added when it is the RP ID's own
|
||||
* host and only the scheme or port drifted.
|
||||
*
|
||||
* The HOST is never widened, even though the browser's rule would run a
|
||||
* ceremony from any subdomain of the RP ID: on an apex RP ID that exact-origin
|
||||
* list is the only thing between a taken-over sibling subdomain and an
|
||||
* assertion this server would accept, and the browser cannot supply it. A
|
||||
* deployment that really spans several hosts under one RP ID lists them in
|
||||
* webauthn_origins / WEBAUTHN_ORIGINS, which is honored verbatim above.
|
||||
*/
|
||||
private expectedOrigins(cfg: WebauthnConfig, resp: unknown): string[] {
|
||||
if (cfg.explicitOrigins) return cfg.origins;
|
||||
const clientOrigin = originFromResponse(resp);
|
||||
if (!clientOrigin || cfg.origins.includes(clientOrigin) || !originWithinRpScope(clientOrigin, cfg.rpID)) {
|
||||
return cfg.origins;
|
||||
}
|
||||
// Parsing cannot throw here: originWithinRpScope only says yes on a URL it parsed.
|
||||
if (new URL(clientOrigin).hostname !== cfg.rpID) return cfg.origins;
|
||||
return [...cfg.origins, clientOrigin];
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-ceremony check of the request's Origin header against the resolved RP
|
||||
* config. A browser on an origin the derived localhost fallback can never
|
||||
@@ -298,7 +278,7 @@ export class PasskeyService {
|
||||
throw new DomainError(400, 'Registration challenge expired. Please try again.');
|
||||
}
|
||||
|
||||
const expectedOrigin = this.expectedOrigins(cfg, resp);
|
||||
const expectedOrigin = passkeyVerificationOrigins(cfg, originFromResponse(resp));
|
||||
let verification;
|
||||
try {
|
||||
verification = await verifyRegistrationResponse({
|
||||
@@ -413,7 +393,7 @@ export class PasskeyService {
|
||||
const cred = await this.webauthnCredentials.findByCredentialId(credId);
|
||||
if (!cred) return { ...AUTH_FAILED };
|
||||
|
||||
const expectedOrigin = this.expectedOrigins(cfg, resp);
|
||||
const expectedOrigin = passkeyVerificationOrigins(cfg, originFromResponse(resp));
|
||||
let verification;
|
||||
try {
|
||||
verification = await verifyAuthenticationResponse({
|
||||
|
||||
@@ -63,13 +63,31 @@ export interface WebauthnConfig {
|
||||
origins: string[];
|
||||
/**
|
||||
* True when `origins` came from operator config (env / admin setting) — such
|
||||
* a list is honored verbatim. False when derived from APP_URL, where the
|
||||
* scheme/port may not match the real browsing origin (TLS-terminating proxy)
|
||||
* and the verifier may widen within the RP ID's scope.
|
||||
* a list is honored verbatim. Derived origins allow the verifier to account
|
||||
* for proxy scheme or port differences on a configured host.
|
||||
*/
|
||||
explicitOrigins: boolean;
|
||||
}
|
||||
|
||||
/** Origins accepted for a registration or sign-in assertion. */
|
||||
export function passkeyVerificationOrigins(config: WebauthnConfig, clientOrigin: string | null): string[] {
|
||||
if (
|
||||
config.explicitOrigins ||
|
||||
!clientOrigin ||
|
||||
config.origins.includes(clientOrigin) ||
|
||||
!originWithinRpScope(clientOrigin, config.rpID)
|
||||
) {
|
||||
return config.origins;
|
||||
}
|
||||
|
||||
// Keep the proxy fallback on the RP host only when a derived origin already uses that host.
|
||||
const clientHost = hostOf(clientOrigin);
|
||||
if (clientHost !== config.rpID || !config.origins.some((origin) => hostOf(origin) === clientHost)) {
|
||||
return config.origins;
|
||||
}
|
||||
return [...config.origins, clientOrigin];
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the WebAuthn Relying Party ID + allowed origins for this deployment.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user