Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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.

### Changed

Expand Down
11 changes: 11 additions & 0 deletions src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -186,6 +187,12 @@ export const options: Options<Required<UserProvidedArgs>> = {
"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:
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 7 additions & 1 deletion src/node/routes/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
6 changes: 6 additions & 0 deletions test/unit/node/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/,
Expand Down
25 changes: 25 additions & 0 deletions test/unit/node/routes/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down