From 83dac5b005d7ec9d69d27c87a4c9a89cf6483a05 Mon Sep 17 00:00:00 2001 From: Harsh Bhanushali Date: Tue, 17 Nov 2026 08:43:52 +0000 Subject: [PATCH 1/3] 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 7b570f7cc6c3866853e7b852fd9dc1c8af9b163f Mon Sep 17 00:00:00 2001 From: Harsh Bhanushali Date: Tue, 17 Nov 2026 08:49:39 +0000 Subject: [PATCH 2/3] Add changelog entry for --cookie-max-age --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3cf276ab19d..651cb4700fb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,9 @@ Code v1.146.0 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. ### Fixed From 82a744cea2f8d02a3372672bfcf49c863e4c78a9 Mon Sep 17 00:00:00 2001 From: Harsh Bhanushali Date: Thu, 3 Dec 2026 03:01:29 +0000 Subject: [PATCH 3/3] Rename --cookie-max-age to --cookie-max-age-seconds --- CHANGELOG.md | 2 +- src/node/cli.ts | 8 ++++---- src/node/routes/login.ts | 2 +- test/unit/node/cli.test.ts | 12 ++++++++---- test/unit/node/routes/login.test.ts | 6 +++--- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 651cb4700fb3..1fda676f4e93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,7 +46,7 @@ Code v1.146.0 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 +- New flag `--cookie-max-age-seconds ` 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. diff --git a/src/node/cli.ts b/src/node/cli.ts index 92b3ce76f149..c743162bac6d 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -58,7 +58,7 @@ export interface UserProvidedCodeArgs { "reconnection-grace-time"?: string "session-socket"?: string "cookie-suffix"?: string - "cookie-max-age"?: number + "cookie-max-age-seconds"?: number "link-protection-trusted-domains"?: string[] // locale is used by both VS Code and code-server. locale?: string @@ -187,7 +187,7 @@ 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": { + "cookie-max-age-seconds": { type: "number", description: "Keep the login cookie for this many seconds so you stay logged in after closing the browser. \n" + @@ -478,8 +478,8 @@ 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 (key === "cookie-max-age-seconds" && !(Number(value) > 0)) { + throw new Error("--cookie-max-age-seconds must be a positive number of seconds.") } if (option.type === OptionalString && value === "false") { diff --git a/src/node/routes/login.ts b/src/node/routes/login.ts index b5467a60ea62..6982db677c7d 100644 --- a/src/node/routes/login.ts +++ b/src/node/routes/login.ts @@ -95,7 +95,7 @@ router.post<{}, string, { password?: string; base?: string } | undefined, { to?: // obfuscation purposes (and as a side effect it handles escaping). // 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"] + const maxAge = req.args["cookie-max-age-seconds"] res.cookie(req.cookieSessionName, hashedPassword, { ...getCookieOptions(req), ...(maxAge ? { maxAge: maxAge * 1000 } : {}), diff --git a/test/unit/node/cli.test.ts b/test/unit/node/cli.test.ts index 3be5b51a3b2a..63103ebfed72 100644 --- a/test/unit/node/cli.test.ts +++ b/test/unit/node/cli.test.ts @@ -272,10 +272,14 @@ 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 parse cookie-max-age-seconds and reject non-positive values", () => { + expect(parse(["--cookie-max-age-seconds", "3600"])).toEqual({ "cookie-max-age-seconds": 3600 }) + expect(() => parse(["--cookie-max-age-seconds", "0"])).toThrowError( + /--cookie-max-age-seconds must be a positive number/, + ) + expect(() => parse(["--cookie-max-age-seconds=abc"])).toThrowError( + /--cookie-max-age-seconds must be a positive number/, + ) }) it("should error if idle-timeout-seconds is too low", () => { diff --git a/test/unit/node/routes/login.test.ts b/test/unit/node/routes/login.test.ts index cd10937dbfb7..ba98eb530ffc 100644 --- a/test/unit/node/routes/login.test.ts +++ b/test/unit/node/routes/login.test.ts @@ -101,7 +101,7 @@ describe("login", () => { expect(htmlContent).toContain("Incorrect password") }) - it("should set a session cookie without --cookie-max-age", async () => { + it("should set a session cookie without --cookie-max-age-seconds", async () => { const resp = await codeServer().fetch("/login", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, @@ -113,10 +113,10 @@ describe("login", () => { expect(cookie).not.toMatch(/Max-Age|Expires/i) }) - it("should set Max-Age on the login cookie with --cookie-max-age", async () => { + it("should set Max-Age on the login cookie with --cookie-max-age-seconds", async () => { await codeServer().dispose() process.env.PASSWORD = "test" - _codeServer = await integration.setup(["--auth=password", "--cookie-max-age=3600"], "") + _codeServer = await integration.setup(["--auth=password", "--cookie-max-age-seconds=3600"], "") const resp = await codeServer().fetch("/login", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" },