Skip to content
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
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ $ SECRET=the_webhook_secret
$ TMP_DATA_FILE=/tmp/smoke.data

$ echo -n "{\"action\":\"test\"}" > $TMP_DATA_FILE
$ SIGN=$(openssl dgst -sha1 -hmac $SECRET $TMP_DATA_FILE | cut -d" " -f2)
$ curl --request POST --header "X-Hub-Signature: sha1=$SIGN" --header "X-Github-Event: test" --header "X-GitHub-Delivery: fake" --data-binary "@$TMP_DATA_FILE" $LAMBDA_URL
$ SIGN=$(openssl dgst -sha256 -hmac $SECRET $TMP_DATA_FILE | cut -d" " -f2)
$ curl --request POST --header "X-Hub-Signature-256: sha256=$SIGN" --header "X-Github-Event: test" --header "X-GitHub-Delivery: fake" --data-binary "@$TMP_DATA_FILE" $LAMBDA_URL
{"ok":true} <-- Concent for Probot v10: {"message":"Received test.test"}
```

Expand All @@ -87,10 +87,15 @@ Add yours!

## Common errors

| Http Code | Message | Description |
| --------- | ---------------------------- | -------------------------------------------------------------------------------------------------- |
| 403 | Missing Authentication Token | Bad endpoint (this one is not binded on Lambda) |
| 500 | Internal server error | Incorrect headers value (`X-GitHub-Delivery`, `X-GitHub-Event`, `X-Hub-Signature`) or Probot error |
| Http Code | Message | Description |
| --------- | ---------------------------- | ------------------------------------------------------------------------------------------------------ |
| 400 | Missing required headers | A webhook header is missing; the JSON response lists the headers and links to CloudWatch documentation |
| 403 | Missing Authentication Token | Bad endpoint (this one is not binded on Lambda) |
| 500 | Internal server error | Incorrect header values or a Probot error |

Webhooks require `X-GitHub-Delivery`, `X-GitHub-Event`, and a SHA-256
signature. Use `X-Hub-Signature-256`; a SHA-256 signature in
`X-Hub-Signature` is also accepted as a fallback.

## Probot v11 migration key points

Expand Down
28 changes: 27 additions & 1 deletion lambda-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,33 @@ export default async function lambdaFunction(probot, event, context) {
}

// lowercase all headers to respect headers insensitivity (RFC 7230 $3.2 'Header Fields', see issue #62)
const headersLowerCase = lowercaseKeys(event.headers);
const headersLowerCase = lowercaseKeys(event.headers ?? {});
const missingHeaders = [];

if (!headersLowerCase["x-github-delivery"]) {
missingHeaders.push("X-GitHub-Delivery");
}
if (!headersLowerCase["x-github-event"]) {
missingHeaders.push("X-GitHub-Event");
}
if (
!headersLowerCase["x-hub-signature-256"] &&
!headersLowerCase["x-hub-signature"]
) {
missingHeaders.push("X-Hub-Signature-256 or X-Hub-Signature");
}

if (missingHeaders.length > 0) {
return {
statusCode: 400,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
error: `Missing required headers: ${missingHeaders.join(", ")}`,
documentation_url:
"https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html",
}),
};
}

// this will be simpler once we ship `verifyAndParse()`
// see https://github.com/octokit/webhooks.js/issues/379
Expand Down
90 changes: 90 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from "node:path";
import { readFileSync } from "node:fs";
import { createHmac } from "node:crypto";
import { jest } from "@jest/globals";
import fetchMock from "fetch-mock";

import { createLambdaFunction, Probot, ProbotOctokit } from "../index.js";
Expand Down Expand Up @@ -158,6 +159,95 @@ describe("@probot/adapter-aws-lambda-serverless", () => {
expect(wasCommentCreated()).toBe(true);
});

test.each([
[
{ "X-GitHub-Event": "push", "X-Hub-Signature-256": signature },
"X-GitHub-Delivery",
],
[
{ "X-GitHub-Delivery": "eventid123", "X-Hub-Signature": signature },
"X-GitHub-Event",
],
[
{ "X-GitHub-Delivery": "eventid123", "X-GitHub-Event": "push" },
"X-Hub-Signature-256 or X-Hub-Signature",
],
[
{
"X-GitHub-Delivery": "",
"X-GitHub-Event": "push",
"X-Hub-Signature-256": signature,
},
"X-GitHub-Delivery",
],
[
undefined,
"X-GitHub-Delivery, X-GitHub-Event, X-Hub-Signature-256 or X-Hub-Signature",
],
[
null,
"X-GitHub-Delivery, X-GitHub-Event, X-Hub-Signature-256 or X-Hub-Signature",
],
])(
"returns a helpful error for missing headers (%s)",
async (headers, missing) => {
const verifyAndReceive = jest.spyOn(probot.webhooks, "verifyAndReceive");
const fn = createLambdaFunction(app, { probot });

const response = await fn({ headers, body: payload }, {});

expect(response.statusCode).toBe(400);
expect(response.headers).toEqual({ "Content-Type": "application/json" });
expect(JSON.parse(response.body)).toEqual({
error: `Missing required headers: ${missing}`,
documentation_url:
"https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html",
});
expect(verifyAndReceive).not.toHaveBeenCalled();
},
);

test("accepts the SHA-256 signature header and prefers it over the legacy header", async () => {
const verifyAndReceive = jest.spyOn(probot.webhooks, "verifyAndReceive");
const fn = createLambdaFunction(() => {}, { probot });

const response = await fn(
{
headers: {
"X-GitHub-Delivery": "eventid123",
"X-GitHub-Event": "push",
"X-Hub-Signature-256": signature,
"X-Hub-Signature": "invalid",
},
body: payload,
},
{},
);

expect(response.statusCode).toBe(200);
expect(verifyAndReceive).toHaveBeenCalledWith(
expect.objectContaining({ signature }),
);
});

test("still rejects an invalid signature", async () => {
const fn = createLambdaFunction(() => {}, { probot });

await expect(
fn(
{
headers: {
"X-GitHub-Delivery": "eventid123",
"X-GitHub-Event": "push",
"X-Hub-Signature-256": "sha256=invalid",
},
body: payload,
},
{},
),
).rejects.toThrow();
});

test("waits for application loading and only loads once", async () => {
let loads = 0;
const fn = createLambdaFunction(
Expand Down