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(node) Sanitize URLs in Span descriptions and breadcrumbs (PII) #7206

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 25 additions & 5 deletions packages/node/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,28 @@ function _createWrappedRequestMethodFactory(

const scope = getCurrentHub().getScope();

const spanData: Record<string, string> = {
aldenquimby marked this conversation as resolved.
Show resolved Hide resolved
url: requestUrl,
method: requestOptions.method || 'GET',
};
if (requestOptions.hash) {
// strip leading "#"
aldenquimby marked this conversation as resolved.
Show resolved Hide resolved
spanData['http.fragment'] = requestOptions.hash.substring(1);
}
if (requestOptions.search) {
// strip leading "?"
spanData['http.query'] = requestOptions.search.substring(1);
}

// TODO potential breaking change... shouldCreateSpanForRequest no longer has access to query + fragment, is that a problem?
aldenquimby marked this conversation as resolved.
Show resolved Hide resolved
if (scope && tracingOptions && shouldCreateSpan(requestUrl)) {
parentSpan = scope.getSpan();

if (parentSpan) {
requestSpan = parentSpan.startChild({
description: `${requestOptions.method || 'GET'} ${requestUrl}`,
description: `${spanData.method} ${spanData.url}`,
op: 'http.client',
data: spanData,
});

if (shouldAttachTraceData(requestUrl)) {
Expand Down Expand Up @@ -253,7 +268,7 @@ function _createWrappedRequestMethodFactory(
// eslint-disable-next-line @typescript-eslint/no-this-alias
const req = this;
if (breadcrumbsEnabled) {
addRequestBreadcrumb('response', requestUrl, req, res);
addRequestBreadcrumb('response', spanData, req, res);
}
if (requestSpan) {
if (res.statusCode) {
Expand All @@ -268,7 +283,7 @@ function _createWrappedRequestMethodFactory(
const req = this;

if (breadcrumbsEnabled) {
addRequestBreadcrumb('error', requestUrl, req);
addRequestBreadcrumb('error', spanData, req);
}
if (requestSpan) {
requestSpan.setHttpStatus(500);
Expand All @@ -283,7 +298,12 @@ function _createWrappedRequestMethodFactory(
/**
* Captures Breadcrumb based on provided request/response pair
*/
function addRequestBreadcrumb(event: string, url: string, req: http.ClientRequest, res?: http.IncomingMessage): void {
function addRequestBreadcrumb(
event: string,
spanData: Record<string, string>,
req: http.ClientRequest,
res?: http.IncomingMessage,
): void {
if (!getCurrentHub().getIntegration(Http)) {
return;
}
Expand All @@ -294,7 +314,7 @@ function addRequestBreadcrumb(event: string, url: string, req: http.ClientReques
data: {
method: req.method,
status_code: res && res.statusCode,
url,
...spanData,
},
type: 'http',
},
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/integrations/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export function extractUrl(requestOptions: RequestOptions): string {
// Don't log standard :80 (http) and :443 (https) ports to reduce the noise
const port =
!requestOptions.port || requestOptions.port === 80 || requestOptions.port === 443 ? '' : `:${requestOptions.port}`;
const path = requestOptions.path ? requestOptions.path : '/';
// do not include search or hash in span descriptions, per https://github.com/getsentry/develop/pull/773
aldenquimby marked this conversation as resolved.
Show resolved Hide resolved
const path = requestOptions.pathname || '/';

return `${protocol}//${hostname}${port}${path}`;
}
Expand Down
33 changes: 33 additions & 0 deletions packages/node/test/integrations/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,39 @@ describe('tracing', () => {
expect(loggerLogSpy).toBeCalledWith('HTTP Integration is skipped because of instrumenter configuration.');
});

it('omits query and fragment from description and adds to span data instead', () => {
nock('http://dogs.are.great').get('/spaniel?tail=wag&cute=true#learn-more').reply(200);

const transaction = createTransactionOnScope();
const spans = (transaction as unknown as Span).spanRecorder?.spans as Span[];

http.get('http://dogs.are.great/spaniel?tail=wag&cute=true#learn-more');

expect(spans.length).toEqual(2);

// our span is at index 1 because the transaction itself is at index 0
expect(spans[1].description).toEqual('GET http://dogs.are.great/spaniel');
expect(spans[1].op).toEqual('http.client');
expect(spans[1].data.method).toEqual('GET');
expect(spans[1].data.url).toEqual('http://dogs.are.great/spaniel');
expect(spans[1].data['http.query']).toEqual('tail=wag&cute=true');
expect(spans[1].data['http.fragment']).toEqual('learn-more');
});

it('omits username and password entirely from span', () => {
aldenquimby marked this conversation as resolved.
Show resolved Hide resolved
aldenquimby marked this conversation as resolved.
Show resolved Hide resolved
nock('http://username:[email protected]').get('/').reply(200);

const transaction = createTransactionOnScope();
const spans = (transaction as unknown as Span).spanRecorder?.spans as Span[];

http.get('http://username:[email protected]/');

expect(spans.length).toEqual(2);

// our span is at index 1 because the transaction itself is at index 0
expect(spans[1].description).toEqual('GET http://dogs.are.great/');
aldenquimby marked this conversation as resolved.
Show resolved Hide resolved
});

describe('Tracing options', () => {
beforeEach(() => {
// hacky way of restoring monkey patched functions
Expand Down