Skip to content

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

Remember browser extension autofill entry in multi-step login forms and 2FA entry flows (#1953)

Leendert de Borste81bfa559a5b
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
index fb269e6f7..9d454f828 100644
--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
@@ -6,7 +6,7 @@ import type { EncryptionKeyDerivationParams } from '@/utils/dist/core/models/met
import { 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 } from '@/utils/itemMatcher/ItemMatcher';
+import { filterItems, AutofillMatchingMode, extractRootDomain } from '@/utils/itemMatcher/ItemMatcher';
import { LocalPreferencesService } from '@/utils/LocalPreferencesService';
import { RecentlySelectedItemService } from '@/utils/RecentlySelectedItemService';
import { SqliteClient } from '@/utils/SqliteClient';
@@ -380,15 +380,19 @@ function filterItemsByUrl(items: Item[], currentUrl: string, pageTitle: string,
* If the item is not in the filtered results, fetch it from the vault and add it.
*
* @param items - The filtered items array
- * @param domain - The current domain for recently selected item validation
+ * @param rootDomain - The current root domain for recently selected item validation
* @param allItems - All items from the vault (to fetch recently selected if not in filtered)
- * @returns The items array with recently selected item prioritized
+ * @returns The items (with recently selected prioritized) and the matched id, if any
*/
-async function prioritizeRecentlySelectedItem(items: Item[], domain: string, allItems: Item[]): Promise<Item[]> {
- const recentlySelectedId = await RecentlySelectedItemService.getRecentlySelected(domain);
+async function prioritizeRecentlySelectedItem(
+ items: Item[],
+ rootDomain: string,
+ allItems: Item[]
+): Promise<{ items: Item[], recentlySelectedId: string | null }> {
+ const recentlySelectedId = await RecentlySelectedItemService.getRecentlySelected(rootDomain);
if (!recentlySelectedId) {
- return items;
+ return { items, recentlySelectedId: null };
}
// Find the recently selected item in the filtered results
@@ -402,7 +406,7 @@ async function prioritizeRecentlySelectedItem(items: Item[], domain: string, all
...items.slice(0, recentlySelectedIndex),
...items.slice(recentlySelectedIndex + 1)
];
- return reorderedItems;
+ return { items: reorderedItems, recentlySelectedId };
}
// Item is not in filtered results - fetch it from all items and prepend it
@@ -410,24 +414,25 @@ async function prioritizeRecentlySelectedItem(items: Item[], domain: string, all
if (!recentlySelectedItem) {
// Item not found in vault (might have been deleted)
- return items;
+ return { items, recentlySelectedId: null };
}
// Prepend the recently selected item to the filtered results
- return [recentlySelectedItem, ...items];
+ return { items: [recentlySelectedItem, ...items], recentlySelectedId };
}
/**
- * Extract domain from URL for recently selected item scoping.
+ * Extract the root domain from a URL for recently-selected item scoping.
+ * Uses root domain (e.g. `example.com` for both `accounts.example.com` and `login.example.com`)
+ * so multi-step login flows that span subdomains still match.
* @param url - The full URL
- * @returns The domain or the original URL if parsing fails
+ * @returns The root domain, or the original URL if parsing fails
*/
-function extractDomain(url: string): string {
+async function extractRootDomainFromUrl(url: string): Promise<string> {
try {
const urlObj = new URL(url);
- return urlObj.hostname;
+ return await extractRootDomain(urlObj.hostname);
} catch {
- // If URL parsing fails, return the original URL
return url;
}
}
@@ -503,13 +508,13 @@ export async function handleGetFilteredItems(
const filteredItems = await filterItemsByUrl(allItems, message.currentUrl, message.pageTitle, message.matchingMode);
// Prioritize recently selected item for multi-step login flows (opt-in only)
- let prioritizedItems = filteredItems;
if (message.includeRecentlySelected) {
- const domain = extractDomain(message.currentUrl);
- prioritizedItems = await prioritizeRecentlySelectedItem(filteredItems, domain, allItems);
+ const rootDomain = await extractRootDomainFromUrl(message.currentUrl);
+ const prioritized = await prioritizeRecentlySelectedItem(filteredItems, rootDomain, allItems);
+ return { success: true, items: prioritized.items, recentlySelectedId: prioritized.recentlySelectedId };
}
- return { success: true, items: prioritizedItems };
+ return { success: true, items: filteredItems };
} catch (error) {
console.error('Error getting filtered items:', error);
// E-304: Item read failed
@@ -1843,10 +1848,10 @@ export async function handleGetItemsWithTotp(
const filteredItems = await filterItemsByUrl(itemsWithTotp, message.currentUrl, message.pageTitle, message.matchingMode);
// Prioritize recently selected item for multi-step login flows
- const domain = extractDomain(message.currentUrl);
- const prioritizedItems = await prioritizeRecentlySelectedItem(filteredItems, domain, itemsWithTotp);
+ const rootDomain = await extractRootDomainFromUrl(message.currentUrl);
+ const prioritized = await prioritizeRecentlySelectedItem(filteredItems, rootDomain, itemsWithTotp);
- return { success: true, items: prioritizedItems };
+ return { success: true, items: prioritized.items, recentlySelectedId: prioritized.recentlySelectedId };
} catch (error) {
console.error('Error getting items with TOTP:', error);
return { success: false, error: formatErrorWithCode(await t('common.errors.unknownError'), AppErrorCode.ITEM_READ_FAILED) };
@@ -1962,7 +1967,8 @@ export async function handleSetRecentlySelected(
message: { itemId: string; domain: string }
): Promise<{ success: boolean }> {
try {
- await RecentlySelectedItemService.setRecentlySelected(message.itemId, message.domain);
+ const rootDomain = await extractRootDomain(message.domain);
+ await RecentlySelectedItemService.setRecentlySelected(message.itemId, rootDomain);
return { success: true };
} catch (error) {
console.error('Error setting recently selected item:', error);
@@ -1977,7 +1983,8 @@ export async function handleGetRecentlySelected(
message: { domain: string }
): Promise<{ success: boolean; itemId?: string | null }> {
try {
- const itemId = await RecentlySelectedItemService.getRecentlySelected(message.domain);
+ const rootDomain = await extractRootDomain(message.domain);
+ const itemId = await RecentlySelectedItemService.getRecentlySelected(rootDomain);
return { success: true, itemId };
} catch (error) {
console.error('Error getting recently selected item:', error);
diff --git a/apps/browser-extension/src/entrypoints/contentScript/Popup.ts b/apps/browser-extension/src/entrypoints/contentScript/Popup.ts
index a50140aa8..57660013c 100644
--- a/apps/browser-extension/src/entrypoints/contentScript/Popup.ts
+++ b/apps/browser-extension/src/entrypoints/contentScript/Popup.ts
@@ -141,7 +141,7 @@ export function openAutofillPopup(input: HTMLInputElement, container: HTMLElemen
}, 'background') as ItemsResponse;
if (response.success) {
- await createAutofillPopup(input, response.items, container);
+ await createAutofillPopup(input, response.items, container, response.recentlySelectedId);
} else {
// Check if the user has dismissed the vault locked popup (only for auto-show, not manual clicks)
if (!forceShow) {
@@ -190,7 +190,7 @@ export function openTotpPopup(input: HTMLInputElement, container: HTMLElement, f
}, 'background') as ItemsResponse;
if (response.success) {
- await createTotpPopup(input, response.items, container);
+ await createTotpPopup(input, response.items, container, response.recentlySelectedId);
} else {
// Check if the user has dismissed the vault locked popup (only for auto-show, not manual clicks)
if (!forceShow) {
@@ -211,7 +211,7 @@ export function openTotpPopup(input: HTMLInputElement, container: HTMLElement, f
* Create TOTP autofill popup showing items with 2FA codes.
* Matches the styling of the regular autofill popup.
*/
-async function createTotpPopup(input: HTMLInputElement, items: Item[] | undefined, rootContainer: HTMLElement) : Promise<void> {
+async function createTotpPopup(input: HTMLInputElement, items: Item[] | undefined, rootContainer: HTMLElement, recentlySelectedId?: string | null) : Promise<void> {
const searchPlaceholder = await t('content.searchVault');
const hideFor1HourText = await t('content.hideFor1Hour');
const hidePermanentlyText = await t('content.hidePermanently');
@@ -230,7 +230,7 @@ async function createTotpPopup(input: HTMLInputElement, items: Item[] | undefine
items = [];
}
- updateTotpPopupContent(items, credentialList, input, rootContainer, noTotpItemsText);
+ updateTotpPopupContent(items, credentialList, input, rootContainer, noTotpItemsText, recentlySelectedId);
// Add divider
const divider = document.createElement('div');
@@ -260,7 +260,7 @@ async function createTotpPopup(input: HTMLInputElement, items: Item[] | undefine
if (searchTerm === '') {
// If search is empty, show the initially URL-filtered items
- updateTotpPopupContent(items, credentialList, input, rootContainer, noTotpItemsText);
+ updateTotpPopupContent(items, credentialList, input, rootContainer, noTotpItemsText, recentlySelectedId);
} else {
// Search in TOTP items only
const response = await sendMessage('SEARCH_ITEMS_WITH_TOTP', {
@@ -268,10 +268,11 @@ async function createTotpPopup(input: HTMLInputElement, items: Item[] | undefine
}, 'background') as ItemsResponse;
if (response.success && response.items) {
+ // Search results don't carry prioritization, so don't highlight any item
updateTotpPopupContent(response.items, credentialList, input, rootContainer, noTotpItemsText);
} else {
// On error, fallback to showing initial filtered items
- updateTotpPopupContent(items, credentialList, input, rootContainer, noTotpItemsText);
+ updateTotpPopupContent(items, credentialList, input, rootContainer, noTotpItemsText, recentlySelectedId);
}
}
});
@@ -398,7 +399,7 @@ async function createTotpPopup(input: HTMLInputElement, items: Item[] | undefine
* @param rootContainer - The root container element.
* @param noMatchesText - Text to show when no items match.
*/
-function updateTotpPopupContent(items: Item[], itemList: HTMLElement | null, input: HTMLInputElement, rootContainer: HTMLElement, noMatchesText?: string) : void {
+function updateTotpPopupContent(items: Item[], itemList: HTMLElement | null, input: HTMLInputElement, rootContainer: HTMLElement, noMatchesText?: string, recentlySelectedId?: string | null) : void {
if (!itemList) {
itemList = document.getElementById('aliasvault-credential-list') as HTMLElement;
}
@@ -438,7 +439,8 @@ function updateTotpPopupContent(items: Item[], itemList: HTMLElement | null, inp
items.forEach(item => {
const secret = secrets[item.Id];
- const itemElement = createTotpItem(item, secret, input, rootContainer, hasSecrets ? codeElements : undefined);
+ const isRecentlySelected = recentlySelectedId != null && item.Id === recentlySelectedId;
+ const itemElement = createTotpItem(item, secret, input, rootContainer, hasSecrets ? codeElements : undefined, isRecentlySelected);
itemList!.appendChild(itemElement);
});
@@ -512,7 +514,8 @@ function createTotpItem(
secret: string | undefined,
input: HTMLInputElement,
rootContainer: HTMLElement,
- codeElements?: Map<string, { codeSpan: HTMLSpanElement; pieChart: SVGPathElement }>
+ codeElements?: Map<string, { codeSpan: HTMLSpanElement; pieChart: SVGPathElement }>,
+ isRecentlySelected: boolean = false
): HTMLElement {
const itemElement = document.createElement('div');
itemElement.className = 'av-credential-item';
@@ -534,10 +537,25 @@ function createTotpItem(
const itemTextContainer = document.createElement('div');
itemTextContainer.className = 'av-credential-text';
- // Service name (primary text)
+ // Service name (primary text) with optional "recently used" indicator
const serviceName = document.createElement('div');
serviceName.className = 'av-service-name';
- serviceName.textContent = item.Name || '';
+
+ if (isRecentlySelected) {
+ const serviceNameContainer = document.createElement('div');
+ serviceNameContainer.style.display = 'flex';
+ serviceNameContainer.style.alignItems = 'center';
+ serviceNameContainer.style.gap = '4px';
+
+ const serviceNameText = document.createElement('span');
+ serviceNameText.textContent = item.Name || '';
+ serviceNameContainer.appendChild(serviceNameText);
+ serviceNameContainer.appendChild(createRecentlySelectedIcon());
+
+ serviceName.appendChild(serviceNameContainer);
+ } else {
+ serviceName.textContent = item.Name || '';
+ }
// TOTP code display beneath title (like username in normal credentials)
const detailsContainer = document.createElement('div');
@@ -685,7 +703,7 @@ export function createLoadingPopup(input: HTMLInputElement, message: string, roo
* @param itemList - The item list element.
* @param input - The input element that triggered the popup. Required when filling items to know which form to fill.
*/
-export async function updatePopupContent(items: Item[], itemList: HTMLElement | null, input: HTMLInputElement, rootContainer: HTMLElement, noMatchesText?: string) : Promise<void> {
+export async function updatePopupContent(items: Item[], itemList: HTMLElement | null, input: HTMLInputElement, rootContainer: HTMLElement, noMatchesText?: string, recentlySelectedId?: string | null) : Promise<void> {
if (!itemList) {
itemList = document.getElementById('aliasvault-credential-list') as HTMLElement;
}
@@ -698,10 +716,42 @@ export async function updatePopupContent(items: Item[], itemList: HTMLElement |
itemList.innerHTML = '';
// Add items using the shared function
- const itemElements = createItemList(items, input, rootContainer, noMatchesText);
+ const itemElements = createItemList(items, input, rootContainer, noMatchesText, recentlySelectedId);
itemElements.forEach(element => itemList.appendChild(element));
}
+/**
+ * Build a small history-arrow SVG badge indicating this item was the most recently
+ * autofilled on this site, so the user can quickly spot it among multiple matches.
+ */
+function createRecentlySelectedIcon(): SVGSVGElement {
+ const svgNS = 'http://www.w3.org/2000/svg';
+ const svg = document.createElementNS(svgNS, 'svg');
+ svg.setAttribute('viewBox', '0 0 24 24');
+ svg.setAttribute('fill', 'none');
+ svg.setAttribute('stroke', '#6b7280');
+ svg.setAttribute('stroke-width', '2');
+ svg.setAttribute('stroke-linecap', 'round');
+ svg.setAttribute('stroke-linejoin', 'round');
+ svg.style.width = '12px';
+ svg.style.height = '12px';
+ svg.style.flexShrink = '0';
+
+ const arc = document.createElementNS(svgNS, 'path');
+ arc.setAttribute('d', 'M3 12a9 9 0 1 0 9-9 9.74 9.74 0 0 0-6.74 2.74L3 8');
+ svg.appendChild(arc);
+
+ const corner = document.createElementNS(svgNS, 'polyline');
+ corner.setAttribute('points', '3 3 3 8 8 8');
+ svg.appendChild(corner);
+
+ const innerHands = document.createElementNS(svgNS, 'path');
+ innerHands.setAttribute('d', 'M12 7v5l4 2');
+ svg.appendChild(innerHands);
+
+ return svg;
+}
+
/**
* Remove existing popup (if any exists).
*/
@@ -728,7 +778,7 @@ export function removeExistingPopup(container: HTMLElement) : void {
/**
* Create auto-fill popup
*/
-export async function createAutofillPopup(input: HTMLInputElement, items: Item[] | undefined, rootContainer: HTMLElement) : Promise<void> {
+export async function createAutofillPopup(input: HTMLInputElement, items: Item[] | undefined, rootContainer: HTMLElement, recentlySelectedId?: string | null) : Promise<void> {
// Get all translations first
const newText = await t('content.new');
const searchPlaceholder = await t('content.searchVault');
@@ -751,7 +801,7 @@ export async function createAutofillPopup(input: HTMLInputElement, items: Item[]
items = [];
}
- await updatePopupContent(items, credentialList, input, rootContainer, noMatchesText);
+ await updatePopupContent(items, credentialList, input, rootContainer, noMatchesText, recentlySelectedId);
// Add divider
const divider = document.createElement('div');
@@ -920,7 +970,7 @@ export async function createAutofillPopup(input: HTMLInputElement, items: Item[]
// Handle search input.
let searchTimeout: NodeJS.Timeout | null = null;
searchInput.addEventListener('input', async () => {
- await handleSearchInput(searchInput, items, rootContainer, searchTimeout, credentialList, input, noMatchesText);
+ await handleSearchInput(searchInput, items, rootContainer, searchTimeout, credentialList, input, noMatchesText, recentlySelectedId);
});
// Close button
@@ -1145,7 +1195,7 @@ export async function createVaultLockedPopup(input: HTMLInputElement, rootContai
* @param input - The input field that triggered the popup
* @param noMatchesText - Text to show when no matches found
*/
-async function handleSearchInput(searchInput: HTMLInputElement, initialItems: Item[], rootContainer: HTMLElement, searchTimeout: NodeJS.Timeout | null, itemList: HTMLElement | null, input: HTMLInputElement, noMatchesText?: string) : Promise<void> {
+async function handleSearchInput(searchInput: HTMLInputElement, initialItems: Item[], rootContainer: HTMLElement, searchTimeout: NodeJS.Timeout | null, itemList: HTMLElement | null, input: HTMLInputElement, noMatchesText?: string, recentlySelectedId?: string | null) : Promise<void> {
if (searchTimeout) {
clearTimeout(searchTimeout);
}
@@ -1153,8 +1203,8 @@ async function handleSearchInput(searchInput: HTMLInputElement, initialItems: It
const searchTerm = searchInput.value.trim();
if (searchTerm === '') {
- // If search is empty, show the initially URL-filtered items
- await updatePopupContent(initialItems, itemList, input, rootContainer, noMatchesText);
+ // If search is empty, show the initially URL-filtered items with the recently-used star restored
+ await updatePopupContent(initialItems, itemList, input, rootContainer, noMatchesText, recentlySelectedId);
} else {
// Search in full vault with search term
const response = await sendMessage('GET_SEARCH_ITEMS', {
@@ -1162,10 +1212,11 @@ async function handleSearchInput(searchInput: HTMLInputElement, initialItems: It
}, 'background') as ItemsResponse;
if (response.success && response.items) {
+ // Search results don't carry prioritization, so don't highlight any item
await updatePopupContent(response.items, itemList, input, rootContainer, noMatchesText);
} else {
// On error, fallback to showing initial filtered items
- await updatePopupContent(initialItems, itemList, input, rootContainer, noMatchesText);
+ await updatePopupContent(initialItems, itemList, input, rootContainer, noMatchesText, recentlySelectedId);
}
}
}
@@ -1176,7 +1227,7 @@ async function handleSearchInput(searchInput: HTMLInputElement, initialItems: It
* @param items - The items to display.
* @param input - The input element that triggered the popup. Required when filling items to know which form to fill.
*/
-function createItemList(items: Item[], input: HTMLInputElement, rootContainer: HTMLElement, noMatchesText?: string): HTMLElement[] {
+function createItemList(items: Item[], input: HTMLInputElement, rootContainer: HTMLElement, noMatchesText?: string, recentlySelectedId?: string | null): HTMLElement[] {
const elements: HTMLElement[] = [];
if (items.length > 0) {
@@ -1214,6 +1265,11 @@ function createItemList(items: Item[], input: HTMLInputElement, rootContainer: H
serviceNameText.textContent = item.Name || '';
serviceNameContainer.appendChild(serviceNameText);
+ // Add "recently used" indicator if this item was the last autofill on this site
+ if (recentlySelectedId != null && item.Id === recentlySelectedId) {
+ serviceNameContainer.appendChild(createRecentlySelectedIcon());
+ }
+
// Add passkey indicator if item has a passkey
if (item.HasPasskey) {
const passkeyIcon = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
diff --git a/apps/browser-extension/src/utils/types/messaging/ItemsResponse.ts b/apps/browser-extension/src/utils/types/messaging/ItemsResponse.ts
index 8a20d9dad..4dbe71a4a 100644
--- a/apps/browser-extension/src/utils/types/messaging/ItemsResponse.ts
+++ b/apps/browser-extension/src/utils/types/messaging/ItemsResponse.ts
@@ -3,5 +3,6 @@ import type { Item } from "@/utils/dist/core/models/vault";
export type ItemsResponse = {
success: boolean,
error?: string,
- items?: Item[]
+ items?: Item[],
+ recentlySelectedId?: string | null
};