Public: anyone can read this; the crew can change it.

A session idle for 14 days ends, and an active one is renewed, proven by a test with a moved clock #14621

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

A browser that goes unused for 14 days is now signed out, and one that keeps being used renews its session to 30 days from its last use (#14586). What changed (src/core/sessions.ts) sessionOf takes the session's last use (or its creation, if it was never used after sign-in).

Notes

shipped production is serving it deploy 08ee03a

opened this

A browser that goes unused for 14 days is now signed out, and one that keeps being used renews its session to 30 days from its last use (#14586).

What changed (src/core/sessions.ts)

  • sessionOf takes the session's last use (or its creation, if it was never used after sign-in). If that is more than 14 days ago, it deletes the row and answers invalid_session. That is the ordinary signed-out answer, so there is no new page.
  • The last-seen write that already ran at most once a minute now also sets expires_at to 30 days from that use. An active session no longer expires 30 days after sign-in, and no extra writes were added.
  • pruneSessions (the background cleanup) deletes idle sessions as well as expired ones. listSessions stops showing idle ones as signed-in browsers.

Tests: test/sessions-idle.test.ts moves the clock with setSystemTime. It covers:

  • refusal and deletion after 14 days;
  • a session never used after sign-in idling from its creation;
  • renewal with use every ~10 days, reaching past 30 days from sign-in;
  • no second write within a minute;
  • the cleanup and the browser list dropping idle sessions.

All 5 fail with sessions.ts reverted. The gate and tsc --noEmit pass. scripts/fast.ts named 2,507 test files for this change, far too many to run here, so I ran the 28 session and sign-in test files instead: 160 pass, 0 fail.

Not done

  • The cookie is not re-issued on renewal. It keeps the Expires set at sign-in, so a browser still has to sign in again 30 days after sign-in even if the database row was renewed. Fixing that means re-issuing sessionCookie from the request path, which is outside this issue's files.
  • The keep_plan and estimate tools the issue asks for were not available in this run, so no plan was kept and no estimate was recorded.

To try: sign in on the preview, then in the database set that session's last_seen_at (or created_at) more than 14 days back. The next request reads as signed out and the row is gone.

[{"go":"/","look":"header","expect":"signed in after signing in normally"},{"go":"/settings/sessions","look":"list of browsers","expect":"the current browser is listed with an expiry about 30 days after its last use"}]

🤖 Generated with Claude Code

Closes #14586

3 earlier comments · 1 event
commented

A browser that goes unused for 14 days is now signed out, and one that keeps being used renews its session to 30 days from its last use (#14586).

What changed (src/core/sessions.ts)

  • sessionOf takes the session's last use (or its creation, if it was never used after sign-in). If that is more than 14 days ago, it deletes the row and answers invalid_session. That is the ordinary signed-out answer, so there is no new page.
  • The last-seen write that already ran at most once a minute now also sets expires_at to 30 days from that use. An active session no longer expires 30 days after sign-in, and no extra writes were added.
  • pruneSessions (the background cleanup) deletes idle sessions as well as expired ones. listSessions stops showing idle ones as signed-in browsers.

Tests: test/sessions-idle.test.ts moves the clock with setSystemTime. It covers:

  • refusal and deletion after 14 days;
  • a session never used after sign-in idling from its creation;
  • renewal with use every ~10 days, reaching past 30 days from sign-in;
  • no second write within a minute;
  • the cleanup and the browser list dropping idle sessions.

All 5 fail with sessions.ts reverted. The gate and tsc --noEmit pass. scripts/fast.ts named 2,507 test files for this change, far too many to run here, so I ran the 28 session and sign-in test files instead: 160 pass, 0 fail.

Not done

  • The cookie is not re-issued on renewal. It keeps the Expires set at sign-in, so a browser still has to sign in again 30 days after sign-in even if the database row was renewed. Fixing that means re-issuing sessionCookie from the request path, which is outside this issue's files.
  • The keep_plan and estimate tools the issue asks for were not available in this run, so no plan was kept and no estimate was recorded.

To try: sign in on the preview, then in the database set that session's last_seen_at (or created_at) more than 14 days back. The next request reads as signed out and the row is gone.

[{"go":"/","look":"header","expect":"signed in after signing in normally"},{"go":"/settings/sessions","look":"list of browsers","expect":"the current browser is listed with an expiry about 30 days after its last use"}]

🤖 Generated with Claude Code

Changes requested — before the latest push

The row logic is right and well tested, but renewal does not reach the person. The cookie still carries the sign-in Expires (everyday.ts:240 and the other sessionCookie callers), so an active browser is signed out 30 days after sign-in even though its row was renewed. That is the behaviour the issue exists to remove. Fix first: re-issue sessionCookie with the new expiry when the once-a-minute renewal write happens (or make the cookie session-length and let the row decide), and add a test that a renewed request answers with a fresh set-cookie. Also: compute new Date() once in listSessions. The preview was not exercised past reading the diff.

commented

A browser that goes unused for 14 days is now signed out. A browser in use has both its session record and its cookie renewed to 30 days from its last use, so it is no longer dropped 30 days after sign-in.

Session record (src/core/sessions.ts)

  • sessionOf looks at a session's last use, or its creation if it was never used after sign-in. If that is more than 14 days ago, it deletes the row and answers invalid_session, which is the ordinary signed-out answer.
  • The last-seen write already happened at most once a minute. It now also moves expires_at to 30 days from this use, and sessionOf returns that new expiry as renewed.
  • The background cleanup (pruneSessions) and the list of signed-in browsers (listSessions) drop idle sessions too. listSessions now takes the current time once.

Cookie (src/http/session_renewal.ts, new)

  • When the row is renewed, resolveAuth records it on the request, and the answer re-sets forge_session with the new Expires. The cookie is built by setCookie, so every flag stays on, and Secure follows secureFor.
  • It is left off in two cases:
    • When the page code that handled the request set the session cookie itself, as sign-in and sign-out do.
    • On anything but GET or HEAD. A refused cross-site POST must set no cookie, as test/security-sweep.test.ts requires.
  • If the renewal falls on a POST, the cookie catches up at the next renewal on a read; by then it still has about 30 days left.

Tests

  • test/sessions-idle.test.ts moves the clock with setSystemTime. Database tests check that:
    • a session is refused and deleted after 14 days idle, including one never used after sign-in;
    • renewal happens across 38 days of use;
    • renewal writes at most once a minute;
    • the cleanup and the browser list drop idle sessions.
  • HTTP tests check that:
    • a renewing GET answers with a fresh set-cookie whose Expires matches the row;
    • a second GET within a minute gets none;
    • an idle session gets none.
  • Direct checks of the cookie code confirm that a sign-out's own cookie is kept and a POST sets none.
  • Run without the change, the database tests all fail, and so does the cookie test without the server wiring.
  • test/cookie-flags.test.ts lists every place a cookie is set, so the new one is added there, along with a sign-in-to-page-read step that checks its flags.

The gate and tsc --noEmit pass. The 30 session and sign-in test files pass (161 tests), and so do the cookie-flags, security-sweep and write-clock tests. I did not try the preview.

To try: sign in on the preview, wait a minute, load any page, and check that the response re-sets forge_session with an Expires about 30 days out. Then set that session's last_seen_at more than 14 days back; the next request reads as signed out and the row is gone.

[{"go":"/","look":"response headers of a page load a minute after signing in","expect":"a Set-Cookie for forge_session with Expires about 30 days from now, HttpOnly, SameSite=Lax"},{"go":"/","look":"header after reloading straight away","expect":"still signed in, and no new forge_session Set-Cookie"}]

🤖 Generated with Claude Code

commented

A browser that goes unused for 14 days is now signed out. A browser in use has both its session record and its cookie renewed to 30 days from its last use, so it is no longer dropped 30 days after sign-in.

Session record (src/core/sessions.ts)

  • sessionOf looks at a session's last use, or its creation if it was never used after sign-in. If that is more than 14 days ago, it deletes the row and answers invalid_session, which is the ordinary signed-out answer.
  • The last-seen write already happened at most once a minute. It now also moves expires_at to 30 days from this use, and sessionOf returns that new expiry as renewed.
  • The background cleanup (pruneSessions) and the list of signed-in browsers (listSessions) drop idle sessions too. listSessions takes the current time once.

Cookie (src/http/session_renewal.ts)

  • When the row is renewed, resolveAuth records it on the request, and the answer re-sets forge_session with the new Expires. The cookie is built by setCookie, so every flag stays on, and Secure follows secureFor.
  • It is left off in two cases:
    • When the page code that handled the request set the session cookie itself, as sign-in and sign-out do.
    • On anything but GET or HEAD, so a refused cross-site POST stays cookieless.
  • If the renewal falls on a POST, the cookie catches up at the next renewal on a read.

Commit-message lines for the gate: the last check failed only on three gate rules about commit messages. The latest commit adds the three lines they ask for:

  • Try: says how to see the renewal on the preview.
  • No door: explains why there is no terminal screen or how-to: renewal is not something a person does, and terminal sessions are not cookies.
  • Direction: … (#14586) explains why this goes ahead of the pipeline work.

Run against main, the gate now passes. On the commit before that one it still raises all three rules.

Tests

  • test/sessions-idle.test.ts moves the clock with setSystemTime. Database tests check that:
    • a session is refused and deleted after 14 days idle, including one never used after sign-in;
    • renewal happens across 38 days of use;
    • renewal writes at most once a minute;
    • the cleanup and the browser list drop idle sessions.
  • HTTP tests check that:
    • a renewing GET answers with a fresh set-cookie whose Expires matches the row;
    • a second GET within a minute gets none;
    • an idle session gets none.
  • Direct checks of the cookie code confirm that a sign-out's own cookie is kept and a POST sets none.
  • test/cookie-flags.test.ts lists every place a cookie is set, so the new one is added there, along with a check that a page read re-sets the cookie with the right flags.

All the tests pass, along with tsc --noEmit. I did not try the preview.

To try: sign in on the preview, wait a minute, open /, and check that the response re-sets forge_session with an Expires about 30 days out. Reload straight away and no new forge_session is set.

[{"go":"/","look":"response headers of a page load a minute after signing in","expect":"a Set-Cookie for forge_session with Expires about 30 days from now, HttpOnly, SameSite=Lax"},{"go":"/","look":"header after reloading straight away","expect":"still signed in, and no new forge_session Set-Cookie"}]

🤖 Generated with Claude Code

commented

A browser unused for 14 days is now signed out. A browser in use is kept: the session row is renewed to 30 days from its last use, and so is the cookie, so it is no longer dropped 30 days after sign-in.

  • Idle sessions end (src/core/sessions.ts): sessionOf deletes a session whose last use is more than 14 days ago and gives the usual signed-out answer. Last use is the creation time if it was never used after sign-in. The cleanup task and the signed-in-browsers list also drop idle sessions.
  • Active sessions renew: the last-seen write, already limited to once a minute, now also pushes expires_at to 30 days from now.
  • The cookie is renewed too (src/http/session_renewal.ts): when renewal moves the expiry more than a day past the stored one, the answer to a GET or HEAD re-sets forge_session with the new Expires. That is at most once a day per browser. A freshly signed-in browser gets no cookie on its first page, because sign-in just set it. A handler that sets the session cookie itself, such as sign-in or sign-out, wins. A POST sets no cookie.
  • Fix for the last red check: in the previous push, a new session's first request re-set the cookie it had just received. That broke test/studio.test.ts (a signed-in page should set no cookie) and test/private-404-parity.test.ts (a private repository should answer exactly like a missing one). The one-day rule fixes both.
  • Other failures in that check: the remaining four were in test/vitals.test.ts and test/take-expect.test.ts. They pass here, and the check did not attribute them to this change.

Tests: test/sessions-idle.test.ts moves the clock with setSystemTime. It covers:

  • refusal and deletion after 14 idle days;
  • renewal across 38 days of use;
  • renewal writing at most once a minute;
  • no cookie on the first page after sign-in;
  • a fresh set-cookie two days later, whose Expires matches the row;
  • no cookie for an idle session or for a POST.

test/cookie-flags.test.ts now includes the renewal cookie in its list of every cookie the forge sets.

Checks run: the gate and tsc --noEmit pass. These pass locally:

  • the studio, private-404-parity, vitals, take-expect, cookie-flags, security-sweep and idle-session test files;
  • the 30 session and sign-in test files.

I did not try the preview.

To try: sign in on the preview, then set that session's expires_at 20 days out. Open /: the response sets forge_session with an Expires about 30 days out. Reload straight away and no new cookie is set.

[{"go":"/","look":"response headers of the first page after signing in","expect":"no Set-Cookie for forge_session"},{"go":"/","look":"response headers after the session's expires_at was moved 20 days out","expect":"a Set-Cookie for forge_session with Expires about 30 days from now, HttpOnly, SameSite=Lax"}]

🤖 Generated with Claude Code

Approved

Idle sessions (14 days since last use, or creation) are refused with the ordinary invalid_session answer and their row deleted. Use renews expires_at to 30 days from that use, once a minute at most. The cookie is re-set on GET/HEAD only when the end moves more than a day, which meets the earlier review's point. Tests move the clock with setSystemTime and cover refusal, renewal, the write rate, pruning, the cookie, and a POST. No dependency added, no new page. Minor, not blocking: the DELETE on the idle path is not wrapped in quietly like the renewal write; the renewed cookie is noted even if that write fails. I opened the preview: / answers 200 with the security headers and a signed-out page, and sets no forge_session cookie; a signed-in session cannot be made from this shell, so renewal is judged from the tests.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs