Skip to content

Commit f4db242

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

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

github/github.go

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

2627
// Event defines a GitHub hook event type
@@ -166,7 +167,9 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
166167
}
167168

168169
signature = strings.TrimPrefix(signature, "sha256=")
169-
170+
if len(signature) < 6 {
171+
return nil, ErrWrongHubSignatureHeader
172+
}
170173
mac := hmac.New(sha256.New, []byte(hook.secret))
171174
_, _ = mac.Write(payload)
172175
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: CommitCommentEvent,
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)