Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub webhooks: check signature #2493

Merged
merged 7 commits into from
Apr 17, 2017

Conversation

francois2metz
Copy link
Contributor

@francois2metz francois2metz commented Mar 5, 2017

Fix #1661

I added the check of the signature to github webhooks. If no secret is set in the config file, no check is performed (make it backward compatible).

Required for all PRs:

  • CHANGELOG.md updated (we recommend not updating this until the PR has been approved by a maintainer)
  • Sign CLA (if not already signed)

@francois2metz francois2metz force-pushed the plugin/gh_mac_validation branch from dcf29bb to 68d6d6d Compare March 5, 2017 17:10
@@ -29,6 +33,13 @@ func (gh *GithubWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
return
}

if gh.Secret != "" && !checkSignature(gh.Secret, data, r.Header["X-Hub-Signature"][0]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will panic if the header X-Hub-Signature doesn't exist. It's recommended to use r.Header.Get("X-Hub-Signature") instead.

Same thing with the X-Github-Event up above. I know it's pre-existing code, but would probably be good time to fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Fixed.

@sparrc sparrc added this to the 1.4.0 milestone Mar 6, 2017
@francois2metz
Copy link
Contributor Author

Note: I tested that in production: with and without secret, and with a non matching secret.

data, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

if gh.Secret != "" && !checkSignature(gh.Secret, data, r.Header.Get("X-Hub-Signature")) {
log.Printf("I! Fail to check the github webhook signature\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be E!?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

return hmac.Equal([]byte(signature), []byte(generateSignature(secret, data)))
}

func generateSignature(secret string, data []byte) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of generating the signature on every github event, you should generate it just once and then cache for future calls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generation is dependant of the body of the event. Not sure what I can cache. The string to byte op?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, nevermind then

@danielnelson danielnelson modified the milestones: 1.3.0, 1.4.0 Apr 15, 2017
@danielnelson
Copy link
Contributor

Can you add this to the changelog?

@francois2metz francois2metz force-pushed the plugin/gh_mac_validation branch from 1994ba3 to cad84e6 Compare April 16, 2017 10:15
@francois2metz
Copy link
Contributor Author

Rebased and updated the changelog and the readme.

@danielnelson danielnelson merged commit 58ee962 into influxdata:master Apr 17, 2017
@francois2metz francois2metz deleted the plugin/gh_mac_validation branch April 17, 2017 19:59
vlamug pushed a commit to vlamug/telegraf that referenced this pull request May 30, 2017
maxunt pushed a commit that referenced this pull request Jun 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants