Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Improve formDetector to exclude hidden/fake fields (#1788)
Leendert de Borst4c3d206d64b8
diff --git a/apps/browser-extension/src/utils/formDetector/FormDetector.ts b/apps/browser-extension/src/utils/formDetector/FormDetector.tsindex b2d89586a..ed3fc11c7 100644--- a/apps/browser-extension/src/utils/formDetector/FormDetector.ts+++ b/apps/browser-extension/src/utils/formDetector/FormDetector.ts@@ -204,6 +204,89 @@ export class FormDetector {return wrapper;}+ /**+ * Check if an input field is likely a fake/honeypot field used to prevent autofill.+ * These fields are intentionally hidden from users but present in the DOM.+ * Common patterns:+ * - Fields with "fake" in name/id+ * - Fields with tabindex="-1" (not keyboard accessible)+ * - Fields in containers styled to be invisible+ * - Fields with aria-hidden="true"+ * - Fields with role="presentation"+ * - Honeypot-named fields (honeypot, bot-field, etc.)+ */+ private isLikelyFakeField(input: HTMLInputElement): boolean {+ /*+ * Check for "fake" in name, id, or class+ */+ const name = (input.getAttribute('name') || '').toLowerCase();+ const id = (input.id || '').toLowerCase();+ const className = (input.className || '').toLowerCase();++ if (name.includes('fake') || id.includes('fake') || className.includes('fake')) {+ return true;+ }++ /*+ * Check for honeypot patterns in name or id+ */+ const honeypotPatterns = ['honeypot', 'bot-field', 'hp-', 'hidden-field', 'bot_check', 'antispam'];+ if (honeypotPatterns.some(pattern => name.includes(pattern) || id.includes(pattern))) {+ return true;+ }++ /*+ * Check for aria-hidden="true" which explicitly marks the field as hidden from assistive tech+ * This is often used for fake fields that shouldn't be interacted with+ */+ if (input.getAttribute('aria-hidden') === 'true') {+ return true;+ }++ /*+ * Check for role="presentation" which indicates the element is for presentation only+ */+ if (input.getAttribute('role') === 'presentation') {+ return true;+ }++ /*+ * Check for tabindex="-1" which indicates field is not meant for user interaction+ * Combined with suspicious positioning or naming+ */+ const tabIndex = input.getAttribute('tabindex');+ if (tabIndex === '-1') {+ /*+ * If tabindex=-1 AND the field is in a hidden container, it's likely fake+ * The visibility check will catch this, but we add extra confirmation+ */+ const parent = input.parentElement;+ if (parent) {+ const parentStyle = this.document.defaultView?.getComputedStyle(parent);+ if (parentStyle) {+ const height = parseFloat(parentStyle.height);+ const overflow = parentStyle.overflow;+ if (height === 0 && overflow === 'hidden') {+ return true;+ }+ }+ }+ }++ /*+ * Check for readonly AND hidden combination (sometimes used for fake fields)+ * Note: legitimate readonly fields should be visible, so if it's readonly + not visible, it's suspicious+ */+ if (input.readOnly) {+ const style = this.document.defaultView?.getComputedStyle(input);+ if (style && (style.display === 'none' || style.visibility === 'hidden')) {+ return true;+ }+ }++ return false;+ }+/*** Get the actual input element from a potentially custom element.* This handles any element with shadow DOM containing input elements.@@ -314,6 +397,78 @@ export class FormDetector {}return false;}++ /*+ * Check if element has zero or near-zero dimensions (effectively invisible)+ * This catches various hiding techniques:+ * - height:0, width:0, max-height:0, max-width:0+ * - position:absolute with clip/clip-path+ * - Any combination that results in no visible pixels+ */+ const height = parseFloat(style.height);+ const width = parseFloat(style.width);+ const maxHeight = parseFloat(style.maxHeight);+ const maxWidth = parseFloat(style.maxWidth);++ // Check if element has zero dimensions+ if (height === 0 || width === 0 || maxHeight === 0 || maxWidth === 0) {+ // Cache and return false for this element and all its parents+ let parent: HTMLElement | null = current;+ while (parent) {+ if (checkOpacity) {+ this.visibilityCache.set(parent, false);+ }+ parent = parent.parentElement;+ }+ return false;+ }++ /*+ * Check for elements positioned off-screen using absolute/fixed positioning+ * Common technique: position:absolute; left:-9999px or top:-9999px+ */+ if (style.position === 'absolute' || style.position === 'fixed') {+ const left = parseFloat(style.left);+ const top = parseFloat(style.top);+ const right = parseFloat(style.right);+ const bottom = parseFloat(style.bottom);++ // If positioned far off-screen (more than 5000px away)+ if (left < -5000 || top < -5000 || right < -5000 || bottom < -5000) {+ let parent: HTMLElement | null = current;+ while (parent) {+ if (checkOpacity) {+ this.visibilityCache.set(parent, false);+ }+ parent = parent.parentElement;+ }+ return false;+ }+ }++ /*+ * Check for CSS clip property that hides the element+ * Note: clip is deprecated but still widely used for hiding elements+ * Example: clip: rect(0,0,0,0)+ */+ const clipValue = style.getPropertyValue('clip');+ if (clipValue && clipValue !== 'auto' && clipValue.includes('0')) {+ // Simple check for clip: rect(0,0,0,0) or similar+ const clipMatch = clipValue.match(/rect\((\d+)[^\d]+(\d+)[^\d]+(\d+)[^\d]+(\d+)\)/);+ if (clipMatch) {+ const [, top, right, bottom, left] = clipMatch.map(Number);+ if (top === 0 && right === 0 && bottom === 0 && left === 0) {+ let parent: HTMLElement | null = current;+ while (parent) {+ if (checkOpacity) {+ this.visibilityCache.set(parent, false);+ }+ parent = parent.parentElement;+ }+ return false;+ }+ }+ }} catch {// If we can't get computed style, cache and return true for this element and all its parentslet parent: HTMLElement | null = current;@@ -380,6 +535,13 @@ export class FormDetector {continue;}+ /*+ * Skip fake/honeypot fields (e.g., fields with "fake" in name/id, tabindex="-1", etc.)+ */+ if (this.isLikelyFakeField(input as HTMLInputElement)) {+ continue;+ }+// When user clicks a field, skip opacity checks to support transition animationsif (checkVisibility) {const isVisible = this.isElementVisible(input, true);diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.emby-connect.test.ts b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.emby-connect.test.tsnew file mode 100644index 000000000..fd2e50acd--- /dev/null+++ b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.emby-connect.test.ts@@ -0,0 +1,71 @@+import { describe, it, expect } from 'vitest';++import { FormDetector } from '../FormDetector';++import { createTestDom } from './TestUtils';++describe('FormDetector - Emby Connect login form', () => {+ const htmlFile = 'emby-connect-login.html';++ it('should detect login form', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;+ const formDetector = new FormDetector(document);+ expect(formDetector.containsLoginForm()).toBe(true);+ });++ it('should ignore hidden fake username/password fields with height:0', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ // Click on the real email/username field+ const emailInput = document.getElementById('embyinput0');+ const formDetector = new FormDetector(document, emailInput as HTMLElement);+ const form = formDetector.getForm();++ expect(form).toBeTruthy();++ // Should detect the real email field (labeled as "Email" in the form), not the fake username field+ // This field can be used for username/email login+ expect(form?.emailField).toBeTruthy();+ expect(form?.emailField?.className).toContain('txtUser');+ expect(form?.emailField?.id).toBe('embyinput0');++ // Should detect the real password field (txtPassword), not the fake one+ expect(form?.passwordField).toBeTruthy();+ expect(form?.passwordField?.className).toContain('txtPassword');+ expect(form?.passwordField?.name).not.toBe('fakepasswordremembered');+ });++ it('should not autofill hidden fake fields', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ // Click on the fake username field (should not be detected as autofillable)+ const fakeUsernameInput = document.querySelector('input[name="fakeusernameremembered"]');+ const formDetector = new FormDetector(document, fakeUsernameInput as HTMLElement);++ // Should not consider the fake field as an autofill-triggerable field+ expect(formDetector.isAutofillTriggerableField()).toBe(false);+ });++ it('should detect real username field when clicked', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ const usernameInput = document.getElementById('embyinput0');+ const formDetector = new FormDetector(document, usernameInput as HTMLElement);++ expect(formDetector.isAutofillTriggerableField()).toBe(true);+ });++ it('should detect real password field when clicked', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ const passwordInput = document.getElementById('embyinput1');+ const formDetector = new FormDetector(document, passwordInput as HTMLElement);++ expect(formDetector.isAutofillTriggerableField()).toBe(true);+ });+});diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.hidden-fields.test.ts b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.hidden-fields.test.tsnew file mode 100644index 000000000..a82e8510a--- /dev/null+++ b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.hidden-fields.test.ts@@ -0,0 +1,139 @@+import { describe, it, expect } from 'vitest';++import { FormDetector } from '../FormDetector';++import { createTestDom } from './TestUtils';++describe('FormDetector - Various hidden field techniques', () => {+ const htmlFile = 'hidden-fields-various.html';++ it('should detect login form with real fields', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;+ const formDetector = new FormDetector(document);+ expect(formDetector.containsLoginForm()).toBe(true);+ });++ it('should ignore fields in height:0 container', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ const realEmail = document.getElementById('real-email');+ const formDetector = new FormDetector(document, realEmail as HTMLElement);+ const form = formDetector.getForm();++ expect(form).toBeTruthy();+ expect(form?.emailField?.id).toBe('real-email');+ expect(form?.passwordField?.id).toBe('real-password');++ // Should not detect fake fields+ expect(form?.emailField?.name).not.toBe('fake1');+ expect(form?.passwordField?.name).not.toBe('fake1pass');+ });++ it('should ignore fields in width:0 container', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ const realEmail = document.getElementById('real-email');+ const formDetector = new FormDetector(document, realEmail as HTMLElement);+ const form = formDetector.getForm();++ expect(form).toBeTruthy();+ expect(form?.emailField?.name).not.toBe('fake2');+ expect(form?.passwordField?.name).not.toBe('fake2pass');+ });++ it('should ignore fields positioned off-screen', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ const realEmail = document.getElementById('real-email');+ const formDetector = new FormDetector(document, realEmail as HTMLElement);+ const form = formDetector.getForm();++ expect(form).toBeTruthy();+ expect(form?.emailField?.name).not.toBe('fake3');+ expect(form?.passwordField?.name).not.toBe('fake3pass');+ });++ it('should ignore honeypot fields', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ const realEmail = document.getElementById('real-email');+ const formDetector = new FormDetector(document, realEmail as HTMLElement);+ const form = formDetector.getForm();++ expect(form).toBeTruthy();+ expect(form?.emailField?.name).not.toBe('honeypot');+ expect(form?.emailField?.name).not.toBe('hp-field');+ expect(form?.emailField?.name).not.toBe('bot_check');+ expect(form?.emailField?.name).not.toBe('antispam-field');+ });++ it('should ignore fields with aria-hidden="true"', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ const realEmail = document.getElementById('real-email');+ const formDetector = new FormDetector(document, realEmail as HTMLElement);+ const form = formDetector.getForm();++ expect(form).toBeTruthy();+ expect(form?.emailField?.name).not.toBe('hidden-username');+ expect(form?.passwordField?.name).not.toBe('hidden-password');+ });++ it('should ignore fields with role="presentation"', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ const realEmail = document.getElementById('real-email');+ const formDetector = new FormDetector(document, realEmail as HTMLElement);+ const form = formDetector.getForm();++ expect(form).toBeTruthy();+ expect(form?.emailField?.name).not.toBe('presentation-field');+ });++ it('should not trigger autofill on fake field with tabindex=-1', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ const fakeField = document.querySelector('input[name="fake1"]');+ const formDetector = new FormDetector(document, fakeField as HTMLElement);++ expect(formDetector.isAutofillTriggerableField()).toBe(false);+ });++ it('should not trigger autofill on honeypot field', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ const honeypotField = document.querySelector('input[name="honeypot"]');+ const formDetector = new FormDetector(document, honeypotField as HTMLElement);++ expect(formDetector.isAutofillTriggerableField()).toBe(false);+ });++ it('should trigger autofill on real email field', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ const realEmail = document.getElementById('real-email');+ const formDetector = new FormDetector(document, realEmail as HTMLElement);++ expect(formDetector.isAutofillTriggerableField()).toBe(true);+ });++ it('should trigger autofill on real password field', () => {+ const dom = createTestDom(htmlFile);+ const document = dom.window.document;++ const realPassword = document.getElementById('real-password');+ const formDetector = new FormDetector(document, realPassword as HTMLElement);++ expect(formDetector.isAutofillTriggerableField()).toBe(true);+ });+});diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/emby-connect-login.html b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/emby-connect-login.htmlnew file mode 100644index 000000000..96e412230--- /dev/null+++ b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/emby-connect-login.html@@ -0,0 +1,57 @@+<div class="scrollSlider flex-grow flex-direction-column padded-left padded-right padded-top-page padded-bottom-page scrollSliderY focuscontainer-y navout-up">++ <div class="readOnlyContent" style="margin:0 auto;">++ <h1 style="margin-top:0;">Sign in with Emby Connect</h1>++ <p>With Emby Connect you can easily access your Emby Server wherever you are and share servers with your family and friends.</p>++ <div class="pinLogin hide">+ <p class="pinCodeHeader">To sign in with Emby Connect, use a mobile device or computer to visit <a is="emby-linkbutton" class="lnkPinSignIn button-link emby-button emby-button-focusscale" href="https://emby.media/pin" target="_blank">https://emby.media/pin</a> and enter the following pin code:</p>+ <h1 class="pinCodeValue" style="text-align: center;"></h1>+ <p class="pinMessage hide" style="text-align: center; margin: -1em auto 2em; color: orange; width: 70%;"></p>++ <button is="emby-button" class="raised block btnNewPin emby-button emby-button-focusscale"><span>Generate New Pin</span></button>+ </div>++ <form class="manualLoginForm">++ <div style="height:0; overflow: hidden;"><input type="text" name="fakeusernameremembered" tabindex="-1"><input type="password" name="fakepasswordremembered" tabindex="-1"></div>++ <div class="inputContainer">+ <label class="inputLabel" for="embyinput0">Email</label><input is="emby-input" type="text" class="txtUser emby-input" required="required" label="Email" id="embyinput0" autocomplete="off" data-av-field-type="username" data-av-autocomplete="off">+ <div class="fieldDescription">This is the email address that you used to setup your Emby Connect account.</div>+ </div>+ <div class="inputContainer">+ <label class="inputLabel" for="embyinput1">Password</label><input is="emby-input" type="password" class="txtPassword emby-input" required="required" label="Password" id="embyinput1">+ </div>+ <button is="emby-button" type="submit" class="raised accent block emby-button emby-button-focusscale">+ <span>Sign In</span>+ </button>++ <a is="emby-linkbutton" class="raised cancel block btnForgotPassword emby-button emby-button-focusscale" target="_blank" href="https://emby.media/community/index.php?/lostpassword" data-autohide="true">+ <span>Forgot Password?</span>+ </a>++ <div class="terms hide fieldDescription" style="margin-top: 2em; color: #ccc;">Emby is designed to help you manage your personal media library, such as home videos and photos. Please see our <a is="emby-linkbutton" class="lnkTerms button-link emby-button emby-button-focusscale" href="https://emby.media/terms" target="_blank">terms of use</a>. The use of any Emby software constitutes acceptance of these terms.</div>+ </form>++ <div class="skip" style="text-align: center;">+ <button is="emby-button" class="raised block btnSkipConnect emby-button emby-button-focusscale">+ <span>Skip</span>+ </button>+ <div class="fieldDescription">Connect to server manually</div>+ </div>++ <div style="margin-top: 1.5em;">+ <!-- The new user section is not needed for the pin login because the pin page has a signup link. Keep this around for when manual connect login is added. -->+ <div class="newUsers">+ <h2 style="margin-bottom:0;">New Users</h2>+ <a is="emby-linkbutton" class="raised cancel block btnSignup emby-button emby-button-focusscale" href="https://emby.media/community/index.php?/register" target="_blank">+ <span>Sign Up</span>+ </a>+ </div>+ <div class="terms fieldDescription" style="margin-top: .5em;"></div>+ </div>+ </div>+</div>diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/hidden-fields-various.html b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/hidden-fields-various.htmlnew file mode 100644index 000000000..0aab80223--- /dev/null+++ b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/hidden-fields-various.html@@ -0,0 +1,45 @@+<!-- Test form with various hidden field techniques -->+<form>+ <!-- Technique 1: height:0 overflow:hidden (Emby-style) -->+ <div style="height:0; overflow: hidden;">+ <input type="text" name="fake1" tabindex="-1">+ <input type="password" name="fake1pass" tabindex="-1">+ </div>++ <!-- Technique 2: width:0 -->+ <div style="width:0;">+ <input type="text" name="fake2">+ <input type="password" name="fake2pass">+ </div>++ <!-- Technique 3: position absolute off-screen -->+ <div style="position:absolute; left:-9999px;">+ <input type="text" name="fake3">+ <input type="password" name="fake3pass">+ </div>++ <!-- Technique 4: honeypot naming -->+ <input type="text" name="honeypot" style="display:none;">+ <input type="text" name="hp-field" style="display:none;">+ <input type="text" name="bot_check" style="display:none;">+ <input type="text" name="antispam-field" style="display:none;">++ <!-- Technique 5: aria-hidden -->+ <input type="text" name="hidden-username" aria-hidden="true">+ <input type="password" name="hidden-password" aria-hidden="true">++ <!-- Technique 6: role="presentation" -->+ <input type="text" name="presentation-field" role="presentation">++ <!-- Real visible fields -->+ <div>+ <label for="real-email">Email</label>+ <input type="email" id="real-email" name="email">+ </div>+ <div>+ <label for="real-password">Password</label>+ <input type="password" id="real-password" name="password">+ </div>++ <button type="submit">Sign In</button>+</form>