From 76400d6908e0a38d9b29f91f7d1527a291de1b18 Mon Sep 17 00:00:00 2001 From: Eoghan Murray Date: Wed, 10 Jul 2024 14:09:31 +0100 Subject: [PATCH] Fix that we would have tried to capture an asset with a malformed url with { origins: true } --- packages/rrweb-snapshot/src/utils.ts | 13 ++++++------- packages/rrweb-snapshot/test/utils.test.ts | 3 +++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/rrweb-snapshot/src/utils.ts b/packages/rrweb-snapshot/src/utils.ts index d01ad91d7d..69d0c83925 100644 --- a/packages/rrweb-snapshot/src/utils.ts +++ b/packages/rrweb-snapshot/src/utils.ts @@ -550,18 +550,17 @@ export function shouldIgnoreAsset( for (const origin of originsToIgnore) { if (url.startsWith(origin)) return true; } - + let urlOrigin; + try { + urlOrigin = new URL(url).origin; + } catch (e) { + return true; // something went wrong, ignore! + } // Check the origins const captureOrigins = config.origins; if (typeof captureOrigins === 'boolean') { return !captureOrigins; } else if (Array.isArray(captureOrigins)) { - let urlOrigin; - try { - urlOrigin = new URL(url).origin; - } catch (e) { - return true; // something went wrong, ignore! - } return !captureOrigins.includes(urlOrigin); } return true; // no config, ignore! diff --git a/packages/rrweb-snapshot/test/utils.test.ts b/packages/rrweb-snapshot/test/utils.test.ts index 059972fc6a..d744794221 100644 --- a/packages/rrweb-snapshot/test/utils.test.ts +++ b/packages/rrweb-snapshot/test/utils.test.ts @@ -311,6 +311,9 @@ describe('utils', () => { shouldIgnoreAsset('http:', { origins: ['http://example.com'] }), ).toBe(true); }); + + it(`should ignore malformed url even with origins: true`, () => { + expect(shouldIgnoreAsset('http:', { origins: true })).toBe(true); }); });