mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 00:43:15 +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.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import type { AppSettingsRepository } from '../../../src/db/repositories/AppSettings.repository';
|
||||
import { WebauthnConfigService, originWithinRpScope } from '../../../src/nest/auth/webauthn-config.service';
|
||||
import {
|
||||
WebauthnConfigService,
|
||||
originWithinRpScope,
|
||||
passkeyVerificationOrigins,
|
||||
} from '../../../src/nest/auth/webauthn-config.service';
|
||||
import { Logger } from '@nestjs/common';
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
@@ -182,6 +186,39 @@ describe('WebauthnConfigService.resolve', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('passkeyVerificationOrigins', () => {
|
||||
it('WAC-019: keeps the application origin when the relying party ID is shared', async () => {
|
||||
appUrlRef.value = 'https://trek.example.org';
|
||||
settingsStore.set('webauthn_rp_id', 'example.org');
|
||||
const config = (await svc.resolve())!;
|
||||
|
||||
expect(passkeyVerificationOrigins(config, 'https://example.org')).toEqual(['https://trek.example.org']);
|
||||
expect(passkeyVerificationOrigins(config, 'https://trek.example.org')).toEqual(['https://trek.example.org']);
|
||||
});
|
||||
|
||||
it('WAC-020: tolerates a proxy scheme and port on the configured RP host', async () => {
|
||||
appUrlRef.value = 'http://trek.example.org:3001';
|
||||
const config = (await svc.resolve())!;
|
||||
|
||||
expect(passkeyVerificationOrigins(config, 'https://trek.example.org:8443')).toEqual([
|
||||
'http://trek.example.org:3001',
|
||||
'https://trek.example.org:8443',
|
||||
]);
|
||||
expect(passkeyVerificationOrigins(config, 'https://other.trek.example.org')).toEqual(config.origins);
|
||||
});
|
||||
|
||||
it('WAC-021: applies an explicit list exactly, including its ports', async () => {
|
||||
settingsStore.set('webauthn_rp_id', 'example.org');
|
||||
settingsStore.set('webauthn_origins', 'https://example.org, https://trek.example.org');
|
||||
const config = (await svc.resolve())!;
|
||||
|
||||
expect(passkeyVerificationOrigins(config, 'https://example.org:8443')).toEqual([
|
||||
'https://example.org',
|
||||
'https://trek.example.org',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
// The scope rule mirrors what the browser enforces for a ceremony: https-only
|
||||
// (http for loopback dev), hostname equal to the RP ID or a subdomain of it.
|
||||
// Anything laxer would weaken origin binding, so each edge is pinned.
|
||||
|
||||
Reference in New Issue
Block a user