Skip to content

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

Integrate browser extension save prompt check with public suffix list (#2161)

Leendert de Borst52e382937a11
diff --git a/apps/browser-extension/src/entrypoints/background.ts b/apps/browser-extension/src/entrypoints/background.ts
index 269566b9d..deaebb2b8 100644
--- a/apps/browser-extension/src/entrypoints/background.ts
+++ b/apps/browser-extension/src/entrypoints/background.ts
@@ -330,6 +330,7 @@ export default defineBackground({
// Remember login save state (for surviving page navigation)
onMessage('STORE_SAVE_PROMPT_STATE', ({ data, sender }) => handleStoreSavePromptState({ tabId: sender.tab!.id!, state: data }));
+ onMessage('GET_SAVE_PROMPT_STATE', ({ data, sender }) => handleGetSavePromptState({ tabId: sender.tab!.id!, ...data }));
onMessage('CLEAR_SAVE_PROMPT_STATE', ({ sender }) => handleClearSavePromptState({ tabId: sender.tab!.id! }));
// Track last autofilled credential (for "Add URL to existing credential" prompt)
diff --git a/apps/browser-extension/src/entrypoints/background/SavePromptStateHandler.ts b/apps/browser-extension/src/entrypoints/background/SavePromptStateHandler.ts
index 6955456e1..9249e0893 100644
--- a/apps/browser-extension/src/entrypoints/background/SavePromptStateHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/SavePromptStateHandler.ts
@@ -4,6 +4,8 @@
* Also tracks the last autofilled credential per tab for the "Add URL" feature.
*/
+import { extractRootDomain } from '@aliasvault/client/rust/RustCore';
+
import type { SavePromptPersistedState, LastAutofilledCredential } from '@/utils/loginDetector';
/** In-memory storage for save prompt state, keyed by tab ID */
@@ -54,14 +56,31 @@ export function handleStoreSavePromptState(
}
/**
- * Get save prompt state for a tab.
- * @param data - The tab ID.
+ * Whether a save prompt stored on `stateDomain` may be shown on `currentDomain` (same host or same root domain).
+ * @param stateDomain - The hostname the prompt was persisted on.
+ * @param currentDomain - The hostname of the page asking for the prompt.
+ * @returns True when the prompt may be restored.
+ */
+async function isRelatedDomain(stateDomain: string, currentDomain: string): Promise<boolean> {
+ if (stateDomain === currentDomain) {
+ return true;
+ }
+ if (!stateDomain || !currentDomain) {
+ return false;
+ }
+ const [stateRoot, currentRoot] = await Promise.all([extractRootDomain(stateDomain), extractRootDomain(currentDomain)]);
+ return stateRoot === currentRoot;
+}
+
+/**
+ * Get save prompt state for a tab, or null when it expired or belongs to an unrelated domain.
+ * @param data - The tab ID and the hostname of the page asking.
* @returns The stored state or null.
*/
-export function handleGetSavePromptState(
- data: { tabId: number }
-): { success: boolean; state: SavePromptPersistedState | null } {
- const { tabId } = data;
+export async function handleGetSavePromptState(
+ data: { tabId: number; currentDomain: string }
+): Promise<{ success: boolean; state: SavePromptPersistedState | null }> {
+ const { tabId, currentDomain } = data;
const state = savePromptStateByTab.get(tabId) || null;
if (state) {
@@ -69,14 +88,9 @@ export function handleGetSavePromptState(
const elapsedSinceSave = Date.now() - state.savedAt;
const adjustedRemainingTime = state.remainingTimeMs - elapsedSinceSave;
- if (adjustedRemainingTime <= 0) {
- // Timer expired, clean up
- savePromptStateByTab.delete(tabId);
- const timer = stateExpiryTimers.get(tabId);
- if (timer) {
- clearTimeout(timer);
- stateExpiryTimers.delete(tabId);
- }
+ if (adjustedRemainingTime <= 0 || !(await isRelatedDomain(state.domain, currentDomain))) {
+ // Timer expired or the tab navigated to an unrelated site, clean up
+ handleClearSavePromptState({ tabId });
return { success: true, state: null };
}
diff --git a/apps/browser-extension/src/entrypoints/contentScript/SavePrompt.ts b/apps/browser-extension/src/entrypoints/contentScript/SavePrompt.ts
index 8284cd07d..7cfdc2246 100644
--- a/apps/browser-extension/src/entrypoints/contentScript/SavePrompt.ts
+++ b/apps/browser-extension/src/entrypoints/contentScript/SavePrompt.ts
@@ -758,7 +758,7 @@ function handleBeforeUnload(): void {
*/
export async function getPersistedSavePromptState(): Promise<SavePromptPersistedState | null> {
try {
- const response = await sendMessage('GET_SAVE_PROMPT_STATE');
+ const response = await sendMessage('GET_SAVE_PROMPT_STATE', { currentDomain: window.location.hostname });
if (!response.success || !response.state) {
return null;
@@ -766,23 +766,6 @@ export async function getPersistedSavePromptState(): Promise<SavePromptPersisted
const state = response.state;
- // Validate the state is still relevant
- const currentDomain = window.location.hostname;
-
- // Check if we're on the same domain (or a related domain after redirect)
- if (state.domain !== currentDomain) {
- // Allow if we're on a subdomain or parent domain
- const isRelatedDomain = currentDomain.endsWith(`.${state.domain}`) ||
- state.domain.endsWith(`.${currentDomain}`) ||
- // Also allow if they share the same base domain (e.g., login.example.com -> app.example.com)
- getBaseDomain(currentDomain) === getBaseDomain(state.domain);
-
- if (!isRelatedDomain) {
- await clearPersistedSavePromptState();
- return null;
- }
- }
-
// The background script already adjusts the remaining time, but check if expired
if (state.remainingTimeMs <= 0) {
await clearPersistedSavePromptState();
@@ -807,18 +790,6 @@ export async function clearPersistedSavePromptState(): Promise<void> {
}
}
-/**
- * Extract the base domain from a hostname (e.g., "login.example.com" -> "example.com").
- */
-function getBaseDomain(hostname: string): string {
- const parts = hostname.split('.');
- if (parts.length <= 2) {
- return hostname;
- }
- // Return the last two parts (this is a simple heuristic, doesn't handle all TLDs)
- return parts.slice(-2).join('.');
-}
-
/**
* Create and show the "Add URL to existing credential" prompt banner.
* This is shown when a user logs in using an autofilled credential that doesn't have a URL match.
diff --git a/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts b/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts
index d1b07bfcc..5d46abf81 100644
--- a/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts
+++ b/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts
@@ -79,7 +79,7 @@ export interface IExtensionMessageProtocol {
GET_MATCHING_PASSKEYS(data: { rpId: string; allowCredentialIds?: string[] }): MatchingPasskeysResponse;
GET_PERSISTED_FORM_VALUES(): any | null;
GET_REQUEST_DATA(data: any): PendingPasskeyRequest | null;
- GET_SAVE_PROMPT_STATE(): { success: boolean; state: SavePromptPersistedState | null };
+ GET_SAVE_PROMPT_STATE(data: { currentDomain: string }): { success: boolean; state: SavePromptPersistedState | null };
GET_SEARCH_ITEMS(data: { searchTerm: string }): ItemsResponse;
GET_SYNC_STATE(): VaultSyncState;
GET_TOTP_SECRETS(data: { items: ItemRef[] }): { success: boolean; secrets?: Record<string, TotpSecret>; error?: string };