Skip to content

Commit 3c11a81

Browse files
author
Adnan Rahic
committed
refactor: multiple client secrets
1 parent 082f3ba commit 3c11a81

3 files changed

Lines changed: 69 additions & 9 deletions

File tree

config/examples/vercel-input-es-output.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ input:
55
module: input-vercel
66
port: 8400
77
useIndexFromUrlPath: true
8-
clientSecret: <CLIENT_SECRET>
98
# workers: 4
9+
clientSecrets:
10+
- <CLIENT_SECRET>
11+
- <CLIENT_SECRET>
1012

1113

1214
outputFilter:

lib/plugins/input/vercel.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,27 @@ class Vercel {
106106
}
107107

108108
verifySignature (req, body) {
109-
const signature = crypto
110-
.createHmac('sha1', this.config.clientSecret)
111-
.update(body)
112-
.digest('hex')
113-
114-
if (this.config.debug) {
115-
consoleLogger.log("Vercel signature didn't match")
109+
if (!Array.isArray(this.config.clientSecrets)) {
110+
if (this.config.debug) {
111+
consoleLogger.log('clientSecrets config value is not an array. Please set it to an array.')
112+
}
113+
return
116114
}
117115

118-
return signature === req.headers['x-zeit-signature']
116+
const verified = this.config.clientSecrets.some(clientSecret => {
117+
const signature = crypto
118+
.createHmac('sha1', clientSecret)
119+
.update(body)
120+
.digest('hex')
121+
122+
if (this.config.debug) {
123+
consoleLogger.log(`Vercel signature didn't match for Vercel Client Secret: ${clientSecret}`)
124+
}
125+
126+
return signature === req.headers['x-zeit-signature']
127+
})
128+
129+
return verified
119130
}
120131

121132
HttpHandler (req, res) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* global describe, it */
2+
const assert = require('assert')
3+
const crypto = require('crypto')
4+
const sampleClientSecret = 'idmnMEd7Yx4QmgzZpZ4axXoe'
5+
const sampleBody = {
6+
id: 1,
7+
message: '1'
8+
}
9+
const sampleBodyBuf = Buffer.from(JSON.stringify(sampleBody))
10+
const sampleSignature = crypto
11+
.createHmac('sha1', sampleClientSecret)
12+
.update(sampleBodyBuf)
13+
.digest('hex')
14+
15+
const sampleReq = {
16+
headers: {
17+
'x-zeit-signature': sampleSignature
18+
}
19+
}
20+
const configWithArrayWithTwoClientSecrets = {
21+
clientSecrets: [sampleClientSecret, sampleClientSecret]
22+
}
23+
const configWithArrayWithOneClientSecret = {
24+
clientSecrets: [sampleClientSecret]
25+
}
26+
const EventEmitter = require('events')
27+
const evem = new EventEmitter()
28+
29+
/**
30+
* Init Vercel Class
31+
*/
32+
const Vercel = require('../../lib/plugins/input/vercel')
33+
const vercelWithArrayWithTwoSecrets = new Vercel(configWithArrayWithTwoClientSecrets, evem)
34+
const vercelWithArrayWithOneSecret = new Vercel(configWithArrayWithOneClientSecret, evem)
35+
36+
describe('verifySignature should', function () {
37+
it('return true for an array with 2 secrets', function (done) {
38+
const signature = vercelWithArrayWithTwoSecrets.verifySignature(sampleReq, sampleBodyBuf)
39+
assert.strictEqual(signature, true)
40+
done()
41+
})
42+
it('return true for an array with 1 secret', function (done) {
43+
const signature = vercelWithArrayWithOneSecret.verifySignature(sampleReq, sampleBodyBuf)
44+
assert.strictEqual(signature, true)
45+
done()
46+
})
47+
})

0 commit comments

Comments
 (0)