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(debugsymbolicator): Don't trace debug symbolicator source context Metro Dev Server requests #3553

Merged
merged 16 commits into from
Jan 31, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

- Prevent pod install crash when visionos is not present ([#3548](https://github.com/getsentry/sentry-react-native/pull/3548))
- Fetch Organization slug from `@sentry/react-native/expo` config when uploading artifacts ([#3557](https://github.com/getsentry/sentry-react-native/pull/3557))
- Remove 404 Http Client Errors reports for Metro Dev Server Requests ([#3553](https://github.com/getsentry/sentry-react-native/pull/3553))

## 5.17.0

Expand Down
31 changes: 24 additions & 7 deletions src/js/integrations/debugsymbolicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { addContextToFrame, logger } from '@sentry/utils';

import { getFramesToPop, isErrorLike } from '../utils/error';
import { ReactNativeLibraries } from '../utils/rnlibraries';
import { createStealthXhr, XHR_READYSTATE_DONE } from '../utils/xhr';
import type * as ReactNative from '../vendor/react-native';

const INTERNAL_CALLSITES_REGEX = new RegExp(['ReactNativeRenderer-dev\\.js$', 'MessageQueue\\.js$'].join('|'));
Expand Down Expand Up @@ -200,14 +201,30 @@ export class DebugSymbolicator implements Integration {
* Get source context for segment
*/
private async _fetchSourceContext(url: string, segments: Array<string>, start: number): Promise<string | null> {
const response = await fetch(`${url}${segments.slice(start).join('/')}`, {
method: 'GET',
});
return new Promise(resolve => {
const fullUrl = `${url}${segments.slice(start).join('/')}`;

if (response.ok) {
return response.text();
}
return null;
const xhr = createStealthXhr();
if (!xhr) {
resolve(null);
return;
}

xhr.open('GET', fullUrl, true);
xhr.send();

xhr.onreadystatechange = (): void => {
if (xhr.readyState === XHR_READYSTATE_DONE) {
if (xhr.status !== 200) {
resolve(null);
}
resolve(xhr.responseText);
}
};
xhr.onerror = (): void => {
resolve(null);
};
});
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/js/utils/worldwide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ReactNativeInternalGlobal extends InternalGlobal {
nativeFabricUIManager: unknown;
ErrorUtils?: ErrorUtils;
expo?: ExpoGlobalObject;
XMLHttpRequest?: typeof XMLHttpRequest;
}

/** Get's the global object for the current JavaScript runtime */
Expand Down
41 changes: 41 additions & 0 deletions src/js/utils/xhr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { RN_GLOBAL_OBJ } from './worldwide';

const __sentry_original__ = '__sentry_original__';

type XMLHttpRequestWithSentryOriginal = XMLHttpRequest & {
open: typeof XMLHttpRequest.prototype.open & { [__sentry_original__]?: typeof XMLHttpRequest.prototype.open };
send: typeof XMLHttpRequest.prototype.send & { [__sentry_original__]?: typeof XMLHttpRequest.prototype.send };
};

/**
* The DONE ready state for XmlHttpRequest
*
* Defining it here as a constant b/c XMLHttpRequest.DONE is not always defined
* (e.g. during testing, it is `undefined`)
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState}
*/
export const XHR_READYSTATE_DONE = 4;

/**
* Creates a new XMLHttpRequest object which is not instrumented by the SDK.
*
* This request won't be captured by the HttpClient Errors integration
* and won't be added to breadcrumbs and won't be traced.
*/
export function createStealthXhr(
customGlobal: { XMLHttpRequest?: typeof XMLHttpRequest } = RN_GLOBAL_OBJ,
): XMLHttpRequest | null {
if (!customGlobal.XMLHttpRequest) {
return null;
}

const xhr: XMLHttpRequestWithSentryOriginal = new customGlobal.XMLHttpRequest();
if (xhr.open.__sentry_original__) {
xhr.open = xhr.open.__sentry_original__;
}
if (xhr.send.__sentry_original__) {
xhr.send = xhr.send.__sentry_original__;
}
return xhr;
}
76 changes: 76 additions & 0 deletions test/utils/xhr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { createStealthXhr } from '../../src/js/utils/xhr';

describe('xhr', () => {
it('creates xhr and calls monkey patched methods if original was not preserved', () => {
const XMLHttpRequestMock = getXhrMock();
const globalMock = createGlobalMock(XMLHttpRequestMock);

const xhr = createStealthXhr(globalMock);

xhr!.open('GET', 'https://example.com');
xhr!.send();

expect(xhr!.open).toHaveBeenCalledWith('GET', 'https://example.com');
expect(xhr!.send).toHaveBeenCalled();
});

it('monkey patched xhr is not called when original is preserved', () => {
const XMLHttpRequestMock = getXhrMock();
const globalMock = createGlobalMock(XMLHttpRequestMock);

const { xhrOpenMonkeyPatch, xhrSendMonkeyPatch } = mockSentryPatchWithOriginal(globalMock);

const xhr = createStealthXhr(globalMock);

xhr!.open('GET', 'https://example.com');
xhr!.send();

expect(xhrOpenMonkeyPatch).not.toHaveBeenCalled();
expect(xhrSendMonkeyPatch).not.toHaveBeenCalled();
expect(xhr!.open).toHaveBeenCalledWith('GET', 'https://example.com');
expect(xhr!.send).toHaveBeenCalled();
});
});

function createGlobalMock(xhr: unknown) {
return {
XMLHttpRequest: xhr as typeof XMLHttpRequest,
};
}

function getXhrMock() {
function XhrMock() {}

XhrMock.prototype.open = jest.fn();
XhrMock.prototype.send = jest.fn();

return XhrMock;
}

type WithSentryOriginal<T> = T & { __sentry_original__?: T };

function mockSentryPatchWithOriginal(globalMock: { XMLHttpRequest: typeof XMLHttpRequest }): {
xhrOpenMonkeyPatch: jest.Mock;
xhrSendMonkeyPatch: jest.Mock;
} {
const originalOpen = globalMock.XMLHttpRequest.prototype.open;
const originalSend = globalMock.XMLHttpRequest.prototype.send;

const xhrOpenMonkeyPatch = jest.fn();
const xhrSendMonkeyPatch = jest.fn();

globalMock.XMLHttpRequest.prototype.open = xhrOpenMonkeyPatch;
globalMock.XMLHttpRequest.prototype.send = xhrSendMonkeyPatch;

(
globalMock.XMLHttpRequest.prototype.open as WithSentryOriginal<typeof XMLHttpRequest.prototype.open>
).__sentry_original__ = originalOpen;
(
globalMock.XMLHttpRequest.prototype.send as WithSentryOriginal<typeof XMLHttpRequest.prototype.send>
).__sentry_original__ = originalSend;

return {
xhrOpenMonkeyPatch,
xhrSendMonkeyPatch,
};
}
Loading