Skip to content

Commit ef45a13

Browse files
authoredFeb 16, 2022
[fix] Readd the empty userinfo to url.href (#226)
If the userinfo is present but empty, the parsed host is also empty, and `url.pathname` is not `'/'`, then readd the empty userinfo to `url.href`, otherwise the original invalid URL might be transformed into a valid one with `url.pathname` as host.
1 parent 88df234 commit ef45a13

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
 

‎index.js

+11
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,17 @@ function toString(stringify) {
539539
} else if (url.password) {
540540
result += ':'+ url.password;
541541
result += '@';
542+
} else if (
543+
url.protocol !== 'file:' &&
544+
isSpecial(url.protocol) &&
545+
!url.host &&
546+
url.pathname !== '/'
547+
) {
548+
//
549+
// Add back the empty userinfo, otherwise the original invalid URL
550+
// might be transformed into a valid one with `url.pathname` as host.
551+
//
552+
result += '@';
542553
}
543554

544555
result += url.host + url.pathname;

‎test/test.js

+59
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,65 @@ describe('url-parse', function () {
771771
assume(parsed.pathname).equals('/');
772772
assume(parsed.href).equals('http://user%40:pas%3As%40@www.example.com/');
773773
});
774+
775+
it('adds @ to href if auth and host are empty', function () {
776+
var parsed, i = 0;
777+
var urls = [
778+
'http:@/127.0.0.1',
779+
'http::@/127.0.0.1',
780+
'http:/@/127.0.0.1',
781+
'http:/:@/127.0.0.1',
782+
'http://@/127.0.0.1',
783+
'http://:@/127.0.0.1',
784+
'http:///@/127.0.0.1',
785+
'http:///:@/127.0.0.1'
786+
];
787+
788+
for (; i < urls.length; i++) {
789+
parsed = parse(urls[i]);
790+
791+
assume(parsed.protocol).equals('http:');
792+
assume(parsed.auth).equals('');
793+
assume(parsed.username).equals('');
794+
assume(parsed.password).equals('');
795+
assume(parsed.host).equals('');
796+
assume(parsed.hostname).equals('');
797+
assume(parsed.pathname).equals('/127.0.0.1');
798+
assume(parsed.origin).equals('null');
799+
assume(parsed.href).equals('http://@/127.0.0.1');
800+
assume(parsed.toString()).equals('http://@/127.0.0.1');
801+
}
802+
803+
urls = [
804+
'http:@/',
805+
'http:@',
806+
'http::@/',
807+
'http::@',
808+
'http:/@/',
809+
'http:/@',
810+
'http:/:@/',
811+
'http:/:@',
812+
'http://@/',
813+
'http://@',
814+
'http://:@/',
815+
'http://:@'
816+
];
817+
818+
for (i = 0; i < urls.length; i++) {
819+
parsed = parse(urls[i]);
820+
821+
assume(parsed.protocol).equals('http:');
822+
assume(parsed.auth).equals('');
823+
assume(parsed.username).equals('');
824+
assume(parsed.password).equals('');
825+
assume(parsed.host).equals('');
826+
assume(parsed.hostname).equals('');
827+
assume(parsed.pathname).equals('/');
828+
assume(parsed.origin).equals('null');
829+
assume(parsed.href).equals('http:///');
830+
assume(parsed.toString()).equals('http:///');
831+
}
832+
});
774833
});
775834

776835
it('accepts multiple ???', function () {

0 commit comments

Comments
 (0)
Please sign in to comment.