Skip to content

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

Extend LoginDetector to also handle AJAX based form submits (#1694)

Leendert de Borst204c0e031dc0
diff --git a/apps/browser-extension/src/utils/loginDetector/LoginDetector.ts b/apps/browser-extension/src/utils/loginDetector/LoginDetector.ts
index b43e7a73d..c13f8c2cf 100644
--- a/apps/browser-extension/src/utils/loginDetector/LoginDetector.ts
+++ b/apps/browser-extension/src/utils/loginDetector/LoginDetector.ts
@@ -5,7 +5,8 @@ import type { CapturedLogin, LoginSubmissionEvent, LoginCaptureCallback } from '
/**
* Detects login form submissions and extracts credentials.
- * Currently supports traditional form submissions.
+ * Supports both traditional form submissions and AJAX-based logins
+ * (where submit buttons trigger JavaScript handlers instead of native form submit).
*
* When a login is detected and the credentials are not yet in the vault,
* this enables offering to save them.
@@ -19,6 +20,9 @@ export class LoginDetector {
/** Track forms we're monitoring to avoid duplicate listeners */
private monitoredForms: WeakSet<HTMLFormElement> = new WeakSet();
+ /** Track buttons we're monitoring to avoid duplicate listeners */
+ private monitoredButtons: WeakSet<HTMLElement> = new WeakSet();
+
/** MutationObserver for dynamically added forms */
private mutationObserver: MutationObserver | null = null;
@@ -111,6 +115,12 @@ export class LoginDetector {
}
}
+ // 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;
}
@@ -123,7 +133,7 @@ export class LoginDetector {
}
/**
- * Set up MutationObserver to detect dynamically added forms.
+ * Set up MutationObserver to detect dynamically added forms and buttons.
*/
private setupMutationObserver(): void {
this.mutationObserver = new MutationObserver((mutations) => {
@@ -135,6 +145,9 @@ export class LoginDetector {
// Check for forms inside added elements
const forms = node.querySelectorAll('form');
forms.forEach(form => this.monitorForm(form));
+
+ // Check for buttons added to existing forms
+ this.checkForNewButtonsInForms(node);
}
}
}
@@ -147,7 +160,50 @@ export class LoginDetector {
}
/**
- * Add submit event listener to a form.
+ * Check if a newly added element contains buttons that belong to monitored forms.
+ */
+ private checkForNewButtonsInForms(element: HTMLElement): void {
+ // Check if the element itself is a button
+ if (this.isSubmitButton(element)) {
+ const form = element.closest('form');
+ if (form && this.monitoredForms.has(form)) {
+ this.monitorButton(element, form);
+ }
+ }
+
+ // Check for buttons inside the element
+ const buttons = element.querySelectorAll<HTMLElement>(
+ 'button[type="submit"], input[type="submit"], button:not([type]), [role="button"]'
+ );
+
+ for (const button of buttons) {
+ const form = button.closest('form');
+ if (form && this.monitoredForms.has(form)) {
+ this.monitorButton(button, form);
+ }
+ }
+ }
+
+ /**
+ * Check if an element is a submit button.
+ */
+ private isSubmitButton(element: HTMLElement): boolean {
+ const tagName = element.tagName.toLowerCase();
+
+ if (tagName === 'button') {
+ const type = element.getAttribute('type');
+ return type === 'submit' || type === null;
+ }
+
+ if (tagName === 'input') {
+ return element.getAttribute('type') === 'submit';
+ }
+
+ return element.getAttribute('role') === 'button';
+ }
+
+ /**
+ * Add submit event listener to a form and monitor its submit buttons.
*/
private monitorForm(form: HTMLFormElement): void {
if (this.monitoredForms.has(form)) {
@@ -160,6 +216,69 @@ export class LoginDetector {
form.addEventListener('submit', (event) => {
this.handleFormSubmit(form, event);
}, { capture: true });
+
+ // Also monitor submit buttons for AJAX-based logins
+ this.monitorSubmitButtons(form);
+ }
+
+ /**
+ * Find and monitor submit buttons within a form.
+ * This handles cases where sites use JavaScript to submit forms via AJAX
+ * instead of native form submission.
+ */
+ private monitorSubmitButtons(form: HTMLFormElement): void {
+ // Find all potential submit buttons
+ const buttons = form.querySelectorAll<HTMLElement>(
+ 'button[type="submit"], input[type="submit"], button:not([type]), [role="button"]'
+ );
+
+ for (const button of buttons) {
+ this.monitorButton(button, form);
+ }
+ }
+
+ /**
+ * Add click listener to a submit button.
+ */
+ private monitorButton(button: HTMLElement, form: HTMLFormElement): void {
+ if (this.monitoredButtons.has(button)) {
+ return;
+ }
+
+ this.monitoredButtons.add(button);
+
+ // Use capture phase to catch the click before any preventDefault
+ button.addEventListener('click', () => {
+ this.handleButtonClick(form);
+ }, { capture: true });
+ }
+
+ /**
+ * Handle submit button click.
+ * This captures credentials when the button is clicked, which works for
+ * both native form submissions and AJAX-based logins.
+ */
+ private handleButtonClick(form: HTMLFormElement): void {
+ const credentials = this.extractCredentialsFromForm(form);
+
+ if (credentials) {
+ this.pendingSubmission = {
+ form,
+ fields: {
+ username: credentials.usernameField,
+ password: credentials.passwordField,
+ },
+ method: 'ajax',
+ };
+
+ // Build and emit the captured login
+ const capturedLogin = this.buildCapturedLogin(
+ credentials.username,
+ credentials.password
+ );
+
+ this.emitLoginDebounced(capturedLogin);
+ }
}
/**
diff --git a/apps/browser-extension/src/utils/loginDetector/__tests__/LoginDetector.buttonClick.test.ts b/apps/browser-extension/src/utils/loginDetector/__tests__/LoginDetector.buttonClick.test.ts
new file mode 100644
index 000000000..ac5dd07ca
--- /dev/null
+++ b/apps/browser-extension/src/utils/loginDetector/__tests__/LoginDetector.buttonClick.test.ts
@@ -0,0 +1,262 @@
+import { describe, it, expect, afterEach, vi } from 'vitest';
+
+import { LoginDetector } from '../LoginDetector';
+
+import {
+ createTestEnvironment,
+ cleanupTestEnvironment,
+ setGlobalWindow,
+ setGlobalHTMLElements,
+ MockMutationObserver,
+} from './TestUtils';
+
+import type { CapturedLogin } from '../types';
+
+describe('LoginDetector button click capture', () => {
+ let detector: LoginDetector;
+
+ afterEach(() => {
+ if (detector) {
+ detector.destroy();
+ }
+ cleanupTestEnvironment();
+ });
+
+ describe('submit button click', () => {
+ it('should capture login when submit button is clicked (AJAX-style)', () => {
+ const { document, window } = createTestEnvironment(`
+ <html><body>
+ <form id="login-form">
+ <input type="email" id="email" name="email" value="[email protected]">
+ <input type="password" id="password" name="password" value="testpass123">
+ <button type="submit" id="submit-btn">Login</button>
+ </form>
+ </body></html>
+ `);
+ setGlobalWindow(window);
+
+ detector = new LoginDetector(document);
+ detector.initialize();
+
+ const callback = vi.fn();
+ detector.onLoginCapture(callback);
+
+ // Simulate button click (like AJAX login would do)
+ const button = document.getElementById('submit-btn');
+ button?.dispatchEvent(new window.Event('click', { bubbles: true }));
+
+ vi.runAllTimers();
+
+ expect(callback).toHaveBeenCalledTimes(1);
+
+ const capturedLogin: CapturedLogin = callback.mock.calls[0][0];
+ expect(capturedLogin.username).toBe('[email protected]');
+ expect(capturedLogin.password).toBe('testpass123');
+ expect(capturedLogin.domain).toBe('example.com');
+ });
+
+ it('should capture login when button without type attribute is clicked', () => {
+ const { document, window } = createTestEnvironment(`
+ <html><body>
+ <form id="login-form">
+ <input type="text" id="username" name="username" value="testuser">
+ <input type="password" id="password" name="password" value="mypassword">
+ <button id="login-btn">Sign In</button>
+ </form>
+ </body></html>
+ `);
+ setGlobalWindow(window);
+
+ detector = new LoginDetector(document);
+ detector.initialize();
+
+ const callback = vi.fn();
+ detector.onLoginCapture(callback);
+
+ const button = document.getElementById('login-btn');
+ button?.dispatchEvent(new window.Event('click', { bubbles: true }));
+
+ vi.runAllTimers();
+
+ expect(callback).toHaveBeenCalledTimes(1);
+ expect(callback.mock.calls[0][0].username).toBe('testuser');
+ expect(callback.mock.calls[0][0].password).toBe('mypassword');
+ });
+
+ it('should capture login when input[type=submit] is clicked', () => {
+ const { document, window } = createTestEnvironment(`
+ <html><body>
+ <form id="login-form">
+ <input type="email" id="email" name="email" value="[email protected]">
+ <input type="password" id="password" name="password" value="secret123">
+ <input type="submit" id="submit-input" value="Log In">
+ </form>
+ </body></html>
+ `);
+ setGlobalWindow(window);
+
+ detector = new LoginDetector(document);
+ detector.initialize();
+
+ const callback = vi.fn();
+ detector.onLoginCapture(callback);
+
+ const submitInput = document.getElementById('submit-input');
+ submitInput?.dispatchEvent(new window.Event('click', { bubbles: true }));
+
+ vi.runAllTimers();
+
+ expect(callback).toHaveBeenCalledTimes(1);
+ expect(callback.mock.calls[0][0].username).toBe('[email protected]');
+ expect(callback.mock.calls[0][0].password).toBe('secret123');
+ });
+
+ it('should not capture when button type is "button" (non-submit)', () => {
+ const { document, window } = createTestEnvironment(`
+ <html><body>
+ <form id="login-form">
+ <input type="text" id="username" name="username" value="testuser">
+ <input type="password" id="password" name="password" value="testpass">
+ <button type="button" id="cancel-btn">Cancel</button>
+ <button type="submit" id="submit-btn">Login</button>
+ </form>
+ </body></html>
+ `);
+ setGlobalWindow(window);
+
+ detector = new LoginDetector(document);
+ detector.initialize();
+
+ const callback = vi.fn();
+ detector.onLoginCapture(callback);
+
+ // Click the cancel button (type="button")
+ const cancelButton = document.getElementById('cancel-btn');
+ cancelButton?.dispatchEvent(new window.Event('click', { bubbles: true }));
+
+ vi.runAllTimers();
+
+ // Should not have captured from cancel button
+ expect(callback).not.toHaveBeenCalled();
+ });
+ });
+
+ describe('deduplication', () => {
+ it('should deduplicate when both button click and form submit fire', () => {
+ const { document, window } = createTestEnvironment(`
+ <html><body>
+ <form id="login-form">
+ <input type="text" id="username" name="username" value="testuser">
+ <input type="password" id="password" name="password" value="testpass123">
+ <button type="submit" id="submit-btn">Login</button>
+ </form>
+ </body></html>
+ `);
+ setGlobalWindow(window);
+
+ detector = new LoginDetector(document);
+ detector.initialize();
+
+ const callback = vi.fn();
+ detector.onLoginCapture(callback);
+
+ const form = document.querySelector('form');
+ const button = document.getElementById('submit-btn');
+
+ // Simulate real scenario: click fires first, then submit
+ button?.dispatchEvent(new window.Event('click', { bubbles: true }));
+ form?.dispatchEvent(new window.Event('submit', { bubbles: true }));
+
+ vi.runAllTimers();
+
+ // Should only capture once due to debouncing
+ expect(callback).toHaveBeenCalledTimes(1);
+ });
+ });
+
+ describe('dynamically added buttons', () => {
+ it('should capture from dynamically added submit button', () => {
+ const { document, window } = createTestEnvironment(`
+ <html><body>
+ <form id="login-form">
+ <input type="email" id="email" name="email" value="[email protected]">
+ <input type="password" id="password" name="password" value="dynamicpass">
+ </form>
+ </body></html>
+ `);
+
+ setGlobalHTMLElements(window);
+ setGlobalWindow(window);
+
+ detector = new LoginDetector(document);
+ detector.initialize();
+
+ const callback = vi.fn();
+ detector.onLoginCapture(callback);
+
+ // Dynamically add a submit button
+ const form = document.getElementById('login-form') as HTMLFormElement;
+ const button = document.createElement('button');
+ button.type = 'submit';
+ button.id = 'dynamic-submit';
+ button.textContent = 'Submit';
+ form?.appendChild(button);
+
+ // Trigger MutationObserver manually with the new button
+ const observer = (document.body as unknown as { __mutationObserver: MockMutationObserver }).__mutationObserver;
+ if (observer) {
+ observer.trigger([{
+ type: 'childList',
+ addedNodes: [button] as unknown as NodeList,
+ removedNodes: [] as unknown as NodeList,
+ target: form,
+ previousSibling: null,
+ nextSibling: null,
+ attributeName: null,
+ attributeNamespace: null,
+ oldValue: null,
+ }]);
+ }
+
+ vi.runAllTimers();
+
+ // Click the dynamically added button
+ button.dispatchEvent(new window.Event('click', { bubbles: true }));
+
+ vi.runAllTimers();
+
+ expect(callback).toHaveBeenCalledTimes(1);
+ expect(callback.mock.calls[0][0].username).toBe('[email protected]');
+ });
+ });
+
+ describe('role="button" elements', () => {
+ it('should capture login when element with role="button" is clicked', () => {
+ const { document, window } = createTestEnvironment(`
+ <html><body>
+ <form id="login-form">
+ <input type="text" id="username" name="username" value="roleuser">
+ <input type="password" id="password" name="password" value="rolepass">
+ <div role="button" id="custom-btn">Login</div>
+ </form>
+ </body></html>
+ `);
+ setGlobalWindow(window);
+
+ detector = new LoginDetector(document);
+ detector.initialize();
+
+ const callback = vi.fn();
+ detector.onLoginCapture(callback);
+
+ const button = document.getElementById('custom-btn');
+ button?.dispatchEvent(new window.Event('click', { bubbles: true }));
+
+ vi.runAllTimers();
+
+ expect(callback).toHaveBeenCalledTimes(1);
+ expect(callback.mock.calls[0][0].username).toBe('roleuser');
+ expect(callback.mock.calls[0][0].password).toBe('rolepass');
+ });
+ });
+});
diff --git a/apps/browser-extension/src/utils/loginDetector/__tests__/LoginDetector.excludedDomains.test.ts b/apps/browser-extension/src/utils/loginDetector/__tests__/LoginDetector.excludedDomains.test.ts
index 5534d0cb7..0d341bb41 100644
--- a/apps/browser-extension/src/utils/loginDetector/__tests__/LoginDetector.excludedDomains.test.ts
+++ b/apps/browser-extension/src/utils/loginDetector/__tests__/LoginDetector.excludedDomains.test.ts
@@ -46,3 +46,110 @@ describe('LoginDetector excluded domains', () => {
});
});
});
+
+describe('LoginDetector av-disable attribute', () => {
+ let detector: LoginDetector;
+
+ afterEach(() => {
+ if (detector) {
+ detector.destroy();
+ }
+ cleanupTestEnvironment();
+ });
+
+ it('should skip when av-disable="true" is set on body', () => {
+ const { document, window } = createTestEnvironment(
+ `<html><body av-disable="true">
+ <form>
+ <input type="text" value="user">
+ <input type="password" value="pass">
+ </form>
+ </body></html>`
+ );
+ setGlobalWindow(window);
+
+ detector = new LoginDetector(document);
+ detector.initialize();
+
+ const callback = vi.fn();
+ detector.onLoginCapture(callback);
+
+ const form = document.querySelector('form');
+ form?.dispatchEvent(new window.Event('submit', { bubbles: true }));
+
+ vi.runAllTimers();
+ expect(callback).not.toHaveBeenCalled();
+ });
+
+ it('should skip when av-disable="true" is set on html element', () => {
+ const { document, window } = createTestEnvironment(
+ `<html av-disable="true"><body>
+ <form>
+ <input type="text" value="user">
+ <input type="password" value="pass">
+ </form>
+ </body></html>`
+ );
+ setGlobalWindow(window);
+
+ detector = new LoginDetector(document);
+ detector.initialize();
+
+ const callback = vi.fn();
+ detector.onLoginCapture(callback);
+
+ const form = document.querySelector('form');
+ form?.dispatchEvent(new window.Event('submit', { bubbles: true }));
+
+ vi.runAllTimers();
+ expect(callback).not.toHaveBeenCalled();
+ });
+
+ it('should not skip when av-disable is not set', () => {
+ const { document, window } = createTestEnvironment(
+ `<html><body>
+ <form>
+ <input type="text" value="user">
+ <input type="password" value="pass">
+ </form>
+ </body></html>`
+ );
+ setGlobalWindow(window);
+
+ detector = new LoginDetector(document);
+ detector.initialize();
+
+ const callback = vi.fn();
+ detector.onLoginCapture(callback);
+
+ const form = document.querySelector('form');
+ form?.dispatchEvent(new window.Event('submit', { bubbles: true }));
+
+ vi.runAllTimers();
+ expect(callback).toHaveBeenCalled();
+ });
+
+ it('should not skip when av-disable="false"', () => {
+ const { document, window } = createTestEnvironment(
+ `<html><body av-disable="false">
+ <form>
+ <input type="text" value="user">
+ <input type="password" value="pass">
+ </form>
+ </body></html>`
+ );
+ setGlobalWindow(window);
+
+ detector = new LoginDetector(document);
+ detector.initialize();
+
+ const callback = vi.fn();
+ detector.onLoginCapture(callback);
+
+ const form = document.querySelector('form');
+ form?.dispatchEvent(new window.Event('submit', { bubbles: true }));
+
+ vi.runAllTimers();
+ expect(callback).toHaveBeenCalled();
+ });
+});