Skip to content
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

feat: Add support for inbound webhooks that use form data #1998

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ typesense_data
postgres_data
redis_data
/.run/go test github.com_frain-dev_convoy_server.run.xml

convoy-ee
jirevwe marked this conversation as resolved.
Show resolved Hide resolved
convoy-ce
47 changes: 45 additions & 2 deletions api/ingest.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package api

import (
"encoding/json"
"errors"
"strings"

"github.com/frain-dev/convoy/pkg/msgpack"

"io"
Expand Down Expand Up @@ -136,8 +139,7 @@ func (a *ApplicationHandler) IngestEvent(w http.ResponseWriter, r *http.Request)

// 3.1 On Failure
// Return 400 Bad Request.
body := io.LimitReader(r.Body, int64(maxIngestSize))
payload, err := io.ReadAll(body)
payload, err := extractPayloadFromIngestEventReq(r, maxIngestSize)
if err != nil {
_ = render.Render(w, r, util.NewErrorResponse(err.Error(), http.StatusBadRequest))
return
Expand Down Expand Up @@ -218,6 +220,47 @@ func (a *ApplicationHandler) IngestEvent(w http.ResponseWriter, r *http.Request)
}
}

const (
applicationJsonContentType = "application/json"
multipartFormDataContentType = "multipart/form-data"
)

func extractPayloadFromIngestEventReq(r *http.Request, maxIngestSize uint64) ([]byte, error) {
var contentType string
rawContentType := strings.ToLower(
strings.TrimSpace(
r.Header.Get("Content-Type"),
),
)

// We split the rawContentType using the first semicolon as the delimiter because go-chi has a weird way
// of handling the form-data content type. It adds a semicolon after the boundary and we need to remove it.
// Example: multipart/form-data; boundary=--------------------------879783787191406952783600
if contentTypeSlice := strings.SplitN(rawContentType, ";", 2); len(contentTypeSlice) == 0 {
// always default to json if no content type is specified
contentType = applicationJsonContentType
} else {
contentType = strings.TrimSpace(contentTypeSlice[0])
}

switch contentType {
case multipartFormDataContentType:
if err := r.ParseMultipartForm(int64(maxIngestSize)); err != nil {
return nil, err
}
data := make(map[string][]string)
for k, v := range r.Form {
data[k] = v
}
return json.Marshal(data)

default:
// To avoid introducing a breaking change, we are keeping the old behaviour of assuming
// the content type is JSON if the content type is not specified/unsupported.
return io.ReadAll(io.LimitReader(r.Body, int64(maxIngestSize)))
}
}

func (a *ApplicationHandler) HandleCrcCheck(w http.ResponseWriter, r *http.Request) {
maskId := chi.URLParam(r, "maskID")
source, err := postgres.NewSourceRepo(a.A.DB, a.A.Cache).FindSourceByMaskID(r.Context(), maskId)
Expand Down
66 changes: 66 additions & 0 deletions api/ingest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package api

import (
"bytes"
"encoding/json"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func Test_extractPayloadFromIngestEventReq(t *testing.T) {
t.Run("application/json content type", func(t *testing.T) {
jsonBody := []byte(`{"key": "value"}`)

req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(jsonBody))
req.Header.Set("Content-Type", "application/json")

payload, err := extractPayloadFromIngestEventReq(req, 1024)
require.NoError(t, err)
require.Equal(t, jsonBody, payload)
})

t.Run("multipart/form-data content type", func(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
_ = writer.WriteField("key1", "value1")
_ = writer.WriteField("key2", "value2")
require.NoError(t, writer.Close())

req := httptest.NewRequest(http.MethodPost, "/", body)
req.Header.Set("Content-Type", "multipart/form-data; boundary="+writer.Boundary())

payload, err := extractPayloadFromIngestEventReq(req, 1024)
require.NoError(t, err)

var form map[string][]string
require.NoError(t, json.Unmarshal(payload, &form))

require.Equal(t, "value1", form["key1"][0])
require.Equal(t, "value2", form["key2"][0])
})

t.Run("content type not specified", func(t *testing.T) {
jsonBody := []byte(`{"key": "value"}`)

req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(jsonBody))

payload, err := extractPayloadFromIngestEventReq(req, 1024)
require.NoError(t, err)
require.Equal(t, jsonBody, payload)
})

t.Run("unsupported content type", func(t *testing.T) {
jsonBody := []byte(`{"key": "value"}`)

req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(jsonBody))
req.Header.Set("Content-Type", "text/html")

payload, err := extractPayloadFromIngestEventReq(req, 1024)
require.NoError(t, err)
require.Equal(t, jsonBody, payload)
})
}