@@ -2,21 +2,22 @@ package github
2
2
3
3
import (
4
4
"crypto/hmac"
5
- "crypto/sha1 "
5
+ "crypto/sha256 "
6
6
"encoding/hex"
7
7
"encoding/json"
8
8
"errors"
9
9
"fmt"
10
10
"io"
11
11
"net/http"
12
+ "strings"
12
13
)
13
14
14
15
// parse errors
15
16
var (
16
17
ErrEventNotSpecifiedToParse = errors .New ("no Event specified to parse" )
17
18
ErrInvalidHTTPMethod = errors .New ("invalid HTTP Method" )
18
19
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" )
20
21
ErrEventNotFound = errors .New ("event not defined to be parsed" )
21
22
ErrParsingPayload = errors .New ("error parsing payload" )
22
23
ErrHMACVerificationFailed = errors .New ("HMAC verification failed" )
@@ -159,15 +160,18 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
159
160
160
161
// If we have a Secret set, we should check the MAC
161
162
if len (hook .secret ) > 0 {
162
- signature := r .Header .Get ("X-Hub-Signature" )
163
+ signature := r .Header .Get ("X-Hub-Signature-256 " )
163
164
if len (signature ) == 0 {
164
165
return nil , ErrMissingHubSignatureHeader
165
166
}
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 ))
167
171
_ , _ = mac .Write (payload )
168
172
expectedMAC := hex .EncodeToString (mac .Sum (nil ))
169
173
170
- if ! hmac .Equal ([]byte (signature [ 5 :] ), []byte (expectedMAC )) {
174
+ if ! hmac .Equal ([]byte (signature ), []byte (expectedMAC )) {
171
175
return nil , ErrHMACVerificationFailed
172
176
}
173
177
}
0 commit comments