Skip to content

Node.js based forum software built for the modern web

fix(auth): #14793, clear forceLogin before session regeneration to prevent double re-login challenge

Julian Lam40fc44fa5476
diff --git a/public/src/admin/modules/relogin-timer.js b/public/src/admin/modules/relogin-timer.js
index 0e80be26fe..acce1a3edc 100644
--- a/public/src/admin/modules/relogin-timer.js
+++ b/public/src/admin/modules/relogin-timer.js
@@ -27,7 +27,7 @@ export function show() {
closeButton: false,
message: logoutMessage,
callback: function () {
- window.location.href = config.relative_path + '/login';
+ window.location.href = config.relative_path + '/login?local=1';
},
});
alertShown = true;
diff --git a/src/controllers/authentication.js b/src/controllers/authentication.js
index 2ec606b947..901da4c9d7 100644
--- a/src/controllers/authentication.js
+++ b/src/controllers/authentication.js
@@ -302,7 +302,12 @@ function continueLogin(strategy, req, res, next) {
(res.locals.redirectAfterLogin || redirectAfterLogin)(req, res, `${nconf.get('relative_path')}/reset/${code}`);
} else {
delete req.query.lang;
- await authenticationController.doLogin(req, userData.uid);
+ // Capture whether this is a forced re-login before clearing forceLogin.
+ // We clear forceLogin BEFORE doLogin so that passport's session
+ // regenerate + merge doesn't carry the stale flag into the new session.
+ const wasForceLogin = !!req.session.forceLogin;
+ delete req.session.forceLogin;
+ await authenticationController.doLogin(req, userData.uid, wasForceLogin);
let destination;
if (req.session.returnTo) {
destination = req.session.returnTo.startsWith('http') ?
@@ -328,7 +333,7 @@ function redirectAfterLogin(req, res, destination) {
}
}
-authenticationController.doLogin = async function (req, uid) {
+authenticationController.doLogin = async function (req, uid, wasForceLogin) {
if (!uid) {
return;
}
@@ -336,10 +341,10 @@ authenticationController.doLogin = async function (req, uid) {
const loginAsync = util.promisify(req.login).bind(req);
const keepSessionInfo = (req?.res?.locals?.reroll !== false) && (!req.loggedIn || isSelf);
await loginAsync({ uid: uid }, { keepSessionInfo });
- await authenticationController.onSuccessfulLogin(req, uid);
+ await authenticationController.onSuccessfulLogin(req, uid, true, wasForceLogin);
};
-authenticationController.onSuccessfulLogin = async function (req, uid, trackSession = true) {
+authenticationController.onSuccessfulLogin = async function (req, uid, trackSession = true, wasForceLogin) {
/*
* Older code required that this method be called from within the SSO plugin.
* That behaviour is no longer required, onSuccessfulLogin is now automatically
@@ -347,7 +352,7 @@ authenticationController.onSuccessfulLogin = async function (req, uid, trackSess
* only if the user is logging in as themselves and not forcing a reauth.
*/
const isSelfRelogin = req.loggedIn && parseInt(req.uid, 10) === parseInt(uid, 10);
- if (isSelfRelogin && !req.session.forceLogin) {
+ if (isSelfRelogin && !req.session.forceLogin && !wasForceLogin) {
return true;
}
@@ -363,11 +368,12 @@ authenticationController.onSuccessfulLogin = async function (req, uid, trackSess
req.session.meta = {};
const now = Date.now();
- if (req.session.forceLogin) {
+ if (wasForceLogin) {
req.session.meta.reAuthAt = now;
} else {
delete req.session.meta.reAuthAt;
}
+ // Safety net: clear forceLogin if it somehow survived (e.g., from SSO paths)
delete req.session.forceLogin;
// Associate IP used during login with user account
req.session.meta.ip = req.ip;