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

fix: ingore webhook headers support when body is json array #5063

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions gateway/webhook/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,18 @@ func TestIntegrationWebhook(t *testing.T) {
assert.JSONEq(t, string(p), string(batch.Batch[0]))
}

r, err = errDB.GetUnprocessed(ctx, jobsdb.GetQueryParams{
WorkspaceID: workspaceID,
// ParameterFilters: []jobsdb.ParameterFilterT{{
// Name: "source_id",
// Value: sourceID,
// }},
JobsLimit: 1,
})
require.Eventually(t, func() bool {
r, err = errDB.GetUnprocessed(ctx, jobsdb.GetQueryParams{
WorkspaceID: workspaceID,
ParameterFilters: []jobsdb.ParameterFilterT{{
Name: "source_id",
Value: sourceID,
}},
JobsLimit: 10,
})
return err == nil && len(r.Jobs) == len(tc.Output.ErrQueue)
}, time.Second, time.Millisecond*10)

require.NoError(t, err)
assert.Len(t, r.Jobs, len(tc.Output.ErrQueue))
for i, p := range tc.Output.ErrQueue {
Expand Down
12 changes: 7 additions & 5 deletions gateway/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func getXHeaders(req *http.Request) map[string]string {
return xHeaders
}

func prepareRequestBody(req *http.Request, sourceType string, sourceListForParsingParams []string) ([]byte, error) {
func (webhook *HandleT) prepareRequestBody(req *http.Request, sourceType string, sourceListForParsingParams []string) ([]byte, error) {
defer func() {
_ = req.Body.Close()
}()
Expand All @@ -305,11 +305,13 @@ func prepareRequestBody(req *http.Request, sourceType string, sourceListForParsi

xHeaders := getXHeaders(req)
if len(xHeaders) > 0 {
body, err = sjson.SetBytes(body, "headers", xHeaders)
bodyWithHeaders, err := sjson.SetBytes(body, "headers", xHeaders)
if err != nil {
return nil, errors.New(response.InvalidJSON)
// When headers are not set in body, log the error and continue without setting headers
webhook.logger.Infof("Error while setting headers for source type=%s in body: %s", sourceType, err.Error())
} else {
body = bodyWithHeaders
}

}

return body, nil
Expand Down Expand Up @@ -343,7 +345,7 @@ func (bt *batchWebhookTransformerT) batchTransformLoop() {
var payloadArr [][]byte
var webRequests []*webhookT
for _, req := range breq.batchRequest {
body, err := prepareRequestBody(req.request, breq.sourceType, bt.webhook.config.sourceListForParsingParams)
body, err := bt.webhook.prepareRequestBody(req.request, breq.sourceType, bt.webhook.config.sourceListForParsingParams)
if err != nil {
req.done <- transformerResponse{Err: response.GetStatus(err.Error())}
continue
Expand Down
16 changes: 15 additions & 1 deletion gateway/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,12 @@ func TestRecordWebhookErrors(t *testing.T) {
}

func TestPrepareRequestBody(t *testing.T) {
initWebhook()
ctrl := gomock.NewController(t)
mockGW := mockWebhook.NewMockGateway(ctrl)
statsStore, err := memstats.New()
require.NoError(t, err)
webhookHandler := Setup(mockGW, transformer.NewNoOpService(), statsStore)
type requestOpts struct {
method string
target string
Expand All @@ -508,6 +514,7 @@ func TestPrepareRequestBody(t *testing.T) {
name string
req *http.Request
sourceType string
description string
includeQueryParams bool
wantError bool
expectedResponse string
Expand Down Expand Up @@ -555,6 +562,13 @@ func TestPrepareRequestBody(t *testing.T) {
sourceType: "shopify",
expectedResponse: `{"key":"value","query_parameters":{},"headers":{"X-Key":"header-value"}}`,
},
{
name: "Some payload with headers for sendgrid",
req: createRequest(requestOpts{method: http.MethodPost, target: "http://example.com", body: strings.NewReader(`[{"key":"value"}]`), headers: map[string]string{"X-Key": "header-value"}}),
sourceType: "webhook",
description: "Request Body is array so headers won't be included",
expectedResponse: `[{"key":"value"}]`,
},
{
name: "Some payload with query parameters for Adjust",
req: createRequest(requestOpts{method: http.MethodPost, target: "http://example.com", body: strings.NewReader(`{"key1":"value1"}`), params: map[string]string{"key2": "value2"}}),
Expand All @@ -571,7 +585,7 @@ func TestPrepareRequestBody(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := prepareRequestBody(tc.req, tc.sourceType, []string{"adjust", "shopify"})
result, err := webhookHandler.prepareRequestBody(tc.req, tc.sourceType, []string{"adjust", "shopify"})
if tc.wantError {
require.Error(t, err)
return
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ require (
github.com/rudderlabs/rudder-go-kit v0.38.0
github.com/rudderlabs/rudder-observability-kit v0.0.3
github.com/rudderlabs/rudder-schemas v0.5.1
github.com/rudderlabs/rudder-transformer/go v0.0.0-20240812044419-23196ec42acf
github.com/rudderlabs/rudder-transformer/go v0.0.0-20240904053719-1777c6cb23f9
github.com/rudderlabs/sql-tunnels v0.1.7
github.com/rudderlabs/sqlconnect-go v1.9.0
github.com/samber/lo v1.47.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1134,8 +1134,8 @@ github.com/rudderlabs/rudder-observability-kit v0.0.3 h1:vZtuZRkGX+6rjaeKtxxFE2Y
github.com/rudderlabs/rudder-observability-kit v0.0.3/go.mod h1:6UjAh3H6rkE0fFLh7z8ZGQEQbKtUkRfhWOf/OUhfqW8=
github.com/rudderlabs/rudder-schemas v0.5.1 h1:g4I5wp2yA6ZWQZ1MjSNn4zby3XctG/TOgbYUW3dS4z4=
github.com/rudderlabs/rudder-schemas v0.5.1/go.mod h1:JoDTB9nCDXwRz+G+aYwP3Fj42HLssKARxsFFm+qqgb4=
github.com/rudderlabs/rudder-transformer/go v0.0.0-20240812044419-23196ec42acf h1:nsU2tKjPV/sbmOoIk39ncFT8D5HBDVppmrCWO0v9HsU=
github.com/rudderlabs/rudder-transformer/go v0.0.0-20240812044419-23196ec42acf/go.mod h1:3NGitPz4pYRRZ6Xt09S+8hb0tHK/9pZcKJ3OgOTaSmE=
github.com/rudderlabs/rudder-transformer/go v0.0.0-20240904053719-1777c6cb23f9 h1:ecY7YIVRITu04y8fcRUdcTASlFUtzOtjhsyu8P6b4eY=
github.com/rudderlabs/rudder-transformer/go v0.0.0-20240904053719-1777c6cb23f9/go.mod h1:3NGitPz4pYRRZ6Xt09S+8hb0tHK/9pZcKJ3OgOTaSmE=
github.com/rudderlabs/sql-tunnels v0.1.7 h1:wDCRl6zY4M5gfWazf7XkSTGQS3yjBzUiUgEMBIfHNDA=
github.com/rudderlabs/sql-tunnels v0.1.7/go.mod h1:5f7+YL49JHYgteP4rAgqKnr4K2OadB0oIpUS+Tt3sPM=
github.com/rudderlabs/sqlconnect-go v1.9.0 h1:icLgqvVQ15Vh+oP7epA0b0yK6sIzxRVwPlRzOoDNVRA=
Expand Down
Loading