Skip to content

Commit 46597e7

Browse files
mikey923CodeQL Automationgr2m
authored
fix: handle missing Content-Type header with null check (#805)
* Bugfix for Unhandled Exception Bugfix for missing null/undefined check in middleware.ts for the "content-type" header. Issue introduced by b7aee15. * Remove extra space * Add test case for missing content-type header * Linted * Use falsy check for cleaner comparison Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com> Co-authored-by: CodeQL Automation <application-security@marriott.com> Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
1 parent b9a2966 commit 46597e7

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/middleware/node/middleware.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ export async function middleware(
4747
// Check if the Content-Type header is `application/json` and allow for charset to be specified in it
4848
// Otherwise, return a 415 Unsupported Media Type error
4949
// See https://github.com/octokit/webhooks.js/issues/158
50-
if (!request.headers["content-type"].startsWith("application/json")) {
50+
if (
51+
!request.headers["content-type"] ||
52+
!request.headers["content-type"].startsWith("application/json")
53+
) {
5154
response.writeHead(415, {
5255
"content-type": "application/json",
5356
accept: "application/json",

test/integration/node-middleware.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,36 @@ describe("createNodeMiddleware(webhooks)", () => {
139139
server.close();
140140
});
141141

142+
test("Handles Missing Content-Type", async () => {
143+
const webhooks = new Webhooks({
144+
secret: "mySecret",
145+
});
146+
147+
const server = createServer(createNodeMiddleware(webhooks)).listen();
148+
149+
// @ts-expect-error complains about { port } although it's included in returned AddressInfo interface
150+
const { port } = server.address();
151+
const response = await fetch(
152+
`http://localhost:${port}/api/github/webhooks`,
153+
{
154+
method: "POST",
155+
headers: {
156+
"X-GitHub-Delivery": "123e4567-e89b-12d3-a456-426655440000",
157+
"X-GitHub-Event": "push",
158+
"X-Hub-Signature-256": signatureSha256,
159+
},
160+
body: pushEventPayload,
161+
}
162+
);
163+
164+
await expect(response.text()).resolves.toBe(
165+
'{"error":"Unsupported \\"Content-Type\\" header value. Must be \\"application/json\\""}'
166+
);
167+
expect(response.status).toEqual(415);
168+
169+
server.close();
170+
});
171+
142172
test("Handles invalid JSON", async () => {
143173
const webhooks = new Webhooks({
144174
secret: "mySecret",

0 commit comments

Comments
 (0)