From 0bddaf0704df23febc4521b8ef07466bdb3630cd Mon Sep 17 00:00:00 2001 From: robandpdx <95243761+robandpdx@users.noreply.github.com> Date: Mon, 30 Nov 2026 20:02:08 +0000 Subject: [PATCH] fix: return helpful errors for missing webhook headers Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- README.md | 17 +++++---- lambda-function.js | 28 ++++++++++++++- test/index.test.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index fcdfc11..a85748d 100644 --- a/README.md +++ b/README.md @@ -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"} ``` @@ -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 diff --git a/lambda-function.js b/lambda-function.js index ee4743e..4791b95 100644 --- a/lambda-function.js +++ b/lambda-function.js @@ -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 diff --git a/test/index.test.js b/test/index.test.js index 863600b..6d61e4a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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"; @@ -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(