Skip to content

Commit 0504ad8

Browse files
authored
fix: handles verify error (#917)
handles verify error
1 parent c9b66b1 commit 0504ad8

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
- master
66
- next
77
- beta
8-
- "v*.x" # maintenance release branches, e.g. v1.x
8+
- "*.x" # maintenance release branches, e.g. 1.x
99
jobs:
1010
release:
1111
name: release

src/verify-and-receive.ts

Lines changed: 1 addition & 1 deletion
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

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

501501
server.close();
502502
});
503+
504+
test("Handles invalid signature", async () => {
505+
expect.assertions(3);
506+
507+
const webhooks = new Webhooks({
508+
secret: "mySecret",
509+
});
510+
511+
webhooks.onError((error) => {
512+
expect(error.message).toContain(
513+
"signature does not match event payload and secret"
514+
);
515+
});
516+
517+
const log = {
518+
debug: jest.fn(),
519+
info: jest.fn(),
520+
warn: jest.fn(),
521+
error: jest.fn(),
522+
};
523+
const middleware = createNodeMiddleware(webhooks, { log });
524+
const server = createServer(middleware).listen();
525+
526+
// @ts-expect-error complains about { port } although it's included in returned AddressInfo interface
527+
const { port } = server.address();
528+
529+
const response = await fetch(
530+
`http://localhost:${port}/api/github/webhooks`,
531+
{
532+
method: "POST",
533+
headers: {
534+
"Content-Type": "application/json",
535+
"X-GitHub-Delivery": "1",
536+
"X-GitHub-Event": "push",
537+
"X-Hub-Signature-256": "",
538+
},
539+
body: pushEventPayload,
540+
}
541+
);
542+
543+
expect(response.status).toEqual(400);
544+
await expect(response.text()).resolves.toContain(
545+
"Error: [@octokit/webhooks] signature does not match event payload and secret"
546+
);
547+
548+
server.close();
549+
});
503550
});

0 commit comments

Comments
 (0)