From 09a04471ac9b97448b03abf2d0de527ad6c2cbc6 Mon Sep 17 00:00:00 2001 From: Harsh Bhanushali Date: Tue, 17 Nov 2026 08:43:52 +0000 Subject: [PATCH 1/2] Add --cookie-max-age to keep the login cookie across browser sessions --- src/node/cli.ts | 11 +++++++++++ src/node/routes/login.ts | 8 +++++++- test/unit/node/cli.test.ts | 6 ++++++ test/unit/node/routes/login.test.ts | 25 +++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/node/cli.ts b/src/node/cli.ts index cd8e96781558..92b3ce76f149 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -58,6 +58,7 @@ export interface UserProvidedCodeArgs { "reconnection-grace-time"?: string "session-socket"?: string "cookie-suffix"?: string + "cookie-max-age"?: number "link-protection-trusted-domains"?: string[] // locale is used by both VS Code and code-server. locale?: string @@ -186,6 +187,12 @@ export const options: Options> = { "Adds a suffix to the cookie. This can prevent a collision of cookies for subdomains, making them explixit. \n" + "Without this flag, no suffix is used. This can also be set with CODE_SERVER_COOKIE_SUFFIX set to any string.", }, + "cookie-max-age": { + type: "number", + description: + "Keep the login cookie for this many seconds so you stay logged in after closing the browser. \n" + + "Without this flag the cookie only lasts for the browser session.", + }, "disable-file-downloads": { type: "boolean", description: @@ -471,6 +478,10 @@ export const parse = ( throw new Error("--idle-timeout-seconds must be greater than 60 seconds.") } + if (key === "cookie-max-age" && !(Number(value) > 0)) { + throw new Error("--cookie-max-age must be a positive number of seconds.") + } + if (option.type === OptionalString && value === "false") { continue } diff --git a/src/node/routes/login.ts b/src/node/routes/login.ts index e3f797e76471..b5467a60ea62 100644 --- a/src/node/routes/login.ts +++ b/src/node/routes/login.ts @@ -93,7 +93,13 @@ router.post<{}, string, { password?: string; base?: string } | undefined, { to?: if (isPasswordValid) { // The hash does not add any actual security but we do it for // obfuscation purposes (and as a side effect it handles escaping). - 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"] + res.cookie(req.cookieSessionName, hashedPassword, { + ...getCookieOptions(req), + ...(maxAge ? { maxAge: maxAge * 1000 } : {}), + }) const to = (typeof req.query.to === "string" && req.query.to) || "/" return redirect(req, res, to, { to: undefined }) diff --git a/test/unit/node/cli.test.ts b/test/unit/node/cli.test.ts index ebd0ab58a61a..3be5b51a3b2a 100644 --- a/test/unit/node/cli.test.ts +++ b/test/unit/node/cli.test.ts @@ -272,6 +272,12 @@ describe("parser", () => { expect(() => parse(["--log", "invalid"])).toThrowError(/--log valid values: \[trace, debug, info, warn, error\]/) }) + it("should parse cookie-max-age and reject non-positive values", () => { + expect(parse(["--cookie-max-age", "3600"])).toEqual({ "cookie-max-age": 3600 }) + expect(() => parse(["--cookie-max-age", "0"])).toThrowError(/--cookie-max-age must be a positive number/) + expect(() => parse(["--cookie-max-age=abc"])).toThrowError(/--cookie-max-age must be a positive number/) + }) + it("should error if idle-timeout-seconds is too low", () => { expect(() => parse(["--idle-timeout-seconds=60"])).toThrowError( /--idle-timeout-seconds must be greater than 60 seconds/, diff --git a/test/unit/node/routes/login.test.ts b/test/unit/node/routes/login.test.ts index e7c9857bc607..cd10937dbfb7 100644 --- a/test/unit/node/routes/login.test.ts +++ b/test/unit/node/routes/login.test.ts @@ -101,6 +101,31 @@ describe("login", () => { expect(htmlContent).toContain("Incorrect password") }) + it("should set a session cookie without --cookie-max-age", async () => { + const resp = await codeServer().fetch("/login", { + method: "POST", + headers: { "Content-Type": "application/x-www-form-urlencoded" }, + body: "password=test", + redirect: "manual", + }) + const cookie = resp.headers.get("set-cookie") || "" + expect(cookie).toContain("code-server-session=") + expect(cookie).not.toMatch(/Max-Age|Expires/i) + }) + + it("should set Max-Age on the login cookie with --cookie-max-age", async () => { + await codeServer().dispose() + process.env.PASSWORD = "test" + _codeServer = await integration.setup(["--auth=password", "--cookie-max-age=3600"], "") + const resp = await codeServer().fetch("/login", { + method: "POST", + headers: { "Content-Type": "application/x-www-form-urlencoded" }, + body: "password=test", + redirect: "manual", + }) + expect(resp.headers.get("set-cookie")).toContain("Max-Age=3600") + }) + it("should return correct app-name", async () => { process.env.PASSWORD = previousEnvPassword const appName = "testnäme" From 4e26a6051a324f4b6b8cd8ab1bbcc71c85439f94 Mon Sep 17 00:00:00 2001 From: Harsh Bhanushali Date: Tue, 17 Nov 2026 08:49:39 +0000 Subject: [PATCH 2/2] Add changelog entry for --cookie-max-age --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9089dbb3e1ab..0651ab9c2e75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,9 @@ Code v1.145.3 returning, so `code-server --wait` can be used as `EDITOR` or `GIT_EDITOR`. Before, code-server's own script failed with `Unknown option --wait`, so this only worked when the integrated terminal found the `remote-cli` script first. +- New flag `--cookie-max-age ` that keeps the login cookie for the given + time so you stay logged in after closing the browser. Without it the cookie + is still a session cookie. ### Changed