Skip to content

Commit

Permalink
fix: case-insensitive headers (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
axel3rd authored Feb 25, 2021
1 parent 0d7f338 commit 96ced73
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 15 deletions.
17 changes: 9 additions & 8 deletions lambda-function.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
module.exports = lambdaFunction;

const lowercaseKeys = require("lowercase-keys");

async function lambdaFunction(probot, event, context) {
try {
// Ends function immediately after callback
context.callbackWaitsForEmptyEventLoop = false;

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

// this will be simpler once we ship `verifyAndParse()`
// see https://github.com/octokit/webhooks.js/issues/379
await probot.webhooks.verifyAndReceive({
id:
event.headers["X-GitHub-Delivery"] ||
event.headers["x-github-delivery"],
name: event.headers["X-GitHub-Event"] || event.headers["x-github-event"],
id: headersLowerCase["x-github-delivery"],
name: headersLowerCase["x-github-event"],
signature:
event.headers["X-Hub-Signature-256"] ||
event.headers["x-hub-signature-256"] ||
event.headers["X-Hub-Signature"] ||
event.headers["x-hub-signature"],
headersLowerCase["x-hub-signature-256"] ||
headersLowerCase["x-hub-signature"],
payload: JSON.parse(event.body),
});

Expand Down
22 changes: 18 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"repository": "https://github.com/probot/adapter-aws-lambda-serverless",
"dependencies": {
"@probot/get-private-key": "^1.1.0",
"probot": "^11.0.6"
"probot": "^11.0.6",
"lowercase-keys": "^2.0.0"
},
"devDependencies": {
"@types/jest": "^26.0.20",
Expand Down
79 changes: 77 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const nock = require("nock");
const path = require("path");

const { createLambdaFunction, Probot, ProbotOctokit } = require("../index");
const app = require("./fixtures/app");
Expand Down Expand Up @@ -28,7 +29,7 @@ describe("@probot/adapter-aws-lambda-serverless", () => {
"/repos/probot/adapter-adapter-aws-lambda-serverless/commits/headcommitsha123/comments",
(requestBody) => {
expect(requestBody).toStrictEqual({
body: "Hello from test/fixtures/app.js",
body: `Hello from test${path.sep}fixtures${path.sep}app.js`,
});

return true;
Expand Down Expand Up @@ -65,7 +66,7 @@ describe("@probot/adapter-aws-lambda-serverless", () => {
"/repos/probot/adapter-adapter-aws-lambda-serverless/commits/headcommitsha123/comments",
(requestBody) => {
expect(requestBody).toStrictEqual({
body: "Hello from test/fixtures/app.js",
body: `Hello from test${path.sep}fixtures${path.sep}app.js`,
});

return true;
Expand Down Expand Up @@ -93,4 +94,78 @@ describe("@probot/adapter-aws-lambda-serverless", () => {

expect(mock.activeMocks()).toStrictEqual([]);
});

test("GitHub request headers", async () => {
const fn = createLambdaFunction(app, { probot });

const mock = nock("https://api.github.com")
.post(
"/repos/probot/adapter-adapter-aws-lambda-serverless/commits/headcommitsha123/comments",
(requestBody) => {
expect(requestBody).toStrictEqual({
body: `Hello from test${path.sep}fixtures${path.sep}app.js`,
});

return true;
}
)
.reply(201, {});

const context = {};
const payload = JSON.stringify(require("./fixtures/push.json"));
const signature = probot.webhooks.sign(payload);
const event = {
headers: {
"X-Github-Delivery": "eventid123",
"X-Github-Event": "push",
"X-Hub-Signature": signature,
},
body: payload,
};

await fn(event, context);

expect(context).toStrictEqual({
callbackWaitsForEmptyEventLoop: false,
});

expect(mock.activeMocks()).toStrictEqual([]);
});

test("camelcase request headers (#62)", async () => {
const fn = createLambdaFunction(app, { probot });

const mock = nock("https://api.github.com")
.post(
"/repos/probot/adapter-adapter-aws-lambda-serverless/commits/headcommitsha123/comments",
(requestBody) => {
expect(requestBody).toStrictEqual({
body: `Hello from test${path.sep}fixtures${path.sep}app.js`,
});

return true;
}
)
.reply(201, {});

const context = {};
const payload = JSON.stringify(require("./fixtures/push.json"));
const signature = probot.webhooks.sign(payload);
const event = {
headers: {
"X-Github-Delivery": "EventId123",
"X-Github-Event": "push",
"X-Hub-Signature": signature,
},
body: payload,
};

await fn(event, context);

expect(context).toStrictEqual({
callbackWaitsForEmptyEventLoop: false,
});

expect(mock.activeMocks()).toStrictEqual([]);
});
});

0 comments on commit 96ced73

Please sign in to comment.