Skip to content

Commit b60bde1

Browse files
authored
Handle MIME parameters in Content-Type Header (#1922)
Fixes: #1874.
1 parent b11061d commit b60bde1

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

github/messages.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"hash"
2121
"io/ioutil"
22+
"mime"
2223
"net/http"
2324
"net/url"
2425
"strings"
@@ -157,7 +158,14 @@ func messageMAC(signature string) ([]byte, func() hash.Hash, error) {
157158
func ValidatePayload(r *http.Request, secretToken []byte) (payload []byte, err error) {
158159
var body []byte // Raw body that GitHub uses to calculate the signature.
159160

160-
switch ct := r.Header.Get("Content-Type"); ct {
161+
ct := r.Header.Get("Content-Type")
162+
163+
mediatype, _, err := mime.ParseMediaType(ct)
164+
if err != nil {
165+
mediatype = ""
166+
}
167+
168+
switch mediatype {
161169
case "application/json":
162170
var err error
163171
if body, err = ioutil.ReadAll(r.Body); err != nil {

github/messages_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,33 @@ func TestValidatePayload_BadRequestBody(t *testing.T) {
229229
}
230230
}
231231

232+
func TestValidatePayload_InvalidContentTypeParams(t *testing.T) {
233+
req, err := http.NewRequest("POST", "http://localhost/event", nil)
234+
if err != nil {
235+
t.Fatalf("NewRequest: %v", err)
236+
}
237+
req.Header.Set("Content-Type", "application/json; charset=")
238+
if _, err = ValidatePayload(req, nil); err == nil {
239+
t.Error("ValidatePayload = nil, want err")
240+
}
241+
}
242+
243+
func TestValidatePayload_ValidContentTypeParams(t *testing.T) {
244+
var requestBody = `{"yo":true}`
245+
buf := bytes.NewBufferString(requestBody)
246+
247+
req, err := http.NewRequest("POST", "http://localhost/event", buf)
248+
if err != nil {
249+
t.Fatalf("NewRequest: %v", err)
250+
}
251+
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
252+
253+
_, err = ValidatePayload(req, nil)
254+
if err != nil {
255+
t.Error("ValidatePayload = nil, want err")
256+
}
257+
}
258+
232259
func TestParseWebHook(t *testing.T) {
233260
tests := []struct {
234261
payload interface{}

0 commit comments

Comments
 (0)