diff --git a/internal/infrastructure/captureintake/receiver.go b/internal/infrastructure/captureintake/receiver.go index 12ba7fe47..6f191da8d 100644 --- a/internal/infrastructure/captureintake/receiver.go +++ b/internal/infrastructure/captureintake/receiver.go @@ -7,10 +7,8 @@ import ( "encoding/json" "io" "log/slog" - "mime" "net" "net/http" - "net/url" "time" "github.com/samcharles93/archie-core/internal/domain/access" @@ -104,12 +102,6 @@ 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 { @@ -127,7 +119,7 @@ func (rc *Receiver) ServeHTTP(w http.ResponseWriter, r *http.Request) { ReceivedAt: time.Now().UTC(), Source: path, RemoteAddr: r.RemoteAddr, - ContentType: contentType, + ContentType: r.Header.Get("Content-Type"), Headers: string(redactedHeaders), Body: string(redactedBody), Authenticated: authenticated, @@ -206,25 +198,6 @@ 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 deleted file mode 100644 index 9a9b673cf..000000000 --- a/internal/infrastructure/captureintake/receiver_test.go +++ /dev/null @@ -1,25 +0,0 @@ -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) - } - } -}