39 lines
1.6 KiB
JavaScript
39 lines
1.6 KiB
JavaScript
"use strict";
|
|
|
|
// adpanel:preauth-session-ttl — anonymous sessions (login CSRF token,
|
|
// bot-proof seed, pending captcha login) must not inherit the 365-day
|
|
// authenticated lifetime: every cookieless GET /login writes one, so a
|
|
// year-long TTL turns the login page into an unbounded store-growth
|
|
// primitive. Setting cookie.maxAge also resets originalMaxAge, so rolling
|
|
// refreshes keep the short lifetime and both stores honor it
|
|
// (connect-redis: cookie.expires; session-file-store: originalMaxAge).
|
|
// Login, captcha completion and remember-restore call regenerate(), which
|
|
// restores the configured authenticated lifetime.
|
|
const { resolveClampedInt } = require("./clamped-int");
|
|
|
|
const DEFAULT_PREAUTH_SESSION_MAX_AGE_MS = 30 * 60 * 1000;
|
|
const MIN_MS = 5 * 60 * 1000;
|
|
const MAX_MS = 24 * 60 * 60 * 1000;
|
|
|
|
// adpanel:env-int-clamp — resolves through the ONE shared clamped-int helper
|
|
// (floor policy, the helper's default): a parsed "0" or negative is nonsense
|
|
// for a lifetime and clamps to the 5-minute floor — it can never disable
|
|
// the cap.
|
|
function resolvePreauthSessionMaxAgeMs(env = process.env) {
|
|
return resolveClampedInt(env, "PREAUTH_SESSION_MAX_AGE_MS", {
|
|
fallback: DEFAULT_PREAUTH_SESSION_MAX_AGE_MS,
|
|
min: MIN_MS,
|
|
max: MAX_MS,
|
|
});
|
|
}
|
|
|
|
function createPreauthSessionTtlMiddleware({ maxAgeMs = DEFAULT_PREAUTH_SESSION_MAX_AGE_MS } = {}) {
|
|
return function preauthSessionTtl(req, _res, next) {
|
|
const sess = req.session;
|
|
if (sess && !sess.user && sess.cookie) sess.cookie.maxAge = maxAgeMs;
|
|
next();
|
|
};
|
|
}
|
|
|
|
module.exports = { DEFAULT_PREAUTH_SESSION_MAX_AGE_MS, resolvePreauthSessionMaxAgeMs, createPreauthSessionTtlMiddleware };
|