Skip to content

Commit

Permalink
Handle undefined body
Browse files Browse the repository at this point in the history
In the latest Express it seems the body is undefined when no data is
passed (instead of being empty).
  • Loading branch information
code-asher committed Apr 16, 2024
1 parent 3d8d544 commit fb2afbd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ export const getCookieOptions = (req: express.Request): express.CookieOptions =>
// URL of that page) and the relative path to the root as given to it by the
// backend. Using these two we can determine the true absolute root.
const url = new URL(
req.query.base || req.body.base || "/",
req.query.href || req.body.href || "http://" + (req.headers.host || "localhost"),
req.query.base || req.body?.base || "/",
req.query.href || req.body?.href || "http://" + (req.headers.host || "localhost"),
)
return {
domain: getCookieDomain(url.host, req.args["proxy-domain"]),
Expand Down
4 changes: 2 additions & 2 deletions src/node/routes/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ router.get("/", async (req, res) => {
res.send(await getRoot(req))
})

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

try {
Expand Down
57 changes: 31 additions & 26 deletions src/node/vscodeSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export interface EditorSessionEntry {
}

interface DeleteSessionRequest {
socketPath: string
socketPath?: string
}

interface AddSessionRequest {
entry: EditorSessionEntry
entry?: EditorSessionEntry
}

interface GetSessionResponse {
Expand All @@ -40,37 +40,42 @@ export async function makeEditorSessionManagerServer(
// eslint-disable-next-line import/no-named-as-default-member
router.use(express.json())

router.get("/session", async (req, res) => {
const filePath = req.query.filePath as string
if (!filePath) {
res.status(HttpCode.BadRequest).send("filePath is required")
return
}
try {
const socketPath = await editorSessionManager.getConnectedSocketPath(filePath)
const response: GetSessionResponse = { socketPath }
res.json(response)
} catch (error: unknown) {
res.status(HttpCode.ServerError).send(error)
}
})
router.get<{}, GetSessionResponse | string | unknown, undefined, { filePath?: string }>(
"/session",
async (req, res) => {
const filePath = req.query.filePath
if (!filePath) {
res.status(HttpCode.BadRequest).send("filePath is required")
return
}
try {
const socketPath = await editorSessionManager.getConnectedSocketPath(filePath)
const response: GetSessionResponse = { socketPath }
res.json(response)
} catch (error: unknown) {
res.status(HttpCode.ServerError).send(error)
}
},
)

router.post("/add-session", async (req, res) => {
const request = req.body as AddSessionRequest
if (!request.entry) {
router.post<{}, string, AddSessionRequest | undefined>("/add-session", async (req, res) => {
const entry = req.body?.entry
if (!entry) {
res.status(400).send("entry is required")
return
}
editorSessionManager.addSession(request.entry)
res.status(200).send()
editorSessionManager.addSession(entry)
res.status(200).send("session added")
})

router.post("/delete-session", async (req, res) => {
const request = req.body as DeleteSessionRequest
if (!request.socketPath) {
router.post<{}, string, DeleteSessionRequest | undefined>("/delete-session", async (req, res) => {
const socketPath = req.body?.socketPath
if (!socketPath) {
res.status(400).send("socketPath is required")
return
}
editorSessionManager.deleteSession(request.socketPath)
res.status(200).send()
editorSessionManager.deleteSession(socketPath)
res.status(200).send("session deleted")
})

const server = http.createServer(router)
Expand Down
7 changes: 2 additions & 5 deletions test/unit/node/routes/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,10 @@ describe("login", () => {
}
})

it("should return HTML with 'Missing password' message", async () => {
it("should return 'Missing password' without body", async () => {
const resp = await codeServer().fetch("/login", { method: "POST" })

expect(resp.status).toBe(200)

const htmlContent = await resp.text()

expect(resp.status).toBe(200)
expect(htmlContent).toContain("Missing password")
})

Expand Down

0 comments on commit fb2afbd

Please sign in to comment.