forked from gnarf/node-notifier-server
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgithub-notifier.js
140 lines (117 loc) · 3.71 KB
/
github-notifier.js
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
const crypto = require('crypto');
const util = require('util');
const EventEmitter2 = require('eventemitter2').EventEmitter2;
function Notifier (config = {}) {
EventEmitter2.call(this, {
wildcard: true,
delimiter: '/'
});
this.webhookSecret = config.webhookSecret || '';
// Pre-bind to ease usage as a callback
this.handler = this.handler.bind(this);
}
util.inherits(Notifier, EventEmitter2);
/**
* @param {http.IncomingMessage} request <https://nodejs.org/docs/latest-v12.x/api/http.html#http_class_http_incomingmessage>
* @param {http.ServerResponse} response <https://nodejs.org/docs/latest-v12.x/api/http.html#http_class_http_serverresponse
*/
Notifier.prototype.handler = function (request, response) {
const notifier = this;
if (request.headers['content-type'] === 'application/x-www-form-urlencoded') {
notifier.emit('error', 'Unsupported content type');
response.writeHead(415);
response.end();
request.destroy();
return;
}
request.setEncoding('utf8');
let body = '';
request.on('data', function onData (chunk) {
body += chunk;
});
request.on('end', function onEnd () {
// Accept the request and close the connection
// SECURITY: We decide on and close the response regardless of,
// and prior to, any secret-based signature validation, so as to not
// expose details about the outcome or timing of it to external clients.
response.writeHead(202);
response.end();
notifier.process(request, body);
});
};
Notifier.prototype.process = function (req, payload) {
const secret = this.webhookSecret;
if (secret) {
const hmac = crypto.createHmac('sha256', secret);
hmac.update(payload);
const expected = Buffer.from('sha256=' + hmac.digest('hex'));
const actual = Buffer.from(req.headers['x-hub-signature-256'] || '');
if (actual.length !== expected.length) {
// Invalid signature, discard misformatted signature
// that can't be compared with timingSafeEqual()
return;
}
if (!crypto.timingSafeEqual(actual, expected)) {
// Invalid signature, discard unauthorized event
return;
}
}
const eventType = req.headers['x-github-event'];
// Ignore ping events that are sent when a new webhook is created
if (eventType === 'ping') {
return;
}
// Delay parsing until after signature validation to reduce impact of large payloads
let data;
try {
data = JSON.parse(payload);
} catch (e) {
// Invalid data, stop processing
this.emit('error', e);
return;
}
const processor = this.processors[eventType] || this.processors._default;
const processed = processor(data);
const event = {
// Handle common properties
owner: data.repository.owner.login,
repo: data.repository.name,
type: eventType,
...processed.event
};
// Emit event rooted on the owner/repo
let eventName = event.owner + '/' + event.repo + '/' + event.type;
if (processed.postfix) {
eventName += '/' + processed.postfix;
}
this.emit(eventName, event);
};
Notifier.prototype.processors = {};
Notifier.prototype.processors._default = function (data) {
return {
event: {}
};
};
Notifier.prototype.processors.push = function (data) {
const event = {
commit: data.after
};
let postfix = null;
if (/^refs\/(heads|tags)\//.test(data.ref)) {
postfix = data.ref.slice(5);
const refParts = data.ref.split('/');
const refType = refParts[1];
// Preserve slashes in namespace-like branch names
const refDest = refParts.slice(2).join('/');
if (refType === 'heads') {
event.branch = refDest;
} else if (refType === 'tags') {
event.tag = refDest;
}
}
return {
postfix: postfix,
event: event
};
};
exports.Notifier = Notifier;