Skip to content

Commit 1257dc7

Browse files
committed
breaking: signature option no longer handled in .receive() method
BREAKING CHANGE: Before, this would throw an error ```js webhooks.receive({id, name, data, signature: invalid}) ``` Now, the `signature` option is ignored as it’s assumed that the request has been already validated. This makes testing much easier, see probot/probot#335 (comment). If you use the EventHandler directly in a framework like hapi, make sure to verify the request before using the receive method using `eventHandler.verify(data, signature)`
1 parent 6c6e9ea commit 1257dc7

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

event-handler/receive.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
module.exports = receiverHandle
44

5-
const verify = require('../verify')
65
const wrapErrorHandler = require('./wrap-error-handler')
76

87
// main handler function
@@ -23,23 +22,6 @@ function receiverHandle (state, options) {
2322
throw new Error('Event data not passed')
2423
}
2524

26-
if (!options.signature) {
27-
throw new Error('Event signature not passed')
28-
}
29-
30-
const matchesSignature = verify(
31-
state.secret,
32-
options.data,
33-
options.signature
34-
)
35-
36-
if (!matchesSignature) {
37-
const error = new Error('Signature does not match')
38-
error.status = 400
39-
40-
return Promise.reject(error)
41-
}
42-
4325
let hooks = [].concat(
4426
state.hooks[`${options.name}.${options.data.action}`],
4527
state.hooks[options.name],

index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ module.exports = createWebhooksApi
33
const createEventHandler = require('./event-handler')
44
const middleware = require('./middleware/middleware')
55

6-
const sign = require('./sign')
7-
const verify = require('./verify')
8-
96
function createWebhooksApi (options) {
107
if (!options) {
118
options = {}
@@ -19,8 +16,8 @@ function createWebhooksApi (options) {
1916
const webhooksMiddleware = middleware.bind(null, state)
2017

2118
return {
22-
sign: sign.bind(null, options.secret),
23-
verify: verify.bind(null, options.secret),
19+
sign: state.eventHandler.sign,
20+
verify: state.eventHandler.verify,
2421
on: state.eventHandler.on,
2522
removeListener: state.eventHandler.removeListener,
2623
receive: state.eventHandler.receive,

middleware/middleware.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ function middleware (state, request, response, next) {
4949
request.on('end', () => {
5050
const payload = Buffer.concat(dataChunks).toString()
5151

52+
const matchesSignature = state.eventHandler.verify(
53+
payload,
54+
signature
55+
)
56+
57+
if (!matchesSignature) {
58+
response.statusCode = 400
59+
response.end('x-hub-signature does not match event payload and secret')
60+
return
61+
}
62+
5263
state.eventHandler.receive({
5364
id: id,
5465
name: eventName,

0 commit comments

Comments
 (0)