+
Skip to content

Handle MIME parameters in Content-Type Header #1922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion github/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"hash"
"io/ioutil"
"mime"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -157,7 +158,14 @@ func messageMAC(signature string) ([]byte, func() hash.Hash, error) {
func ValidatePayload(r *http.Request, secretToken []byte) (payload []byte, err error) {
var body []byte // Raw body that GitHub uses to calculate the signature.

switch ct := r.Header.Get("Content-Type"); ct {
ct := r.Header.Get("Content-Type")

mediatype, _, err := mime.ParseMediaType(ct)
if err != nil {
mediatype = ""
}

switch mediatype {
case "application/json":
var err error
if body, err = ioutil.ReadAll(r.Body); err != nil {
Expand Down
27 changes: 27 additions & 0 deletions github/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,33 @@ func TestValidatePayload_BadRequestBody(t *testing.T) {
}
}

func TestValidatePayload_InvalidContentTypeParams(t *testing.T) {
req, err := http.NewRequest("POST", "http://localhost/event", nil)
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
req.Header.Set("Content-Type", "application/json; charset=")
if _, err = ValidatePayload(req, nil); err == nil {
t.Error("ValidatePayload = nil, want err")
}
}

func TestValidatePayload_ValidContentTypeParams(t *testing.T) {
var requestBody = `{"yo":true}`
buf := bytes.NewBufferString(requestBody)

req, err := http.NewRequest("POST", "http://localhost/event", buf)
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")

_, err = ValidatePayload(req, nil)
if err != nil {
t.Error("ValidatePayload = nil, want err")
}
}

func TestParseWebHook(t *testing.T) {
tests := []struct {
payload interface{}
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载