Skip to content

Commit 4f6a399

Browse files
StevenMaudeCopilot
andcommitted
Switch to SHA-256 HMAC
Remove uses of SHA-1 entirely. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 18aafaf commit 4f6a399

6 files changed

Lines changed: 13 additions & 20 deletions

File tree

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func ActionMakeTokens(_ context.Context, c *cli.Command) error {
159159
log.Fatalf("URL %q doesn't parse: %v", arg, err)
160160
}
161161

162-
mac := hookbot.Sha1HMAC(key, argURL.Path)
162+
mac := hookbot.Sha256HMAC(key, argURL.Path)
163163
if c.Bool("bare") {
164164
fmt.Println(mac)
165165
} else {

pkg/hookbot/auth.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package hookbot
22

33
import (
44
"crypto/hmac"
5-
"crypto/sha1"
5+
"crypto/sha256"
66
"crypto/subtle"
77
"fmt"
88
"net/http"
99
"regexp"
1010
"strings"
1111
)
1212

13-
func Sha1HMAC(key, payload string) string {
14-
mac := hmac.New(sha1.New, []byte(key))
13+
func Sha256HMAC(key, payload string) string {
14+
mac := hmac.New(sha256.New, []byte(key))
1515
_, _ = mac.Write([]byte(payload))
1616
return fmt.Sprintf("%x", mac.Sum(nil))
1717
}
@@ -67,14 +67,14 @@ func (h *Hookbot) IsKeyOK(w http.ResponseWriter, r *http.Request) bool {
6767

6868
// Try all subpaths and see if any of them matches the given MAC.
6969
for _, subpath := range subpaths(r.URL.Path) {
70-
expectedMac := Sha1HMAC(h.key, subpath)
70+
expectedMac := Sha256HMAC(h.key, subpath)
7171
if SecureEqual(givenMac, expectedMac) {
7272
return true
7373
}
7474

7575
// See if HMAC matches the URL without the {/pub,/sub} prefix.
7676
// These tokens are valid for both pub and sub.
77-
expectedMac = Sha1HMAC(h.key, noPrefix(subpath))
77+
expectedMac = Sha256HMAC(h.key, noPrefix(subpath))
7878
if SecureEqual(givenMac, expectedMac) {
7979
return true
8080
}

pkg/hookbot/auth_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestAuthMissingFail(t *testing.T) {
4242
func TestAuthInvalidSecret(t *testing.T) {
4343
w, r := MakeRequest("POST", "/pub/", "MESSAGE")
4444

45-
token := Sha1HMAC(TEST_KEY, "/pub/not/the/same/as/above") // bad secret
45+
token := Sha256HMAC(TEST_KEY, "/pub/not/the/same/as/above") // bad secret
4646
r.SetBasicAuth(token, "")
4747

4848
func() {
@@ -61,7 +61,7 @@ func TestAuthInvalidSecret(t *testing.T) {
6161
func TestAuthSuccess(t *testing.T) {
6262
w, r := MakeRequest("POST", "/pub/place", "MESSAGE")
6363

64-
token := Sha1HMAC(TEST_KEY, "/pub/place")
64+
token := Sha256HMAC(TEST_KEY, "/pub/place")
6565
r.SetBasicAuth(token, "")
6666

6767
func() {
@@ -80,7 +80,7 @@ func TestAuthSuccess(t *testing.T) {
8080
func TestAuthPubSub(t *testing.T) {
8181

8282
// Valid for both pub and sub, for lack of {/pub,/sub} prefix
83-
token := Sha1HMAC(TEST_KEY, "/place")
83+
token := Sha256HMAC(TEST_KEY, "/place")
8484

8585
w, r := MakeRequest("POST", "/pub/place", "MESSAGE")
8686
r.SetBasicAuth(token, "")
@@ -118,7 +118,7 @@ func TestAuthPubSub(t *testing.T) {
118118
func TestAuthSuccessSubstring(t *testing.T) {
119119
w, r := MakeRequest("POST", "/pub/place/sub/sub/sub", "MESSAGE")
120120

121-
token := Sha1HMAC(TEST_KEY, "/pub/place/")
121+
token := Sha256HMAC(TEST_KEY, "/pub/place/")
122122
r.SetBasicAuth(token, "")
123123

124124
func() {
@@ -138,7 +138,7 @@ func TestAuthSuccessSubstring(t *testing.T) {
138138
func TestAuthFailSubstring(t *testing.T) {
139139
w, r := MakeRequest("POST", "/pub/post", "MESSAGE")
140140

141-
token := Sha1HMAC(TEST_KEY, "/pub/po")
141+
token := Sha256HMAC(TEST_KEY, "/pub/po")
142142
r.SetBasicAuth(token, "")
143143

144144
func() {

pkg/hookbot/hookbot_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestPubSub(t *testing.T) {
1313
messages = hookbot.Add("test/topic").c
1414

1515
w, r := MakeRequest("POST", "/test/topic", "MESSAGE")
16-
token := Sha1HMAC(TEST_KEY, "/test/topic")
16+
token := Sha256HMAC(TEST_KEY, "/test/topic")
1717
r.SetBasicAuth(token, "")
1818

1919
hookbot.ServeHTTP(w, r)

pkg/router/github/auth.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@ package github
22

33
import (
44
"crypto/hmac"
5-
"crypto/sha1"
65
"crypto/sha256"
76
"crypto/subtle"
87
"encoding/json"
98
"fmt"
109
"log"
1110
)
1211

13-
func Sha1HMAC(key string, payload []byte) string {
14-
mac := hmac.New(sha1.New, []byte(key))
15-
_, _ = mac.Write(payload)
16-
return fmt.Sprintf("%x", mac.Sum(nil))
17-
}
18-
1912
func Sha256HMAC(key string, payload []byte) string {
2013
mac := hmac.New(sha256.New, []byte(key))
2114
_, _ = mac.Write(payload)

pkg/router/github/github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func ActionRoute(_ context.Context, c *cli.Command) error {
7575
outbound := make(chan listen.Message, 1)
7676

7777
publish := func(m hookbot.Message) bool {
78-
token := Sha1HMAC(c.String("key"), []byte(m.Topic))
78+
token := Sha256HMAC(c.String("key"), []byte(m.Topic))
7979

8080
outURL := fmt.Sprintf("https://%v@%v/pub/%s", token, target.Host, m.Topic)
8181

0 commit comments

Comments
 (0)