Skip to content
This repository was archived by the owner on Aug 26, 2020. It is now read-only.

Commit 71393ad

Browse files
committed
Verify signature and emit verified-repo-events on valid requests
1 parent 3de4b29 commit 71393ad

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,18 @@ robot.emit "github-repo-event", eventBody
3232

3333
For details on these fields, see the [Github Webhook documentation](https://developer.github.com/webhooks/).
3434

35-
**SECURITY WARNING**: This script does not currently validate the Github Secret to verify that the webhook came from Github. So, if someone knows the URL to your Hubot, they can spoof webhooks and issue your Hubot commands. So, for now be careful about exposing commands like `destroy company`, etc. I plan to validate these webhooks soon. In the meantime, patches are welcome. :)
35+
### Securing Your Webhooks
36+
To ensure non-github sources cannot send messages to your hubot, set an
37+
environment variable named `GITHUB_WEBHOOK_SECRET` to your [Github hooks
38+
secret](https://developer.github.com/v3/repos/hooks/#create-a-hook).
3639

40+
This will emit an additional event `github-verified-repo-event` when a webhook
41+
requests signature was generated with your shared secret.
42+
43+
It will also raise an exceptin in the event the secret is not valid, but this
44+
should not block listeners to `github-repo-event`.
45+
46+
### Consuming the event
3747
You can consume it like so from one of your scripts:
3848
```coffeescript
3949
@robot.on "github-repo-event", (repo_event) =>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"dependencies": {
3333
"querystring": "^0.2.0",
34-
"url": "^0.10.3"
34+
"url": "^0.10.3",
35+
"verify-github-webhook": "^1.0.1"
3536
}
3637
}

src/hubot-github-webhook-listener.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ module.exports = (robot) ->
5252
robot.logger.info("Github post received: ", req)
5353
eventBody =
5454
eventType : req.headers["x-github-event"]
55-
signature : req.headers["X-Hub-Signature"]
56-
deliveryId : req.headers["X-Github-Delivery"]
55+
signature : req.headers["x-hub-signature"]
56+
deliveryId : req.headers["x-github-delivery"]
5757
payload : req.body
5858
query : querystring.parse(url.parse(req.url).query)
5959

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
verifyGithubWebhook = require('verify-github-webhook').default
2+
3+
verifySignature = (signature, payload, secret) ->
4+
payload = new Buffer(JSON.stringify(payload))
5+
return true unless secret?
6+
verifyGithubWebhook(signature, payload, secret)
7+
8+
module.exports = (robot) ->
9+
secret = process.env['GITHUB_WEBHOOK_SECRET']
10+
robot.on 'github-repo-event', (repoEvent) =>
11+
unless verifySignature(repoEvent.signature, repoEvent.payload, secret)
12+
throw new Error('Invalid Webhook Signature')
13+
14+
robot.logger.info('received valid webhook, notifying consumers')
15+
robot.emit "github-verified-repo-event", repoEvent

0 commit comments

Comments
 (0)