Skip to content

Commit fb2afbd

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 fb2afbd

File tree

4 files changed

+37
-35
lines changed

4 files changed

+37
-35
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

+31-26
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,37 +40,42 @@ 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
45-
if (!filePath) {
46-
res.status(HttpCode.BadRequest).send("filePath is required")
47-
return
48-
}
49-
try {
50-
const socketPath = await editorSessionManager.getConnectedSocketPath(filePath)
51-
const response: GetSessionResponse = { socketPath }
52-
res.json(response)
53-
} catch (error: unknown) {
54-
res.status(HttpCode.ServerError).send(error)
55-
}
56-
})
43+
router.get<{}, GetSessionResponse | string | unknown, undefined, { filePath?: string }>(
44+
"/session",
45+
async (req, res) => {
46+
const filePath = req.query.filePath
47+
if (!filePath) {
48+
res.status(HttpCode.BadRequest).send("filePath is required")
49+
return
50+
}
51+
try {
52+
const socketPath = await editorSessionManager.getConnectedSocketPath(filePath)
53+
const response: GetSessionResponse = { socketPath }
54+
res.json(response)
55+
} catch (error: unknown) {
56+
res.status(HttpCode.ServerError).send(error)
57+
}
58+
},
59+
)
5760

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

67-
router.post("/delete-session", async (req, res) => {
68-
const request = req.body as DeleteSessionRequest
69-
if (!request.socketPath) {
71+
router.post<{}, string, DeleteSessionRequest | undefined>("/delete-session", async (req, res) => {
72+
const socketPath = req.body?.socketPath
73+
if (!socketPath) {
7074
res.status(400).send("socketPath is required")
75+
return
7176
}
72-
editorSessionManager.deleteSession(request.socketPath)
73-
res.status(200).send()
77+
editorSessionManager.deleteSession(socketPath)
78+
res.status(200).send("session deleted")
7479
})
7580

7681
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)