From 5f575e4c5d617825a0f1d377d90e6e3c10c52452 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 24 Nov 2022 15:07:58 +0100 Subject: [PATCH 1/2] Move the `validateGitHubWebHook` function into its own file That way, things are organized better, and easier to test locally, too. This commit is best viewed with `--color-moved`. Signed-off-by: Johannes Schindelin --- LeaveMeAloneGitHubApp/index.js | 28 +------------------ .../validate-github-webhook.js | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 27 deletions(-) create mode 100644 LeaveMeAloneGitHubApp/validate-github-webhook.js diff --git a/LeaveMeAloneGitHubApp/index.js b/LeaveMeAloneGitHubApp/index.js index b073d948..e7357840 100644 --- a/LeaveMeAloneGitHubApp/index.js +++ b/LeaveMeAloneGitHubApp/index.js @@ -1,30 +1,4 @@ -const crypto = require('crypto') - -/** Validates the signature added by GitHub */ -const validateGitHubWebHook = (context) => { - const secret = process.env['GITHUB_WEBHOOK_SECRET'] - if (!secret) { - throw new Error('Webhook secret not configured') - } - if (context.req.method !== 'POST') { - throw new Error(`Unexpected method: ${context.req.method}`) - } - if (context.req.headers['content-type'] !== 'application/json') { - throw new Error(`Unexpected content type: ${context.req.headers['content-type']}`) - } - const signature = context.req.headers['x-hub-signature-256'] - if (!signature) { - throw new Error('Missing X-Hub-Signature') - } - const sha256 = signature.match(/^sha256=(.*)/) - if (!sha256) { - throw new Error(`Unexpected X-Hub-Signature format: ${signature}`) - } - const computed = crypto.createHmac('sha256', secret).update(context.req.rawBody).digest('hex') - if (sha256[1] !== computed) { - throw new Error('Incorrect X-Hub-Signature') - } -} +const validateGitHubWebHook = require('./validate-github-webhook') /** Sends a JWT-authenticated GitHub API request */ const gitHubApiRequestAsApp = require('./github-api-request-as-app') diff --git a/LeaveMeAloneGitHubApp/validate-github-webhook.js b/LeaveMeAloneGitHubApp/validate-github-webhook.js new file mode 100644 index 00000000..ada3cc56 --- /dev/null +++ b/LeaveMeAloneGitHubApp/validate-github-webhook.js @@ -0,0 +1,28 @@ +const crypto = require('crypto') + +/** Validates the signature added by GitHub */ +module.exports = (context) => { + const secret = process.env['GITHUB_WEBHOOK_SECRET'] + if (!secret) { + throw new Error('Webhook secret not configured') + } + if (context.req.method !== 'POST') { + throw new Error(`Unexpected method: ${context.req.method}`) + } + if (context.req.headers['content-type'] !== 'application/json') { + throw new Error(`Unexpected content type: ${context.req.headers['content-type']}`) + } + const signature = context.req.headers['x-hub-signature-256'] + if (!signature) { + throw new Error('Missing X-Hub-Signature') + } + const sha256 = signature.match(/^sha256=(.*)/) + if (!sha256) { + throw new Error(`Unexpected X-Hub-Signature format: ${signature}`) + } + const computed = crypto.createHmac('sha256', secret).update(context.req.rawBody).digest('hex') + if (sha256[1] !== computed) { + throw new Error('Incorrect X-Hub-Signature') + } +} + From d319ff1dbe43b07bbaba434d6d0a06deaafa986f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 24 Nov 2022 22:18:03 +0100 Subject: [PATCH 2/2] Introduce short-and-sweet functions to send an HTTP response By calling `return ok(...)` or `return withStatus(...)`, it is now really easy and concise to exit from the Azure Function by sending an HTTP response. Signed-off-by: Johannes Schindelin --- LeaveMeAloneGitHubApp/index.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/LeaveMeAloneGitHubApp/index.js b/LeaveMeAloneGitHubApp/index.js index e7357840..03eef184 100644 --- a/LeaveMeAloneGitHubApp/index.js +++ b/LeaveMeAloneGitHubApp/index.js @@ -4,15 +4,23 @@ const validateGitHubWebHook = require('./validate-github-webhook') const gitHubApiRequestAsApp = require('./github-api-request-as-app') module.exports = async function (context, req) { + const withStatus = (status, headers, body) => { + context.res = { + status, + headers, + body + } + } + + const ok = (body) => { + withStatus(undefined, undefined, body) + } + try { validateGitHubWebHook(context) } catch (e) { context.log(e) - context.res = { - status: 403, - body: `Go away, you are not a valid GitHub webhook: ${e}`, - } - return + return withStatus(403, undefined, `Go away, you are not a valid GitHub webhook: ${e}`) } if (req.headers['x-github-event'] === 'installation' && req.body.action === 'created') { @@ -26,10 +34,7 @@ module.exports = async function (context, req) { } } catch (e) { context.log(e) - context.res = { - status: 500, - body: `Error:\n${e}`, - } + return withStatus(500, undefined, `Error:\n${e}`) } return } @@ -39,8 +44,5 @@ module.exports = async function (context, req) { context.log("Got body") context.log(req.body) - context.res = { - // status: 200, /* Defaults to 200 */ - body: `Received event ${req.headers["x-github-event"]}` - } + ok(`Received event ${req.headers["x-github-event"]}`) }