Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add av-enable and av-suppress-save flags support to browser extension (#2100)
Leendert de Borst0d78ea76b4dd
diff --git a/apps/browser-extension/src/entrypoints/content.ts b/apps/browser-extension/src/entrypoints/content.tsindex 0bff91897..948a66fa1 100644--- a/apps/browser-extension/src/entrypoints/content.ts+++ b/apps/browser-extension/src/entrypoints/content.ts@@ -8,6 +8,7 @@ import { openAutofillPopup, openTotpPopup, removeExistingPopup, createUpgradeReqimport { showSavePrompt, showAddUrlPrompt, isSavePromptVisible, updateSavePromptLogin, getPersistedSavePromptState, restoreSavePromptFromState, restoreAddUrlPromptFromState } from '@/entrypoints/contentScript/SavePrompt';import { initializeWebAuthnInterceptor } from '@/entrypoints/contentScript/WebAuthnInterceptor';+import { isAvAutofillAllowed, isAvSuppressSave } from '@/utils/autofill/Autofill';import { DEFAULT_POPUP_TYPE, isPopupType, popupTypeForFieldType, POPUP_TYPES, type PopupType } from '@/utils/autofill/PopupTypes';import { FormDetector } from '@/utils/formDetector/FormDetector';import { LocalPreferencesService } from '@/utils/LocalPreferencesService';@@ -490,9 +491,21 @@ export default defineContentScript({return;}- // Check if element itself, html or body has av-disable attribute like av-disable="true"- const avDisable = ((e.target as HTMLElement).getAttribute('av-disable') ?? document.body?.getAttribute('av-disable') ?? document.documentElement.getAttribute('av-disable')) === 'true';- if (avDisable) {+ /*+ * Honour av-disable / av-enable opt-in markers. The nearest ancestor wins, so a host page+ * can globally disable autofill via av-disable="true" on <body> while still opting specific+ * subtrees back in with av-enable="true".+ */+ if (!isAvAutofillAllowed(e.target as Element)) {+ return;+ }++ /*+ * Honour av-suppress-save: skip the popup (and therefore the icon) entirely when there is+ * no stored credential that matches this URL, so we don't invite the user to "create new"+ * on pages where storing the credential isn't desired by default (e.g. AliasVault's own web login form).+ */+ if (isAvSuppressSave(e.target as Element) && !await hasMatchingCredentialForCurrentUrl()) {return;}@@ -591,6 +604,26 @@ export default defineContentScript({return { success: true };});+ /**+ * Check whether at least one stored credential matches the current URL.+ * Used by av-suppress-save pages to decide whether the autofill popup should appear at all.+ * Returns false when the vault is locked or when no matches exist.+ */+ async function hasMatchingCredentialForCurrentUrl(): Promise<boolean> {+ try {+ const matchingMode = await LocalPreferencesService.getAutofillMatchingMode();+ const response = await sendMessage('GET_FILTERED_ITEMS', {+ currentUrl: window.location.href,+ pageTitle: document.title,+ matchingMode,+ includeRecentlySelected: false,+ });+ return response.success && (response.items?.length ?? 0) > 0;+ } catch {+ return false;+ }+ }+/*** Check if autofill is disabled for the current site (site-specific settings only).* @returns True if site allows autofill, false if site has disabled it@@ -627,6 +660,14 @@ export default defineContentScript({return;}+ /*+ * av-suppress-save respects forceShow (e.g. explicit OPEN_AUTOFILL_POPUP from context menu)+ * but otherwise hides the popup unless a matching credential is already stored.+ */+ if (!forceShow && isAvSuppressSave(inputElement) && !await hasMatchingCredentialForCurrentUrl()) {+ return;+ }+const detectedFieldType = formDetector.getDetectedFieldType();const popupType = popupTypeForFieldType(detectedFieldType);diff --git a/apps/browser-extension/src/utils/autofill/Autofill.ts b/apps/browser-extension/src/utils/autofill/Autofill.tsnew file mode 100644index 000000000..5e8c8a1fd--- /dev/null+++ b/apps/browser-extension/src/utils/autofill/Autofill.ts@@ -0,0 +1,62 @@+/**+ * Helpers for deciding whether AliasVault's autofill UI is allowed for a given+ * DOM element. The web app uses `av-disable="true"` on its `<body>` to opt out+ * of extension autofill globally, and individual subtrees can opt back in with+ * `av-enable="true"` so the extension can fill the master-password login form+ * even when the rest of the app stays disabled. A second flag,+ * `av-suppress-save="true"`, can be combined with `av-enable` to keep autofill+ * available for matching credentials while hiding the "save this login" and+ * "create new" affordances — useful on pages where storing the credential+ * shouldn't be encouraged (e.g. AliasVault's own login form).+ */++/**+ * Walk up the DOM from the given element and determine whether AliasVault+ * autofill is allowed. The nearest ancestor (or the element itself) carrying+ * an `av-enable="true"` or `av-disable="true"` attribute decides — closer+ * `av-enable` overrides a further-up `av-disable`. When neither attribute is+ * present anywhere up the chain, autofill is allowed.+ * @param element - The element to start walking from (typically the focused input).+ * @returns True when autofill is allowed for this element.+ */+export function isAvAutofillAllowed(element: Element | null): boolean {+ let current: Element | null = element;+ while (current) {+ if (current.getAttribute('av-enable') === 'true') {+ return true;+ }+ if (current.getAttribute('av-disable') === 'true') {+ return false;+ }+ current = current.parentElement;+ }+ return true;+}++/**+ * Walk up the DOM from the given element and determine whether save-related+ * affordances should be suppressed. Closest ancestor wins — an explicit+ * `av-suppress-save="false"` deeper in the tree overrides a `="true"` higher+ * up. Defaults to false (not suppressed) when the attribute is absent.+ *+ * When this returns true, callers should:+ * - skip the "save this login" capture/prompt entirely;+ * - skip showing the autofill popup unless a matching credential already exists+ * (so the user isn't nudged toward creating a new credential here).+ * @param element - The element to start walking from.+ * @returns True when save-related affordances should be suppressed.+ */+export function isAvSuppressSave(element: Element | null): boolean {+ let current: Element | null = element;+ while (current) {+ const value = current.getAttribute('av-suppress-save');+ if (value === 'true') {+ return true;+ }+ if (value === 'false') {+ return false;+ }+ current = current.parentElement;+ }+ return false;+}diff --git a/apps/browser-extension/src/utils/autofill/__tests__/Autofill.test.ts b/apps/browser-extension/src/utils/autofill/__tests__/Autofill.test.tsnew file mode 100644index 000000000..286820822--- /dev/null+++ b/apps/browser-extension/src/utils/autofill/__tests__/Autofill.test.ts@@ -0,0 +1,105 @@+import { JSDOM } from 'jsdom';+import { describe, it, expect } from 'vitest';++import { isAvAutofillAllowed, isAvSuppressSave } from '../Autofill';++/**+ * Build a JSDOM environment from the given HTML and return its first <input>.+ */+const buildInput = (html: string): { input: HTMLInputElement; window: Window } => {+ const dom = new JSDOM(html);+ const input = dom.window.document.querySelector('input');+ if (!input) {+ throw new Error('Test fixture must contain an <input> element');+ }+ return { input: input as HTMLInputElement, window: dom.window as unknown as Window };+};++describe('isAvAutofillAllowed', () => {+ it('returns true when no markers are present anywhere', () => {+ const { input } = buildInput('<html><body><form><input></form></body></html>');+ expect(isAvAutofillAllowed(input)).toBe(true);+ });++ it('returns false when av-disable="true" is set on body', () => {+ const { input } = buildInput('<html><body av-disable="true"><form><input></form></body></html>');+ expect(isAvAutofillAllowed(input)).toBe(false);+ });++ it('returns false when av-disable="true" is set on the html element', () => {+ const { input } = buildInput('<html av-disable="true"><body><form><input></form></body></html>');+ expect(isAvAutofillAllowed(input)).toBe(false);+ });++ it('returns true when a closer av-enable="true" overrides av-disable on body', () => {+ const { input } = buildInput(+ '<html><body av-disable="true"><div av-enable="true"><form><input></form></div></body></html>'+ );+ expect(isAvAutofillAllowed(input)).toBe(true);+ });++ it('returns true when av-enable="true" sits on the form itself', () => {+ const { input } = buildInput(+ '<html><body av-disable="true"><form av-enable="true"><input></form></body></html>'+ );+ expect(isAvAutofillAllowed(input)).toBe(true);+ });++ it('returns true when av-enable="true" sits directly on the input', () => {+ const { input } = buildInput(+ '<html><body av-disable="true"><form><input av-enable="true"></form></body></html>'+ );+ expect(isAvAutofillAllowed(input)).toBe(true);+ });++ it('returns false when a closer av-disable="true" overrides av-enable higher up', () => {+ const { input } = buildInput(+ '<html><body av-enable="true"><div av-disable="true"><form><input></form></div></body></html>'+ );+ expect(isAvAutofillAllowed(input)).toBe(false);+ });++ it('ignores non-"true" values (treats av-disable="false" as absent)', () => {+ const { input } = buildInput('<html><body av-disable="false"><form><input></form></body></html>');+ expect(isAvAutofillAllowed(input)).toBe(true);+ });++ it('returns true for a null element', () => {+ expect(isAvAutofillAllowed(null)).toBe(true);+ });+});++describe('isAvSuppressSave', () => {+ it('returns false when no markers are present', () => {+ const { input } = buildInput('<html><body><form><input></form></body></html>');+ expect(isAvSuppressSave(input)).toBe(false);+ });++ it('returns true when av-suppress-save="true" is on an ancestor', () => {+ const { input } = buildInput(+ '<html><body><form av-suppress-save="true"><input></form></body></html>'+ );+ expect(isAvSuppressSave(input)).toBe(true);+ });++ it('returns true when av-suppress-save="true" is on the element itself', () => {+ const { input } = buildInput('<html><body><form><input av-suppress-save="true"></form></body></html>');+ expect(isAvSuppressSave(input)).toBe(true);+ });++ it('returns false when a closer av-suppress-save="false" overrides a "true" higher up', () => {+ const { input } = buildInput(+ '<html><body av-suppress-save="true"><div av-suppress-save="false"><form><input></form></div></body></html>'+ );+ expect(isAvSuppressSave(input)).toBe(false);+ });++ it('ignores non-"true"/"false" values (treats av-suppress-save="" as absent)', () => {+ const { input } = buildInput('<html><body av-suppress-save=""><form><input></form></body></html>');+ expect(isAvSuppressSave(input)).toBe(false);+ });++ it('returns false for a null element', () => {+ expect(isAvSuppressSave(null)).toBe(false);+ });+});diff --git a/apps/browser-extension/src/utils/loginDetector/LoginDetector.ts b/apps/browser-extension/src/utils/loginDetector/LoginDetector.tsindex c13f8c2cf..6417f6a19 100644--- a/apps/browser-extension/src/utils/loginDetector/LoginDetector.ts+++ b/apps/browser-extension/src/utils/loginDetector/LoginDetector.ts@@ -1,3 +1,4 @@+import { isAvAutofillAllowed, isAvSuppressSave } from '@/utils/autofill/Autofill';import { extractFaviconUrlSimple } from '@/utils/favicon';import { FormDetector } from '@/utils/formDetector/FormDetector';@@ -30,11 +31,6 @@ export class LoginDetector {private captureDebounceTimer: number | null = null;private lastCapturedLogin: CapturedLogin | null = null;- /** Domains to exclude from login detection (only AliasVault itself) */- private static readonly EXCLUDED_DOMAINS = [- 'aliasvault.net',- ];-/*** Creates a new LoginDetector instance.* @param document - The document to monitor for login form submissions.@@ -52,11 +48,6 @@ export class LoginDetector {return;}- // Check if we should skip this page- if (this.shouldSkipPage()) {- return;- }-// Monitor existing formsthis.monitorExistingForms();@@ -102,28 +93,6 @@ export class LoginDetector {return () => this.callbacks.delete(callback);}- /**- * Check if the current page should be skipped for login detection.- */- private shouldSkipPage(): boolean {- const hostname = window.location.hostname.toLowerCase();-- // Check excluded domains- for (const domain of LoginDetector.EXCLUDED_DOMAINS) {- if (hostname === domain || hostname.endsWith(`.${domain}`)) {- return true;- }- }-- // Check if av-disable attribute is set on body or html element- const avDisable = (this.document.body?.getAttribute('av-disable') ?? this.document.documentElement.getAttribute('av-disable')) === 'true';- if (avDisable) {- return true;- }-- return false;- }-/*** Find and monitor all existing forms on the page.*/@@ -210,6 +179,18 @@ export class LoginDetector {return;}+ if (!isAvAutofillAllowed(form)) {+ return;+ }++ /*+ * Pages with av-suppress-save="true" want autofill for matching credentials but should never+ * prompt the user to save what they just typed — skip submission monitoring entirely.+ */+ if (isAvSuppressSave(form)) {+ return;+ }+this.monitoredForms.add(form);// Use capture phase to get the event before it might be prevented