fix(auth): token-based handoff for cross-subdomain SSO
Domain=.sami cookies are silently rejected by real browsers - .sami is an unregistered custom TLD, so browsers treat sami itself as the effective public suffix and refuse to set a cookie scoped to it (the same rule that stops a site from setting a supercookie for all of .com). Confirmed via curl verbose (cookie dropped, domain must not set cookies for sami) and via the Firefox console on the actual device (Cookie rejected for invalid domain) for the same cookie. The session cookie set on status.sami after TOTP verify could never reach plex.sami/jellyfin.sami/emby.sami/chat.sami no matter how the cookie itself was built - prior fixes tonight left this mechanism untouched, which is why the loop persisted. Fix: /totp/verify mints a short-lived (60s) single-use opaque token. The status.sami frontend appends it to the redirect URL when bouncing the user back to a gated service. That services login page exchanges the token via the new public GET /api/v1/auth/sso-exchange for a host-only session cookie (no Domain attribute - always accepted). isSessionValid only checks the cookies HMAC signature, never its Domain, so the host-only cookie validates identically to the cross-domain one on every existing check with zero changes to that logic.
This commit is contained in:
@@ -299,6 +299,54 @@ module.exports = function configureMiddleware(app, {
|
||||
return false;
|
||||
}
|
||||
|
||||
// ── Cross-subdomain SSO token handoff ──
|
||||
// Domain=.sami cookies are silently rejected by real browsers: .sami is an
|
||||
// unregistered custom TLD, so browsers treat "sami" itself as the effective
|
||||
// public suffix and refuse to set a cookie scoped to it (the same rule that
|
||||
// stops a site from setting a supercookie for all of .com). That means the
|
||||
// session cookie set on status.sami never reaches plex.sami/jellyfin.sami/
|
||||
// etc, and cross-subdomain SSO can never work via a shared cookie no matter
|
||||
// how the cookie itself is constructed.
|
||||
//
|
||||
// Fix: after TOTP verify, mint a short-lived single-use opaque token and
|
||||
// pass it in the redirect URL back to the target service. That service's
|
||||
// origin exchanges the token (via /auth/sso-exchange) for its OWN host-only
|
||||
// cookie (no Domain attribute — always accepted, since it's scoped to the
|
||||
// exact host that set it). isSessionValid/verifySessionCookie don't care
|
||||
// about the cookie's Domain at all, only its HMAC signature, so a host-only
|
||||
// cookie validates identically to the cross-domain one — no changes needed
|
||||
// to any existing session-check code path.
|
||||
const ssoHandoffTokens = new Map();
|
||||
const SSO_HANDOFF_TTL_MS = 60 * 1000;
|
||||
|
||||
function createHandoffToken() {
|
||||
const token = crypto.randomBytes(24).toString('base64url');
|
||||
ssoHandoffTokens.set(token, { exp: Date.now() + SSO_HANDOFF_TTL_MS });
|
||||
return token;
|
||||
}
|
||||
|
||||
function redeemHandoffToken(token) {
|
||||
if (!token) return false;
|
||||
const entry = ssoHandoffTokens.get(token);
|
||||
ssoHandoffTokens.delete(token); // one-time use regardless of outcome
|
||||
return !!entry && entry.exp > Date.now();
|
||||
}
|
||||
|
||||
function setHostOnlySessionCookie(res, durationKey) {
|
||||
const durationMs = SESSION_DURATIONS[durationKey];
|
||||
if (!durationMs) return;
|
||||
const maxAge = Math.floor(durationMs / 1000);
|
||||
const payload = { v: true, exp: Date.now() + durationMs };
|
||||
const payloadB64 = Buffer.from(JSON.stringify(payload)).toString('base64url');
|
||||
const key = cryptoUtils.loadOrCreateKey();
|
||||
const sig = crypto.createHmac('sha256', key).update(payloadB64).digest('base64url');
|
||||
// No Domain attribute — host-only, so it's always accepted regardless of
|
||||
// the .sami public-suffix issue described above.
|
||||
res.setHeader('Set-Cookie',
|
||||
`${SESSION_COOKIE_NAME}=${payloadB64}.${sig}; Max-Age=${maxAge}; Path=/; HttpOnly; Secure; SameSite=Lax`
|
||||
);
|
||||
}
|
||||
|
||||
// ── Public routes (bypass TOTP and JWT auth) ──
|
||||
// Routes here are accessible without authentication. By default the
|
||||
// monitoring/health-check endpoints are public so the dashboard can
|
||||
@@ -339,6 +387,11 @@ module.exports = function configureMiddleware(app, {
|
||||
{ path: '/api/v1/auth/gate/', prefix: true },
|
||||
{ path: '/api/v1/auth/app-token/', prefix: true },
|
||||
{ path: '/api/v1/auth/login-page', exact: true, method: 'GET' },
|
||||
// Must be public: a fresh cross-subdomain visitor has no session yet by
|
||||
// definition — that's exactly the gap /auth/sso-exchange closes. The
|
||||
// endpoint itself only accepts a valid single-use handoff token minted
|
||||
// moments earlier by a successful TOTP verify, so this isn't an open door.
|
||||
{ path: '/api/v1/auth/sso-exchange', exact: true, method: 'GET' },
|
||||
// DC-046 pluggable auth endpoints — public by design (they ARE login).
|
||||
// Use :provider placeholder; today's only provider is TOTP, but the
|
||||
// route is parameterized so DC-047's email provider just works.
|
||||
@@ -585,6 +638,9 @@ module.exports = function configureMiddleware(app, {
|
||||
clearSessionCookie,
|
||||
isSessionValid,
|
||||
ipSessions,
|
||||
renewCSRFToken
|
||||
renewCSRFToken,
|
||||
createHandoffToken,
|
||||
redeemHandoffToken,
|
||||
setHostOnlySessionCookie
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user