Two-step sign-in no longer says Your MFA session expired and signs you out right after the code is typed: the code boxes send the code on its sixth digit, so Enter pressed out of habit, a password manager or phone keyboard sending it too, or a double click sent it twice and the second copy arrived on the session id the first had just replaced - each form on the sign-in page and each self-sending code form now goes once, a second copy that still gets through is taken on to the signed-in session by the old id kept for a minute with only the new id and a digest of the step's pending token (signInNewSession() and signInMovedSession() in functions/sign_in.php), the role choice and passkey steps get the same, a wrong code sent twice says it is wrong, the code step is recognized without its button, and the code can come up to five minutes after the password instead of two; 26.12.4 changelog

This commit is contained in:
johnnyq
2026-12-01 18:17:51 -05:00
parent a7776c342e
commit d9e1472e90
5 changed files with 186 additions and 13 deletions

View File

@@ -2,6 +2,24 @@
This file documents all notable changes made to ITFlow.
## [26.12.4]
### Upgrading to 26.12.4
Update from Maintenance > Update as usual. There are no database changes.
### Bug Fixes
- Two-step sign-in no longer says "Your MFA session expired" right after the code is typed. The code boxes send the code on its sixth digit, so pressing Enter out of habit - or a password manager or phone keyboard sending it too, or a double click on Verify & Sign In - sent it a second time, and that second copy signed you out. Each form on the sign-in page now goes once, a second copy that still gets through takes you into ITFlow, and a wrong code sent twice says the code is wrong.
- The two-step code can be typed up to five minutes after the password, up from two - time to find the phone and the right account in the authenticator app.
- A password manager or phone that fills in the two-step code and sends the form itself signs you in, instead of going back to the email and password.
### Developer Updates
- `signInNewSession()` in `functions/sign_in.php` replaces `session_regenerate_id(true)` at both sign-ins in `login.php`. After the role choice, the code or a passkey, the old session id is kept for `SIGN_IN_MOVED_SECONDS` (60) holding only the new id and a SHA-256 of that step's pending token; `signInMovedSession()` takes a request with the old id and the same token on to the signed-in session and sends its cookie, so a second copy of the form posted before the first answer came back isn't left with an empty session. An id planted before the sign-in has no token and gets nothing. A password-only or provider sign-in renews the id as before.
- `login.php`: the role, code and passkey steps call `signInMovedOn()` when their pending sign-in is gone - into the app for a second copy or a browser already signed in, otherwise "expired" as before. The code step is recognized by `pending_mfa_token` as well as the `mfa_login` button, a retried code keeps its pending token, and `SIGN_IN_CODE_SECONDS` (300) is the code step's window.
- `js/login_prevent_resubmit.js` lets each form on the sign-in page submit once (until the page comes back from history, or 10 seconds), and `js/otp_input.js` does the same for any form with a `data-otp-submit` code field - turning on MFA, the client portal's two-step setup and shared-link codes.
## [26.12.3]
### Upgrading to 26.12.3

View File

@@ -245,3 +245,62 @@ function signInNewIpIds(array $rows) {
function signInNewIpBadge() {
return '<span class="badge rounded-pill bg-warning-subtle text-warning-emphasis border border-warning-subtle fw-normal text-nowrap" title="The first time this account signed in from this IP address" data-new-ip><i class="fas fa-map-marker-alt me-1"></i>New IP</span>';
}
/** How long the session id a sign-in replaced still leads on to the signed-in one, for a second copy of that sign-in (signInNewSession()). */
const SIGN_IN_MOVED_SECONDS = 60;
/**
* A new session id for someone who just signed in (CWE-384), keeping what the
* session holds. After a step that carried a pending sign-in token - the role
* choice, the two-step code, a passkey - the old id isn't deleted: for
* SIGN_IN_MOVED_SECONDS it holds only the new id and a digest of that token.
* A second copy of the same form sent before the first answer came back - Enter
* after the code boxes had already sent it, a password manager or a phone
* keyboard submitting too, a double click - still carries the old id. Deleted,
* it found nothing, answered that the sign-in had expired, and left the browser,
* which never got the first answer, signed out. Now it is taken on to the
* signed-in session (signInMovedSession()). Only the browser that was signing in
* has the token, so an id someone planted before the sign-in leads nowhere.
*/
function signInNewSession($pending_token = '') {
if (!is_string($pending_token) || $pending_token === '' || session_status() !== PHP_SESSION_ACTIVE || headers_sent()) {
session_regenerate_id(true);
return;
}
$data = $_SESSION;
$new_id = session_create_id();
$_SESSION = ['signed_in_moved' => ['id' => $new_id, 'at' => time(), 'token' => hash('sha256', $pending_token)]];
session_write_close();
signInOpenSession($new_id);
$_SESSION = $data;
}
/**
* A second copy of a sign-in that went through a moment ago (signInNewSession()):
* when this session is the one it left and $posted_token is that sign-in's, carry
* on in the signed-in session - its cookie goes back with this answer - and true.
* False, with nothing changed, otherwise.
*/
function signInMovedSession($posted_token) {
$moved = $_SESSION['signed_in_moved'] ?? null;
if (!is_array($moved) || !is_string($posted_token) || $posted_token === '' || headers_sent()
|| time() - intval($moved['at'] ?? 0) > SIGN_IN_MOVED_SECONDS
|| !hash_equals((string) ($moved['token'] ?? ''), hash('sha256', $posted_token))) {
return false;
}
session_write_close();
signInOpenSession((string) $moved['id']);
return !empty($_SESSION['logged']);
}
/**
* Go on in session $id, made by this server a moment ago. Strict mode, which
* turns away an id the browser brings that the server doesn't have, would refuse
* it before it is written; this request is done choosing its session, so it stays
* off for the rest of it (session settings can't change while one is open).
*/
function signInOpenSession($id) {
ini_set('session.use_strict_mode', '0');
session_id($id);
session_start();
}

View File

@@ -1,3 +1,39 @@
// Reloading the sign-in page asks for it afresh instead of sending the last form again
if (window.history.replaceState) {
window.history.replaceState(null,null,window.location.href);
window.history.replaceState(null, null, window.location.href);
}
// Each form on the sign-in page goes once. The code boxes send the code on its sixth digit, so Enter
// pressed out of habit - or a password manager or phone keyboard submitting too, or a double click -
// sent it a second time before the first answer was back, and that copy came back "expired"
// (signInMovedSession() in functions/sign_in.php). Free again when the page comes back from the
// browser's history, or after a while in case the first was stopped
(function () {
'use strict';
document.addEventListener('submit', function (e) {
if (e.target.dataset.sending) {
e.preventDefault();
e.stopImmediatePropagation();
}
}, true);
window.addEventListener('submit', function (e) {
const form = e.target;
if (e.defaultPrevented || !(form instanceof HTMLFormElement)) {
return;
}
form.dataset.sending = '1';
setTimeout(function () {
delete form.dataset.sending;
}, 10000);
});
window.addEventListener('pageshow', function (e) {
if (e.persisted) {
document.querySelectorAll('form[data-sending]').forEach(function (form) {
delete form.dataset.sending;
});
}
});
})();

View File

@@ -167,6 +167,41 @@
window.itflowOtp = init;
// A form its code sends goes once: Enter after the sixth digit, out of habit - or a password
// manager or phone keyboard submitting too - would send it again before the first answer is
// back, and the second copy finds the code already used. Free again when the page comes back
// from the browser's history, or after a while in case the first was stopped. The sign-in page
// does this for all of its forms (js/login_prevent_resubmit.js)
function sendsItself(form) {
return form instanceof HTMLFormElement && form.querySelector('input[data-otp-submit]') !== null;
}
document.addEventListener('submit', function (e) {
if (e.target.dataset.sending && sendsItself(e.target)) {
e.preventDefault();
e.stopImmediatePropagation();
}
}, true);
window.addEventListener('submit', function (e) {
const form = e.target;
if (e.defaultPrevented || !sendsItself(form)) {
return;
}
form.dataset.sending = '1';
setTimeout(function () {
delete form.dataset.sending;
}, 10000);
});
window.addEventListener('pageshow', function (e) {
if (e.persisted) {
document.querySelectorAll('form[data-sending]').forEach(function (form) {
delete form.dataset.sending;
});
}
});
// A window with a code to type - straight into the first box
document.addEventListener('shown.bs.modal', function (e) {
const input = e.target.querySelector('input[data-otp][autofocus]');

View File

@@ -148,17 +148,37 @@ function pendingExpired($sess, $ttl_seconds = 120) {
return !$sess || empty($sess['created']) || (time() - intval($sess['created']) > $ttl_seconds);
}
// How long after the password the two-step code can take - finding the account among many in an authenticator app, or the phone itself
const SIGN_IN_CODE_SECONDS = 300;
// A step whose pending sign-in is gone: a second copy of a sign-in that just went through (signInMovedSession()), or
// a form from before this browser signed in - on into the app, rather than "expired" for someone who is signed in
function signInMovedOn($posted_token) {
if (!signInMovedSession($posted_token) && empty($_SESSION['logged'])) {
return;
}
if (!empty($_SESSION['client_logged_in'])) {
redirect('client/index.php');
}
// The page they were on their way to, as after signing in
$last_visited = base64_decode((string) ($_GET['last_visited'] ?? ''));
redirect(str_starts_with($last_visited, '/agent') || str_starts_with($last_visited, '/admin') ? $last_visited : 'agent/');
}
// POST handling
// Back from an identity provider, which login_provider.php has checked - a GET, as the provider's redirect was
$is_provider_return = $_SERVER['REQUEST_METHOD'] === 'GET' && is_string($_GET['provider_login'] ?? null) && $_GET['provider_login'] !== '';
if (($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_POST['role_choice']) || isset($_POST['mfa_login']) || isset($_POST['passkey_login']))) || $is_provider_return) {
// The code step by its token, not only its button: a password manager or phone that fills the code and sends the form itself sends no button
$is_code_post = $_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['mfa_login']) || (isset($_POST['pending_mfa_token']) && !isset($_POST['login']) && !isset($_POST['role_choice'])));
if (($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_POST['role_choice']) || $is_code_post || isset($_POST['passkey_login']))) || $is_provider_return) {
$role_choice = $_POST['role_choice'] ?? null;
$is_login_step = isset($_POST['login']);
$is_role_step = isset($_POST['role_choice']) && !$is_login_step && !isset($_POST['mfa_login']);
$is_mfa_step = isset($_POST['mfa_login']);
$is_role_step = isset($_POST['role_choice']) && !$is_login_step && !$is_code_post;
$is_mfa_step = $is_code_post;
// After login_passkey.php checked a passkey - which counts as two-step sign-in
$is_passkey_step = isset($_POST['passkey_login']) && !$is_login_step && !$is_mfa_step && !$is_role_step;
// After login_provider.php: an identity provider signed them in - its own two-step sign-in stands in for ours
@@ -186,6 +206,7 @@ if (($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($
$sess = $_SESSION['pending_dual_login'] ?? null;
if (pendingExpired($sess) || empty($posted_token) || empty($sess['token']) || !hash_equals($sess['token'], $posted_token)) {
signInMovedOn($posted_token);
unset($_SESSION['pending_dual_login']);
header("HTTP/1.1 401 Unauthorized");
$response = "
@@ -211,7 +232,8 @@ if (($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($
$posted_token = $_POST['pending_mfa_token'] ?? '';
$sess = $_SESSION['pending_mfa_login'] ?? null;
if (pendingExpired($sess) || empty($posted_token) || empty($sess['token']) || !hash_equals($sess['token'], $posted_token)) {
if (pendingExpired($sess, SIGN_IN_CODE_SECONDS) || empty($posted_token) || empty($sess['token']) || !hash_equals($sess['token'], $posted_token)) {
signInMovedOn($posted_token);
unset($_SESSION['pending_mfa_login']);
header("HTTP/1.1 401 Unauthorized");
$response = "
@@ -237,6 +259,7 @@ if (($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($
$sess = $_SESSION['pending_passkey_login'] ?? null;
if (pendingExpired($sess) || empty($posted_token) || empty($sess['token']) || !hash_equals($sess['token'], $posted_token)) {
signInMovedOn($posted_token);
unset($_SESSION['pending_passkey_login']);
header("HTTP/1.1 401 Unauthorized");
$response = "
@@ -664,8 +687,9 @@ if (($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($
$session_user_id = $user_id;
logAudit("Login", "Success", "$user_name successfully logged in $extended_log", 0, $user_id);
// New session ID for the authenticated session (CWE-384)
session_regenerate_id(true);
// New session ID for the authenticated session (CWE-384) - after the role choice, code or passkey, the old one
// leads on to it for a minute, for a second copy of the same form (signInNewSession())
signInNewSession($is_role_step || $is_mfa_step || $is_passkey_step ? $posted_token : '');
$_SESSION['user_id'] = $user_id;
$_SESSION['csrf_token'] = randomString(32);
@@ -709,8 +733,9 @@ if (($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($
} else {
// MFA required — store *only what we need*, not password
$pending_mfa_token = bin2hex(random_bytes(32));
// MFA required — store *only what we need*, not password. A retried code keeps its token, so a
// second copy of a wrong code is checked as one more, not taken for a sign-in that ran out
$pending_mfa_token = ($is_mfa_step && !empty($pending_mfa['token'])) ? (string) $pending_mfa['token'] : bin2hex(random_bytes(32));
// The vault: from the role choice it was opened at step 1, on a retried code it was opened before,
// and from the initial login step it is opened now, with the password in memory (vaultSignIn())
@@ -843,8 +868,8 @@ if (($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($
if ($client_id && $contact_id && $client_can_sign_in && !$client_mfa_done) {
// Ask for the code - keeping only who it is for, never the password
$pending_mfa_token = bin2hex(random_bytes(32));
// Ask for the code - keeping only who it is for, never the password; a retried code keeps its token, as for agents
$pending_mfa_token = ($is_mfa_step && !empty($pending_mfa['token'])) ? (string) $pending_mfa['token'] : bin2hex(random_bytes(32));
$_SESSION['pending_mfa_login'] = [
'type' => 'client',
'email' => $user_email,
@@ -876,8 +901,8 @@ if (($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($
$client_mfa_how .= ', remembering this browser';
}
// New session ID for the authenticated session (CWE-384)
session_regenerate_id(true);
// New session ID for the authenticated session (CWE-384), as for agents (signInNewSession())
signInNewSession($is_role_step || $is_mfa_step || $is_passkey_step ? $posted_token : '');
$_SESSION['client_logged_in'] = true;
$_SESSION['client_id'] = $client_id;