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