Skip to content

Commit

Permalink
[fix] Use the last occurence of the at sign for userinfo
Browse files Browse the repository at this point in the history
Correctly handle usernames and passwords containing the at sign (@).
  • Loading branch information
lpinca committed Jan 8, 2022
1 parent 82c4908 commit cb7fecb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
15 changes: 12 additions & 3 deletions index.js
Expand Up @@ -304,7 +304,11 @@ function Url(address, location, parser) {
if (parse !== parse) {
url[key] = address;
} else if ('string' === typeof parse) {
if (~(index = address.indexOf(parse))) {
index = parse === '@'
? address.lastIndexOf(parse)
: address.indexOf(parse);

if (~index) {
if ('number' === typeof instruction[2]) {
url[key] = address.slice(0, index);
address = address.slice(index + instruction[2]);
Expand Down Expand Up @@ -372,8 +376,13 @@ function Url(address, location, parser) {
url.username = url.password = '';
if (url.auth) {
instruction = url.auth.split(':');
url.username = instruction[0];
url.password = instruction[1] || '';
url.username = instruction[0]
? encodeURIComponent(decodeURIComponent(instruction[0]))
: '';
url.password = instruction[1]
? encodeURIComponent(decodeURIComponent(instruction[1]))
: '';
url.auth = instruction.join(':');
}

url.origin = url.protocol !== 'file:' && isSpecial(url.protocol) && url.host
Expand Down
44 changes: 44 additions & 0 deletions test/test.js
Expand Up @@ -689,6 +689,50 @@ describe('url-parse', function () {
assume(parsed.hostname).equals('www.example.com');
assume(parsed.href).equals(url);
});

it('handles @ in username', function () {
var url = 'http://user@@www.example.com/'
, parsed = parse(url);

assume(parsed.protocol).equals('http:');
assume(parsed.username).equals('user%40');
assume(parsed.password).equals('');
assume(parsed.hostname).equals('www.example.com');
assume(parsed.pathname).equals('/');
assume(parsed.href).equals('http://user%40@www.example.com/');

url = 'http://user%40@www.example.com/';
parsed = parse(url);

assume(parsed.protocol).equals('http:');
assume(parsed.username).equals('user%40');
assume(parsed.password).equals('');
assume(parsed.hostname).equals('www.example.com');
assume(parsed.pathname).equals('/');
assume(parsed.href).equals('http://user%40@www.example.com/');
});

it('handles @ in password', function () {
var url = 'http://user@:pass@@www.example.com/'
, parsed = parse(url);

assume(parsed.protocol).equals('http:');
assume(parsed.username).equals('user%40');
assume(parsed.password).equals('pass%40');
assume(parsed.hostname).equals('www.example.com');
assume(parsed.pathname).equals('/');
assume(parsed.href).equals('http://user%40:pass%40@www.example.com/');

url = 'http://user%40:pass%40@www.example.com/'
parsed = parse(url);

assume(parsed.protocol).equals('http:');
assume(parsed.username).equals('user%40');
assume(parsed.password).equals('pass%40');
assume(parsed.hostname).equals('www.example.com');
assume(parsed.pathname).equals('/');
assume(parsed.href).equals('http://user%40:pass%40@www.example.com/');
});
});

it('accepts multiple ???', function () {
Expand Down

0 comments on commit cb7fecb

Please sign in to comment.