Skip to content

Commit 2618438

Browse files
nickfloydgr2m
andauthored
fix: Handle verify error (#916)
* update publish pattern to make sure maintenance branches are picked up * fix: Handle verify error (#915) * fix: handle error thrown by `verify` method (#914) * test: handle error thrown by `verify` method * fix: handle error thrown by `verify` method * test: remove `.only` * lint fix --------- Co-authored-by: Gregor Martynus <[email protected]> --------- Co-authored-by: Gregor Martynus <[email protected]>
1 parent c5e041d commit 2618438

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name: Release
55
- main
66
- next
77
- beta
8-
- v*.x
8+
- "+([0-9]).x"
99
jobs:
1010
release:
1111
name: release

src/verify-and-receive.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export async function verifyAndReceive(
2020
? toNormalizedJsonString(event.payload)
2121
: event.payload,
2222
event.signature
23-
);
23+
).catch(() => false);
2424

2525
if (!matchesSignature) {
2626
const error = new Error(

test/integration/node-middleware.test.ts

+47
Original file line numberDiff line numberDiff line change
@@ -571,4 +571,51 @@ describe("createNodeMiddleware(webhooks)", () => {
571571

572572
server.close();
573573
});
574+
575+
test("Handles invalid signature", async () => {
576+
expect.assertions(3);
577+
578+
const webhooks = new Webhooks({
579+
secret: "mySecret",
580+
});
581+
582+
webhooks.onError((error) => {
583+
expect(error.message).toContain(
584+
"signature does not match event payload and secret"
585+
);
586+
});
587+
588+
const log = {
589+
debug: jest.fn(),
590+
info: jest.fn(),
591+
warn: jest.fn(),
592+
error: jest.fn(),
593+
};
594+
const middleware = createNodeMiddleware(webhooks, { log });
595+
const server = createServer(middleware).listen();
596+
597+
// @ts-expect-error complains about { port } although it's included in returned AddressInfo interface
598+
const { port } = server.address();
599+
600+
const response = await fetch(
601+
`http://localhost:${port}/api/github/webhooks`,
602+
{
603+
method: "POST",
604+
headers: {
605+
"Content-Type": "application/json",
606+
"X-GitHub-Delivery": "1",
607+
"X-GitHub-Event": "push",
608+
"X-Hub-Signature-256": "",
609+
},
610+
body: pushEventPayload,
611+
}
612+
);
613+
614+
expect(response.status).toEqual(400);
615+
await expect(response.text()).resolves.toBe(
616+
'{"error":"Error: [@octokit/webhooks] signature does not match event payload and secret"}'
617+
);
618+
619+
server.close();
620+
});
574621
});

0 commit comments

Comments
 (0)