Skip to content
Draft
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
8 changes: 6 additions & 2 deletions packages/remix/src/server/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ export async function errorHandleDataFunction(
return handleCallbackErrors(
async () => {
if (name === 'action' && span) {
const options = getClient()?.getOptions() as RemixOptions | undefined;
const client = getClient();
const options = client?.getOptions() as RemixOptions | undefined;

if (options?.sendDefaultPii && options.captureActionFormDataKeys) {
if (
client?.getDataCollectionOptions().httpBodies.includes('incomingRequest') &&
options?.captureActionFormDataKeys
) {
await storeFormDataKeys(args, span, options.captureActionFormDataKeys);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/remix/src/server/instrumentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ function wrapRequestHandler<T extends ServerBuild | (() => ServerBuild | Promise
method: request.method,
...httpHeadersToSpanAttributes(
winterCGHeadersToDict(request.headers),
clientOptions.sendDefaultPii ?? false,
getClient()?.getDataCollectionOptions(),
),
},
},
Expand Down
4 changes: 3 additions & 1 deletion packages/remix/src/server/integrations/opentelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const _remixIntegration = (() => {
const options = client?.getOptions() as RemixOptions | undefined;

instrumentRemix({
actionFormDataAttributes: options?.sendDefaultPii ? options?.captureActionFormDataKeys : undefined,
actionFormDataAttributes: client?.getDataCollectionOptions().httpBodies.includes('incomingRequest')
? options?.captureActionFormDataKeys
: undefined,
});
},

Expand Down
77 changes: 77 additions & 0 deletions packages/remix/test/server/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { Client } from '@sentry/core';
import * as core from '@sentry/core';

vi.mock('../../src/utils/utils', () => ({
storeFormDataKeys: vi.fn(),
}));

import { storeFormDataKeys } from '../../src/utils/utils';
import { errorHandleDataFunction } from '../../src/server/errors';

function createMockClient(httpBodies: string[] = []): Client {
return {
getDataCollectionOptions: () => ({
userInfo: false,
cookies: true,
httpHeaders: { request: true, response: true },
httpBodies,
queryParams: true,
genAI: { inputs: true, outputs: true },
stackFrameVariables: true,
frameContextLines: 5,
}),
getOptions: () => ({
captureActionFormDataKeys: { username: true },
}),
} as unknown as Client;
}

describe('errorHandleDataFunction', () => {
beforeEach(() => {
vi.clearAllMocks();
});

afterEach(() => {
vi.restoreAllMocks();
});

it('captures form data when httpBodies includes incomingRequest', async () => {
vi.spyOn(core, 'getClient').mockReturnValue(createMockClient(['incomingRequest']));
vi.spyOn(core, 'handleCallbackErrors').mockImplementation(async fn => fn());

const mockSpan = { setAttribute: vi.fn() } as any;
const mockArgs = { request: new Request('http://localhost', { method: 'POST' }) } as any;
const origFn = vi.fn().mockResolvedValue(new Response());

await errorHandleDataFunction.call(null, origFn, 'action', mockArgs, mockSpan);

expect(storeFormDataKeys).toHaveBeenCalledWith(mockArgs, mockSpan, { username: true });
});

it('does NOT capture form data when httpBodies is empty', async () => {
vi.spyOn(core, 'getClient').mockReturnValue(createMockClient([]));
vi.spyOn(core, 'handleCallbackErrors').mockImplementation(async fn => fn());

const mockSpan = { setAttribute: vi.fn() } as any;
const mockArgs = { request: new Request('http://localhost', { method: 'POST' }) } as any;
const origFn = vi.fn().mockResolvedValue(new Response());

await errorHandleDataFunction.call(null, origFn, 'action', mockArgs, mockSpan);

expect(storeFormDataKeys).not.toHaveBeenCalled();
});

it('does NOT capture form data for loader functions', async () => {
vi.spyOn(core, 'getClient').mockReturnValue(createMockClient(['incomingRequest']));
vi.spyOn(core, 'handleCallbackErrors').mockImplementation(async fn => fn());

const mockSpan = { setAttribute: vi.fn() } as any;
const mockArgs = { request: new Request('http://localhost') } as any;
const origFn = vi.fn().mockResolvedValue(new Response());

await errorHandleDataFunction.call(null, origFn, 'loader', mockArgs, mockSpan);

expect(storeFormDataKeys).not.toHaveBeenCalled();
});
});
Loading