Skip to content

Commit cc67f35

Browse files
committed
Swaps in SHA256 signature (fixes #13)
1 parent 4d48ca5 commit cc67f35

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

githubhook.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package githubhook
33

44
import (
55
"crypto/hmac"
6-
"crypto/sha1"
6+
"crypto/sha256"
77
"encoding/hex"
88
"encoding/json"
99
"errors"
@@ -37,11 +37,12 @@ type Hook struct {
3737
Payload []byte
3838
}
3939

40-
const signaturePrefix = "sha1="
41-
const signatureLength = 45 // len(SignaturePrefix) + len(hex(sha1))
40+
const signaturePrefix = "sha256="
41+
const prefixLength = len(signaturePrefix)
42+
const signatureLength = prefixLength + (sha256.Size * 2)
4243

4344
func signBody(secret, body []byte) []byte {
44-
computed := hmac.New(sha1.New, secret)
45+
computed := hmac.New(sha256.New, secret)
4546
computed.Write(body)
4647
return []byte(computed.Sum(nil))
4748
}
@@ -55,13 +56,15 @@ func (h *Hook) SignedBy(secret []byte) bool {
5556
return false
5657
}
5758

58-
actual := make([]byte, 20)
59-
hex.Decode(actual, []byte(h.Signature[5:]))
59+
actual := make([]byte, sha256.Size)
60+
hex.Decode(actual, []byte(h.Signature[prefixLength:]))
6061

61-
return hmac.Equal(signBody(secret, h.Payload), actual)
62+
expected := signBody(secret, h.Payload)
63+
64+
return hmac.Equal(expected, actual)
6265
}
6366

64-
// Extract unmarshals Payload into a destination interface.
67+
// Extract hook's JSON payload into dst
6568
func (h *Hook) Extract(dst interface{}) error {
6669
return json.Unmarshal(h.Payload, dst)
6770
}
@@ -73,7 +76,7 @@ func New(req *http.Request) (hook *Hook, err error) {
7376
return nil, errors.New("Unknown method!")
7477
}
7578

76-
if hook.Signature = req.Header.Get("x-hub-signature"); len(hook.Signature) == 0 {
79+
if hook.Signature = req.Header.Get("x-hub-signature-256"); len(hook.Signature) == 0 {
7780
return nil, errors.New("No signature!")
7881
}
7982

githubhook_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package githubhook
22

33
import (
44
"crypto/hmac"
5-
"crypto/sha1"
5+
"crypto/sha256"
66
"encoding/hex"
77
"fmt"
88
"net/http"
@@ -29,11 +29,11 @@ func expectParseError(t *testing.T, msg string, r *http.Request) {
2929
}
3030

3131
func signature(body string) string {
32-
dst := make([]byte, 40)
33-
computed := hmac.New(sha1.New, []byte(testSecret))
32+
dst := make([]byte, sha256.Size*2)
33+
computed := hmac.New(sha256.New, []byte(testSecret))
3434
computed.Write([]byte(body))
3535
hex.Encode(dst, computed.Sum(nil))
36-
return "sha1=" + string(dst)
36+
return signaturePrefix + string(dst)
3737
}
3838

3939
func TestNonPost(t *testing.T) {
@@ -48,20 +48,20 @@ func TestMissingSignature(t *testing.T) {
4848

4949
func TestMissingEvent(t *testing.T) {
5050
r, _ := http.NewRequest("POST", "/path", nil)
51-
r.Header.Add("x-hub-signature", "bogus signature")
51+
r.Header.Add("x-hub-signature-256", "bogus signature")
5252
expectNewError(t, "No event!", r)
5353
}
5454

5555
func TestMissingEventId(t *testing.T) {
5656
r, _ := http.NewRequest("POST", "/path", nil)
57-
r.Header.Add("x-hub-signature", "bogus signature")
57+
r.Header.Add("x-hub-signature-256", "bogus signature")
5858
r.Header.Add("x-github-event", "bogus event")
5959
expectNewError(t, "No event Id!", r)
6060
}
6161

6262
func TestInvalidSignature(t *testing.T) {
6363
r, _ := http.NewRequest("POST", "/path", strings.NewReader("..."))
64-
r.Header.Add("x-hub-signature", "bogus signature")
64+
r.Header.Add("x-hub-signature-256", "bogus signature")
6565
r.Header.Add("x-github-event", "bogus event")
6666
r.Header.Add("x-github-delivery", "bogus id")
6767
expectParseError(t, "Invalid signature", r)
@@ -72,7 +72,7 @@ func TestValidSignature(t *testing.T) {
7272
body := "{}"
7373

7474
r, _ := http.NewRequest("POST", "/path", strings.NewReader(body))
75-
r.Header.Add("x-hub-signature", signature(body))
75+
r.Header.Add("x-hub-signature-256", signature(body))
7676
r.Header.Add("x-github-event", "bogus event")
7777
r.Header.Add("x-github-delivery", "bogus id")
7878

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy