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

Headers insensitive #66

Merged
merged 3 commits into from
Feb 25, 2021
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: 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for adding the comment & referencing the issue 👍🏼

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([]);
});
});