From d4bd0aaf6ff390b2a3c800cc06a8ebdb007fe467 Mon Sep 17 00:00:00 2001 From: Aaron Loe Date: Thu, 18 Dec 2025 16:15:56 -0800 Subject: [PATCH] fix: browser check for user agent use standard way of checking for browser runtime Ticket: CE-8933 --- modules/sdk-api/src/bitgoAPI.ts | 2 +- modules/sdk-api/test/unit/bitgoAPI.ts | 73 +++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/modules/sdk-api/src/bitgoAPI.ts b/modules/sdk-api/src/bitgoAPI.ts index 6867e9330a..f1e126b71a 100644 --- a/modules/sdk-api/src/bitgoAPI.ts +++ b/modules/sdk-api/src/bitgoAPI.ts @@ -413,7 +413,7 @@ export class BitGoAPI implements BitGoBase { // prevent IE from caching requests req.set('If-Modified-Since', 'Mon, 26 Jul 1997 05:00:00 GMT'); - if (!(process as any).browser && this._userAgent) { + if (typeof window === 'undefined' && this._userAgent) { // If not in the browser, set the User-Agent. Browsers don't allow // setting of User-Agent, so we must disable this when run in the // browser (browserify sets process.browser). diff --git a/modules/sdk-api/test/unit/bitgoAPI.ts b/modules/sdk-api/test/unit/bitgoAPI.ts index 6b2f206d9d..626f79fdde 100644 --- a/modules/sdk-api/test/unit/bitgoAPI.ts +++ b/modules/sdk-api/test/unit/bitgoAPI.ts @@ -408,6 +408,79 @@ describe('Constructor', function () { }); }); + describe('User-Agent header based on environment', function () { + afterEach(function () { + nock.cleanAll(); + sinon.restore(); + }); + + it('should set User-Agent header when running in Node.js (typeof window === undefined)', async function () { + const bitgo = new BitGoAPI({ + env: 'custom', + customRootURI: 'https://app.example.local', + userAgent: 'TestAgent/1.0', + }); + + // Ensure we're in a Node.js environment by verifying window is undefined + (typeof window).should.equal('undefined'); + + const scope = nock('https://app.example.local') + .get('/api/v1/ping') + .matchHeader('User-Agent', 'TestAgent/1.0') + .reply(200, { status: 'ok' }); + + await bitgo.ping({ + reqId: { + toString: () => 'test-123', + inc: () => { + /* mock */ + }, + } as any, + }); + + scope.isDone().should.be.true(); + }); + + it('should not set User-Agent header when running in browser (typeof window !== undefined)', async function () { + // Mock the window object to simulate browser environment + const windowStub = { location: 'mock' }; + (global as any).window = windowStub; + + try { + const bitgo = new BitGoAPI({ + env: 'custom', + customRootURI: 'https://app.example.local', + userAgent: 'TestAgent/1.0', + }); + + const scope = nock('https://app.example.local') + .get('/api/v1/ping') + .reply(function () { + // Verify User-Agent header is NOT set to our custom value + const userAgent = this.req.headers['user-agent']; + if (userAgent && userAgent.includes('TestAgent/1.0')) { + throw new Error('User-Agent should not be set in browser environment'); + } + return [200, { status: 'ok' }]; + }); + + await bitgo.ping({ + reqId: { + toString: () => 'test-123', + inc: () => { + /* mock */ + }, + } as any, + }); + + scope.isDone().should.be.true(); + } finally { + // Clean up the global window mock + delete (global as any).window; + } + }); + }); + describe('constants parameter', function () { it('should allow passing constants via options and expose via fetchConstants', async function () { const bitgo = new BitGoAPI({