Skip to content

Commit 34163fc

Browse files
jordienrclaude
andauthored
fix(studio): duplicate Content-Type on webhook log drains (supabase#46673)
## Problem Webhook log drains (project and org/audit) deliver requests with a malformed, duplicated `Content-Type: application/jsonapplication/json` header. On at least some receivers this breaks body parsing, so the delivered body appears empty even though it is present (confirmed with gzip both on and off). Root cause: the create form seeds a default `Content-Type: application/json` header for webhook drains, and the logflare webhook adaptor's `Tesla.Middleware.JSON` also sets `content-type: application/json` when it encodes the body. Both are sent, and the receiver concatenates the two same-named headers. ## Fix Stop seeding `Content-Type` in the webhook default headers (`getDefaultHeadersByType`). The delivery side already sets it, so a single clean header is sent. OTLP keeps its `application/x-protobuf` default because the OTLP delivery path uses `json: false` and does not set a content type itself. Updated the form tests that assumed the seeded header (the added-header row is now index 0 instead of 1, and the duplicate-header test now adds two explicit rows). ## How to test - Create a webhook (Custom Endpoint) audit log drain pointing at a request bin. - Trigger an audit event and inspect the delivered request: `Content-Type` should be a single `application/json`, and the JSON body should be visible. ## Note This fixes the common case (the seeded default). A user who manually adds a `Content-Type` header to a webhook drain would still hit the duplication; the robust cross-team fix would be for the logflare webhook adaptor to drop an incoming `content-type` before its JSON middleware sets one. Flagging for the logs team. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Log Drain header handling corrected: webhook drains no longer add a default Content-Type; other drain types retain their appropriate defaults. Empty header rows are no longer submitted. * **Tests** * Updated tests to match new header indexing, validation behavior, and submission expectations. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 2cb7f0c commit 34163fc

3 files changed

Lines changed: 18 additions & 18 deletions

File tree

apps/studio/components/interfaces/LogDrains/LogDrainDestinationSheetForm.test.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ describe('LogDrainDestinationSheetForm', () => {
8585
useTrackMock.mockReturnValue(trackMock)
8686
})
8787

88-
it('shows the JSON content type header for webhook create mode', async () => {
88+
it('does not prefill a Content-Type header for webhook create mode', async () => {
8989
renderForm()
9090

9191
await screen.findByRole('dialog')
9292

93-
expect(screen.getByDisplayValue('Content-Type')).toBeInTheDocument()
94-
expect(screen.getByDisplayValue('application/json')).toBeInTheDocument()
93+
expect(screen.queryByDisplayValue('Content-Type')).not.toBeInTheDocument()
94+
expect(screen.queryByDisplayValue('application/json')).not.toBeInTheDocument()
9595
})
9696

9797
it('wraps the save button with the save destination shortcut while open', async () => {
@@ -169,8 +169,8 @@ describe('LogDrainDestinationSheetForm', () => {
169169
const headerNameInputs = screen.getAllByPlaceholderText('Header name')
170170
const headerValueInputs = screen.getAllByPlaceholderText('Header value')
171171

172-
await user.type(headerNameInputs[1], 'X-API-Key')
173-
await user.type(headerValueInputs[1], 'secret-key')
172+
await user.type(headerNameInputs[0], 'X-API-Key')
173+
await user.type(headerValueInputs[0], 'secret-key')
174174

175175
submitForm()
176176

@@ -179,7 +179,6 @@ describe('LogDrainDestinationSheetForm', () => {
179179
expect.objectContaining({
180180
type: 'webhook',
181181
headers: {
182-
'Content-Type': 'application/json',
183182
'X-API-Key': 'secret-key',
184183
},
185184
})
@@ -199,12 +198,15 @@ describe('LogDrainDestinationSheetForm', () => {
199198
'https://logs.example.com/ingest'
200199
)
201200
await user.click(screen.getByRole('button', { name: 'Add a new header' }))
201+
await user.click(screen.getByRole('button', { name: 'Add a new header' }))
202202

203203
const headerNameInputs = screen.getAllByPlaceholderText('Header name')
204204
const headerValueInputs = screen.getAllByPlaceholderText('Header value')
205205

206-
await user.type(headerNameInputs[1], 'Content-Type')
207-
await user.type(headerValueInputs[1], 'application/custom')
206+
await user.type(headerNameInputs[0], 'X-Custom')
207+
await user.type(headerValueInputs[0], 'one')
208+
await user.type(headerNameInputs[1], 'X-Custom')
209+
await user.type(headerValueInputs[1], 'two')
208210

209211
submitForm()
210212

@@ -231,11 +233,9 @@ describe('LogDrainDestinationSheetForm', () => {
231233
expect(onSubmit).toHaveBeenCalledWith(
232234
expect.objectContaining({
233235
type: 'webhook',
234-
headers: {
235-
'Content-Type': 'application/json',
236-
},
237236
})
238237
)
238+
expect(onSubmit.mock.calls[0][0]).not.toHaveProperty('headers')
239239
expect(screen.queryByText('undefined')).not.toBeInTheDocument()
240240
})
241241

@@ -253,7 +253,7 @@ describe('LogDrainDestinationSheetForm', () => {
253253
await user.click(screen.getByRole('button', { name: 'Add a new header' }))
254254

255255
const headerNameInputs = screen.getAllByPlaceholderText('Header name')
256-
await user.type(headerNameInputs[1], 'X-Draft-Only')
256+
await user.type(headerNameInputs[0], 'X-Draft-Only')
257257

258258
submitForm()
259259

@@ -276,7 +276,7 @@ describe('LogDrainDestinationSheetForm', () => {
276276
await user.click(screen.getByRole('button', { name: 'Add a new header' }))
277277

278278
const headerValueInputs = screen.getAllByPlaceholderText('Header value')
279-
await user.type(headerValueInputs[1], 'draft-value')
279+
await user.type(headerValueInputs[0], 'draft-value')
280280

281281
submitForm()
282282

apps/studio/components/interfaces/LogDrains/LogDrains.utils.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ describe('getHeadersSectionDescription', () => {
3636
})
3737

3838
describe('getDefaultHeadersByType', () => {
39-
it('returns the JSON content type header for webhook destinations', () => {
40-
expect(getDefaultHeadersByType('webhook')).toEqual({
41-
'Content-Type': 'application/json',
42-
})
39+
it('does not return a default Content-Type header for webhook destinations', () => {
40+
expect(getDefaultHeadersByType('webhook')).toEqual({})
4341
})
4442

4543
it('returns the protobuf content type header for OTLP destinations', () => {

apps/studio/components/interfaces/LogDrains/LogDrains.utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ export const HEADER_VALIDATION_ERRORS = {
4040
VALUE_REQUIRED: 'Header value is required',
4141
} as const
4242

43+
// Webhook drains intentionally omit a Content-Type default: the delivery side already sets
44+
// `application/json`, and seeding it here produces a duplicated Content-Type header that can
45+
// break body parsing on the receiver. OTLP needs it since its delivery does not set one.
4346
const DEFAULT_HEADERS_BY_TYPE: Partial<Record<LogDrainType, Record<string, string>>> = {
44-
webhook: { 'Content-Type': 'application/json' },
4547
otlp: { 'Content-Type': 'application/x-protobuf' },
4648
}
4749

0 commit comments

Comments
 (0)