From e54eedbad56fb2895679a2776031a7be39f8d1f2 Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Thu, 25 May 2023 20:15:23 +0300 Subject: [PATCH 1/4] Keep the recent `XMLHttpRequest` methods on disable --- src/utils/XhrNetworkInterceptor.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/utils/XhrNetworkInterceptor.ts b/src/utils/XhrNetworkInterceptor.ts index 86e19e9b16..ad79f2d4fa 100644 --- a/src/utils/XhrNetworkInterceptor.ts +++ b/src/utils/XhrNetworkInterceptor.ts @@ -24,9 +24,9 @@ export interface NetworkData { } const XMLHttpRequest = global.XMLHttpRequest; -const originalXHROpen = XMLHttpRequest.prototype.open; -const originalXHRSend = XMLHttpRequest.prototype.send; -const originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; +let originalXHROpen = XMLHttpRequest.prototype.open; +let originalXHRSend = XMLHttpRequest.prototype.send; +let originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; let onProgressCallback: ProgressCallback | null; let onDoneCallback: NetworkDataCallback | null; @@ -63,6 +63,15 @@ export default { onProgressCallback = callback; }, enableInterception() { + // Prevents infinite calls to XMLHttpRequest.open when enabling interception multiple times + if (isInterceptorEnabled) { + return; + } + + originalXHROpen = XMLHttpRequest.prototype.open; + originalXHRSend = XMLHttpRequest.prototype.send; + originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; + XMLHttpRequest.prototype.open = function (method, url, ...args) { _reset(); network.url = url; From cd6bf04285a80380454b27c78ce463d1abb9f08f Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Thu, 25 May 2023 20:15:40 +0300 Subject: [PATCH 2/4] Add unit tests --- test/utils/XhrNetworkInterceptor.spec.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/utils/XhrNetworkInterceptor.spec.ts b/test/utils/XhrNetworkInterceptor.spec.ts index 5258d533bb..a34f79c031 100644 --- a/test/utils/XhrNetworkInterceptor.spec.ts +++ b/test/utils/XhrNetworkInterceptor.spec.ts @@ -29,6 +29,29 @@ describe('Network Interceptor', () => { FakeRequest.send(); }); + it('should call patched XMLHttpRequest methods', () => { + Interceptor.disableInterception(); + + // Patch XMLHttpRequest.open + const originalXHROpen = XMLHttpRequest.prototype.open; + const patchedCode = jest.fn(); + XMLHttpRequest.prototype.open = function (...args: Parameters) { + patchedCode(); + originalXHROpen.apply(this, args); + }; + + // Enable and disable network interception to see if disabling network interception + // keeps the patched XMLHttpRequest methods + Interceptor.enableInterception(); + Interceptor.disableInterception(); + + FakeRequest.open(method, url); + + expect(patchedCode).toHaveBeenCalledTimes(1); + + XMLHttpRequest.prototype.open = originalXHROpen; + }); + it('should set network object on calling setRequestHeader', (done) => { const requestHeaders = { 'content-type': 'application/json', token: '9u4hiudhi3bf' }; From 767f3aa29bf7da9de653779477617dd60755a3b0 Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Thu, 25 May 2023 20:39:37 +0300 Subject: [PATCH 3/4] Change unit test title --- test/utils/XhrNetworkInterceptor.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/utils/XhrNetworkInterceptor.spec.ts b/test/utils/XhrNetworkInterceptor.spec.ts index a34f79c031..b014bc9d9c 100644 --- a/test/utils/XhrNetworkInterceptor.spec.ts +++ b/test/utils/XhrNetworkInterceptor.spec.ts @@ -29,7 +29,7 @@ describe('Network Interceptor', () => { FakeRequest.send(); }); - it('should call patched XMLHttpRequest methods', () => { + it('should keep patched XMLHttpRequest methods', () => { Interceptor.disableInterception(); // Patch XMLHttpRequest.open From aa42fb33f00eac26d44131b7abe1c2837c136ba0 Mon Sep 17 00:00:00 2001 From: Ahmed Mahmoud Date: Thu, 25 May 2023 20:54:04 +0300 Subject: [PATCH 4/4] Update Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65b999032b..d6d38c347d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Fix an issue with unhandled JavaScript crashes being reported as native Android crashes ([#980](https://github.com/Instabug/Instabug-React-Native/pull/980)). - Fix an issue with the Android sourcemaps upload script, causing the build to fail on older versions of Gradle ([#970](https://github.com/Instabug/Instabug-React-Native/pull/970)), closes [#969](https://github.com/Instabug/Instabug-React-Native/issues/969). - Fix an issue with the Android sourcemaps upload script, causing the build to fail when using product flavors ([#975](https://github.com/Instabug/Instabug-React-Native/pull/975)), closes [#974](https://github.com/Instabug/Instabug-React-Native/issues/974). +- Fix an issue with the network interceptor reverting the user's changes to `XMLHttpRequest` after disabling network logging ([#984](https://github.com/Instabug/Instabug-React-Native/pull/984)), closes [#981](https://github.com/Instabug/Instabug-React-Native/issues/981). ## [11.10.0](https://github.com/Instabug/Instabug-React-Native/compare/v11.9.1...11.10.0) (April 20, 2023)