Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle error thrown by verify method #914

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/verify-and-receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function verifyAndReceive(
state.secret,
event.payload,
event.signature,
);
).catch(() => false);

if (!matchesSignature) {
const error = new Error(
Expand Down
47 changes: 47 additions & 0 deletions test/integration/node-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,4 +604,51 @@ describe("createNodeMiddleware(webhooks)", () => {

server.close();
});

test("Handles invalid signature", async () => {
expect.assertions(3);

const webhooks = new Webhooks({
secret: "mySecret",
});

webhooks.onError((error) => {
expect(error.message).toContain(
"signature does not match event payload and secret",
);
});

const log = {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};
const middleware = createNodeMiddleware(webhooks, { log });
const server = createServer(middleware).listen();

// @ts-expect-error complains about { port } although it's included in returned AddressInfo interface
const { port } = server.address();

const response = await fetch(
`http://localhost:${port}/api/github/webhooks`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"X-GitHub-Delivery": "1",
"X-GitHub-Event": "push",
"X-Hub-Signature-256": "",
},
body: pushEventPayload,
},
);

expect(response.status).toEqual(400);
await expect(response.text()).resolves.toBe(
'{"error":"Error: [@octokit/webhooks] signature does not match event payload and secret"}',
);

server.close();
});
});