Skip to content

Commit 5554ed1

Browse files
committed
Add length check to github signature
Signed-off-by: AdamKorcz <[email protected]>
1 parent c3b1a44 commit 5554ed1

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

github/github.go

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var (
2020
ErrEventNotFound = errors.New("event not defined to be parsed")
2121
ErrParsingPayload = errors.New("error parsing payload")
2222
ErrHMACVerificationFailed = errors.New("HMAC verification failed")
23+
ErrWrongHubSignatureHeader = errors.New("Invalid Github signature")
2324
)
2425

2526
// Event defines a GitHub hook event type
@@ -163,6 +164,9 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
163164
if len(signature) == 0 {
164165
return nil, ErrMissingHubSignatureHeader
165166
}
167+
if len(signature) < 6 {
168+
return nil, ErrWrongHubSignatureHeader
169+
}
166170
mac := hmac.New(sha1.New, []byte(hook.secret))
167171
_, _ = mac.Write(payload)
168172
expectedMAC := hex.EncodeToString(mac.Sum(nil))

github/github_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ func TestBadRequests(t *testing.T) {
5858
payload io.Reader
5959
headers http.Header
6060
}{
61+
{
62+
name: "ShortSignature",
63+
event: CreateEvent,
64+
payload: bytes.NewBuffer([]byte("{12345}")),
65+
headers: http.Header{
66+
"X-Github-Event": []string{"commit_comment"},
67+
"X-Hub-Signature": []string{"sha1"},
68+
},
69+
},
6170
{
6271
name: "BadNoEventHeader",
6372
event: CreateEvent,

0 commit comments

Comments
 (0)