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(node): Remove Cookie header from event if cookies should not be sent to Sentry #5898

Merged
merged 1 commit into from Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions packages/node/src/requestdata.ts
Expand Up @@ -174,6 +174,12 @@ export function extractRequestData(
switch (key) {
case 'headers': {
requestData.headers = headers;

// Remove the Cookie header in case cookie data should not be included in the event
if (!include.includes('cookies')) {
delete (requestData.headers as { cookie?: string }).cookie;
}

break;
}
case 'method': {
Expand Down
31 changes: 31 additions & 0 deletions packages/node/test/requestdata.test.ts
Expand Up @@ -298,6 +298,37 @@ describe.each([oldExtractRequestData, newExtractRequestData])(
});
});

describe('headers', () => {
it('removes the `Cookie` header from requestdata.headers, if `cookies` is not set in the options', () => {
const mockReq = {
cookies: { foo: 'bar' },
headers: { cookie: 'foo=bar', otherHeader: 'hello' },
};
const optionsWithCookies = ['headers'];

const [req, options] = formatArgs(fn, mockReq, optionsWithCookies);

expect(fn(req, options as any)).toStrictEqual({
headers: { otherHeader: 'hello' },
});
});

it('includes the `Cookie` header in requestdata.headers, if `cookies` is not set in the options', () => {
const mockReq = {
cookies: { foo: 'bar' },
headers: { cookie: 'foo=bar', otherHeader: 'hello' },
};
const optionsWithCookies = ['headers', 'cookies'];

const [req, options] = formatArgs(fn, mockReq, optionsWithCookies);

expect(fn(req, options as any)).toStrictEqual({
headers: { otherHeader: 'hello', cookie: 'foo=bar' },
cookies: { foo: 'bar' },
});
});
});

describe('cookies', () => {
it('uses `req.cookies` if available', () => {
const mockReq = {
Expand Down