Summary
A flaw in the Grav Login Plugin's two-factor authentication flow allows an attacker who knows a victim's correct password to replace the pending user's TOTP secret before authorization is completed.
The attacker may then generate a valid replacement code and finish logging in without the victim's original second factor.
Affected context
The issue occurs in Grav Login Plugin deployments that use the login flow with TOTP-based two-factor authentication. It becomes risky when a user has passed password verification but remains in a pending TOTP session, and the secret-regeneration task can be invoked without confirming that the user is fully authorized.
Exploit preconditions
- Attack surface: The Grav Login Plugin's authentication and two-factor secret-regeneration task
- Required access: Knowledge of the victim's valid password and the ability to submit requests to the login flow
- Trigger point: Invocation of the secret-regeneration task during the pending TOTP challenge
- Why exploitation is possible: The task checks that a pending-session user exists but does not require the user to be authorized, allowing the active TOTP secret to be replaced before second-factor authentication completes
Why this happens
The underlying bug is an authorization-state validation failure. A sensitive account-management operation is permitted based only on the existence of a pending user session, rather than requiring successful completion of the authentication flow.
This maps to CWE-287: Improper Authentication, because the application accepts a newly generated second-factor credential without adequately proving that the requester has already satisfied the required authentication stages. The insecure pattern is allowing pre-authentication or partially authenticated sessions to modify credentials that protect the account.
Potential impact
- An attacker could bypass the victim's intended second authentication factor.
- An attacker could gain unauthorized access to the victim's account.
- Account data and administrative functions may be exposed or altered.
- An attacker could establish persistence by changing other account security settings after login.
General mitigation guidance
Mitigation generally focuses on enforcing authentication state and protecting credential-changing operations.
Common defensive patterns include:
- Require fully authorized user state before allowing TOTP secret generation, replacement, or disclosure.
- Keep credential-management actions unavailable during pending or partially authenticated sessions.
- Bind sensitive operations to an authenticated server-side session and verify the intended account explicitly.
- Avoid returning newly generated authentication secrets through responses unless the requester has completed the required authorization checks.
- Apply least privilege to account-management capabilities and require reauthentication for high-impact security changes.
- Treat all client-controlled session identifiers and workflow transitions as untrusted inputs.
How teams detect issues like this
Organizations can look for this class of issue through:
- Static analysis rules identifying credential or MFA-secret changes reachable from pre-authentication handlers.
- Code review checks that compare authorization-state validation with the sensitivity of the requested operation.
- Tests that attempt account-security actions during password-pending, MFA-pending, expired, and partially authenticated sessions.
- Runtime monitoring for TOTP secret regeneration followed immediately by successful authentication from the same or an unusual client.
- Policy enforcement requiring explicit authorization checks before credential-management endpoints are exposed.
Automated security review tools may help identify workflow paths where pending sessions access fully authenticated functionality.

