diff --git a/lib/internal/url.js b/lib/internal/url.js index 6ada5db3d80613..9815d035a15676 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -285,7 +285,8 @@ Object.defineProperties(URL.prototype, { if (ctx.host !== undefined) { ret += '//'; const has_username = typeof ctx.username === 'string'; - const has_password = typeof ctx.password === 'string'; + const has_password = typeof ctx.password === 'string' && + ctx.password !== ''; if (has_username || has_password) { if (has_username) ret += ctx.username; diff --git a/test/fixtures/url-tests.json b/test/fixtures/url-tests.json index 44d665b99f5bb6..1ba3e6ee4ba86f 100644 --- a/test/fixtures/url-tests.json +++ b/test/fixtures/url-tests.json @@ -32,6 +32,66 @@ "search": "?b", "hash": "#c" }, + { + "input": "https://test:@test", + "base": "about:blank", + "href": "https://test@test/", + "origin": "https://test", + "protocol": "https:", + "username": "test", + "password": "", + "host": "test", + "hostname": "test", + "port": "", + "pathname": "/", + "search": "", + "hash": "" + }, + { + "input": "https://:@test", + "base": "about:blank", + "href": "https://test/", + "origin": "https://test", + "protocol": "https:", + "username": "", + "password": "", + "host": "test", + "hostname": "test", + "port": "", + "pathname": "/", + "search": "", + "hash": "" + }, + { + "input": "non-special://test:@test/x", + "base": "about:blank", + "href": "non-special://test@test/x", + "origin": "null", + "protocol": "non-special:", + "username": "test", + "password": "", + "host": "test", + "hostname": "test", + "port": "", + "pathname": "/x", + "search": "", + "hash": "" + }, + { + "input": "non-special://:@test/x", + "base": "about:blank", + "href": "non-special://test/x", + "origin": "null", + "protocol": "non-special:", + "username": "", + "password": "", + "host": "test", + "hostname": "test", + "port": "", + "pathname": "/x", + "search": "", + "hash": "" + }, { "input": "http:foo.com", "base": "http://example.org/foo/bar", @@ -3098,7 +3158,7 @@ { "input": "http:a:@www.example.com", "base": "about:blank", - "href": "http://a:@www.example.com/", + "href": "http://a@www.example.com/", "origin": "http://www.example.com", "protocol": "http:", "username": "a", @@ -3113,7 +3173,7 @@ { "input": "http:/a:@www.example.com", "base": "about:blank", - "href": "http://a:@www.example.com/", + "href": "http://a@www.example.com/", "origin": "http://www.example.com", "protocol": "http:", "username": "a", @@ -3128,7 +3188,7 @@ { "input": "http://a:@www.example.com", "base": "about:blank", - "href": "http://a:@www.example.com/", + "href": "http://a@www.example.com/", "origin": "http://www.example.com", "protocol": "http:", "username": "a", @@ -3173,7 +3233,7 @@ { "input": "http://:@www.example.com", "base": "about:blank", - "href": "http://:@www.example.com/", + "href": "http://www.example.com/", "origin": "http://www.example.com", "protocol": "http:", "username": "",