Skip to content

Add --cookie-max-age to keep the login cookie across browser sessions - #8147

Merged
code-asher merged 3 commits into
coder:mainfrom
harshbhx:fix/7301-cookie-max-age
Dec 3, 2026
Merged

code-asher merged 3 commits into
coder:mainfrom
harshbhx:fix/7301-cookie-max-age

Conversation

@harshbhx

@harshbhx harshbhx commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7301

Summary

Adds an opt-in --cookie-max-age-seconds <seconds> flag so the login cookie can outlive the browser session.

Problem

The login cookie is set without Max-Age/Expires, so it is a session cookie and users must log in again every time the browser is closed (#7301). There is no setting to change this.

Solution

A new numeric flag cookie-max-age-seconds. When set, the cookie set in src/node/routes/login.ts gets maxAge = seconds * 1000. Without the flag behaviour is unchanged. Non-positive or non-numeric values throw at startup, like --idle-timeout-seconds. The max age is added at login only, not in getCookieOptions, because logout reuses those options to clear the cookie.

Changes

File Change
src/node/cli.ts new cookie-max-age-seconds option, description, validation
src/node/routes/login.ts apply maxAge when setting the login cookie
CHANGELOG.md Unreleased entry
test/unit/node/cli.test.ts parsing and validation
test/unit/node/routes/login.test.ts login sets Max-Age=3600 with the flag, no Max-Age/Expires without it

Before / After

Before: Set-Cookie: code-server-session=...; Path=/; HttpOnly; SameSite=Lax
After (--cookie-max-age-seconds=3600): ...; Max-Age=3600; Expires=...

How to test

  1. PASSWORD=x code-server --cookie-max-age-seconds 3600, log in, check the cookie expiry in devtools; restart the browser and confirm you are still logged in.
  2. Without the flag the cookie is still a session cookie.

Testing done

  • New unit tests; the two login tests and the cli test fail without the change, pass with it (96/96 in cli + login suites)
  • Prettier check on the touched files
  • Full suite and e2e only via fork CI; not tried in a real browser

Notes for reviewers

Documented in the CHANGELOG and --help. No env var yet; happy to add CODE_SERVER_COOKIE_MAX_AGE if you want one.

@harshbhx
harshbhx requested a review from a team as a code owner November 19, 2026 19:46
@harshbhx
harshbhx force-pushed the fix/7301-cookie-max-age branch from 4e26a60 to 7b570f7 Compare November 22, 2026 15:12
@harshbhx

Copy link
Copy Markdown
Contributor Author

Hi @code-asher, friendly ping when you have time. Checks are green, and I'm happy to rework the approach if you'd rather handle the cookie lifetime differently.

@code-asher code-asher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! I think we just add the unit to the flag name and we are golden.

Comment thread src/node/routes/login.ts Outdated
res.cookie(req.cookieSessionName, hashedPassword, getCookieOptions(req))
// The max age is only set here, not in getCookieOptions, because those
// options are also used to clear the cookie on logout.
const maxAge = req.args["cookie-max-age"]

@code-asher code-asher Dec 2, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh just had a thought, maybe we call this cookie-max-age-seconds to match other flags like idle-timeout-seconds. Just to make it really clear what unit this flag represents.

(And also because in the future I think it would be cool to support strings like 10m for 10 minutes with all our time-based flags, and we would want to leave the unit-less version of the flags available for that.)

@harshbhx

harshbhx commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Done in 82a744c: renamed to --cookie-max-age-seconds (CLI option, error message, login route, tests, changelog). Good call on leaving the unit-less name free for 10m-style values later.

@code-asher code-asher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome thank you!

@code-asher
code-asher merged commit e7e329f into coder:main Dec 3, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow configuring login cookie with an expiry duration

2 participants