Skip to content

fix(FOUR-34260): store per-user random TOTP secrets for 2FA - #9127

Open
gproly wants to merge 20 commits into
developfrom
bugfix/FOUR-34260
Open

gproly wants to merge 20 commits into
developfrom
bugfix/FOUR-34260

Conversation

@gproly

@gproly gproly commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Stop deriving 2FA secrets from public uuid and username, which allowed anyone with those fields to forge valid codes. Generate secrets with TOTP::generate(), persist encrypted on users.two_factor_secret, and add regression tests for predictable-code rejection and normal 2FA flow.

Existing Authenticator App enrollments must scan a new QR after deploy.

https://processmaker.atlassian.net/browse/FOUR-34260

ci:deploy

Stop deriving 2FA secrets from public uuid and username, which allowed
anyone with those fields to forge valid codes. Generate secrets with
TOTP::generate(), persist encrypted on users.two_factor_secret, and add
regression tests for predictable-code rejection and normal 2FA flow.

Existing Authenticator App enrollments must scan a new QR after deploy.

https://processmaker.atlassian.net/browse/FOUR-34260
@cursor

cursor Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

PR Summary

High Risk
This is a security fix in authentication: login gating, secret storage, and enrollment rules change, and existing authenticator users must re-enroll after deploy.

Overview
Replaces predictable 2FA TOTP secrets (previously derivable from public uuid and username) with per-user random secrets persisted as encrypted two_factor_secret and two_factor_delivery_secret, plus a migration for the new columns.

Validation and flows now use stored secrets (or a session-pending authenticator secret until QR enrollment completes). Mixed email/SMS + authenticator setups require a delivery step-up (email/SMS code first) before authenticator enrollment; session challenge method snapshots taken at login prevent narrowing 2FA preferences or bypassing delivery while the gate is open. The user API rejects self-service preferences_2fa changes until web-session 2FA is completed when that snapshot is active.

Login/logout clears the new 2FA session keys; the 2FA OTP and QR views are refactored onto the shared auth layout. Regression tests cover predictable-code rejection, enrollment, mixed-method flows, and preference-update guards.

Reviewed by Cursor Bugbot for commit 4675165. Bugbot is set up for automated code reviews on this repo. Configure here.

@julceslauhub julceslauhub left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved

… users

Stop deriving 2FA codes from public uuid and username. Persist CSPRNG
secrets encrypted on users.two_factor_secret, add regression tests, and
on login route legacy users without a secret to QR (auth app only) or
force a fresh email/SMS code.

Run the migration on deploy. Authenticator App users must scan a new QR.

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

- TwoFactorAuthNotification mail subject and body line
- TwoFactorAuthController invalid code errors

Fixes sync mail send hanging on /2fa when subject used _().

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review — 1 finding(s).

  • [HIGH] ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php:137 — A password-authenticated session that has not completed 2FA can GET /2fa/auth_app_qr. displayAuthAppQr only checks that a user is present, then generateQr calls getUserTotpSecret, which returns the stored two_factor_secret or mints one and saveQuietly()s it. That secret is placed in the otpauth provisioning URI and returned as the QR image. The same secret is used for email/SMS OTPs (sha512, 8 digits, 300s) and for authenticator codes (default 6-digit TOTP). validateCode treats any 6-character code as authenticator mode, does not consult getValid2FAPreferences, and the controller stores the result in 2fa-validated, which the 2fa middleware accepts as completion. This holds for email-only and SMS-only users and when a secret is already stored. The new redirect in displayTwoFactorAuthForm only sends authenticator-only users with an empty secret to this page; it does not authorize the route. Showing a QR once for that incomplete authenticator enrollment can stay; the route must not mint or re-show the secret for other methods or after a secret already exists, and a 6-digit code must be rejected unless Authenticator App is an enabled method.
    • Impact: An attacker who has the victim's password, or that pre-2FA session, recovers the long-term second-factor secret and can pass the 2FA gate without the victim's email, phone, or authenticator. two_factor_secret is now a confidential persisted credential rather than a value derived from public identity, but this path still discloses it to a session that has only passed primary authentication, and the shared secret unlocks email and SMS as well as authenticator.
    • Remediation: Allow displayAuthAppQr to mint and show a QR only when Authenticator App is an enabled method and enrollment is still incomplete. After two_factor_secret exists, do not return it again to a session that has not just completed enrollment; require a verified second factor before any later display. Keep the email/SMS OTP secret separate from the authenticator secret, and in validateCode accept a 6-digit authenticator code only when Authenticator App is one of that user's enabled methods.

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

Refactor otp and auth_app_qr to extend auth.layouts.auth instead of legacy
standalone HTML, background assets, and inline CSS. Use existing skip-language-
selector and skip-auth-language-scripts sections so 2FA matches password-reset
branding without changing the auth layout.

https://processmaker.atlassian.net/browse/FOUR-34260
Use fresh() instead of User::refresh() on /2fa display so two_factor_secret
is read from the database without invalidating permissions cache.

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review — 1 finding(s).

  • [HIGH] ProcessMaker/Http/Controllers/Auth/TwoFactorAuthController.php:137 — A password session that has not set 2fa-validated can GET /2fa/auth_app_qr and receive the durable TOTP seed in the provisioning QR. displayAuthAppQr only checks that a user is present, then generateQr embeds users.two_factor_secret in an otpauth URI. That same seed is what validateCode accepts: code length alone selects the 6-digit authenticator profile or the 8-digit sha512/300s email/SMS profile. After the seed exists, every later password login can read it again, and an email/SMS-only user can open the route directly even though authenticator is not their method.
    • Impact: This is the second-factor possession boundary. Password login already creates a session, and the 2fa middleware only wraps later application routes, so the pre-2FA session is the actor 2FA is meant to stop. The new stored secret is no longer recomputable from uuid and username, but this route still returns it, so a phished or reused password is enough to mint a valid code and set 2fa-validated. That includes users whose only methods are email or SMS, because their emailed or texted code is derived from the seed on the QR. The one-time authenticator enrollment redirect for an empty secret is the intended setup step; repeating the seed, or showing it when authenticator is not enabled, removes the second factor.
    • Remediation: Serve the provisioning QR only for initial authenticator enrollment: authenticator must be an enabled method for that user, and two_factor_secret must still be empty. Call generateQr only in that case, then require a confirming code from the app. After the seed is stored, deny /2fa/auth_app_qr to any session that has not already passed 2FA, including a direct request when the challenge-page link is hidden. Do not create or return the email/SMS seed from this route. Keep validateCode from accepting a profile the user's enabled methods do not allow, so a seed disclosed during authenticator setup cannot also satisfy an email or SMS challenge.

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

…ment

Store email/SMS TOTP in two_factor_delivery_secret and authenticator TOTP in
two_factor_secret. Gate /2fa/auth_app_qr on enabled Authenticator App and empty
enrollment secret; reject six-digit codes unless the method is allowed.

Extend the users migration, tighten OTP link visibility, and add unit/feature
coverage for predictable-secret bypass and QR route access.

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

Combine authenticator-method and existing-secret checks into one redirect
to /2fa so displayAuthAppQr stays within the return-statement limit.

https://processmaker.atlassian.net/browse/FOUR-34260
Extract authenticator QR redirect and send-code provisioning into private
helpers so Sonar S3776 stays within the limit.

https://processmaker.atlassian.net/browse/FOUR-34260
Accumulate validation in a $valid flag and return once so validateCode stays
within the return-statement limit without changing 2FA verification behavior.

https://processmaker.atlassian.net/browse/FOUR-34260
…id code

Defer persisting two_factor_secret until a six-digit code verifies. Store the
QR provisioning secret in session so enrollment can be retried without locking
users out of the authenticator link or QR page.

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

Do not redirect authenticator-only users back to the QR page when
PENDING_AUTHENTICATOR_SECRET is in session, so they can enter a code after
scanning. Add a feature test that GET /2fa returns the OTP view after QR enrollment.

https://processmaker.atlassian.net/browse/FOUR-34260
@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

@julceslauhub
julceslauhub self-requested a review November 10, 2026 20:46

@julceslauhub julceslauhub left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

Return configureOtp result directly in TwoFactorAuthentication instead of assigning to $otp

https://processmaker.atlassian.net/browse/FOUR-34260
Sonar flagged the private method as dead code after authenticator
enrollment moved to pending session and resolveAuthenticatorSecret.
Adjust unit test to assert stored secrets are not uuid/username-derived.

https://processmaker.atlassian.net/browse/FOUR-34260
@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

Address Cursor Bugbot finding: do not mint or accept a session TOTP when
email or SMS is enabled and users.two_factor_secret is null. Gate QR
route, validateCode, and OTP link; add feature regression test.

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-acbfe4bfe2.engk8s.processmaker.net

Mixed-method users need a validated email/SMS code before QR enrollment.
Keep blocking password-only session minting (Cursor Bugbot).

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

…roll

Set DELIVERY_STEP_UP after a valid email/SMS code and gate new authenticator
binding on mayEnrollNewAuthenticator. Redirect to the QR screen after
delivery step-up so enrollment is reachable in the login flow (Cursor Bugbot).

https://processmaker.atlassian.net/browse/FOUR-34260
@cursor
cursor Bot requested review from agustinbusso and caleeli November 19, 2026 02:25
Snapshot challenge methods at login; block self-service API updates to
preferences_2fa until TFA is validated. Gate authenticator enrollment on
delivery step-up (session snapshot), redirect to QR after a valid delivery
code, and clear 2FA session keys on logout. Address Cursor Bugbot findings.

https://processmaker.atlassian.net/browse/FOUR-34260

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c913471. Configure here.

Only block preferences_2fa when a login challenge snapshot exists and
TFA is not validated; skip SSO and Bearer-only API profile updates.

https://processmaker.atlassian.net/browse/FOUR-34260
Ignore TFA_VALIDATED from another user's cookie when updating
preferences_2fa via API (Cursor Bugbot).

https://processmaker.atlassian.net/browse/FOUR-34260
@decisions-sonarqube

Copy link
Copy Markdown

Quality Gate failed Quality Gate failed

Failed conditions
5 New issues
5 New Maintainability Issues (required ≤ 0)

See analysis details on SonarQube

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE SonarQube for IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants