Skip to content

Commit 4449e0e

Browse files
StevenMaudeCopilot
andcommitted
Switch to using X-Hub-Signature-256 for GitHub
> Make sure you are using the correct header. GitHub recommends that you > use the `X-Hub-Signature-256` header, which uses the HMAC-SHA256 algorithm. > The X-Hub-Signature header uses the HMAC-SHA1 algorithm and is only > included for legacy purposes. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 1a0f80d commit 4449e0e

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

pkg/hookbot/hookbot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func (h *Hookbot) ServePublish(w http.ResponseWriter, r *http.Request) {
377377
case "github":
378378

379379
body, err = json.Marshal(map[string]interface{}{
380-
"Signature": r.Header.Get("X-Hub-Signature"),
380+
"Signature": r.Header.Get("X-Hub-Signature-256"),
381381
"Event": r.Header.Get("X-GitHub-Event"),
382382
"Delivery": r.Header.Get("X-GitHub-Delivery"),
383383
"Payload": body,

pkg/router/github/auth.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"crypto/hmac"
55
"crypto/sha1"
6+
"crypto/sha256"
67
"crypto/subtle"
78
"encoding/json"
89
"fmt"
@@ -15,6 +16,12 @@ func Sha1HMAC(key string, payload []byte) string {
1516
return fmt.Sprintf("%x", mac.Sum(nil))
1617
}
1718

19+
func Sha256HMAC(key string, payload []byte) string {
20+
mac := hmac.New(sha256.New, []byte(key))
21+
_, _ = mac.Write(payload)
22+
return fmt.Sprintf("%x", mac.Sum(nil))
23+
}
24+
1825
func SecureEqual(x, y string) bool {
1926
if subtle.ConstantTimeCompare([]byte(x), []byte(y)) == 1 {
2027
return true
@@ -39,7 +46,7 @@ func IsValidGithubSignature(secret string, message []byte) bool {
3946
}
4047

4148
expected := m.Signature
42-
got := fmt.Sprintf("sha1=%v", Sha1HMAC(secret, m.Payload))
49+
got := fmt.Sprintf("sha256=%v", Sha256HMAC(secret, m.Payload))
4350

4451
log.Printf("Expected = %v got = %v", expected, got)
4552

0 commit comments

Comments
 (0)