Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add 2FA TOTP test for false-positive email code confirm being recognized as 2FA (#1788)
Leendert de Borst0d0b25baa642
diff --git a/apps/browser-extension/src/utils/formDetector/FieldPatterns.ts b/apps/browser-extension/src/utils/formDetector/FieldPatterns.tsindex 68ad6d86f..3152690c5 100644--- a/apps/browser-extension/src/utils/formDetector/FieldPatterns.ts+++ b/apps/browser-extension/src/utils/formDetector/FieldPatterns.ts@@ -34,6 +34,19 @@ export type DateOptionPatterns = {months: string[][];}+/**+ * Type for email verification context patterns. These patterns are used to detect if a form+ * is for email verification (not TOTP/2FA). Each pattern should detect common phrases+ * found in email verification flows.+ */+export type EmailVerificationPatterns = {+ verifyEmail: RegExp[];+ emailVerification: RegExp[];+ sentCodeToEmail: RegExp[];+ checkEmail: RegExp[];+ confirmEmail: RegExp[];+}+/*** English field patterns to detect English form fields.*/@@ -71,6 +84,39 @@ export const EnglishDateOptionPatterns: DateOptionPatterns = {],};+/**+ * English email verification patterns. These are used to detect email verification forms+ * (not TOTP/2FA forms) by analyzing page content.+ */+export const EnglishEmailVerificationPatterns: EmailVerificationPatterns = {+ verifyEmail: [+ /verify\s+your\s+email/i,+ /verify\s+email\s+address/i,+ /email\s+needs\s+verification/i+ ],+ emailVerification: [+ /email\s+verification/i,+ /verification\s+email/i+ ],+ sentCodeToEmail: [+ /we\s+sent\s+(a\s+)?code\s+to.*@/i,+ /sent\s+(a\s+)?(verification\s+)?code\s+to.*@/i,+ /enter\s+the\s+code\s+we\s+sent\s+to.*@/i,+ /code\s+has\s+been\s+sent\s+to.*@/i,+ /we've\s+sent\s+(a\s+)?code\s+to.*@/i+ ],+ checkEmail: [+ /check\s+your\s+email/i,+ /check\s+your\s+inbox/i,+ /please\s+check\s+your\s+email/i+ ],+ confirmEmail: [+ /confirm\s+your\s+email/i,+ /email\s+confirmation/i,+ /confirm\s+email\s+address/i+ ]+};+/*** English words to filter out from page titles during autofill matching to* prevent generic words from causing false positives.@@ -147,6 +193,38 @@ export const DutchDateOptionPatterns: DateOptionPatterns = {],};+/**+ * Dutch email verification patterns. These are used to detect email verification forms+ * (not TOTP/2FA forms) by analyzing page content.+ */+export const DutchEmailVerificationPatterns: EmailVerificationPatterns = {+ verifyEmail: [+ /verifieer\s+je\s+e-?mail/i,+ /e-?mail\s+verifi[eë]ren/i,+ /controleer\s+je\s+e-?mail/i+ ],+ emailVerification: [+ /e-?mail\s+verificatie/i,+ /verificatie\s+e-?mail/i+ ],+ sentCodeToEmail: [+ /we\s+hebben\s+(een\s+)?code\s+gestuurd\s+naar.*@/i,+ /(een\s+)?code\s+verzonden\s+naar.*@/i,+ /voer\s+de\s+code\s+in\s+die\s+we\s+naar.*@\s+hebben\s+gestuurd/i,+ /code\s+is\s+verzonden\s+naar.*@/i+ ],+ checkEmail: [+ /controleer\s+je\s+e-?mail/i,+ /bekijk\s+je\s+inbox/i,+ /controleer\s+je\s+inbox/i+ ],+ confirmEmail: [+ /bevestig\s+je\s+e-?mail/i,+ /e-?mail\s+bevestiging/i,+ /bevestig\s+e-?mailadres/i+ ]+};+/*** Dutch words to filter out from page titles during autofill matching to* prevent generic words from causing false positives.@@ -240,3 +318,30 @@ export const CombinedStopWords = new Set([...EnglishStopWords,...DutchStopWords]);++/**+ * Combined email verification patterns from all supported languages. These are used to detect+ * email verification forms (not TOTP/2FA forms) by analyzing page content.+ */+export const CombinedEmailVerificationPatterns: EmailVerificationPatterns = {+ verifyEmail: [+ ...EnglishEmailVerificationPatterns.verifyEmail,+ ...DutchEmailVerificationPatterns.verifyEmail+ ],+ emailVerification: [+ ...EnglishEmailVerificationPatterns.emailVerification,+ ...DutchEmailVerificationPatterns.emailVerification+ ],+ sentCodeToEmail: [+ ...EnglishEmailVerificationPatterns.sentCodeToEmail,+ ...DutchEmailVerificationPatterns.sentCodeToEmail+ ],+ checkEmail: [+ ...EnglishEmailVerificationPatterns.checkEmail,+ ...DutchEmailVerificationPatterns.checkEmail+ ],+ confirmEmail: [+ ...EnglishEmailVerificationPatterns.confirmEmail,+ ...DutchEmailVerificationPatterns.confirmEmail+ ]+};diff --git a/apps/browser-extension/src/utils/formDetector/FormDetector.ts b/apps/browser-extension/src/utils/formDetector/FormDetector.tsindex d082dc631..77ab24be3 100644--- a/apps/browser-extension/src/utils/formDetector/FormDetector.ts+++ b/apps/browser-extension/src/utils/formDetector/FormDetector.ts@@ -1,4 +1,4 @@-import { CombinedFieldPatterns, CombinedGenderOptionPatterns, CombinedStopWords } from "./FieldPatterns";+import { CombinedEmailVerificationPatterns, CombinedFieldPatterns, CombinedGenderOptionPatterns, CombinedStopWords } from "./FieldPatterns";import { DetectedFieldType, FormFields } from "./types/FormFields";/**@@ -1007,11 +1007,45 @@ export class FormDetector {return totpField !== null && this.isElementVisible(totpField);}+ /**+ * Check if the page content indicates this is an email verification form.+ * Email verification forms should not be treated as TOTP/2FA forms.+ * Uses language-aware patterns to support multiple languages.+ */+ private isEmailVerificationForm(): boolean {+ // Get page text content for context analysis+ const bodyText = this.document.body?.textContent || '';+ const pageTitle = this.document.title || '';++ // Combine all language-aware email verification patterns+ const allPatterns = [+ ...CombinedEmailVerificationPatterns.verifyEmail,+ ...CombinedEmailVerificationPatterns.emailVerification,+ ...CombinedEmailVerificationPatterns.sentCodeToEmail,+ ...CombinedEmailVerificationPatterns.checkEmail,+ ...CombinedEmailVerificationPatterns.confirmEmail+ ];++ // Check if any email verification pattern matches+ for (const pattern of allPatterns) {+ if (pattern.test(bodyText) || pattern.test(pageTitle)) {+ return true;+ }+ }++ return false;+ }+/*** Find a TOTP/2FA input field in the form.* Uses pattern matching and heuristics specific to TOTP fields.*/private findTotpField(form: HTMLFormElement | null): HTMLInputElement | null {+ // Check if this is an email verification form (not TOTP/2FA)+ if (this.isEmailVerificationForm()) {+ return null;+ }+// First try pattern-based detectionconst candidates = this.findAllInputFields(form,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 2c50d4c07..9ce4039b5 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@@ -1,5 +1,9 @@+import { JSDOM } from 'jsdom';import { describe, expect, it } from 'vitest';+import { readFileSync } from 'fs';+import { join } from 'path';+import { FormDetector } from '@/utils/formDetector/FormDetector';import { FormField, testField } from './TestUtils';describe('FormDetector TOTP tests', () => {@@ -54,4 +58,55 @@ describe('FormDetector TOTP tests', () => {testField(FormField.Totp, 'riot-mfa-0', htmlFile);});++ 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';+ const html = readFileSync(join(__dirname, 'test-forms', htmlFile), 'utf-8');+ const dom = new JSDOM(html, {+ url: 'http://localhost',+ runScripts: 'dangerously',+ resources: 'usable'+ });+ const document = dom.window.document;++ // Set focus on the first input+ const focusedElement = document.getElementById('email-verify-0');+ if (!focusedElement) {+ throw new Error('Focus element not found in test HTML');+ }++ // Create a new form detector with the focused element+ const formDetector = new FormDetector(document, focusedElement);+ const result = formDetector.getForm();++ // The form should NOT be detected at all (null) or at least totpField should be null+ // because this is an email verification form, not a TOTP/2FA form+ expect(result?.totpField).toBeNull();+ });++ it('should NOT detect Dutch email verification form as TOTP', () => {+ const htmlFile = 'nl-email-verification-form1.html';+ const html = readFileSync(join(__dirname, 'test-forms', htmlFile), 'utf-8');+ const dom = new JSDOM(html, {+ url: 'http://localhost',+ runScripts: 'dangerously',+ resources: 'usable'+ });+ const document = dom.window.document;++ // Set focus on the first input+ const focusedElement = document.getElementById('nl-verify-0');+ if (!focusedElement) {+ throw new Error('Focus element not found in test HTML');+ }++ // Create a new form detector with the focused element+ const formDetector = new FormDetector(document, focusedElement);+ const result = formDetector.getForm();++ // The form should NOT be detected as TOTP because it's a Dutch email verification form+ expect(result?.totpField).toBeNull();+ });+ });});diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/en-email-verification-form1.html b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/en-email-verification-form1.htmlnew file mode 100644index 000000000..a5ae37398--- /dev/null+++ b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/en-email-verification-form1.html@@ -0,0 +1,57 @@+<!DOCTYPE html>+<html>+<head>+ <title>Verify Your Email - Riot Games</title>+</head>+<body>+ <div class="sc-ftWlEF jwaHMt">+ <div class="sc-bYOEQd fZKKpq main-background theme-arcane">+ <h5 class="sc-kFuwaP iNZiaL sc-eFqGBh iXlzYU" color="black" dir="ltr">Let's verify your email</h5>+ <h6 data-testid="subtitle" class="sc-kFuwaP kPTutl sc-crPCXo lpqVFG" color="black" dir="ltr">We sent a code to ma-**@****.net. Enter it below to verify.</h6>+ <div class="sc-dExYQz eUYAKH">+ <div class="sc-bOSxlg eRqoVn">+ <div data-testid="otp-input" class="sc-fmSAUk dbLWZE">+ <div class="sc-cbelXg gGiXFv">+ <div class="sc-ktEKTO sc-jegxcv fApNkr iqUvXd field">+ <input minlength="1" maxlength="1" type="text" id="email-verify-0" lang="en" autocomplete="off" pattern="\d*" inputmode="numeric" value="" data-av-autocomplete="off">+ </div>+ <div class="sc-ktEKTO sc-jegxcv fApNkr iqUvXd field">+ <input minlength="1" maxlength="1" type="text" id="email-verify-1" lang="en" autocomplete="off" pattern="\d*" inputmode="numeric" value="">+ </div>+ <div class="sc-ktEKTO sc-jegxcv fApNkr iqUvXd field">+ <input minlength="1" maxlength="1" type="text" id="email-verify-2" lang="en" autocomplete="off" pattern="\d*" inputmode="numeric" value="">+ </div>+ <div class="sc-ktEKTO sc-jegxcv fApNkr iqUvXd field">+ <input minlength="1" maxlength="1" type="text" id="email-verify-3" lang="en" autocomplete="off" pattern="\d*" inputmode="numeric" value="">+ </div>+ <div class="sc-ktEKTO sc-jegxcv fApNkr iqUvXd field">+ <input minlength="1" maxlength="1" type="text" id="email-verify-4" lang="en" autocomplete="off" pattern="\d*" inputmode="numeric" value="">+ </div>+ <div class="sc-ktEKTO sc-jegxcv fApNkr iqUvXd field">+ <input minlength="1" maxlength="1" type="text" id="email-verify-5" lang="en" autocomplete="off" pattern="\d*" inputmode="numeric" value="" data-av-autocomplete="off">+ </div>+ </div>+ </div>+ </div>+ <div class="sc-bqWVmG hsXpWG">+ <button data-testid="btn-otp-submit" class="sc-eDDNvR iqZGUL">+ <div class="sc-gLDzan gohLCB">+ <svg role="img" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg" aria-label="forward" class="sc-hLseeU idnFbI sc-iAEyYk kOjeBb">+ <path d="M22.8011 14.75L14.2234 6.70971L16.0474 5L26.8695 15.1441C27.3732 15.6163 27.3732 16.3817 26.8695 16.8538L16.0474 26.998L14.2234 25.2883L22.7989 17.25H4.75V14.75H22.8011Z"></path>+ </svg>+ </div>+ </button>+ </div>+ <div class="sc-jFJHMl ilTSQE">+ <span class="sc-kFuwaP dYvDdY sc-caslwl jlYTdL" color="black" dir="ltr">+ <a data-testid="otp-resend" class="sc-qRumB fKscrR">Resend code</a>+ </span>+ <span class="sc-kFuwaP dYvDdY sc-caslwl jlYTdL" color="black" dir="ltr">+ <a data-testid="btn-update-email" class="sc-qRumB fKscrR">Use a Different Email</a>+ </span>+ </div>+ </div>+ </div>+ </div>+</body>+</html>diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/nl-email-verification-form1.html b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/nl-email-verification-form1.htmlnew file mode 100644index 000000000..845d618ed--- /dev/null+++ b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/nl-email-verification-form1.html@@ -0,0 +1,22 @@+<!DOCTYPE html>+<html>+<head>+ <title>Verifieer je e-mail</title>+</head>+<body>+ <div class="verification-container">+ <h2>Verifieer je e-mailadres</h2>+ <p>We hebben een code gestuurd naar [email protected]. Voer deze hieronder in.</p>+ <div class="code-inputs">+ <input type="text" id="nl-verify-0" maxlength="1" inputmode="numeric" pattern="\d*" autocomplete="off">+ <input type="text" id="nl-verify-1" maxlength="1" inputmode="numeric" pattern="\d*" autocomplete="off">+ <input type="text" id="nl-verify-2" maxlength="1" inputmode="numeric" pattern="\d*" autocomplete="off">+ <input type="text" id="nl-verify-3" maxlength="1" inputmode="numeric" pattern="\d*" autocomplete="off">+ <input type="text" id="nl-verify-4" maxlength="1" inputmode="numeric" pattern="\d*" autocomplete="off">+ <input type="text" id="nl-verify-5" maxlength="1" inputmode="numeric" pattern="\d*" autocomplete="off">+ </div>+ <button type="submit">Bevestigen</button>+ <a href="#">Code opnieuw verzenden</a>+ </div>+</body>+</html>