Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

fix(extension): validate WebAuthn bridge origin and RP ID

AmarBegoa60d0966e66e
diff --git a/apps/browser-extension/src/entrypoints/background.ts b/apps/browser-extension/src/entrypoints/background.ts
index c5dfc24dd..e32ae0ac0 100644
--- a/apps/browser-extension/src/entrypoints/background.ts
+++ b/apps/browser-extension/src/entrypoints/background.ts
@@ -13,9 +13,72 @@ import { handleCheckAuthStatus, handleClearPersistedFormValues, handleClearSessi
import { LocalPreferencesService } from '@/utils/LocalPreferencesService';
import { onMessage, sendMessage } from "@/utils/messaging/ExtensionMessaging";
+import { validateWebAuthnRequest } from '@/utils/passkey/WebAuthnRequestValidation';
import { defineBackground, browser } from '#imports';
+type WebAuthnMessageSender = {
+ origin?: string;
+ url?: string;
+ tab?: {
+ url?: string;
+ };
+};
+
+type TrustedWebAuthnSenderContext = {
+ origin: string;
+ host: string;
+};
+
+function getTrustedWebAuthnSenderContext(sender: WebAuthnMessageSender): TrustedWebAuthnSenderContext | null {
+ const senderOrigin = typeof sender.origin === 'string' && sender.origin !== 'null'
+ ? sender.origin
+ : undefined;
+ const senderUrl = senderOrigin ?? sender.url ?? sender.tab?.url;
+
+ if (!senderUrl) {
+ return null;
+ }
+
+ try {
+ const url = new URL(senderUrl);
+ if (url.protocol !== 'https:' && !(url.protocol === 'http:' && url.hostname === 'localhost')) {
+ return null;
+ }
+
+ return {
+ origin: url.origin,
+ host: url.hostname,
+ };
+ } catch {
+ return null;
+ }
+}
+
+function handleValidatedWebAuthnCreate(data: any, sender: WebAuthnMessageSender): Promise<any> | { fallback: true } {
+ const senderContext = getTrustedWebAuthnSenderContext(sender);
+ if (!senderContext || !validateWebAuthnRequest('create', data, senderContext.origin, senderContext.host)) {
+ return { fallback: true };
+ }
+
+ return handleWebAuthnCreate({
+ ...data,
+ origin: senderContext.origin,
+ });
+}
+
+function handleValidatedWebAuthnGet(data: any, sender: WebAuthnMessageSender): Promise<any> | { fallback: true } {
+ const senderContext = getTrustedWebAuthnSenderContext(sender);
+ if (!senderContext || !validateWebAuthnRequest('get', data, senderContext.origin, senderContext.host)) {
+ return { fallback: true };
+ }
+
+ return handleWebAuthnGet({
+ ...data,
+ origin: senderContext.origin,
+ });
+}
+
/*
* Register alarm listener at top-level scope.
* [..] Move the event listener registration to the top level of your script.
@@ -154,8 +217,8 @@ export default defineBackground({
// Passkey/WebAuthn management messages
onMessage('GET_WEBAUTHN_SETTINGS', ({ data }) => handleGetWebAuthnSettings(data));
- onMessage('WEBAUTHN_CREATE', ({ data }) => handleWebAuthnCreate(data));
- onMessage('WEBAUTHN_GET', ({ data }) => handleWebAuthnGet(data));
+ onMessage('WEBAUTHN_CREATE', ({ data, sender }) => handleValidatedWebAuthnCreate(data, sender));
+ onMessage('WEBAUTHN_GET', ({ data, sender }) => handleValidatedWebAuthnGet(data, sender));
onMessage('PASSKEY_POPUP_RESPONSE', ({ data }) => handlePasskeyPopupResponse(data));
onMessage('GET_REQUEST_DATA', ({ data }) => handleGetRequestData(data));
diff --git a/apps/browser-extension/src/entrypoints/contentScript/WebAuthnInterceptor.ts b/apps/browser-extension/src/entrypoints/contentScript/WebAuthnInterceptor.ts
index cec74d854..e94210059 100644
--- a/apps/browser-extension/src/entrypoints/contentScript/WebAuthnInterceptor.ts
+++ b/apps/browser-extension/src/entrypoints/contentScript/WebAuthnInterceptor.ts
@@ -5,6 +5,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { sendMessage } from '@/utils/messaging/ExtensionMessaging';
+import type { WebAuthnCreateEventDetail, WebAuthnGetEventDetail } from '@/utils/passkey/webauthn.types';
+import {
+ cloneWebAuthnEventDetail,
+ validateWebAuthnEventDetail
+} from '@/utils/passkey/WebAuthnRequestValidation';
import { browser } from '#imports';
@@ -65,7 +70,8 @@ export async function initializeWebAuthnInterceptor(_ctx: any): Promise<void> {
// Listen for WebAuthn create events from the page
window.addEventListener('aliasvault:webauthn:create', async (event: any) => {
- const { requestId, publicKey, origin } = event.detail;
+ const detail = cloneWebAuthnEventDetail<WebAuthnCreateEventDetail>(event.detail);
+ const requestId = typeof detail?.requestId === 'string' ? detail.requestId : undefined;
/**
* Helper to dispatch event with Firefox compatibility
@@ -93,6 +99,16 @@ export async function initializeWebAuthnInterceptor(_ctx: any): Promise<void> {
};
try {
+ if (!validateWebAuthnEventDetail('create', detail, window.location.origin, window.location.hostname)) {
+ dispatchResponse({
+ requestId,
+ fallback: true
+ });
+ return;
+ }
+
+ const { publicKey, origin } = detail;
+
/**
* Note: We don't block create (registration) requests based on page readiness.
* Registration is always user-initiated (button click), so it's never spurious.
@@ -146,7 +162,8 @@ export async function initializeWebAuthnInterceptor(_ctx: any): Promise<void> {
// Listen for WebAuthn get events from the page
window.addEventListener('aliasvault:webauthn:get', async (event: any) => {
- const { requestId, publicKey, origin } = event.detail;
+ const detail = cloneWebAuthnEventDetail<WebAuthnGetEventDetail>(event.detail);
+ const requestId = typeof detail?.requestId === 'string' ? detail.requestId : undefined;
/**
* Helper to dispatch event with Firefox compatibility
@@ -174,6 +191,16 @@ export async function initializeWebAuthnInterceptor(_ctx: any): Promise<void> {
};
try {
+ if (!validateWebAuthnEventDetail('get', detail, window.location.origin, window.location.hostname)) {
+ dispatchResponse({
+ requestId,
+ fallback: true
+ });
+ return;
+ }
+
+ const { publicKey, origin } = detail;
+
// Block requests if page isn't ready (prevents prefetch/autocomplete popups)
if (!isPageReadyForWebAuthn()) {
dispatchResponse({
diff --git a/apps/browser-extension/src/entrypoints/contentScript/__tests__/WebAuthnInterceptor.test.ts b/apps/browser-extension/src/entrypoints/contentScript/__tests__/WebAuthnInterceptor.test.ts
new file mode 100644
index 000000000..ca680dde9
--- /dev/null
+++ b/apps/browser-extension/src/entrypoints/contentScript/__tests__/WebAuthnInterceptor.test.ts
@@ -0,0 +1,115 @@
+import { describe, expect, it } from 'vitest';
+
+import type { WebAuthnGetEventDetail } from '@/utils/passkey/webauthn.types';
+import {
+ cloneWebAuthnEventDetail,
+ isRpIdAllowedForHost,
+ validateWebAuthnEventDetail,
+ validateWebAuthnRequest
+} from '@/utils/passkey/WebAuthnRequestValidation';
+
+describe('WebAuthnInterceptor request validation', () => {
+ it('allows exact RP ID matches', () => {
+ expect(isRpIdAllowedForHost('example.com', 'example.com')).toBe(true);
+ });
+
+ it('allows parent-domain RP IDs', () => {
+ expect(isRpIdAllowedForHost('example.com', 'login.example.com')).toBe(true);
+ });
+
+ it('rejects sibling or unrelated RP IDs', () => {
+ expect(isRpIdAllowedForHost('accounts.example.com', 'evil.example.com')).toBe(false);
+ expect(isRpIdAllowedForHost('example.com', 'evil.com')).toBe(false);
+ });
+
+ it('rejects a forged origin in create requests', () => {
+ expect(validateWebAuthnEventDetail('create', {
+ requestId: 'request-1',
+ origin: 'https://accounts.example.com',
+ publicKey: {
+ rp: {
+ id: 'accounts.example.com',
+ name: 'Example',
+ },
+ user: {
+ id: 'dXNlcg',
+ displayName: 'User',
+ },
+ challenge: 'Y2hhbGxlbmdl',
+ },
+ }, 'https://evil.example', 'evil.example')).toBe(false);
+ });
+
+ it('rejects a forged RP ID in get requests even when origin is honest', () => {
+ expect(validateWebAuthnEventDetail('get', {
+ requestId: 'request-1',
+ origin: 'https://evil.example',
+ publicKey: {
+ challenge: 'Y2hhbGxlbmdl',
+ rpId: 'accounts.example.com',
+ },
+ }, 'https://evil.example', 'evil.example')).toBe(false);
+ });
+
+ it('allows get requests without explicit RP ID for the current origin', () => {
+ expect(validateWebAuthnEventDetail('get', {
+ requestId: 'request-1',
+ origin: 'https://login.example.com',
+ publicKey: {
+ challenge: 'Y2hhbGxlbmdl',
+ },
+ }, 'https://login.example.com', 'login.example.com')).toBe(true);
+ });
+
+ it('clones page event details before validation and forwarding', () => {
+ const rawDetail = {
+ requestId: 'request-1',
+ origin: 'https://login.example.com',
+ publicKey: {
+ challenge: 'Y2hhbGxlbmdl',
+ },
+ };
+
+ const clonedDetail = cloneWebAuthnEventDetail<WebAuthnGetEventDetail>(rawDetail);
+ rawDetail.origin = 'https://attacker.example.com';
+ rawDetail.publicKey = {
+ challenge: 'bXV0YXRlZA',
+ rpId: 'attacker.example.com',
+ } as typeof rawDetail.publicKey;
+
+ expect(clonedDetail).toEqual({
+ requestId: 'request-1',
+ origin: 'https://login.example.com',
+ publicKey: {
+ challenge: 'Y2hhbGxlbmdl',
+ },
+ });
+ expect(validateWebAuthnEventDetail(
+ 'get',
+ clonedDetail,
+ 'https://login.example.com',
+ 'login.example.com',
+ )).toBe(true);
+ });
+
+ it('rejects malformed request fields after cloning', () => {
+ expect(validateWebAuthnEventDetail('get', {
+ requestId: 'request-1',
+ origin: 'https://login.example.com',
+ publicKey: {
+ challenge: 123,
+ },
+ } as unknown as WebAuthnGetEventDetail, 'https://login.example.com', 'login.example.com')).toBe(false);
+ });
+
+ it('rejects a background request when sender origin and payload origin differ', () => {
+ expect(validateWebAuthnRequest('get', {
+ origin: 'https://victim.example.com',
+ publicKey: {
+ challenge: 'Y2hhbGxlbmdl',
+ rpId: 'victim.example.com',
+ },
+ }, 'https://attacker.example.com', 'attacker.example.com')).toBe(false);
+ });
+});
diff --git a/apps/browser-extension/src/utils/passkey/WebAuthnRequestValidation.ts b/apps/browser-extension/src/utils/passkey/WebAuthnRequestValidation.ts
new file mode 100644
index 000000000..bdf85ef9f
--- /dev/null
+++ b/apps/browser-extension/src/utils/passkey/WebAuthnRequestValidation.ts
@@ -0,0 +1,126 @@
+import type { WebAuthnCreateEventDetail, WebAuthnGetEventDetail } from '@/utils/passkey/webauthn.types';
+
+type WebAuthnRequestType = 'create' | 'get';
+type WebAuthnBridgeDetail = WebAuthnCreateEventDetail | WebAuthnGetEventDetail;
+
+type WebAuthnBridgeRequest = {
+ origin?: unknown;
+ publicKey?: unknown;
+};
+
+/**
+ * Normalize a host or RP ID for WebAuthn comparison.
+ */
+function normalizeWebAuthnHost(value: string | undefined): string | null {
+ if (!value) {
+ return null;
+ }
+
+ const normalized = value.trim().toLowerCase().replace(/\.$/, '');
+ if (!normalized || normalized.includes('/') || normalized.includes(':')) {
+ return null;
+ }
+
+ return normalized;
+}
+
+function isObject(value: unknown): value is Record<string, unknown> {
+ return typeof value === 'object' && value !== null;
+}
+
+/**
+ * Check whether an RP ID is valid for the current origin host.
+ * WebAuthn allows the RP ID to be the current host or a parent domain.
+ * This prevents a page from asking AliasVault to sign for another RP.
+ */
+export function isRpIdAllowedForHost(rpId: string | undefined, host: string): boolean {
+ const normalizedHost = normalizeWebAuthnHost(host);
+ const normalizedRpId = normalizeWebAuthnHost(rpId);
+
+ if (!normalizedHost || !normalizedRpId) {
+ return false;
+ }
+
+ return normalizedHost === normalizedRpId || normalizedHost.endsWith(`.${normalizedRpId}`);
+}
+
+/**
+ * Clone page-provided WebAuthn event data into extension-owned plain data.
+ * The page controls CustomEvent.detail, so do not validate one object and later
+ * forward the original object after an await.
+ */
+export function cloneWebAuthnEventDetail<T extends WebAuthnBridgeDetail>(detail: unknown): T | undefined {
+ if (!isObject(detail)) {
+ return undefined;
+ }
+
+ try {
+ return JSON.parse(JSON.stringify(detail)) as T;
+ } catch {
+ return undefined;
+ }
+}
+
+/**
+ * Validate a WebAuthn request before forwarding it to the passkey signing flow.
+ */
+export function validateWebAuthnRequest(
+ type: WebAuthnRequestType,
+ request: WebAuthnBridgeRequest | undefined,
+ expectedOrigin: string,
+ currentHost: string,
+): boolean {
+ if (
+ !isObject(request) ||
+ typeof request.origin !== 'string' ||
+ !isObject(request.publicKey) ||
+ request.origin !== expectedOrigin
+ ) {
+ return false;
+ }
+
+ if (type === 'create') {
+ const publicKey = request.publicKey;
+ const user = publicKey.user;
+ const rp = publicKey.rp;
+ if (
+ !isObject(user) ||
+ typeof user.id !== 'string' ||
+ typeof publicKey.challenge !== 'string'
+ ) {
+ return false;
+ }
+
+ if (rp !== undefined && !isObject(rp)) {
+ return false;
+ }
+
+ const rpId = isObject(rp) ? rp.id : undefined;
+ return rpId === undefined || (typeof rpId === 'string' && isRpIdAllowedForHost(rpId, currentHost));
+ }
+
+ const publicKey = request.publicKey;
+ if (typeof publicKey.challenge !== 'string') {
+ return false;
+ }
+
+ const rpId = publicKey.rpId;
+ return rpId === undefined || (typeof rpId === 'string' && isRpIdAllowedForHost(rpId, currentHost));
+}
+
+/**
+ * Validate a WebAuthn bridge request from the page before forwarding it to the background script.
+ * The page context can dispatch AliasVault's custom events directly, so security decisions must
+ * use the content script's current window location rather than trusting event.detail.origin.
+ */
+export function validateWebAuthnEventDetail(
+ type: WebAuthnRequestType,
+ detail: WebAuthnBridgeDetail | undefined,
+ expectedOrigin: string,
+ currentHost: string,
+): detail is WebAuthnBridgeDetail {
+ return (
+ typeof detail?.requestId === 'string' &&
+ validateWebAuthnRequest(type, detail, expectedOrigin, currentHost)
+ );
+}