Skip to content

Commit 7042745

Browse files
committed
chore: extracted GitHub secret logic.
These changes extracts the logic related to verifying the secret sent from github.com when webhooks are invoked. Mainly done to enable turning off the secret verification while running tests.
1 parent 73c6a99 commit 7042745

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

‎lib/github-events.js‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
const crypto = require('crypto')
21
const debug = require('debug')('github-events')
32

4-
const secret = process.env.GITHUB_WEBHOOK_SECRET || 'hush-hush'
5-
const isTesting = process.env.NODE_ENV === 'test' || process.env.npm_lifecycle_event === 'test'
6-
7-
const sign = (secret, data) => {
8-
const buffer = new Buffer(data, 'utf8')
9-
return 'sha1=' + crypto.createHmac('sha1', secret).update(buffer).digest('hex')
10-
}
3+
const githubSecret = require('./github-secret')
114

125
module.exports = (app) => {
136
app.post('/hooks/github', (req, res) => {
@@ -17,17 +10,15 @@ module.exports = (app) => {
1710
return res.end()
1811
}
1912

20-
const signature = req.headers['x-hub-signature']
21-
const data = req.body
22-
data.action = data.action ? event + '.' + data.action : event
23-
24-
// don't verify webhook secret when if we're running tests
25-
if (!isTesting && (!signature || signature !== sign(secret, req.raw))) {
13+
if (!githubSecret.isValid(req)) {
2614
res.writeHead(401, 'Invalid Signature')
2715
req.log.error('Invalid GitHub event signature, returning 401')
2816
return res.end()
2917
}
3018

19+
const data = req.body
20+
data.action = data.action ? event + '.' + data.action : event
21+
3122
res.end()
3223

3324
app.emitGhEvent(data, req.log)

‎lib/github-secret.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const crypto = require('crypto')
2+
3+
const secret = process.env.GITHUB_WEBHOOK_SECRET || 'hush-hush'
4+
5+
function sign (data) {
6+
const buffer = new Buffer(data, 'utf8')
7+
return 'sha1=' + crypto.createHmac('sha1', secret).update(buffer).digest('hex')
8+
}
9+
10+
exports.isValid = function isValid (req) {
11+
const signature = req.headers['x-hub-signature']
12+
return signature && signature === sign(req.raw)
13+
}

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"eventsource": "^0.2.1",
2929
"nock": "^8.0.0",
3030
"nodemon": "^1.9.1",
31+
"proxyquire": "^1.7.10",
3132
"request": "^2.72.0",
3233
"standard": "^6.0.7",
3334
"supertest": "^1.2.0",

‎test/integration/node-labels-webhook.test.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@ const path = require('path')
66
const url = require('url')
77
const nock = require('nock')
88
const supertest = require('supertest')
9+
const proxyquire = require('proxyquire')
910

10-
const app = require('../../app')
11+
const testStubs = {
12+
'./github-secret': {
13+
isValid: () => true,
14+
15+
// necessary to make makes proxyquire return this stub
16+
// whenever *any* module tries to require('./github-secret')
17+
'@global': true
18+
}
19+
}
1120

1221
tap.test('Sends POST request to https://api.github.com/repos/nodejs/node/issues/<PR-NUMBER>/labels', (t) => {
22+
const app = proxyquire('../../app', testStubs)
23+
1324
const expectedLabels = ['timers']
1425
const webhookPayload = readFixture('pull-request-opened.json')
1526

0 commit comments

Comments
 (0)