Skip to content

Commit 61c8d35

Browse files
committed
Update github to use sha256 signature
1 parent 69430a8 commit 61c8d35

File tree

2 files changed

+75
-116
lines changed

2 files changed

+75
-116
lines changed

github/github.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@ 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
"io/ioutil"
1212
"net/http"
13+
"strings"
1314
)
1415

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

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

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

0 commit comments

Comments
 (0)