Skip to content

Commit ba87ecc

Browse files
committed
Fix issue where tests lack cookie key.
1 parent 5adc97d commit ba87ecc

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

src/common/http.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ export class HttpError extends Error {
1818
this.name = this.constructor.name
1919
}
2020
}
21+
22+
export enum CookieKeys {
23+
Session = "code-server-session",
24+
}

src/node/http.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as net from "net"
66
import path from "path"
77
import qs from "qs"
88
import { Disposable } from "../common/emitter"
9-
import { HttpCode, HttpError } from "../common/http"
9+
import { CookieKeys, HttpCode, HttpError } from "../common/http"
1010
import { normalize } from "../common/util"
1111
import { AuthType, DefaultedArgs } from "./cli"
1212
import { version as codeServerVersion } from "./constants"
@@ -62,10 +62,6 @@ export const replaceTemplates = <T extends object>(
6262
.replace("{{OPTIONS}}", () => escapeJSON(serverOptions))
6363
}
6464

65-
export enum Cookie {
66-
SessionKey = "code-server-session",
67-
}
68-
6965
/**
7066
* Throw an error if not authorized. Call `next` if provided.
7167
*/
@@ -97,7 +93,7 @@ export const authenticated = async (req: express.Request): Promise<boolean> => {
9793
const passwordMethod = getPasswordMethod(hashedPasswordFromArgs)
9894
const isCookieValidArgs: IsCookieValidArgs = {
9995
passwordMethod,
100-
cookieKey: sanitizeString(req.cookies[Cookie.SessionKey]),
96+
cookieKey: sanitizeString(req.cookies[CookieKeys.Session]),
10197
passwordFromArgs: req.args.password || "",
10298
hashedPasswordFromArgs: req.args["hashed-password"],
10399
}

src/node/routes/login.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { promises as fs } from "fs"
33
import { RateLimiter as Limiter } from "limiter"
44
import * as os from "os"
55
import * as path from "path"
6+
import { CookieKeys } from "../../common/http"
67
import { rootPath } from "../constants"
7-
import { authenticated, Cookie, getCookieDomain, redirect, replaceTemplates } from "../http"
8+
import { authenticated, getCookieDomain, redirect, replaceTemplates } from "../http"
89
import { getPasswordMethod, handlePasswordValidation, humanPath, sanitizeString, escapeHtml } from "../util"
910

1011
// RateLimiter wraps around the limiter library for logins.
@@ -83,7 +84,7 @@ router.post<{}, string, { password: string; base?: string }, { to?: string }>("/
8384
if (isPasswordValid) {
8485
// The hash does not add any actual security but we do it for
8586
// obfuscation purposes (and as a side effect it handles escaping).
86-
res.cookie(Cookie.SessionKey, hashedPassword, {
87+
res.cookie(CookieKeys.Session, hashedPassword, {
8788
domain: getCookieDomain(req.headers.host || "", req.args["proxy-domain"]),
8889
// Browsers do not appear to allow cookies to be set relatively so we
8990
// need to get the root path from the browser since the proxy rewrites

src/node/routes/logout.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Router } from "express"
2-
import { Cookie, getCookieDomain, redirect } from "../http"
2+
import { CookieKeys } from "../../common/http"
3+
import { getCookieDomain, redirect } from "../http"
4+
35
import { sanitizeString } from "../util"
46

57
export const router = Router()
@@ -9,7 +11,7 @@ router.get<{}, undefined, undefined, { base?: string; to?: string }>("/", async
911
const to = sanitizeString(req.query.to) || "/"
1012

1113
// Must use the *identical* properties used to set the cookie.
12-
res.clearCookie(Cookie.SessionKey, {
14+
res.clearCookie(CookieKeys.Session, {
1315
domain: getCookieDomain(req.headers.host || "", req.args["proxy-domain"]),
1416
path: decodeURIComponent(path),
1517
sameSite: "lax",

test/utils/globalSetup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Cookie } from "playwright"
2+
import { CookieKeys } from "../../src/common/http"
23
import { hash } from "../../src/node/util"
34
import { PASSWORD, workspaceDir } from "./constants"
45
import { clean } from "./helpers"
@@ -27,7 +28,7 @@ export default async function () {
2728
domain: "localhost",
2829
expires: -1,
2930
httpOnly: false,
30-
name: "key",
31+
name: CookieKeys.Session,
3132
path: "/",
3233
sameSite: "Lax",
3334
secure: false,

0 commit comments

Comments
 (0)