Skip to content

Commit 00292ff

Browse files
committed
Assert hmac signature is received in mock webhook
1 parent 8756f84 commit 00292ff

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lambdas/mock-webhook-lambda/src/__tests__/index.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ const mockLogger = jest.requireMock(
2222
"@nhs-notify-client-callbacks/logger",
2323
).instance;
2424

25-
const DEFAULT_HEADERS = { "x-api-key": TEST_API_KEY };
25+
const DEFAULT_HEADERS = {
26+
"x-api-key": TEST_API_KEY,
27+
"x-hmac-sha256-signature": "abc123",
28+
};
2629

2730
const createMockEvent = (
2831
body: string | null,
@@ -61,6 +64,18 @@ describe("Mock Webhook Lambda", () => {
6164
const body = JSON.parse(result.body);
6265
expect(body.message).toBe("Unauthorized");
6366
});
67+
68+
it("should return 400 when x-hmac-sha256-signature header is missing", async () => {
69+
const callback = { data: [] };
70+
const event = createMockEvent(JSON.stringify(callback), {
71+
"x-api-key": TEST_API_KEY,
72+
});
73+
const result = await handler(event);
74+
75+
expect(result.statusCode).toBe(400);
76+
const body = JSON.parse(result.body);
77+
expect(body.message).toBe("Missing x-hmac-sha256-signature");
78+
});
6479
});
6580

6681
describe("Happy Path", () => {

lambdas/mock-webhook-lambda/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ async function buildResponse(
5252
};
5353
}
5454

55+
if (!event.headers["x-hmac-sha256-signature"]) {
56+
logger.error("Bad request: missing x-hmac-sha256-signature header");
57+
return {
58+
statusCode: 400,
59+
body: JSON.stringify({ message: "Missing x-hmac-sha256-signature" }),
60+
};
61+
}
62+
5563
if (!event.body) {
5664
logger.error("No event body received");
5765

0 commit comments

Comments
 (0)