Skip to content

Commit 7a45242

Browse files
committed
fix some cases of '' and null host handling in the URL polyfill
1 parent 2af6c38 commit 7a45242

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- Fixed possible removal of unnecessary entries in `URLSearchParam.prototype.delete` polyfill with second argument
2626
- Fixed an error in some cases of non-special URLs without a path in the `URL` polyfill
2727
- Fixed some percent encode cases / character sets in the `URL` polyfill
28+
- Fixed some cases of `''` and `null` host handling in the `URL` polyfill
2829
- Fixed host parsing with `hostname = host:port` in the `URL` polyfill
2930
- Fixed invalid code points handling in UTF-8 decode in the `URLSearchParams` polyfill
3031
- Fixed some cases of serialization in `URL` polyfill (`/.` prefix for non-special URLs with `null` host and path starting with empty segment)

packages/core-js/modules/web.url.constructor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ URLState.prototype = {
396396
if (stateOverride && (
397397
(url.isSpecial() !== hasOwn(specialSchemes, buffer)) ||
398398
(buffer === 'file' && (url.includesCredentials() || url.port !== null)) ||
399-
(url.scheme === 'file' && !url.host)
399+
(url.scheme === 'file' && url.host === '')
400400
)) return;
401401
url.scheme = buffer;
402402
if (stateOverride) {
@@ -700,7 +700,7 @@ URLState.prototype = {
700700
}
701701
} else {
702702
if (url.scheme === 'file' && !url.path.length && isWindowsDriveLetter(buffer)) {
703-
if (url.host) url.host = '';
703+
if (url.host !== null && url.host !== '') url.host = '';
704704
buffer = charAt(buffer, 0) + ':'; // normalize windows drive letter
705705
}
706706
push(url.path, buffer);
@@ -776,7 +776,7 @@ URLState.prototype = {
776776
},
777777
// https://url.spec.whatwg.org/#cannot-have-a-username-password-port
778778
cannotHaveUsernamePasswordPort: function () {
779-
return !this.host || this.cannotBeABaseURL || this.scheme === 'file';
779+
return this.host === null || this.host === '' || this.cannotBeABaseURL || this.scheme === 'file';
780780
},
781781
// https://url.spec.whatwg.org/#include-credentials
782782
includesCredentials: function () {

tests/unit-global/web.url.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ QUnit.test('URL#username', assert => {
221221
url.username = 'username';
222222
assert.same(url.username, 'username');
223223
assert.same(String(url), 'http://username@zloirock.ru/');
224+
225+
// IPv4 address 0.0.0.0 (stored as number 0) should allow username
226+
url = new URL('http://0.0.0.0/');
227+
url.username = 'user';
228+
assert.same(url.username, 'user', 'username settable on 0.0.0.0');
229+
assert.same(String(url), 'http://user@0.0.0.0/', 'href correct after setting username on 0.0.0.0');
224230
}
225231
});
226232

@@ -443,6 +449,12 @@ QUnit.test('URL#port', assert => {
443449
// url.port = 1e10;
444450
// assert.same(url.port, '1234'); // '0' in Chrome
445451
// assert.same(String(url), 'http://zloirock.ru:1234/'); // 'http://zloirock.ru:0/' in Chrome
452+
453+
// IPv4 address 0.0.0.0 (stored as number 0) should allow port
454+
url = new URL('http://0.0.0.0/');
455+
url.port = '8080';
456+
assert.same(url.port, '8080', 'port settable on 0.0.0.0');
457+
assert.same(String(url), 'http://0.0.0.0:8080/', 'href correct after setting port on 0.0.0.0');
446458
}
447459
});
448460

tests/unit-pure/web.url.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ QUnit.test('URL#username', assert => {
222222
url.username = 'username';
223223
assert.same(url.username, 'username');
224224
assert.same(String(url), 'http://username@zloirock.ru/');
225+
226+
// IPv4 address 0.0.0.0 (stored as number 0) should allow username
227+
url = new URL('http://0.0.0.0/');
228+
url.username = 'user';
229+
assert.same(url.username, 'user', 'username settable on 0.0.0.0');
230+
assert.same(String(url), 'http://user@0.0.0.0/', 'href correct after setting username on 0.0.0.0');
225231
}
226232
});
227233

@@ -444,6 +450,12 @@ QUnit.test('URL#port', assert => {
444450
// url.port = 1e10;
445451
// assert.same(url.port, '1234'); // '0' in Chrome
446452
// assert.same(String(url), 'http://zloirock.ru:1234/'); // 'http://zloirock.ru:0/' in Chrome
453+
454+
// IPv4 address 0.0.0.0 (stored as number 0) should allow port
455+
url = new URL('http://0.0.0.0/');
456+
url.port = '8080';
457+
assert.same(url.port, '8080', 'port settable on 0.0.0.0');
458+
assert.same(String(url), 'http://0.0.0.0:8080/', 'href correct after setting port on 0.0.0.0');
447459
}
448460
});
449461

0 commit comments

Comments
 (0)