Skip to content

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

Strip pathnames from to be stored URLs in browser extension (#2231)

Leendert de Borst9d74de5ae021
diff --git a/apps/browser-extension/src/entrypoints/background/PopupMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/PopupMessageHandler.ts
index 33b3c104b..bfe0a31b2 100644
--- a/apps/browser-extension/src/entrypoints/background/PopupMessageHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/PopupMessageHandler.ts
@@ -2,6 +2,7 @@
import { setupContextMenus } from '@/entrypoints/background/ContextMenu';
import { LocalPreferencesService } from '@/utils/LocalPreferencesService';
+import { ServiceDetectionUtility } from '@/utils/serviceDetection/ServiceDetectionUtility';
import { BoolResponse } from '@/utils/types/messaging/BoolResponse';
import { browser } from '#imports';
@@ -48,19 +49,8 @@ export function handleOpenPopupCreateCredential(message: any, sender?: any) : Pr
const sourceTabId = sender?.tab?.id;
const elementIdentifier = message.elementIdentifier;
- // Derive the service URL (origin + pathname) from the page URL passed by the content script.
- let serviceUrl = '';
- if (currentUrl) {
- try {
- const url = new URL(currentUrl);
- // Only include http/https URLs
- if (url.protocol === 'http:' || url.protocol === 'https:') {
- serviceUrl = url.origin + url.pathname;
- }
- } catch (error) {
- console.error('Error parsing current URL:', error);
- }
- }
+ // Derive the service URL (origin only) from the page URL passed by the content script.
+ const serviceUrl = currentUrl ? ServiceDetectionUtility.sanitizeUrl(currentUrl) : '';
// Set a localStorage flag to skip restoring previously persisted form values as we want to start fresh with this explicit create item request.
await LocalPreferencesService.setSkipFormRestore(true);
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
index 6ebd2cd84..2b9f9b71e 100644
--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
@@ -10,6 +10,7 @@ import { EncryptionUtility } from '@/utils/EncryptionUtility';
import { LocalPreferencesService } from '@/utils/LocalPreferencesService';
import { RecentlySelectedItemService } from '@/utils/RecentlySelectedItemService';
import { filterItems, AutofillMatchingMode, extractRootDomain, isUrlAlreadyLinked, generatePassword } from '@/utils/RustCore';
+import { ServiceDetectionUtility } from '@/utils/serviceDetection/ServiceDetectionUtility';
import { SqliteClient } from '@/utils/SqliteClient';
import { getItemWithFallback } from '@/utils/StorageUtility';
import { ApiAuthError } from '@/utils/types/errors/ApiAuthError';
@@ -1684,6 +1685,7 @@ export async function handleAddUrlToCredential(message: { itemId: string; url: s
try {
const sqliteClient = await createVaultSqliteClient();
+ const url = ServiceDetectionUtility.sanitizeUrl(message.url) || message.url;
// Get the existing item
const item = sqliteClient.items.getById(message.itemId);
@@ -1695,26 +1697,23 @@ export async function handleAddUrlToCredential(message: { itemId: string; url: s
const urlFieldIndex = item.Fields?.findIndex(f => f.FieldKey === FieldKey.LoginUrl);
if (urlFieldIndex !== undefined && urlFieldIndex >= 0) {
- // URL field exists - add to it
const existingField = item.Fields![urlFieldIndex];
- const existingUrls = Array.isArray(existingField.Value)
- ? existingField.Value
- : (existingField.Value ? [existingField.Value] : []);
+ const existingUrls = Array.isArray(existingField.Value) ? existingField.Value : (existingField.Value ? [existingField.Value] : []);
/*
* 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)) {
+ if (await isUrlAlreadyLinked(existingUrls as string[], url)) {
return { success: true };
}
// Add the new URL
- item.Fields![urlFieldIndex].Value = [...existingUrls, message.url];
+ item.Fields![urlFieldIndex].Value = [...existingUrls, url];
} else {
// No URL field exists - create one
- const newUrlField = createSystemField(FieldKey.LoginUrl, { value: message.url });
+ const newUrlField = createSystemField(FieldKey.LoginUrl, { value: url });
if (!item.Fields) {
item.Fields = [];
}
diff --git a/apps/browser-extension/src/entrypoints/popup/hooks/useServiceDetection.ts b/apps/browser-extension/src/entrypoints/popup/hooks/useServiceDetection.ts
index 5c06ce5c9..3db53728d 100644
--- a/apps/browser-extension/src/entrypoints/popup/hooks/useServiceDetection.ts
+++ b/apps/browser-extension/src/entrypoints/popup/hooks/useServiceDetection.ts
@@ -11,7 +11,7 @@ import { browser } from '#imports';
type ServiceDetectionResult = {
/** Detected service name (e.g., "GitHub", "Google") */
serviceName: string;
- /** Detected service URL (origin + pathname) */
+ /** Detected service URL (origin only: scheme + host + port) */
serviceUrl: string;
/** Alternative service name suggestions derived from the page title/domain */
suggestedNames: string[];
diff --git a/apps/browser-extension/src/utils/serviceDetection/ServiceDetectionUtility.ts b/apps/browser-extension/src/utils/serviceDetection/ServiceDetectionUtility.ts
index 8e97be4b4..da8c03ac3 100644
--- a/apps/browser-extension/src/utils/serviceDetection/ServiceDetectionUtility.ts
+++ b/apps/browser-extension/src/utils/serviceDetection/ServiceDetectionUtility.ts
@@ -5,6 +5,24 @@ import { FormDetector } from '../formDetector/FormDetector';
* Shared between content script and popup dashboard
*/
export class ServiceDetectionUtility {
+ /**
+ * Sanitize a raw URL down to its origin (scheme + host + port), stripping the
+ * path, query string and fragment.
+ */
+ public static sanitizeUrl(rawUrl: string): string {
+ try {
+ const url = new URL(rawUrl);
+ // Only include http/https URLs
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
+ return '';
+ }
+ return url.origin;
+ } catch (error) {
+ console.error('Error parsing URL:', error);
+ return '';
+ }
+ }
+
/**
* Get service information from the current page
*/
@@ -15,21 +33,8 @@ export class ServiceDetectionUtility {
// Get the current URL
const currentUrl = location.href;
- // Process the URL to extract service URL (origin + pathname)
- let serviceUrl = '';
- try {
- const url = new URL(currentUrl);
- // Only include http/https URLs
- if (url.protocol === 'http:' || url.protocol === 'https:') {
- serviceUrl = url.origin + url.pathname;
- // Remove trailing slash
- if (serviceUrl.endsWith('/')) {
- serviceUrl = serviceUrl.slice(0, -1);
- }
- }
- } catch (error) {
- console.error('Error parsing current URL:', error);
- }
+ // Process the URL to extract service URL (origin only)
+ const serviceUrl = this.sanitizeUrl(currentUrl);
return {
suggestedNames,
@@ -61,15 +66,8 @@ export class ServiceDetectionUtility {
// Use FormDetector logic for service name detection
const suggestedNames = FormDetector.getSuggestedServiceName(mockDocument, location);
- // Get service URL (origin + pathname)
- let serviceUrl = '';
- if (url.protocol === 'http:' || url.protocol === 'https:') {
- serviceUrl = url.origin + url.pathname;
- // Remove trailing slash
- if (serviceUrl.endsWith('/')) {
- serviceUrl = serviceUrl.slice(0, -1);
- }
- }
+ // Get service URL (origin only)
+ const serviceUrl = this.sanitizeUrl(tabUrl);
return {
suggestedNames,
@@ -127,7 +125,7 @@ export type ServiceInfo = {
suggestedNames: string[];
/** Current page URL */
currentUrl: string;
- /** Service URL (origin + pathname) */
+ /** Service URL (origin only: scheme + host + port) */
serviceUrl: string;
/** Domain name without www prefix */
domain: string;