Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add URL normalization to add URL to credential logic (#2011)
Leendert de Borsta98e4e6195ec
diff --git a/apps/browser-extension/src/entrypoints/background.ts b/apps/browser-extension/src/entrypoints/background.tsindex 5d6d85f74..73f2e742b 100644--- a/apps/browser-extension/src/entrypoints/background.ts+++ b/apps/browser-extension/src/entrypoints/background.ts@@ -11,7 +11,7 @@ import { handleGetWebAuthnSettings, handleWebAuthnCreate, handleWebAuthnGet, hanimport { handleOpenPopup, handlePopupWithItem, handleOpenPopupCreateCredential, handleToggleContextMenu } from '@/entrypoints/background/PopupMessageHandler';import { handleStoreSavePromptState, handleGetSavePromptState, handleClearSavePromptState, handleStoreLastAutofilled, handleGetLastAutofilled, handleClearLastAutofilled } from '@/entrypoints/background/SavePromptStateHandler';import { handleStoreTwoFactorState, handleGetTwoFactorState, handleClearTwoFactorState } from '@/entrypoints/background/TwoFactorStateHandler';-import { handleCheckAuthStatus, handleClearPersistedFormValues, handleClearSession, handleClearVaultData, handleLockVault, handleCreateItem, handleGetFilteredItems, handleGetSearchItems, handleGetDefaultEmailDomain, handleGetDefaultIdentitySettings, handleGetEncryptionKey, handleGetEncryptionKeyDerivationParams, handleGetPasswordSettings, handleGetPersistedFormValues, handleGetVault, handlePersistFormValues, handleStoreEncryptionKey, handleStoreEncryptionKeyDerivationParams, handleStoreVaultMetadata, handleSyncVault, handleUploadVault, handleGetEncryptedVault, handleStoreEncryptedVault, handleGetSyncState, handleMarkVaultClean, handleGetServerRevision, handleCheckSyncStatus, handleFullVaultSync, handleCheckLoginDuplicate, handleSaveLoginCredential, handleAddUrlToCredential, handleGetLoginSaveSettings, handleSetLoginSaveEnabled, handleGetItemsWithTotp, handleSearchItemsWithTotp, handleGetTotpSecrets, handleGenerateTotpCode, handleSetRecentlySelected, handleGetRecentlySelected } from '@/entrypoints/background/VaultMessageHandler';+import { handleCheckAuthStatus, handleClearPersistedFormValues, handleClearSession, handleClearVaultData, handleLockVault, handleCreateItem, handleGetFilteredItems, handleGetSearchItems, handleGetDefaultEmailDomain, handleGetDefaultIdentitySettings, handleGetEncryptionKey, handleGetEncryptionKeyDerivationParams, handleGetPasswordSettings, handleGetPersistedFormValues, handleGetVault, handlePersistFormValues, handleStoreEncryptionKey, handleStoreEncryptionKeyDerivationParams, handleStoreVaultMetadata, handleSyncVault, handleUploadVault, handleGetEncryptedVault, handleStoreEncryptedVault, handleGetSyncState, handleMarkVaultClean, handleGetServerRevision, handleCheckSyncStatus, handleFullVaultSync, handleCheckLoginDuplicate, handleSaveLoginCredential, handleAddUrlToCredential, handleIsUrlLinkedToCredential, handleGetLoginSaveSettings, handleSetLoginSaveEnabled, handleGetItemsWithTotp, handleSearchItemsWithTotp, handleGetTotpSecrets, handleGenerateTotpCode, handleSetRecentlySelected, handleGetRecentlySelected } from '@/entrypoints/background/VaultMessageHandler';import { EncryptionKeyDerivationParams } from "@/utils/dist/core/models/metadata";import type { LoginResponse } from "@/utils/dist/core/models/webapi";@@ -79,6 +79,7 @@ export default defineBackground({onMessage('CHECK_LOGIN_DUPLICATE', ({ data }) => handleCheckLoginDuplicate(data as { domain: string; username: string }));onMessage('SAVE_LOGIN_CREDENTIAL', ({ data }) => handleSaveLoginCredential(data as { serviceName: string; username: string; password: string; url: string; domain: string; logoBase64?: string }));onMessage('ADD_URL_TO_CREDENTIAL', ({ data }) => handleAddUrlToCredential(data as { itemId: string; url: string }));+ onMessage('IS_URL_LINKED_TO_CREDENTIAL', ({ data }) => handleIsUrlLinkedToCredential(data as { itemId: string; url: string }));onMessage('GET_LOGIN_SAVE_SETTINGS', () => handleGetLoginSaveSettings());onMessage('SET_LOGIN_SAVE_ENABLED', ({ data }) => handleSetLoginSaveEnabled(data as boolean));diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.tsindex 7bb4ff26d..f600220fd 100644--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts@@ -7,7 +7,7 @@ import type { EncryptionKeyDerivationParams } from '@/utils/dist/core/models/metimport { FieldKey, ItemTypes, createSystemField, type Item } from '@/utils/dist/core/models/vault';import type { Vault, VaultResponse, VaultPostResponse } from '@/utils/dist/core/models/webapi';import { EncryptionUtility } from '@/utils/EncryptionUtility';-import { filterItems, AutofillMatchingMode, extractRootDomain } from '@/utils/itemMatcher/ItemMatcher';+import { filterItems, AutofillMatchingMode, extractRootDomain, isUrlAlreadyLinked } from '@/utils/itemMatcher/ItemMatcher';import { LocalPreferencesService } from '@/utils/LocalPreferencesService';import { RecentlySelectedItemService } from '@/utils/RecentlySelectedItemService';import { SqliteClient } from '@/utils/SqliteClient';@@ -1702,14 +1702,12 @@ export async function handleAddUrlToCredential(message: { itemId: string; url: s? existingField.Value: (existingField.Value ? [existingField.Value] : []);- // Check if URL already exists (normalize for comparison)- const normalizedNewUrl = message.url.toLowerCase().replace(/\/$/, '');- const urlExists = existingUrls.some(url =>- url.toLowerCase().replace(/\/$/, '') === normalizedNewUrl- );-- if (urlExists) {- // URL already exists, nothing to do+ /*+ * Compare on host only (subdomain + domain) so trailing slashes, paths,+ * query strings, fragments, `www.`, and http/https differences don't+ * cause us to store a near-duplicate URL on the credential.+ */+ if (await isUrlAlreadyLinked(existingUrls as string[], message.url)) {return { success: true };}@@ -1740,6 +1738,31 @@ export async function handleAddUrlToCredential(message: { itemId: string; url: s}}+/**+ * Check whether a URL is already linked (host-equivalent) to a credential.+ */+export async function handleIsUrlLinkedToCredential(message: { itemId: string; url: string }): Promise<{ linked: boolean }> {+ try {+ const encryptionKey = await handleGetEncryptionKey();+ if (!encryptionKey) {+ return { linked: false };+ }+ const sqliteClient = await createVaultSqliteClient();+ const item = sqliteClient.items.getById(message.itemId);+ if (!item) {+ return { linked: false };+ }+ const urlField = item.Fields?.find(f => f.FieldKey === FieldKey.LoginUrl);+ const existingUrls = urlField+ ? (Array.isArray(urlField.Value) ? urlField.Value : (urlField.Value ? [urlField.Value] : []))+ : [];+ const linked = await isUrlAlreadyLinked(existingUrls as string[], message.url);+ return { linked };+ } catch {+ return { linked: false };+ }+}+/*** Fetch a favicon from a URL and return it as a Uint8Array.* Returns undefined if the fetch fails or returns an invalid response.diff --git a/apps/browser-extension/src/entrypoints/content.ts b/apps/browser-extension/src/entrypoints/content.tsindex 611954c70..47be24ce4 100644--- a/apps/browser-extension/src/entrypoints/content.ts+++ b/apps/browser-extension/src/entrypoints/content.ts@@ -388,15 +388,28 @@ function initializeLoginDetector(container: HTMLElement): void {}, 'background') as { success: boolean; credential: LastAutofilledCredential | null };if (lastAutofilledResponse.success && lastAutofilledResponse.credential) {- // User used an existing credential: offer to add URL to it- showAddUrlPrompt(container, {- login,- existingCredential: lastAutofilledResponse.credential,- onAddUrl: handleAddUrlToCredential,- onDismiss: handleSavePromptDismiss,- autoDismissMs,- });- return;+ /*+ * Skip the prompt entirely when the submitted URL is already linked+ * (host-equivalent) to the autofilled credential. Without this guard+ * the user would see "Add URL?", click it, and the background dedup+ * would silently no-op — confusing UX.+ */+ const linkCheck = await sendMessage('IS_URL_LINKED_TO_CREDENTIAL', {+ itemId: lastAutofilledResponse.credential.itemId,+ url: login.url,+ }, 'background') as { linked: boolean };++ if (!linkCheck.linked) {+ // User used an existing credential: offer to add URL to it+ showAddUrlPrompt(container, {+ login,+ existingCredential: lastAutofilledResponse.credential,+ onAddUrl: handleAddUrlToCredential,+ onDismiss: handleSavePromptDismiss,+ autoDismissMs,+ });+ return;+ }}} catch {// If check fails, fall back to normal save promptdiff --git a/apps/browser-extension/src/utils/itemMatcher/ItemMatcher.ts b/apps/browser-extension/src/utils/itemMatcher/ItemMatcher.tsindex 0e54f4716..8b3a0f7fc 100644--- a/apps/browser-extension/src/utils/itemMatcher/ItemMatcher.ts+++ b/apps/browser-extension/src/utils/itemMatcher/ItemMatcher.ts@@ -94,3 +94,38 @@ export async function extractRootDomain(domain: string): Promise<string> {await ensureInit();return wasmExtractRootDomain(domain);}++/**+ * Reduce a URL to a comparison key (host only: subdomain + domain).+ * Strips scheme, `www.`, path, query, fragment, and trailing slash so that+ * `https://my.base.com/`, `https://my.base.com`, and+ * `http://www.my.base.com/login?x=1` all collapse to `my.base.com`.+ *+ * Falls back to the lowercased trimmed input when host extraction yields+ * no domain (the Rust extractor rejects reversed-TLD strings like+ * `com.example.app`), so app bundle identifiers still compare by exact match.+ */+export async function getUrlComparisonKey(url: string): Promise<string> {+ const trimmed = url.trim().toLowerCase();+ if (!trimmed) {+ return trimmed;+ }+ const domain = await extractDomain(trimmed);+ return domain.length > 0 ? domain : trimmed;+}++/**+ * True if `newUrl` is already represented in `existingUrls` (host-equivalent).+ */+export async function isUrlAlreadyLinked(existingUrls: string[], newUrl: string): Promise<boolean> {+ const newKey = await getUrlComparisonKey(newUrl);+ if (!newKey) {+ return false;+ }+ for (const existing of existingUrls) {+ if (await getUrlComparisonKey(existing) === newKey) {+ return true;+ }+ }+ return false;+}diff --git a/apps/mobile-app/ios/VaultUI/Selection/CredentialProviderView.swift b/apps/mobile-app/ios/VaultUI/Selection/CredentialProviderView.swiftindex 684bf9f15..8a5039217 100644--- a/apps/mobile-app/ios/VaultUI/Selection/CredentialProviderView.swift+++ b/apps/mobile-app/ios/VaultUI/Selection/CredentialProviderView.swift@@ -411,8 +411,12 @@ public class CredentialProviderViewModel: ObservableObject {}// If the requesting URL is already on this credential, we don't need to prompt again.- let alreadyLinked = credential.serviceUrls.contains { existing in- AutofillUrlNormalizer.normalize(existing).caseInsensitiveCompare(serviceUrl) == .orderedSame+ // Compare on host only (subdomain + domain) so trailing slashes, paths,+ // query strings, fragments, `www.`, and `http`/`https` differences don't+ // cause us to propose linking a duplicate URL.+ let serviceKey = AutofillUrlNormalizer.comparisonKey(serviceUrl)+ let alreadyLinked = !serviceKey.isEmpty && credential.serviceUrls.contains { existing in+ AutofillUrlNormalizer.comparisonKey(existing) == serviceKey}if alreadyLinked {selectionHandler(username, password)diff --git a/apps/mobile-app/ios/VaultUI/Selection/Utils/AutofillUrlNormalizer.swift b/apps/mobile-app/ios/VaultUI/Selection/Utils/AutofillUrlNormalizer.swiftindex ae7c1659d..fc9f21c6b 100644--- a/apps/mobile-app/ios/VaultUI/Selection/Utils/AutofillUrlNormalizer.swift+++ b/apps/mobile-app/ios/VaultUI/Selection/Utils/AutofillUrlNormalizer.swift@@ -1,4 +1,5 @@import Foundation+import RustCoreFramework/// Normalizes service URLs supplied by the iOS autofill subsystem before/// we store them on a credential or compare them to existing credential URLs.@@ -8,7 +9,7 @@ public enum AutofillUrlNormalizer {/// already have a non-http scheme like `chrome://` or `app://`.private static let schemePrefixPattern = "^[a-zA-Z][a-zA-Z0-9+.-]*://"- /// Normalize a service URL/identifier for storage and comparison.+ /// Normalize a service URL/identifier for storage and display./// - Parameter raw: The URL or bare domain as supplied by iOS, possibly/// already lowercased and with a trailing path/query/fragment./// - Returns: A canonical URL string (`https://host[/path]`) suitable for@@ -31,6 +32,32 @@ public enum AutofillUrlNormalizer {}components.query = nilcomponents.fragment = nil- return components.url?.absoluteString ?? withScheme+ let rebuilt = components.url?.absoluteString ?? withScheme+ // Drop a trailing slash so `https://host/` and `https://host` normalize+ // to the same string..+ if rebuilt.hasSuffix("/") && rebuilt.count > 1 {+ return String(rebuilt.dropLast())+ }+ return rebuilt+ }++ /// Comparison key used to decide whether a credential is already linked to+ /// a given service URL. Reduces a URL to its host (subdomain + domain),+ /// stripping scheme, `www.`, path, query, fragment, and trailing slash, so+ /// that `https://my.base.com/`, `https://my.base.com`, and+ /// `http://www.my.base.com/login?x=1` all compare equal.+ ///+ /// Falls back to the lowercased, trimmed input when host extraction yields+ /// no domain (e.g. iOS app bundle identifiers like `com.example.app`,+ /// which the Rust extractor intentionally rejects as reversed-TLD package+ /// names). For those identifiers an exact-string compare is the only safe+ /// option.+ public static func comparisonKey(_ raw: String) -> String {+ let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()+ guard !trimmed.isEmpty else {+ return trimmed+ }+ let domain = extractDomain(url: trimmed)+ return domain.isEmpty ? trimmed : domain}}