Keep Me Signed In - edmozley/freeitsm GitHub Wiki
Keep me signed in
5.12.0 ยท #443
Analysts can stay signed in on their own browser for days, instead of signing in again after a coffee break, a closed browser or a server restart. Off until an administrator turns it on.
Why you were being signed out
An analyst's sign-in is a PHP session and nothing more, so it ends whenever the session does:
| What happened | Why it signed you out |
|---|---|
| About 24 minutes with nothing happening | PHP deletes a session that has been idle that long (session.gc_maxlifetime, 1440 seconds by default) |
| Closing the browser, restarting the computer | The session cookie has no expiry date, so the browser drops it when it closes |
| Restarting the server or the Docker container | Sessions are kept in the server's temporary folder; in the Docker image that is inside the container |
Trust this device does not help with any of these. It only means the next sign-in skips the one-time code; the password is still asked every time. (Trust duration is a separate setting.)
Turning it on
System โ Security โ Keep me signed in โ Keep analysts signed in: Off (the default), 1 day, 7, 14 or 30 days.
Once it is on, the analyst sign-in page shows Keep me signed in for N days under the password. Ticked, that browser stays signed in for the period.
| It ends when... | |
|---|---|
| the period is up | counted from when they ticked it - not extended by use |
| they press Sign out | that browser only |
| their password changes | by them, or reset by an administrator - every browser |
| they are deactivated or locked | every browser |
| the setting is turned Off, or shortened | shortening applies to browsers already kept signed in |
| they sign that browser out in avatar menu โ Signed-in devices | one browser, or Sign out of all other devices |
- Two-factor: the code is asked once, when they tick the box. After that the browser counts as having passed it.
- A copied cookie that is used after the owner's is noticed, and every browser of that analyst stops being kept signed in.
- Not covered: single sign-on (the identity provider decides how long that lasts) and the self-service portal.
Signed-in devices
Avatar menu โ Signed-in devices - every analyst has it, whether or not Keep me signed in is on. Like Netflix's list of devices:
| Shows | |
|---|---|
| The browser | Chrome on Windows, Safari on iOS - the full user agent on hover |
| IP address, when it signed in, when it was last active | last active moves on at most every 5 minutes |
| This device | the browser you are looking from - it has no Sign out here (use Sign out) |
| Kept signed in until โฆ | Keep me signed in was ticked there |
Who can sign out what: an analyst only ever their own devices - the list and the buttons are keyed on the signed-in analyst, so another analyst's device id signs out nothing; nobody signed in gets 401; a POST without the page's CSRF token (a link or form on another site) is refused. An administrator can sign out anyone's: System โ Analysts โ edit โ Signed-in devices, one device or Sign out everywhere (api/system/analyst_sessions.php, requireAdminJson(); never the administrator's own current session).
Sign out beside a device, or Sign out of all other devices: that browser is asked to sign in again at its next click, wherever it is - and its Keep me signed in goes too, so it cannot quietly sign back in. A device stays on the list while its session is open, or while it is kept signed in.
How it works (developers)
All of it is in includes/keep_signed_in.php.
| Piece | Where |
|---|---|
| The token | analyst_login_tokens: selector (24 hex, looked up), validator_hash (SHA-256 of a 32-byte random validator - the validator itself is never stored), previous_hash + rotated_datetime, created_datetime, expires_datetime, user agent, IP |
| The cookie | fitsm_keep = selector:validator, HttpOnly, SameSite=Lax, Secure on HTTPS (requestIsHttps()), expires with the token |
| Issued | keepSignedInIssue() - from auth/login.php on a complete sign-in (no MFA, or a trusted device) and from api/myaccount/verify_login_otp.php after the code (the tick rides through the code step as $_SESSION['mfa_pending_keep']) |
| Restored | keepSignedInAutoRestore(), called at the end of includes/functions.php - every page and endpoint loads that before it checks $_SESSION['analyst_id']. It reopens a read-and-close session to write it |
| Removed | keepSignedInForget() from auth/analyst_logout.php (before the session is wiped); keepSignedInRevoke() from api/myaccount/keep_signed_in.php |
| The setting | keep_signed_in_days (KEEP_SIGNED_IN_CHOICES = 0, 1, 7, 14, 30; anything else reads as Off) |
// keepSignedInRestore(): every check, every time - nothing is "remembered"
WHERE t.selector = ? AND t.expires_datetime > UTC_TIMESTAMP()
AND t.created_datetime > DATE_SUB(UTC_TIMESTAMP(), INTERVAL ? DAY) -- today's setting, not the old one
// then: hash_equals(validator), active, not locked, no must_change_password,
// password_changed_datetime not after the token's created_datetime
Traps
- Every check lives in
keepSignedInRestore(). A password change ends the token becausepassword_changed_datetimeis compared with the token'screated_datetime- so there is no hook to forget at each of the places a password can change. - The validator rotates on every use, and the old one is accepted for two minutes. A page fires several requests at once; after the session has gone two can arrive with the same cookie. Without the grace window the second looked like a stolen cookie and signed the analyst out everywhere.
- Sign out must forget the token before the session is wiped. Afterwards, the next page would sign straight back in from the cookie.
- A known selector with the wrong validator (outside the grace window) deletes every token of that analyst.
Signed-in devices is includes/analyst_sessions.php:
| Piece | Where |
|---|---|
| The rows | analyst_sessions: analyst_id, IP, user agent, signed_in_how (sign_in / sso / kept), kept_token_id, signed_in_datetime, last_seen_datetime, revoked_datetime |
| Recorded | analystSessionTrack(), at the end of includes/functions.php right after the restore: a signed-in session with no $_SESSION['session_row_id'] gets a row - whatever signed it in |
| Signed out | the same call, every request: one primary-key lookup; revoked_datetime set means the session is destroyed there and then (as analyst_active_gate.php does for a deactivated analyst) |
| The list and the buttons | analystSessionList() / analystSessionRevoke() behind api/myaccount/sessions.php; the modal is in includes/waffle-menu.php |
- TRAP: signing a device out must end its Keep me signed in token as well (
kept_token_id), or its next request signs straight back in from the cookie.analystSessionRevoke()deletes both. - TRAP: a missing row is not a sign-out. It is a session from before the table existed, or one tidied away, and is simply recorded again. Only
revoked_datetimesigns anyone out, so a database problem can never sign everybody out. - A browser kept signed in gets a new session (and row) each time it signs back in; the older rows with the same
kept_token_idare removed, so it shows once.
Related
- Back to the page you were on after signing in (#407, 5.8.0) - when a session does end, signing in takes you back to the page you were on. With Keep me signed in, the restore happens before any page sends you to the sign-in page, so you simply stay where you were.
- Signed out in the middle of working (#107) - why active analysts were signed out (API calls never refreshed the session file). This page is the other half: idle time, a closed browser, a restart.
Test: tests/keep-signed-in.php - 43 checks over real HTTP against a disposable install: the tick only when on; signing in with it; the inbox opening from the cookie alone with the session gone, and the control without it; rotation and the race window; a copied cookie; Off, shortened, password changed, deactivated; sign out; no tick, no cookie; and Signed-in devices - two browsers listed with this device and kept-until marked, one signed out from the other (refused at its next click, the other carries on), Sign out of all other devices ending a third browser's Keep me signed in too, and this device unable to sign itself out from the list; and the guards - 401 signed out, refused without the CSRF token, another analyst's device id signing out nothing, a non-administrator refused on the admin endpoint, and an administrator signing another analyst out everywhere while staying signed in.
See also: System ยท Trust duration ignored (#429) ยท Analysts leaving