Skip to content

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

Improve browser extension 2FA autofill for separate digit forms (#2487)

Leendert de Borstca6a88ac7682
diff --git a/apps/browser-extension/src/entrypoints/contentScript/Form.ts b/apps/browser-extension/src/entrypoints/contentScript/Form.ts
index c2fcce2fb..ccb1b6ae1 100644
--- a/apps/browser-extension/src/entrypoints/contentScript/Form.ts
+++ b/apps/browser-extension/src/entrypoints/contentScript/Form.ts
@@ -6,6 +6,7 @@ import { LOGO_MARK_SVG } from '@/utils/constants/logo';
import { logFailure } from '@/utils/Diagnostics';
import { FormDetector } from '@/utils/formDetector/FormDetector';
import { FormFiller } from '@/utils/formDetector/FormFiller';
+import { findSplitTotpInputs, isSplitTotpBox } from '@/utils/formDetector/SplitTotpInputs';
import { DetectedFieldType } from '@/utils/formDetector/types/FormFields';
import { sendMessage } from '@/utils/messaging/ExtensionMessaging';
import { ClickValidator } from '@/utils/security/ClickValidator';
@@ -207,14 +208,7 @@ export function injectIcon(input: HTMLInputElement, container: HTMLElement, fiel
* Skip icon for split TOTP inputs (single-digit fields)
* These are too narrow to show the icon nicely
*/
- const inputMode = actualInput.getAttribute('inputmode');
- const pattern = actualInput.getAttribute('pattern');
- const isNumericSingleDigit = actualInput.maxLength === 1 &&
- (inputMode === 'numeric' ||
- pattern === '[0-9]*' ||
- pattern === '\\d*');
-
- if (isNumericSingleDigit) {
+ if (isSplitTotpBox(actualInput)) {
return;
}
@@ -427,6 +421,13 @@ export async function fillTotpCode(item: ItemRef, input: HTMLInputElement): Prom
return;
}
+ // Split code forms (one box per digit) get one character per box.
+ const splitInputs = findSplitTotpInputs(input, response.code.length);
+ if (splitInputs) {
+ splitInputs.forEach((box, index) => fillSplitTotpBox(box, response.code![index]));
+ return;
+ }
+
// Fill the TOTP field
input.value = response.code;
@@ -434,6 +435,16 @@ export async function fillTotpCode(item: ItemRef, input: HTMLInputElement): Prom
triggerInputEvents(input);
}
+/**
+ * Fill one box of a split TOTP form.
+ */
+function fillSplitTotpBox(box: HTMLInputElement, char: string): void {
+ box.dispatchEvent(new KeyboardEvent('keydown', { key: char, bubbles: true }));
+ box.value = char;
+ triggerInputEvents(box);
+ box.dispatchEvent(new KeyboardEvent('keyup', { key: char, bubbles: true }));
+}
+
/**
* Trigger input events for an element to trigger form validation
* which some websites require before the "continue" button is enabled.
diff --git a/apps/browser-extension/src/utils/formDetector/FormDetector.ts b/apps/browser-extension/src/utils/formDetector/FormDetector.ts
index a0a2cc5d1..ab34a47e7 100644
--- a/apps/browser-extension/src/utils/formDetector/FormDetector.ts
+++ b/apps/browser-extension/src/utils/formDetector/FormDetector.ts
@@ -2,6 +2,7 @@ import { devLog } from "@/utils/devLogger/DevLogger";
import { closestAcrossShadow, collectShadowRoots, getComposedParentElement, getQueryRoot, getShadowHostChain, queryAllDeep } from "@/utils/ShadowDom";
import { CombinedEmailVerificationPatterns, CombinedFieldExclusionPatterns, CombinedFieldPatterns, CombinedGenderOptionPatterns, CombinedStopWords, type FieldPatternEntry, includeTerms, specificIncludeTerms } from "./FieldPatterns";
+import { isInSameSplitTotpGroup } from "./SplitTotpInputs";
import { DetectedFieldType, type FormFields } from "./types/FormFields";
/**
@@ -1747,7 +1748,7 @@ export class FormDetector {
const checkVisibility = false;
const totpField = this.findTotpField(formWrapper as HTMLFormElement | null);
- const isTotpField = totpField !== null && elementsToCheck.includes(totpField);
+ const isTotpField = totpField !== null && elementsToCheck.some(el => el === totpField || (el?.tagName === 'INPUT' && isInSameSplitTotpGroup(el as HTMLInputElement, totpField)));
// A 2FA field that carries an unambiguous signal of its own is classified before the credential field types.
if (isTotpField && this.hasTotpFormContext(totpField)) {
diff --git a/apps/browser-extension/src/utils/formDetector/SplitTotpInputs.ts b/apps/browser-extension/src/utils/formDetector/SplitTotpInputs.ts
new file mode 100644
index 000000000..2d32a481d
--- /dev/null
+++ b/apps/browser-extension/src/utils/formDetector/SplitTotpInputs.ts
@@ -0,0 +1,67 @@
+/** How many ancestors of the clicked box are searched for the rest of the group. */
+const MAX_ANCESTOR_DEPTH = 5;
+
+/** Input types a single-character code box can have. */
+const SPLIT_BOX_TYPES = new Set(['text', 'tel', 'number', 'password']);
+
+/** Most boxes a split code group can have (TOTP codes are 6 to 8 digits). */
+const MAX_SPLIT_BOXES = 8;
+
+/**
+ * Whether an input is a visible single-character box, as used by split TOTP forms.
+ */
+export function isSplitTotpBox(input: HTMLInputElement): boolean {
+ if (input.maxLength !== 1 || input.hidden || input.disabled || !SPLIT_BOX_TYPES.has(input.type?.toLowerCase())) {
+ return false;
+ }
+
+ const style = input.ownerDocument.defaultView?.getComputedStyle(input);
+ return style?.display !== 'none' && style?.visibility !== 'hidden';
+}
+
+/**
+ * Find the split TOTP boxes that together hold a code of `codeLength` characters, in document order.
+ */
+export function findSplitTotpInputs(input: HTMLInputElement, codeLength: number): HTMLInputElement[] | null {
+ if (!isSplitTotpBox(input)) {
+ return null;
+ }
+
+ /*
+ * Walk up until an ancestor holds exactly `codeLength` boxes. Boxes are often wrapped one per
+ * element (or grouped 3 + 3), so the group only shows up a few levels up; stop once an ancestor
+ * holds more boxes than the code has characters, as that is no longer a single code group.
+ */
+ let ancestor = input.parentElement;
+ for (let depth = 0; ancestor && depth < MAX_ANCESTOR_DEPTH; depth++) {
+ const boxes = Array.from(ancestor.querySelectorAll<HTMLInputElement>('input')).filter(isSplitTotpBox);
+ if (boxes.length === codeLength) {
+ return boxes;
+ }
+ if (boxes.length > codeLength) {
+ return null;
+ }
+ ancestor = ancestor.parentElement;
+ }
+
+ return null;
+}
+
+/**
+ * Whether two inputs are boxes of the same split TOTP group.
+ */
+export function isInSameSplitTotpGroup(a: HTMLInputElement, b: HTMLInputElement): boolean {
+ if (a === b || !isSplitTotpBox(a) || !isSplitTotpBox(b)) {
+ return a === b;
+ }
+
+ let ancestor = a.parentElement;
+ for (let depth = 0; ancestor && depth < MAX_ANCESTOR_DEPTH; depth++) {
+ if (ancestor.contains(b)) {
+ return Array.from(ancestor.querySelectorAll<HTMLInputElement>('input')).filter(isSplitTotpBox).length <= MAX_SPLIT_BOXES;
+ }
+ ancestor = ancestor.parentElement;
+ }
+
+ return false;
+}
diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.totp.test.ts b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.totp.test.ts
index d580ec34b..3e2eb7b4c 100644
--- a/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.totp.test.ts
+++ b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.totp.test.ts
@@ -5,6 +5,7 @@ import { JSDOM } from 'jsdom';
import { beforeEach, describe, expect, it } from 'vitest';
import { FormDetector } from '@/utils/formDetector/FormDetector';
+import { findSplitTotpInputs } from '@/utils/formDetector/SplitTotpInputs';
import { DetectedFieldType } from '@/utils/formDetector/types/FormFields';
import { FormField, testField, createTestDom } from './TestUtils';
@@ -230,6 +231,54 @@ describe('FormDetector TOTP tests', () => {
});
});
+ describe('English TOTP form 15 detection (split boxes without inputmode)', () => {
+ const htmlFile = 'en-totp-form15.html';
+ let document: Document;
+
+ beforeEach(() => {
+ const dom = createTestDom(htmlFile);
+ document = dom.window.document;
+ });
+
+ testField(FormField.Totp, 'pincode-1', htmlFile);
+
+ it('should detect every box as TOTP, not only the first', () => {
+ for (let i = 1; i <= 6; i++) {
+ const box = document.getElementById(`pincode-${i}`) as HTMLInputElement;
+ expect(new FormDetector(document, box).getDetectedFieldType()).toBe(DetectedFieldType.Totp);
+ }
+ });
+
+ it('should find all six boxes in order from any box', () => {
+ const expectedIds = ['pincode-1', 'pincode-2', 'pincode-3', 'pincode-4', 'pincode-5', 'pincode-6'];
+ for (const id of expectedIds) {
+ const boxes = findSplitTotpInputs(document.getElementById(id) as HTMLInputElement, 6);
+ expect(boxes?.map(box => box.id)).toEqual(expectedIds);
+ }
+ });
+
+ it('should not treat the boxes as a split group for a code of a different length', () => {
+ expect(findSplitTotpInputs(document.getElementById('pincode-1') as HTMLInputElement, 8)).toBeNull();
+ });
+
+ it('should not treat the hidden token field as a split box', () => {
+ expect(findSplitTotpInputs(document.getElementById('token') as HTMLInputElement, 6)).toBeNull();
+ });
+ });
+
+ describe('Single code fields are not split groups', () => {
+ it('should return null for a regular TOTP field', () => {
+ const document = createTestDom('en-totp-form1.html').window.document;
+ expect(findSplitTotpInputs(document.getElementById('otp') as HTMLInputElement, 6)).toBeNull();
+ });
+
+ it('should find the split group of the Riot Games form', () => {
+ const document = createTestDom('en-totp-form7.html').window.document;
+ const boxes = findSplitTotpInputs(document.getElementById('riot-mfa-3') as HTMLInputElement, 6);
+ expect(boxes?.map(box => box.id)).toEqual(['riot-mfa-0', 'riot-mfa-1', 'riot-mfa-2', 'riot-mfa-3', 'riot-mfa-4', 'riot-mfa-5']);
+ });
+ });
+
describe('Email verification form should NOT be detected as TOTP', () => {
it('should NOT detect English email verification form as TOTP', () => {
const htmlFile = 'en-email-verification-form1.html';
diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/en-totp-form15.html b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/en-totp-form15.html
new file mode 100644
index 000000000..318884eb3
--- /dev/null
+++ b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/en-totp-form15.html
@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+ <title>Verify your identity</title>
+</head>
+
+<body>
+ <div class="ui segment">
+ <h3 class="ui header text-center">
+ Verify your identity
+ </h3>
+
+ <input id="username" type="hidden" value="[email protected]">
+
+ <div class="segment-form">
+ <form action="https://login.example.com/commonauth" method="post" id="totpForm" class="ui large form otp-form">
+ <p class="text-center" id="instruction">Enter the verification code generated by your authenticator app.</p>
+ <div class="field">
+ <input hidden="" type="text" id="token" name="token" class="form-control" placeholder="Verification code">
+ </div>
+
+ <div class="equal width fields">
+ <div class="field mt-5">
+ <input class="text-center p-3" id="pincode-1" name="pincode-1" tabindex="1" placeholder="·" maxlength="1" onkeyup="movetoNext(this, 'pincode-2', null)" autocomplete="off" autofocus="">
+ </div>
+ <div class="field mt-5">
+ <input class="text-center p-3" id="pincode-2" name="pincode-2" tabindex="2" placeholder="·" maxlength="1" onkeyup="movetoNext(this, 'pincode-3', 'pincode-1')" autocomplete="off">
+ </div>
+ <div class="field mt-5">
+ <input class="text-center p-3" id="pincode-3" name="pincode-3" tabindex="3" placeholder="·" maxlength="1" onkeyup="movetoNext(this, 'pincode-4', 'pincode-2')" autocomplete="off">
+ </div>
+ <div class="field mt-5">
+ <input class="text-center p-3" id="pincode-4" name="pincode-4" tabindex="4" placeholder="·" maxlength="1" onkeyup="movetoNext(this, 'pincode-5', 'pincode-3')" autocomplete="off">
+ </div>
+ <div class="field mt-5">
+ <input class="text-center p-3" id="pincode-5" name="pincode-5" tabindex="5" placeholder="·" maxlength="1" onkeyup="movetoNext(this, 'pincode-6', 'pincode-4')" autocomplete="off">
+ </div>
+ <div class="field mt-5">
+ <input class="text-center p-3" id="pincode-6" name="pincode-6" tabindex="6" placeholder="·" maxlength="1" onkeyup="movetoNext(this, null, 'pincode-5')" autocomplete="off">
+ </div>
+ </div>
+
+ <input id="sessionDataKey" type="hidden" name="sessionDataKey" value="00000000-0000-0000-0000-000000000000">
+ <div class="ui divider hidden"></div>
+
+ <input id="multiOptionURI" type="hidden" name="multiOptionURI" value="null">
+ <div class="ui divider hidden"></div>
+ <div>
+ <input type="submit" id="subButton" onclick="sub(); return false;" value="Continue" class="ui primary fluid large button" disabled="disabled">
+ </div>
+ </form>
+ </div>
+
+ <div class="ui" id="enrol-msg">Haven't setup your TOTP authenticator yet? <a href="mailto:[email protected]">Contact support</a></div>
+ </div>
+</body>
+
+</html>