Skip to content

Commit 8e39f5f

Browse files
authored
Fix: Replace crypto (#429)
* Fix: Replace crypto * Remove comments * Upgrade zeit to vercel * Upgrade zeit to vercel
1 parent 4edbf49 commit 8e39f5f

File tree

3 files changed

+9
-17
lines changed

3 files changed

+9
-17
lines changed

modules/webhook/lambdas/webhook/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"dist": "yarn build && cd dist && zip ../webhook.zip index.js"
1212
},
1313
"devDependencies": {
14-
"@octokit/webhooks": "^7.21.0",
1514
"@types/express": "^4.17.9",
1615
"@types/jest": "^26.0.19",
1716
"@types/node": "^14.14.16",
@@ -26,6 +25,6 @@
2625
},
2726
"dependencies": {
2827
"@octokit/rest": "^18.0.12",
29-
"crypto": "^1.0.1"
28+
"@octokit/webhooks": "^7.21.0"
3029
}
31-
}
30+
}

modules/webhook/lambdas/webhook/src/webhook/handler.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
import { IncomingHttpHeaders } from 'http';
2-
import crypto from 'crypto';
2+
import { Webhooks } from '@octokit/webhooks';
33
import { sendActionRequest } from '../sqs';
44
import { EventPayloads } from '@octokit/webhooks';
55
import { KMS } from 'aws-sdk';
66
import { decrypt } from '../kms';
77

8-
function signRequestBody(key: string, body: any) {
9-
return `sha1=${crypto.createHmac('sha1', key).update(body, 'utf8').digest('hex')}`;
10-
}
11-
128
export const handle = async (headers: IncomingHttpHeaders, payload: any): Promise<number> => {
139
// ensure header keys lower case since github headers can contain capitals.
1410
for (const key in headers) {
1511
headers[key.toLowerCase()] = headers[key];
1612
}
1713

18-
const signature = headers['x-hub-signature'];
14+
const signature = headers['x-hub-signature'] as string;
1915
if (!signature) {
2016
console.error("Github event doesn't have signature. This webhook requires a secret to be configured.");
2117
return 500;
@@ -31,13 +27,15 @@ export const handle = async (headers: IncomingHttpHeaders, payload: any): Promis
3127
return 500;
3228
}
3329

34-
const calculatedSig = signRequestBody(secret, payload);
35-
if (signature !== calculatedSig) {
30+
const webhooks = new Webhooks({
31+
secret: secret,
32+
});
33+
if (!webhooks.verify(payload, signature)) {
3634
console.error('Unable to verify signature!');
3735
return 401;
3836
}
3937

40-
const githubEvent = headers['x-github-event'];
38+
const githubEvent = headers['x-github-event'] as string;
4139

4240
console.debug(`Received Github event: "${githubEvent}"`);
4341

modules/webhook/lambdas/webhook/yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,11 +1411,6 @@ cross-spawn@^7.0.0:
14111411
shebang-command "^2.0.0"
14121412
which "^2.0.1"
14131413

1414-
crypto@^1.0.1:
1415-
version "1.0.1"
1416-
resolved "https://registry.yarnpkg.com/crypto/-/crypto-1.0.1.tgz#2af1b7cad8175d24c8a1b0778255794a21803037"
1417-
integrity sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==
1418-
14191414
cssom@^0.4.4:
14201415
version "0.4.4"
14211416
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.4.4.tgz#5a66cf93d2d0b661d80bf6a44fb65f5c2e4e0a10"

0 commit comments

Comments
 (0)