diff --git a/internal/infrastructure/captureintake/receiver.go b/internal/infrastructure/captureintake/receiver.go index 6f191da8d..12ba7fe47 100644 --- a/internal/infrastructure/captureintake/receiver.go +++ b/internal/infrastructure/captureintake/receiver.go @@ -7,8 +7,10 @@ import ( "encoding/json" "io" "log/slog" + "mime" "net" "net/http" + "net/url" "time" "github.com/samcharles93/archie-core/internal/domain/access" @@ -102,6 +104,12 @@ func (rc *Receiver) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Verified on the raw bytes, before redaction parses them. authenticated, unsigned := verify(src, r.Header, body) + contentType := r.Header.Get("Content-Type") + if payload, ok := formPayload(contentType, body); ok { + body, contentType = payload, "application/json" + r.Header.Set("Content-Type", contentType) + } + headers, _ := json.Marshal(r.Header) redactedHeaders, err := webhookguard.RedactPayload(headers) if err != nil { @@ -119,7 +127,7 @@ func (rc *Receiver) ServeHTTP(w http.ResponseWriter, r *http.Request) { ReceivedAt: time.Now().UTC(), Source: path, RemoteAddr: r.RemoteAddr, - ContentType: r.Header.Get("Content-Type"), + ContentType: contentType, Headers: string(redactedHeaders), Body: string(redactedBody), Authenticated: authenticated, @@ -198,6 +206,25 @@ 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. +func formPayload(contentType string, body []byte) ([]byte, bool) { + mt, _, err := mime.ParseMediaType(contentType) + if err != nil || mt != "application/x-www-form-urlencoded" { + return nil, false + } + form, err := url.ParseQuery(string(body)) + if err != nil { + return nil, false + } + payload := form.Get("payload") + if !json.Valid([]byte(payload)) { + return nil, false + } + return []byte(payload), true +} + func (rc *Receiver) logger() *slog.Logger { if rc.Log == nil { return slog.New(slog.DiscardHandler) diff --git a/internal/infrastructure/captureintake/receiver_test.go b/internal/infrastructure/captureintake/receiver_test.go new file mode 100644 index 000000000..9a9b673cf --- /dev/null +++ b/internal/infrastructure/captureintake/receiver_test.go @@ -0,0 +1,25 @@ +package captureintake + +import ( + "net/url" + "testing" +) + +func TestFormPayload(t *testing.T) { + json := `{"ref":"refs/heads/main"}` + tests := []struct { + name, contentType, body, want string + ok bool + }{ + {"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}, + } + for _, tt := range tests { + got, ok := formPayload(tt.contentType, []byte(tt.body)) + if ok != tt.ok || string(got) != tt.want { + t.Errorf("%s: got %q %v, want %q %v", tt.name, got, ok, tt.want, tt.ok) + } + } +}