-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
GitHub webhooks: check signature #2493
Conversation
dcf29bb
to
68d6d6d
Compare
@@ -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]) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Fixed.
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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be E!
?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, nevermind then
Can you add this to the changelog? |
1994ba3
to
cad84e6
Compare
Rebased and updated the changelog and the readme. |
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: