From c0586e465cf0abd27540fb178cd1bbf1a61fa2d8 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 6 Oct 2026 00:37:20 +1100 Subject: [PATCH] fix(captureintake): decode any form-encoded webhook body to JSON --- .../infrastructure/captureintake/receiver.go | 19 ++++++++++++------- .../captureintake/receiver_test.go | 6 ++++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/internal/infrastructure/captureintake/receiver.go b/internal/infrastructure/captureintake/receiver.go index 12ba7fe47..357335193 100644 --- a/internal/infrastructure/captureintake/receiver.go +++ b/internal/infrastructure/captureintake/receiver.go @@ -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 { diff --git a/internal/infrastructure/captureintake/receiver_test.go b/internal/infrastructure/captureintake/receiver_test.go index 9a9b673cf..a2437c304 100644 --- a/internal/infrastructure/captureintake/receiver_test.go +++ b/internal/infrastructure/captureintake/receiver_test.go @@ -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))