Skip to content

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

Add FormDetector excluded field definition tests (#1935)

Leendert de Borstf2c90e90b97d
diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.exclusion.test.ts b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.exclusion.test.ts
new file mode 100644
index 000000000..c2e2606d4
--- /dev/null
+++ b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.exclusion.test.ts
@@ -0,0 +1,56 @@
+import { describe, it, expect } from 'vitest';
+
+import { FormDetector } from '../FormDetector';
+
+import { createTestDom } from './TestUtils';
+
+describe('FormDetector - Field Exclusion Patterns', () => {
+ describe('Real-world scenario: Admin panel with search', () => {
+ const htmlFile = 'exclusion-admin-panel.html';
+
+ it('should not trigger autofill on "Search users..." field in admin panel', () => {
+ const dom = createTestDom(htmlFile);
+ const document = dom.window.document;
+
+ const searchInput = document.getElementById('search');
+ const formDetector = new FormDetector(document, searchInput as HTMLElement);
+
+ // Should not detect as login form (this is the main use case from the user's issue)
+ expect(formDetector.containsLoginForm()).toBe(false);
+ });
+ });
+
+ describe('Exclusion patterns should not affect legitimate login fields', () => {
+ const htmlFile = 'exclusion-legitimate-login.html';
+
+ it('should still detect username field in a real login form', () => {
+ const dom = createTestDom(htmlFile);
+ const document = dom.window.document;
+
+ const usernameInput = document.getElementById('username');
+ const formDetector = new FormDetector(document, usernameInput as HTMLElement);
+
+ // Should detect as login form
+ expect(formDetector.containsLoginForm()).toBe(true);
+
+ // Should detect username field
+ const form = formDetector.getForm();
+ expect(form?.usernameField).toBe(usernameInput);
+ });
+
+ it('should still detect email field in a real login form', () => {
+ const dom = createTestDom(htmlFile);
+ const document = dom.window.document;
+
+ const emailInput = document.getElementById('email');
+ const formDetector = new FormDetector(document, emailInput as HTMLElement);
+
+ // Should detect as login form
+ expect(formDetector.containsLoginForm()).toBe(true);
+
+ // Should detect email field
+ const form = formDetector.getForm();
+ expect(form?.emailField).toBe(emailInput);
+ });
+ });
+});
diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/exclusion-admin-panel.html b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/exclusion-admin-panel.html
new file mode 100644
index 000000000..bbd40fdd5
--- /dev/null
+++ b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/exclusion-admin-panel.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>Admin Panel - Search Users</title>
+</head>
+<body>
+ <!-- Real-world scenario: Admin panel with search (matches user's reported issue) -->
+ <div class="admin-panel">
+ <h1>User Management</h1>
+
+ <!-- Search field that should NOT trigger autofill -->
+ <div class="relative">
+ <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
+ <svg class="w-5 h-5 text-gray-500 dark:text-gray-400" aria-hidden="true" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
+ <path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd"></path>
+ </svg>
+ </div>
+ <input type="text" id="search" placeholder="Search users..." class="w-full px-4 ps-10 py-2 border rounded text-sm text-gray-700 focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" autocomplete="off">
+ </div>
+
+ <!-- User table would go here -->
+ <table>
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Email</th>
+ <th>Actions</th>
+ </tr>
+ </thead>
+ <tbody>
+ <!-- User rows -->
+ </tbody>
+ </table>
+ </div>
+</body>
+</html>
diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/exclusion-legitimate-login.html b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/exclusion-legitimate-login.html
new file mode 100644
index 000000000..dfc6ed989
--- /dev/null
+++ b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/exclusion-legitimate-login.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>Legitimate Login Form</title>
+</head>
+<body>
+ <!-- This form should STILL be detected as a login form despite having search fields on the same page -->
+
+ <!-- Search bar in navigation (should be excluded) -->
+ <nav>
+ <input type="text" id="nav-search" name="search" placeholder="Search...">
+ </nav>
+
+ <!-- Legitimate login form (should be detected) -->
+ <form id="login-form">
+ <div>
+ <label for="username">Username</label>
+ <input type="text" id="username" name="username" placeholder="Enter username">
+ </div>
+ <div>
+ <label for="email">Email</label>
+ <input type="email" id="email" name="email" placeholder="Enter email">
+ </div>
+ <div>
+ <label for="password">Password</label>
+ <input type="password" id="password" name="password" placeholder="Enter password">
+ </div>
+ <button type="submit">Login</button>
+ </form>
+</body>
+</html>