fix(captureintake): decode any form-encoded webhook body to JSON
This commit is contained in:
1 parent
4fd53d3f44
commit
c0586e465c
2 files changed
+16
-9
No files matched your search
@@ -206,9 +206,10 @@ func verify(src *source.Source, h http.Header, body []byte) (authenticated, unsi
|
||||
return webhookguard.VerifyHMAC(body, sig, src.Secret), false
|
||||
}
|
||||
|
||||
// formPayload unwraps the JSON a form-encoded delivery carries in its payload
|
||||
// field, GitHub's default webhook content type, so it types and maps like a
|
||||
// JSON delivery.
|
||||
// formPayload turns a form-encoded delivery into JSON so it types and maps
|
||||
// like a JSON one. A payload field holding JSON (GitHub's and Slack's form
|
||||
// shape) is the event itself; any other form becomes an object of its fields,
|
||||
// first value per name.
|
||||
func formPayload(contentType string, body []byte) ([]byte, bool) {
|
||||
mt, _, err := mime.ParseMediaType(contentType)
|
||||
if err != nil || mt != "application/x-www-form-urlencoded" {
|
||||
@@ -218,11 +219,15 @@ func formPayload(contentType string, body []byte) ([]byte, bool) {
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
payload := form.Get("payload")
|
||||
if !json.Valid([]byte(payload)) {
|
||||
return nil, false
|
||||
if payload := form.Get("payload"); json.Valid([]byte(payload)) {
|
||||
return []byte(payload), true
|
||||
}
|
||||
return []byte(payload), true
|
||||
fields := make(map[string]string, len(form))
|
||||
for name := range form {
|
||||
fields[name] = form.Get(name)
|
||||
}
|
||||
out, err := json.Marshal(fields)
|
||||
return out, err == nil
|
||||
}
|
||||
|
||||
func (rc *Receiver) logger() *slog.Logger {
|
||||
|
||||
@@ -13,8 +13,10 @@ func TestFormPayload(t *testing.T) {
|
||||
}{
|
||||
{"github form", "application/x-www-form-urlencoded", "payload=" + url.QueryEscape(json), json, true},
|
||||
{"json passes through", "application/json", json, "", false},
|
||||
{"form without payload", "application/x-www-form-urlencoded", "a=b", "", false},
|
||||
{"payload not json", "application/x-www-form-urlencoded", "payload=nope", "", false},
|
||||
{"github form with charset", "application/x-www-form-urlencoded; charset=utf-8", "payload=" + url.QueryEscape(json), json, true},
|
||||
{"plain form", "application/x-www-form-urlencoded", "a=b&a=c&n=1", `{"a":"b","n":"1"}`, true},
|
||||
{"payload not json", "application/x-www-form-urlencoded", "payload=nope", `{"payload":"nope"}`, true},
|
||||
{"malformed form", "application/x-www-form-urlencoded", "a=%zz", "", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
got, ok := formPayload(tt.contentType, []byte(tt.body))
|
||||
|
||||
Reference in new issue
Block a user