Skip to content

Commit 2b7ea90

Browse files
committed
reject content-type headers that have multiple or duplicate types
1 parent 232307d commit 2b7ea90

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

rest.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ const validateContentType = function (req, res, next) {
5555
statusMessage: `Missing or empty Content-Type header.`
5656
}))
5757
}
58+
if (contentType.includes(",")) {
59+
return next(createExpressError({
60+
statusCode: 415,
61+
statusMessage: `Multiple Content-Type values are not allowed. Provide exactly one Content-Type header.`
62+
}))
63+
}
5864
if (mimeType === "application/json" || mimeType === "application/ld+json") return next()
5965
const isSearchEndpoint = req.path === "/search" || req.path.startsWith("/search/")
6066
if (mimeType === "text/plain" && isSearchEndpoint) return next()

routes/__tests__/contentType.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,22 @@ describe("Content-Type validation middleware", () => {
173173
expect(response.text).toContain("Unsupported Content-Type")
174174
})
175175

176+
it("returns 415 for comma-separated multiple Content-Type values", async () => {
177+
const response = await request(routeTester)
178+
.post("/api/create")
179+
.set("Content-Type", "application/json, text/plain")
180+
.send('{"test":"data"}')
181+
expect(response.statusCode).toBe(415)
182+
expect(response.text).toContain("Multiple Content-Type values are not allowed")
183+
})
184+
185+
it("returns 415 for valid type smuggled via comma after charset", async () => {
186+
const response = await request(routeTester)
187+
.post("/api/create")
188+
.set("Content-Type", "application/json; charset=utf-8, text/plain")
189+
.send('{"test":"data"}')
190+
expect(response.statusCode).toBe(415)
191+
expect(response.text).toContain("Multiple Content-Type values are not allowed")
192+
})
193+
176194
})

0 commit comments

Comments
 (0)