Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update TOTP field detection, refactor FieldPatterns to specific/generic for more flexibility (#2223)
Leendert de Borst8391ffc8fd5f
diff --git a/apps/browser-extension/src/utils/formDetector/FieldPatterns.ts b/apps/browser-extension/src/utils/formDetector/FieldPatterns.tsindex 1492c21a8..e08516686 100644--- a/apps/browser-extension/src/utils/formDetector/FieldPatterns.ts+++ b/apps/browser-extension/src/utils/formDetector/FieldPatterns.ts@@ -1,14 +1,46 @@/**- * A single field pattern entry: terms that match a field type, plus optional- * terms that veto a match. Exclusions are applied with whole-word semantics- * and let us narrow broad include patterns (e.g. "token" hits "test-tokenfield"- * widgets that aren't actually 2FA inputs).+ * The set of terms that mark a field as a given type, split into two tiers:+ * - `specific`: strong terms that identify the field type on their own (e.g. "totp", "2fa").+ * - `generic`: terms too weak to stand alone (e.g. bare "code", "token"); optional.+ *+ * Every entry uses this same shape (omit `generic` when there's nothing weak to list) so+ * the patterns are uniform and easy to maintain.+ *+ * Field-level matching (a field's own name/id/class) uses both tiers — a field named+ * exactly "code" is still a strong hint. Looser context searches (scanning surrounding+ * headings/attributes) use only `specific` to avoid false positives on unrelated forms.+ */+export type IncludePatterns = {+ specific: string[];+ generic?: string[];+}++/**+ * A single field pattern entry: terms that match a field type (see {@link IncludePatterns}),+ * plus optional terms that veto a match. Exclusions are applied with whole-word semantics+ * and let us narrow broad include patterns (e.g. "token" hits "test-tokenfield" widgets+ * that aren't actually 2FA inputs).*/export type FieldPatternEntry = {- include: string[];+ include: IncludePatterns;exclude?: string[];}+/**+ * All include terms for an entry (specific + generic). Used by field-level matching.+ */+export function includeTerms(entry: FieldPatternEntry): string[] {+ return [...entry.include.specific, ...(entry.include.generic ?? [])];+}++/**+ * Only the strong (specific) include terms for an entry, omitting generic ones.+ * Used by looser context searches that need high precision.+ */+export function specificIncludeTerms(entry: FieldPatternEntry): string[] {+ return entry.include.specific;+}+/*** Type for field patterns. These patterns are used to detect individual fields in the form.*/@@ -71,23 +103,29 @@ export type FieldExclusionPatterns = string[];* include terms (e.g. TOTP's "token" matches "test-tokenfield" widgets).*/export const EnglishFieldPatterns: FieldPatterns = {- username: { include: ['username', 'login', 'identifier', 'user'] },- fullName: { include: ['fullname', 'full-name', 'full name'] },- firstName: { include: ['firstname', 'first-name', 'first_name', 'fname', 'name', 'given-name'] },- lastName: { include: ['lastname', 'last-name', 'last_name', 'lname', 'surname', 'family-name'] },- email: { include: ['email', 'mail', 'emailaddress'] },- emailConfirm: { include: ['confirm', 'verification', 'repeat', 'retype', 'verify', 'email2'] },+ username: { include: { specific: ['username', 'login', 'identifier', 'user'] } },+ fullName: { include: { specific: ['fullname', 'full-name', 'full name'] } },+ firstName: { include: { specific: ['firstname', 'first-name', 'first_name', 'fname', 'name', 'given-name'] } },+ lastName: { include: { specific: ['lastname', 'last-name', 'last_name', 'lname', 'surname', 'family-name'] } },+ email: { include: { specific: ['email', 'mail', 'emailaddress'] } },+ emailConfirm: { include: { specific: ['confirm', 'verification', 'repeat', 'retype', 'verify', 'email2'] } },password: {- include: ['password', 'pwd', 'pass'],+ include: { specific: ['password', 'pwd', 'pass'] },exclude: ['otp', 'totp', 'tfa', '2fa', 'mfa', 'twofa', 'authenticator', 'one-time', 'onetime', 'verification-code', 'verificationcode', 'two-factor', 'second-factor', 'auth-code', 'security-code', 'six-digit']},- birthdate: { include: ['birthdate', 'birth-date', 'dob', 'date-of-birth'] },- gender: { include: ['gender', 'sex'] },- birthDateDay: { include: ['-day', 'birthdate_d', 'birthdayday', '_day', 'day'] },- birthDateMonth: { include: ['-month', 'birthdate_m', 'birthdaymonth', '_month', 'month'] },- birthDateYear: { include: ['-year', 'birthdate_y', 'birthdayyear', '_year', 'year'] },+ birthdate: { include: { specific: ['birthdate', 'birth-date', 'dob', 'date-of-birth'] } },+ gender: { include: { specific: ['gender', 'sex'] } },+ birthDateDay: { include: { specific: ['-day', 'birthdate_d', 'birthdayday', '_day', 'day'] } },+ birthDateMonth: { include: { specific: ['-month', 'birthdate_m', 'birthdaymonth', '_month', 'month'] } },+ birthDateYear: { include: { specific: ['-year', 'birthdate_y', 'birthdayyear', '_year', 'year'] } },totp: {- include: ['totp', 'otp', 'one-time', 'onetime', 'six-digit', 'digit-code', 'token', 'authenticator', 'authentication', '2fa', 'twofa', 'two-factor', 'mfa', 'security-code', 'auth-code', 'passcode', 'pin-code', 'pincode', 'google_code', 'verification-code', 'verificationcode', 'tfa', 'tfacode', 'second-factor', 'one time password', 'code'],+ include: {+ // specific: strong terms that can clearly identify a TOTP field on their own.+ specific: ['totp', 'otp', 'one-time', 'onetime', 'six-digit', 'digit-code', 'authenticator', 'authentication', '2fa', 'twofa', 'two-factor', 'mfa', 'security-code', 'auth-code', 'passcode', 'pin-code', 'pincode', 'google_code', 'verification-code', 'verificationcode', 'tfa', 'tfacode', 'second-factor', 'one time password'],+ // generic: terms too weak to match on their own (can create false positives), but can be used for additional context matching.+ generic: ['token', 'code']+ },+ // exclude: whole-word terms that if found negate the TOTP match.exclude: ['test']}};@@ -185,19 +223,19 @@ export const EnglishStopWords = new Set([* Dutch field patterns used to detect Dutch form fields.*/export const DutchFieldPatterns: FieldPatterns = {- username: { include: ['gebruikersnaam', 'gebruiker', 'login', 'identifier'] },- fullName: { include: ['volledige naam'] },- firstName: { include: ['voornaam', 'naam'] },- lastName: { include: ['achternaam'] },- email: { include: ['e-mailadres', 'e-mail'] },- emailConfirm: { include: ['bevestig', 'herhaal', 'verificatie'] },- password: { include: ['wachtwoord', 'pwd'] },- birthdate: { include: ['geboortedatum', 'geboorte-datum'] },- gender: { include: ['geslacht', 'aanhef'] },- birthDateDay: { include: ['dag'] },- birthDateMonth: { include: ['maand'] },- birthDateYear: { include: ['jaar'] },- totp: { include: ['verificatiecode', 'eenmalig', 'authenticatie', 'tweefactor', 'beveiligingscode'] }+ username: { include: { specific: ['gebruikersnaam', 'gebruiker', 'login', 'identifier'] } },+ fullName: { include: { specific: ['volledige naam'] } },+ firstName: { include: { specific: ['voornaam', 'naam'] } },+ lastName: { include: { specific: ['achternaam'] } },+ email: { include: { specific: ['e-mailadres', 'e-mail'] } },+ emailConfirm: { include: { specific: ['bevestig', 'herhaal', 'verificatie'] } },+ password: { include: { specific: ['wachtwoord', 'pwd'] } },+ birthdate: { include: { specific: ['geboortedatum', 'geboorte-datum'] } },+ gender: { include: { specific: ['geslacht', 'aanhef'] } },+ birthDateDay: { include: { specific: ['dag'] } },+ birthDateMonth: { include: { specific: ['maand'] } },+ birthDateYear: { include: { specific: ['jaar'] } },+ totp: { include: { specific: ['verificatiecode', 'eenmalig', 'authenticatie', 'tweefactor', 'beveiligingscode'] } }};/**@@ -295,11 +333,14 @@ import { TranslationEmailPatterns, TranslationUsernamePatterns, TranslationPassw/*** Merge per-field-type entries (include + optional exclude) from one or more- * languages, deduping each list while preserving original order.+ * languages, deduping each list while preserving original order. The specific /+ * generic split of `include` is preserved across the merge.*/function mergeEntries(...entries: FieldPatternEntry[]): FieldPatternEntry {- const include = [...new Set(entries.flatMap(e => e.include))];+ const specific = [...new Set(entries.flatMap(e => e.include.specific))];+ const generic = [...new Set(entries.flatMap(e => e.include.generic ?? []))];const exclude = [...new Set(entries.flatMap(e => e.exclude ?? []))];+ const include: IncludePatterns = generic.length > 0 ? { specific, generic } : { specific };return exclude.length > 0 ? { include, exclude } : { include };}@@ -310,7 +351,7 @@ function mergeEntries(...entries: FieldPatternEntry[]): FieldPatternEntry {* - Translation-based patterns from all supported languages*/export const CombinedFieldPatterns: FieldPatterns = {- username: mergeEntries(EnglishFieldPatterns.username, DutchFieldPatterns.username, { include: TranslationUsernamePatterns }),+ username: mergeEntries(EnglishFieldPatterns.username, DutchFieldPatterns.username, { include: { specific: TranslationUsernamePatterns } }),fullName: mergeEntries(EnglishFieldPatterns.fullName, DutchFieldPatterns.fullName),firstName: mergeEntries(EnglishFieldPatterns.firstName, DutchFieldPatterns.firstName),lastName: mergeEntries(EnglishFieldPatterns.lastName, DutchFieldPatterns.lastName),@@ -322,9 +363,9 @@ export const CombinedFieldPatterns: FieldPatterns = {** Translation patterns are added last to catch all language variations (e.g., "E-post" in Swedish)*/- email: mergeEntries(DutchFieldPatterns.email, EnglishFieldPatterns.email, { include: TranslationEmailPatterns }),+ email: mergeEntries(DutchFieldPatterns.email, EnglishFieldPatterns.email, { include: { specific: TranslationEmailPatterns } }),emailConfirm: mergeEntries(EnglishFieldPatterns.emailConfirm, DutchFieldPatterns.emailConfirm),- password: mergeEntries(EnglishFieldPatterns.password, DutchFieldPatterns.password, { include: TranslationPasswordPatterns }),+ password: mergeEntries(EnglishFieldPatterns.password, DutchFieldPatterns.password, { include: { specific: TranslationPasswordPatterns } }),birthdate: mergeEntries(EnglishFieldPatterns.birthdate, DutchFieldPatterns.birthdate),gender: mergeEntries(EnglishFieldPatterns.gender, DutchFieldPatterns.gender),birthDateDay: mergeEntries(EnglishFieldPatterns.birthDateDay, DutchFieldPatterns.birthDateDay),diff --git a/apps/browser-extension/src/utils/formDetector/FormDetector.ts b/apps/browser-extension/src/utils/formDetector/FormDetector.tsindex e65f7f45d..6b09c0244 100644--- a/apps/browser-extension/src/utils/formDetector/FormDetector.ts+++ b/apps/browser-extension/src/utils/formDetector/FormDetector.ts@@ -1,4 +1,4 @@-import { CombinedEmailVerificationPatterns, CombinedFieldExclusionPatterns, CombinedFieldPatterns, CombinedGenderOptionPatterns, CombinedStopWords, FieldPatternEntry } from "./FieldPatterns";+import { CombinedEmailVerificationPatterns, CombinedFieldExclusionPatterns, CombinedFieldPatterns, CombinedGenderOptionPatterns, CombinedStopWords, FieldPatternEntry, includeTerms, specificIncludeTerms } from "./FieldPatterns";import { DetectedFieldType, FormFields } from "./types/FormFields";/**@@ -624,7 +624,7 @@ export class FormDetector {excludeElements: HTMLInputElement[] = [],checkVisibility: boolean = true): HTMLInputElement[] {- const patterns = entry.include;+ const patterns = includeTerms(entry);// Query for standard input elements, select elements, and elements with type attributesconst standardCandidates = form? Array.from(form.querySelectorAll<HTMLElement>('input, select, [type]'))@@ -993,10 +993,10 @@ export class FormDetector {/** Check if label contains BOTH username and email patterns (dual-purpose field)*/- const labelHasUsername = CombinedFieldPatterns.username.include.some(pattern =>+ const labelHasUsername = includeTerms(CombinedFieldPatterns.username).some(pattern =>labelText.includes(pattern));- const labelHasEmail = CombinedFieldPatterns.email.include.some(pattern =>+ const labelHasEmail = includeTerms(CombinedFieldPatterns.email).some(pattern =>labelText.includes(pattern));@@ -1006,10 +1006,10 @@ export class FormDetector {* 2. AND the field's name/id contains username pattern but NOT email pattern*/if (labelHasUsername && labelHasEmail) {- const hasUsernameInNameOrId = CombinedFieldPatterns.username.include.some(pattern =>+ const hasUsernameInNameOrId = includeTerms(CombinedFieldPatterns.username).some(pattern =>fieldAttributes.includes(pattern));- const hasEmailInNameOrId = CombinedFieldPatterns.email.include.some(pattern =>+ const hasEmailInNameOrId = includeTerms(CombinedFieldPatterns.email).some(pattern =>fieldAttributes.includes(pattern));@@ -1465,11 +1465,52 @@ export class FormDetector {if (maxLength === 1 && inputMode === 'numeric') {return input;}++ /*+ * Check for a 6-character code field whose own attributes give no 2FA signal+ * but whose surrounding form context clearly identifies a two-factor challenge.+ * The strong context patterns keep this from matching unrelated short fields (zip/coupon/PIN inputs).+ */+ if (maxLength === 6 &&+ (inputType === 'text' || inputType === 'tel' || inputType === 'number') &&+ this.hasTotpFormContext(input)) {+ return input;+ }}return null;}+ /**+ * Check whether the form context surrounding an input clearly indicates a two-factor+ * challenge. Walks up a bounded number of ancestors collecting identifying attributes+ * (id/name/class) and nearby heading/legend text, then searches it for the TOTP field+ * patterns. Only the `specific` TOTP terms are used (generic ones like "code"/"token"+ * are skipped) so this fires on an unambiguous 2FA signal; used to classify short code+ * fields that carry no 2FA signal of their own.+ */+ private hasTotpFormContext(input: HTMLInputElement): boolean {+ const parts: string[] = [];+ let node: HTMLElement | null = input;+ let depth = 0;++ while (node && depth < 8) {+ parts.push(node.id);+ parts.push(node.getAttribute('name') ?? '');+ parts.push(node.getAttribute('class') ?? '');++ // Capture heading/legend text that sits as a direct child at this level.+ const headings = node.querySelectorAll(':scope > h1, :scope > h2, :scope > h3, :scope > h4, :scope > h5, :scope > h6, :scope > legend');+ headings.forEach(heading => parts.push(heading.textContent ?? ''));++ node = node.parentElement;+ depth++;+ }++ const context = parts.join(' ').toLowerCase();+ return specificIncludeTerms(CombinedFieldPatterns.totp).some(pattern => context.includes(pattern));+ }+/*** Check if a form contains a likely username or email field.* Skips visibility checks when user clicked a field to support transition animations.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.tsindex ce7c547d2..7402b1380 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@@ -155,6 +155,37 @@ describe('FormDetector TOTP tests', () => {});});+ describe('English TOTP form 13 detection (nic.vi - generic name="key" maxlength=6)', () => {+ const htmlFile = 'en-totp-form13.html';+ let document: Document;+ let formDetector: FormDetector;++ beforeEach(() => {+ const dom = createTestDom(htmlFile);+ document = dom.window.document;+ });++ testField(FormField.Totp, 'key', htmlFile);++ it('should detect the code input field as TOTP when focused', () => {+ const totpInput = document.getElementById('key') as HTMLInputElement;+ expect(totpInput).toBeTruthy();++ formDetector = new FormDetector(document, totpInput);++ const detectedType = formDetector.getDetectedFieldType();+ expect(detectedType).toBe(DetectedFieldType.Totp);+ });++ it('should find the TOTP field when searching from form', () => {+ formDetector = new FormDetector(document, document.body);++ const form = formDetector.getForm();+ expect(form?.totpField).toBeTruthy();+ expect(form?.totpField?.id).toBe('key');+ });+ });+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-form13.html b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/en-totp-form13.htmlnew file mode 100644index 000000000..2121f4ad4--- /dev/null+++ b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/en-totp-form13.html@@ -0,0 +1,41 @@+<!DOCTYPE html>+<html>+<body>+ <div class="card">+ <div class="card-body px-sm-5 py-5">+ <h3 class="card-title">Two-Factor Authentication</h3>++ <div class="alert alert-warning text-center">+ Your second factor is required to complete login.+ </div>++ <form method="post" action="/index.php/login/challenge/verify" id="frmTwoFactorChallenge">+ <input type="hidden" name="token" value="2b8c371a0690b24ec8b7c84b88c36090a6c68637">+ <div class="margin-bottom">+ <div align="center">+ <input id="key" type="text" name="key" maxlength="6" class="form-control input-lg form-control-lg" autofocus="" autocomplete="off" data-av-autocomplete="off">+ <br>+ <input id="btnLogin" type="submit" class="btn btn-primary btn-block btn-lg" value="Login">+ </div>+ </div>+ </form>++ <form method="post" action="/index.php/login/challenge/backup" id="frmTwoFactorBackup" class="w-hidden" style="display: none;">+ <input type="hidden" name="token" value="2b8c371a0690b24ec8b7c84b88c36090a6c68637">+ <div class="margin-bottom">+ <input type="text" name="twofabackupcode" class="form-control form-control-lg" placeholder="Enter Your Backup Code to Login">+ <br>+ <button type="submit" class="btn btn-primary btn-lg btn-block" id="btnLoginBackup">+ Login+ </button>+ </div>+ <p class="text-center">+ <a href="#" class="btn btn-default btn-block mt-2" id="backupCodeCancel">+ Cancel+ </a>+ </p>+ </form>+ </div>+ </div>+</body>+</html>