Skip to content

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

Improve 2FA field detection in browser extension (#2387)

Leendert de Borst0be981fcc2f2
diff --git a/apps/browser-extension/src/utils/formDetector/FormDetector.ts b/apps/browser-extension/src/utils/formDetector/FormDetector.ts
index 85ec0708f..3cba4bdfd 100644
--- a/apps/browser-extension/src/utils/formDetector/FormDetector.ts
+++ b/apps/browser-extension/src/utils/formDetector/FormDetector.ts
@@ -1746,6 +1746,15 @@ export class FormDetector {
*/
const checkVisibility = false;
+ const totpField = this.findTotpField(formWrapper as HTMLFormElement | null);
+ const isTotpField = totpField !== null && elementsToCheck.includes(totpField);
+
+ // A 2FA field that carries an unambiguous signal of its own is classified before the credential field types.
+ if (isTotpField && this.hasTotpFormContext(totpField)) {
+ devLog('[FormDetector] getDetectedFieldType: totp (specific signal) —', describeElement(this.clickedElement));
+ return DetectedFieldType.Totp;
+ }
+
// Check if any of the elements is a username field
const usernameFields = this.findAllInputFields(formWrapper as HTMLFormElement | null, CombinedFieldPatterns.username, ['text'], [], checkVisibility);
if (usernameFields.some(input => elementsToCheck.includes(input))) {
@@ -1769,8 +1778,7 @@ export class FormDetector {
}
// Check if any of the elements is a TOTP field
- const totpField = this.findTotpField(formWrapper as HTMLFormElement | null);
- if (totpField && elementsToCheck.includes(totpField)) {
+ if (isTotpField) {
devLog('[FormDetector] getDetectedFieldType: totp —', describeElement(this.clickedElement));
return DetectedFieldType.Totp;
}
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 7402b1380..031d38e00 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
@@ -186,6 +186,50 @@ describe('FormDetector TOTP tests', () => {
});
});
+ describe('English TOTP form 14 detection (2FA field sharing classes with login fields)', () => {
+ const htmlFile = 'en-totp-form14.html';
+ let document: Document;
+ let formDetector: FormDetector;
+
+ beforeEach(() => {
+ const dom = createTestDom(htmlFile);
+ document = dom.window.document;
+ });
+
+ testField(FormField.Totp, 'totp', htmlFile);
+
+ /*
+ * The 2FA input shares the "login-text-input login-input" classes with the username and
+ * password inputs, which matches the generic "login" username pattern. Its own name/id
+ * says "totp", so it must still be classified as a TOTP field and not as a credential field.
+ */
+ it('should detect the 2FA code input field as TOTP and not as username', () => {
+ const totpInput = document.getElementById('totp') as HTMLInputElement;
+ expect(totpInput).toBeTruthy();
+
+ formDetector = new FormDetector(document, totpInput);
+
+ const detectedType = formDetector.getDetectedFieldType();
+ expect(detectedType).toBe(DetectedFieldType.Totp);
+ });
+
+ it('should still detect the username field in the same form as username', () => {
+ const usernameInput = document.getElementById('username') as HTMLInputElement;
+ expect(usernameInput).toBeTruthy();
+
+ formDetector = new FormDetector(document, usernameInput);
+
+ const detectedType = formDetector.getDetectedFieldType();
+ expect(detectedType).toBe(DetectedFieldType.Username);
+ });
+
+ it('should recognize the form as a login form', () => {
+ formDetector = new FormDetector(document, document.body);
+
+ expect(formDetector.containsLoginForm()).toBe(true);
+ });
+ });
+
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-form14.html b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/en-totp-form14.html
new file mode 100644
index 000000000..dcf8acb12
--- /dev/null
+++ b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/en-totp-form14.html
@@ -0,0 +1,91 @@
+<!DOCTYPE html>
+<html lang="en" class="default">
+
+<head>
+ <title>Sign In</title>
+</head>
+
+<body>
+ <div class="container-scroller">
+ <div class="container-fluid page-body-wrapper full-page-wrapper">
+ <div class="content-wrapper d-flex align-items-center auth auth-bg-1 theme-one">
+ <div class="row w-100">
+ <div class="col-lg-4 mx-auto">
+
+ <div id="login-form-background" class="auto-form-wrapper login-modal" style="background: rgb(34, 36, 55);">
+ <div id="login_opacity" data-value="100" hidden></div>
+ <div class="text-center auto-form-logo">
+ <img src="/static/assets/images/logo_long.svg">
+ </div>
+ <form id="login-form">
+ <input type="hidden" name="_xsrf" value="2|934886e1|b328fbba3e747ffe23c15d290ec4808f|1787949506">
+ <div id="login-inputs"></div>
+ <div class="form-group">
+ <div id="error-field" class="alert alert-danger" role="alert"></div>
+ </div>
+ <div class="form-group">
+ <label class="label">Username</label>
+ <div class="input-group">
+ <input type="text" class="form-control login-text-input login-input" placeholder="Username"
+ name="username" id="username" required="true">
+ </div>
+ </div>
+ <div class="form-group">
+ <label class="label">Password</label>
+ <div class="input-group">
+ <input type="password" class="form-control login-text-input login-input" placeholder="Password"
+ name="password" id="password" required="true">
+ </div>
+ </div>
+ <div class="form-group">
+ <label class="label full-width" data-backupcode="Enter Backup Code"
+ data-2fa="Enter Authenticator Code" id="multi-label"><span id="auth-code-label">Enter Authenticator
+ Code</span>&nbsp;<span class="badge badge-pill badge-info">(If MFA is Enabled)</span>
+ <div class="input-group flex-container">
+ <div id="totp_container">
+ <input type="text" class="form-control login-text-input login-input" placeholder="000000"
+ name="totp" id="totp" autocomplete="off">
+ </div>
+ <select required class="form-control form-control-lg select-css" id="2fa-type" name="type">
+ <option value="totp">Authenticator App</option>
+ <option value="backup_code">Backup Code</option>
+ </select>
+ </div>
+ </label>
+ </div>
+ <div class="form-group">
+ <button class="login-input btn btn-primary submit-btn btn-block">Log In</button>
+ </div>
+ <div id="passkey-section" class="form-group" style="display: none;">
+ <div class="text-center my-2">
+ <span class="text-muted">or</span>
+ </div>
+ <button type="button" id="passkey-login-btn" class="btn btn-outline-primary btn-block">
+ <i class="mdi mdi-key"></i>&nbsp;Sign in with a Passkey
+ </button>
+ </div>
+ <div class="form-group d-flex justify-content-between">
+ <div class="form-check form-check-flat mt-0">
+ &nbsp;
+ </div>
+ <button onclick="resetPass()" id="#resetPass" form=""
+ class="btn btn-outline-primary btn-sm forgot-password ">Forgot Password</button>
+ </div>
+
+ <div class="text-block text-center my-3">
+ <a href="/status" class="text-small forgot-password">View Public Status Page</a>
+ </div>
+ </form>
+
+ </div>
+
+ </div>
+ </div>
+ </div>
+ </div>
+ <!-- content-wrapper ends -->
+ </div>
+ <!-- page-body-wrapper ends -->
+</body>
+
+</html>