Skip to content

Commit 7ec83f2

Browse files
committed
Handle undefined body
In the latest Express it seems the body is undefined when no data is passed (instead of being empty).
1 parent 3d8d544 commit 7ec83f2

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

src/node/http.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ export const getCookieOptions = (req: express.Request): express.CookieOptions =>
319319
// URL of that page) and the relative path to the root as given to it by the
320320
// backend. Using these two we can determine the true absolute root.
321321
const url = new URL(
322-
req.query.base || req.body.base || "/",
323-
req.query.href || req.body.href || "http://" + (req.headers.host || "localhost"),
322+
req.query.base || req.body?.base || "/",
323+
req.query.href || req.body?.href || "http://" + (req.headers.host || "localhost"),
324324
)
325325
return {
326326
domain: getCookieDomain(url.host, req.args["proxy-domain"]),

src/node/routes/login.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ router.get("/", async (req, res) => {
6868
res.send(await getRoot(req))
6969
})
7070

71-
router.post<{}, string, { password: string; base?: string }, { to?: string }>("/", async (req, res) => {
72-
const password = sanitizeString(req.body.password)
71+
router.post<{}, string, { password?: string; base?: string } | undefined, { to?: string }>("/", async (req, res) => {
72+
const password = sanitizeString(req.body?.password)
7373
const hashedPasswordFromArgs = req.args["hashed-password"]
7474

7575
try {

src/node/vscodeSocket.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export interface EditorSessionEntry {
2020
}
2121

2222
interface DeleteSessionRequest {
23-
socketPath: string
23+
socketPath?: string
2424
}
2525

2626
interface AddSessionRequest {
27-
entry: EditorSessionEntry
27+
entry?: EditorSessionEntry
2828
}
2929

3030
interface GetSessionResponse {
@@ -40,8 +40,8 @@ export async function makeEditorSessionManagerServer(
4040
// eslint-disable-next-line import/no-named-as-default-member
4141
router.use(express.json())
4242

43-
router.get("/session", async (req, res) => {
44-
const filePath = req.query.filePath as string
43+
router.get<{}, GetSessionResponse | string | unknown, undefined, { filePath?: string }>("/session", async (req, res) => {
44+
const filePath = req.query.filePath
4545
if (!filePath) {
4646
res.status(HttpCode.BadRequest).send("filePath is required")
4747
return
@@ -55,22 +55,24 @@ export async function makeEditorSessionManagerServer(
5555
}
5656
})
5757

58-
router.post("/add-session", async (req, res) => {
59-
const request = req.body as AddSessionRequest
60-
if (!request.entry) {
58+
router.post<{}, string, AddSessionRequest | undefined>("/add-session", async (req, res) => {
59+
const entry = req.body?.entry
60+
if (!entry) {
6161
res.status(400).send("entry is required")
62+
return
6263
}
63-
editorSessionManager.addSession(request.entry)
64-
res.status(200).send()
64+
editorSessionManager.addSession(entry)
65+
res.status(200).send("session added")
6566
})
6667

67-
router.post("/delete-session", async (req, res) => {
68-
const request = req.body as DeleteSessionRequest
69-
if (!request.socketPath) {
68+
router.post<{}, string, DeleteSessionRequest | undefined>("/delete-session", async (req, res) => {
69+
const socketPath = req.body?.socketPath
70+
if (!socketPath) {
7071
res.status(400).send("socketPath is required")
72+
return
7173
}
72-
editorSessionManager.deleteSession(request.socketPath)
73-
res.status(200).send()
74+
editorSessionManager.deleteSession(socketPath)
75+
res.status(200).send("session deleted")
7476
})
7577

7678
const server = http.createServer(router)

test/unit/node/routes/login.test.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,10 @@ describe("login", () => {
6868
}
6969
})
7070

71-
it("should return HTML with 'Missing password' message", async () => {
71+
it("should return 'Missing password' without body", async () => {
7272
const resp = await codeServer().fetch("/login", { method: "POST" })
73-
74-
expect(resp.status).toBe(200)
75-
7673
const htmlContent = await resp.text()
77-
74+
expect(resp.status).toBe(200)
7875
expect(htmlContent).toContain("Missing password")
7976
})
8077

0 commit comments

Comments
 (0)