Skip to content

Commit 53694f8

Browse files
authored
1 parent c3b1a44 commit 53694f8

File tree

2 files changed

+75
-116
lines changed

2 files changed

+75
-116
lines changed

github/github.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ package github
22

33
import (
44
"crypto/hmac"
5-
"crypto/sha1"
5+
"crypto/sha256"
66
"encoding/hex"
77
"encoding/json"
88
"errors"
99
"fmt"
1010
"io"
1111
"net/http"
12+
"strings"
1213
)
1314

1415
// parse errors
1516
var (
1617
ErrEventNotSpecifiedToParse = errors.New("no Event specified to parse")
1718
ErrInvalidHTTPMethod = errors.New("invalid HTTP Method")
1819
ErrMissingGithubEventHeader = errors.New("missing X-GitHub-Event Header")
19-
ErrMissingHubSignatureHeader = errors.New("missing X-Hub-Signature Header")
20+
ErrMissingHubSignatureHeader = errors.New("missing X-Hub-Signature-256 Header")
2021
ErrEventNotFound = errors.New("event not defined to be parsed")
2122
ErrParsingPayload = errors.New("error parsing payload")
2223
ErrHMACVerificationFailed = errors.New("HMAC verification failed")
@@ -159,15 +160,18 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
159160

160161
// If we have a Secret set, we should check the MAC
161162
if len(hook.secret) > 0 {
162-
signature := r.Header.Get("X-Hub-Signature")
163+
signature := r.Header.Get("X-Hub-Signature-256")
163164
if len(signature) == 0 {
164165
return nil, ErrMissingHubSignatureHeader
165166
}
166-
mac := hmac.New(sha1.New, []byte(hook.secret))
167+
168+
signature = strings.TrimPrefix(signature, "sha256=")
169+
170+
mac := hmac.New(sha256.New, []byte(hook.secret))
167171
_, _ = mac.Write(payload)
168172
expectedMAC := hex.EncodeToString(mac.Sum(nil))
169173

170-
if !hmac.Equal([]byte(signature[5:]), []byte(expectedMAC)) {
174+
if !hmac.Equal([]byte(signature), []byte(expectedMAC)) {
171175
return nil, ErrHMACVerificationFailed
172176
}
173177
}

0 commit comments

Comments
 (0)