Skip to content

Commit a9c18a6

Browse files
author
Nedunchezhiyan-M
committed
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.
1 parent 8bac1df commit a9c18a6

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

packages/react-native/Libraries/Blob/URL.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class URL {
8585
const split = this._url.split('#');
8686
const beforeHash = split[0];
8787
const website = beforeHash.split('://')[1];
88-
if (!website.includes('/')) {
88+
if (website != null && !website.includes('/')) {
8989
this._url = split.join('/#');
9090
}
9191
}

packages/react-native/Libraries/Blob/__tests__/URL-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,9 @@ describe('URL', function () {
168168
'https://example.com/path/to/resource',
169169
);
170170
});
171+
172+
it('should handle URLs with hash but no protocol separator', () => {
173+
// URL with hash but no "://" should not crash
174+
expect(() => new URL('mailto:user@example.com#section')).not.toThrow();
175+
});
171176
});

0 commit comments

Comments
 (0)