Skip to content

Commit 3397e10

Browse files
committed
fix: always block explicitly logged out users
Corrected blocking logic: - User explicitly logged out (inactive sessions) → ALWAYS BLOCK - No session exists (bug/race condition) → depends on strictMode - strictMode: true → block - strictMode: false → allow with warning (default) This ensures logout actually works while protecting against session creation bugs blocking legitimate users.
1 parent 3842487 commit 3397e10

3 files changed

Lines changed: 42 additions & 35 deletions

File tree

server/src/bootstrap.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -656,41 +656,34 @@ async function registerSessionAwareAuthStrategy(strapi, log) {
656656
const hasInactiveSessions = allSessions?.some(s => s.isActive === false);
657657

658658
// DECISION LOGIC:
659-
// 1. If user has inactive sessions = they were explicitly logged out → BLOCK (if strict)
660-
// 2. If user has NO sessions at all = session never created (bug/race condition) → ALLOW
661-
// 3. If strictMode is off → always ALLOW but log warning
659+
// 1. User has inactive sessions = explicitly logged out → ALWAYS BLOCK
660+
// 2. No sessions exist at all = session never created (bug/race condition) → ALLOW
661+
// 3. strictMode controls whether missing sessions block or just warn
662662

663-
if (!strictMode) {
664-
// Non-strict mode: Log warning but allow through
665-
if (totalSessions === 0) {
666-
strapi.log.warn(
667-
`[magic-sessionmanager] [JWT-WARN] No session found for user ${userDocId.substring(0, 8)}... (allowing - session may not have been created)`
668-
);
669-
} else if (hasInactiveSessions) {
670-
strapi.log.warn(
671-
`[magic-sessionmanager] [JWT-WARN] User ${userDocId.substring(0, 8)}... has ${totalSessions} inactive sessions but no active ones (allowing - strictMode off)`
672-
);
673-
}
674-
return decoded; // Allow through in non-strict mode
663+
if (hasInactiveSessions) {
664+
// User was explicitly logged out → ALWAYS BLOCK (this is intentional!)
665+
strapi.log.info(
666+
`[magic-sessionmanager] [JWT-BLOCKED] User ${userDocId.substring(0, 8)}... was logged out (${totalSessions} inactive sessions)`
667+
);
668+
return null; // Block - user was explicitly logged out
675669
}
676670

677-
// Strict mode enabled - more careful about blocking
671+
// No sessions exist at all - session was never created (bug/race condition)
678672
if (totalSessions === 0) {
679-
// No sessions at all - likely a bug or race condition, allow through
673+
if (strictMode) {
674+
// Strict mode: Block even if no sessions (could be security risk)
675+
strapi.log.info(
676+
`[magic-sessionmanager] [JWT-BLOCKED] No sessions exist for user ${userDocId.substring(0, 8)}... (strictMode enabled)`
677+
);
678+
return null;
679+
}
680+
// Non-strict mode: Allow through but warn
680681
strapi.log.warn(
681-
`[magic-sessionmanager] [JWT-ALLOW] No sessions exist for user ${userDocId.substring(0, 8)}... (allowing - possible race condition)`
682+
`[magic-sessionmanager] [JWT-WARN] No session found for user ${userDocId.substring(0, 8)}... (allowing - session may not have been created)`
682683
);
683684
return decoded;
684685
}
685686

686-
if (hasInactiveSessions) {
687-
// User has inactive sessions = explicitly logged out → BLOCK
688-
strapi.log.info(
689-
`[magic-sessionmanager] [JWT-BLOCKED] User ${userDocId.substring(0, 8)}... was logged out (${totalSessions} inactive sessions)`
690-
);
691-
return null; // Block - user was explicitly logged out
692-
}
693-
694687
// Edge case: sessions exist but none are active or inactive (shouldn't happen)
695688
strapi.log.warn(
696689
`[magic-sessionmanager] [JWT-ALLOW] Unexpected session state for user ${userDocId.substring(0, 8)}... (allowing)`

server/src/middlewares/last-seen.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,20 @@ module.exports = ({ strapi, sessionService }) => {
122122

123123
const hasInactiveSessions = allSessions?.some(s => s.isActive === false);
124124

125-
if (strictMode && hasInactiveSessions) {
126-
// Strict mode + user was explicitly logged out → BLOCK
125+
if (hasInactiveSessions) {
126+
// User was explicitly logged out → ALWAYS BLOCK
127127
strapi.log.info(`[magic-sessionmanager] [BLOCKED] Session terminated (user: ${userDocId.substring(0, 8)}...)`);
128128
return ctx.unauthorized('Session has been terminated. Please login again.');
129129
}
130130

131-
// Non-strict mode or no sessions exist → Allow but log
132-
strapi.log.debug(`[magic-sessionmanager] [WARN] No active session for user ${userDocId.substring(0, 8)}... (allowing)`);
131+
// No sessions exist at all - session was never created
132+
if (strictMode) {
133+
strapi.log.info(`[magic-sessionmanager] [BLOCKED] No session exists (user: ${userDocId.substring(0, 8)}..., strictMode)`);
134+
return ctx.unauthorized('No valid session. Please login again.');
135+
}
136+
137+
// Non-strict mode: Allow but log warning
138+
strapi.log.warn(`[magic-sessionmanager] [WARN] No session for user ${userDocId.substring(0, 8)}... (allowing)`);
133139
}
134140

135141
// Store documentId for later use

server/src/policies/session-required.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,25 @@ module.exports = async (policyContext, config, { strapi }) => {
6666

6767
const hasInactiveSessions = allSessions?.some(s => s.isActive === false);
6868

69-
// Only block if strict mode AND user was explicitly logged out
70-
if (strictMode && hasInactiveSessions) {
69+
if (hasInactiveSessions) {
70+
// User was explicitly logged out → ALWAYS BLOCK
7171
strapi.log.info(
7272
`[magic-sessionmanager] [POLICY-BLOCKED] Session terminated (user: ${userDocId.substring(0, 8)}...)`
7373
);
7474
throw new errors.UnauthorizedError('Session terminated. Please login again.');
7575
}
7676

77-
// Non-strict mode or no sessions exist → Allow but log
78-
strapi.log.debug(
79-
`[magic-sessionmanager] [POLICY-WARN] No active session for user ${userDocId.substring(0, 8)}... (allowing)`
77+
// No sessions exist at all - session was never created
78+
if (strictMode) {
79+
strapi.log.info(
80+
`[magic-sessionmanager] [POLICY-BLOCKED] No session exists (user: ${userDocId.substring(0, 8)}..., strictMode)`
81+
);
82+
throw new errors.UnauthorizedError('No valid session. Please login again.');
83+
}
84+
85+
// Non-strict mode: Allow but log warning
86+
strapi.log.warn(
87+
`[magic-sessionmanager] [POLICY-WARN] No session for user ${userDocId.substring(0, 8)}... (allowing)`
8088
);
8189
return true;
8290

0 commit comments

Comments
 (0)