From a9c18a60ebb2dd72f29f705ac88995b54adf3b86 Mon Sep 17 00:00:00 2001 From: Nedunchezhiyan-M Date: Wed, 8 Apr 2026 23:16:06 +0530 Subject: [PATCH] Fix crash in URL constructor when URL has hash but no protocol The URL constructor crashes when parsing a URL that contains a hash but no "://" separator (e.g. "mailto:user@example.com#section"). The split on "://" returns undefined for index [1], and calling .includes('/') on undefined throws a TypeError. Added a null check before accessing the split result. --- packages/react-native/Libraries/Blob/URL.js | 2 +- packages/react-native/Libraries/Blob/__tests__/URL-test.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/react-native/Libraries/Blob/URL.js b/packages/react-native/Libraries/Blob/URL.js index f4f879a51214..236e3ce5a666 100644 --- a/packages/react-native/Libraries/Blob/URL.js +++ b/packages/react-native/Libraries/Blob/URL.js @@ -85,7 +85,7 @@ export class URL { const split = this._url.split('#'); const beforeHash = split[0]; const website = beforeHash.split('://')[1]; - if (!website.includes('/')) { + if (website != null && !website.includes('/')) { this._url = split.join('/#'); } } diff --git a/packages/react-native/Libraries/Blob/__tests__/URL-test.js b/packages/react-native/Libraries/Blob/__tests__/URL-test.js index 72467dc08ebb..cc597a1e780a 100644 --- a/packages/react-native/Libraries/Blob/__tests__/URL-test.js +++ b/packages/react-native/Libraries/Blob/__tests__/URL-test.js @@ -168,4 +168,9 @@ describe('URL', function () { 'https://example.com/path/to/resource', ); }); + + it('should handle URLs with hash but no protocol separator', () => { + // URL with hash but no "://" should not crash + expect(() => new URL('mailto:user@example.com#section')).not.toThrow(); + }); });