forked from rvagg/github-webhook-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-webhook-handler.js
More file actions
227 lines (188 loc) · 6.45 KB
/
github-webhook-handler.js
File metadata and controls
227 lines (188 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const EventEmitter = require('events')
const crypto = require('crypto')
const bl = require('bl')
function findHandler(url, arr) {
if (!Array.isArray(arr)) {
return arr
}
let ret = arr[0]
for (let i = 0; i < arr.length; i++) {
if (url === arr[i].path) {
ret = arr[i]
}
}
return ret
}
function checkType(options) {
if (typeof options !== 'object') {
throw new TypeError('must provide an options object')
}
if (typeof options.path !== 'string') {
throw new TypeError('must provide a \'path\' option')
}
if (typeof options.secret !== 'string') {
throw new TypeError('must provide a \'secret\' option')
}
}
function create(initOptions) {
let options
// validate type of options
if (Array.isArray(initOptions)) {
for (let i = 0; i < initOptions.length; i++) {
checkType(initOptions[i])
}
} else {
checkType(initOptions)
}
// make it an EventEmitter
Object.setPrototypeOf(handler, EventEmitter.prototype)
EventEmitter.call(handler)
handler.sign = sign
handler.verify = verify
return handler
function sign(data) {
return `sha1=${crypto.createHmac('sha1', options.secret).update(data).digest('hex')}`
}
function verify(signature, data) {
const sig = Buffer.from(signature)
const signed = Buffer.from(sign(data))
if (sig.length !== signed.length) {
return false
}
return crypto.timingSafeEqual(sig, signed)
}
function verifyGitee(signature, data, json) {
if (json.sign) {
const sig = Buffer.from(signature)
const signed = Buffer.from(crypto.createHmac('sha256', options.secret).update(`${json.timestamp}\n${options.secret}`).digest('base64'))
if (sig.length !== signed.length) {
return false
}
return crypto.timingSafeEqual(sig, signed)
} else {
return signature === options.secret
}
}
function verifyGitlab(signature) {
return signature === options.secret
}
function verifyGiteaGogs(signature, data, json) {
const expected = crypto.createHmac('sha256', options.secret).update(JSON.stringify(json, null, 2)).digest('hex')
return Buffer.from(expected).equals(Buffer.from(signature))
}
function verifyCodeup(signature) {
return signature === options.secret
}
function handler(req, res, callback) {
let events
options = findHandler(req.url, initOptions)
if (typeof options.events === 'string' && options.events !== '*') {
events = [options.events]
} else if (Array.isArray(options.events) && options.events.indexOf('*') === -1) {
events = options.events
}
if (req.url !== options.path || req.method !== 'POST') {
return callback()
}
function hasError(msg) {
res.writeHead(400, { 'content-type': 'application/json' })
res.end(JSON.stringify({ error: msg }))
const err = new Error(msg)
handler.emit('error', err, req)
callback(err)
}
const ua = req.headers['user-agent']
const keyMap = {
sig: 'x-hub-signature',
event: 'x-github-event',
id: 'x-github-delivery',
verify
}
// gitee
if (ua === 'git-oschina-hook') {
keyMap.sig = 'x-gitee-token'
keyMap.event = 'x-gitee-event'
keyMap.id = 'x-gitee-timestamp'
keyMap.verify = verifyGitee
} else if (req.headers['x-gitlab-token']) {
// gitlab
keyMap.sig = 'x-gitlab-token'
keyMap.event = 'x-gitlab-event'
keyMap.id = 'x-gitlab-token'
keyMap.verify = verifyGitlab
} else if (req.headers['x-gitea-signature']) {
// gitea
keyMap.sig = 'x-gitea-signature'
keyMap.event = 'x-gitea-event'
keyMap.id = 'x-gitea-delivery'
keyMap.verify = verifyGiteaGogs
} else if (req.headers['x-gogs-signature']) {
// gogs
keyMap.sig = 'x-gogs-signature'
keyMap.event = 'x-gogs-event'
keyMap.id = 'x-gogs-delivery'
keyMap.verify = verifyGiteaGogs
} else if (req.headers['x-codeup-token']) {
keyMap.sig = 'x-codeup-token'
keyMap.event = 'x-codeup-event'
keyMap.id = 'x-codeup-delivery'
keyMap.verify = verifyCodeup
}
const sig = req.headers[keyMap.sig]
const event = req.headers[keyMap.event]
const id = req.headers[keyMap.id]
if (!sig) {
return hasError('No X-Hub-Signature found on request')
}
if (!event) {
return hasError('No X-Github-Event found on request')
}
if (!id) {
return hasError('No X-Github-Delivery found on request')
}
if (events && events.indexOf(event) === -1) {
return hasError('X-Github-Event is not acceptable')
}
req.pipe(bl((err, data) => {
if (err) {
return hasError(err.message)
}
let obj
if (!keyMap.verify(sig, data, obj)) {
return hasError(`${keyMap.sig} does not match blob signature`)
}
try {
obj = JSON.parse(data.toString())
} catch (e) {
return hasError(e)
}
res.writeHead(200, { 'content-type': 'application/json' })
res.end('{"ok":true}')
const emitData = {
event: event,
id: id,
payload: obj,
protocol: req.protocol,
host: req.headers.host,
url: req.url,
path: options.path
}
// set common event
function commonEvent(event) {
if (event === 'Push Hook') {
return 'push'
}
if (event === 'Issue Hook') {
return 'issues'
}
if (event === 'Merge Request Hook') {
return 'merge'
}
return event
}
handler.emit(commonEvent(event), emitData)
handler.emit('*', emitData)
}))
}
}
module.exports = create