Skip to content

Commit ad2d036

Browse files
gr2mnickfloyd
authored andcommitted
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`
1 parent 9cfc37b commit ad2d036

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/verify-and-receive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function verifyAndReceive(
1414
state.secret,
1515
event.payload,
1616
event.signature
17-
);
17+
).catch(() => false);
1818

1919
if (!matchesSignature) {
2020
const error = new Error(

test/integration/node-middleware.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,4 +605,51 @@ describe("createNodeMiddleware(webhooks)", () => {
605605

606606
server.close();
607607
});
608+
609+
test("Handles invalid signature", async () => {
610+
expect.assertions(3);
611+
612+
const webhooks = new Webhooks({
613+
secret: "mySecret",
614+
});
615+
616+
webhooks.onError((error) => {
617+
expect(error.message).toContain(
618+
"signature does not match event payload and secret",
619+
);
620+
});
621+
622+
const log = {
623+
debug: jest.fn(),
624+
info: jest.fn(),
625+
warn: jest.fn(),
626+
error: jest.fn(),
627+
};
628+
const middleware = createNodeMiddleware(webhooks, { log });
629+
const server = createServer(middleware).listen();
630+
631+
// @ts-expect-error complains about { port } although it's included in returned AddressInfo interface
632+
const { port } = server.address();
633+
634+
const response = await fetch(
635+
`http://localhost:${port}/api/github/webhooks`,
636+
{
637+
method: "POST",
638+
headers: {
639+
"Content-Type": "application/json",
640+
"X-GitHub-Delivery": "1",
641+
"X-GitHub-Event": "push",
642+
"X-Hub-Signature-256": "",
643+
},
644+
body: pushEventPayload,
645+
},
646+
);
647+
648+
expect(response.status).toEqual(400);
649+
await expect(response.text()).resolves.toBe(
650+
'{"error":"Error: [@octokit/webhooks] signature does not match event payload and secret"}',
651+
);
652+
653+
server.close();
654+
});
608655
});

0 commit comments

Comments
 (0)