Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
url: decode url entities in auth section
Browse files Browse the repository at this point in the history
Fixes #2736.
  • Loading branch information
bnoordhuis committed Feb 20, 2012
1 parent 0cebfc8 commit 86f4846
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
15 changes: 7 additions & 8 deletions lib/url.js
Expand Up @@ -135,19 +135,21 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
// URLs are obnoxious.
var atSign = rest.indexOf('@');
if (atSign !== -1) {
var auth = rest.slice(0, atSign);

// there *may be* an auth
var hasAuth = true;
for (var i = 0, l = nonAuthChars.length; i < l; i++) {
var index = rest.indexOf(nonAuthChars[i]);
if (index !== -1 && index < atSign) {
if (auth.indexOf(nonAuthChars[i]) !== -1) {
// not a valid auth. Something like http://foo.com/bar@baz/
hasAuth = false;
break;
}
}

if (hasAuth) {
// pluck off the auth portion.
out.auth = rest.substr(0, atSign);
out.auth = decodeURIComponent(auth);
rest = rest.substr(atSign + 1);
}
}
Expand Down Expand Up @@ -329,11 +331,8 @@ function urlFormat(obj) {

var auth = obj.auth || '';
if (auth) {
auth = auth.split('@').join('%40');
for (var i = 0, l = nonAuthChars.length; i < l; i++) {
var nAC = nonAuthChars[i];
auth = auth.split(nAC).join(encodeURIComponent(nAC));
}
auth = encodeURIComponent(auth);
auth = auth.replace(/%3A/i, ':');
auth += '@';
}

Expand Down
22 changes: 21 additions & 1 deletion test/simple/test-url.js
Expand Up @@ -71,6 +71,26 @@ var parseTests = {
'pathname': '/',
'path': '/'
},
'http://user@www.example.com/' : {
'href': 'http://user@www.example.com/',
'protocol': 'http:',
'slashes': true,
'auth': 'user',
'host': 'www.example.com',
'hostname': 'www.example.com',
'pathname': '/',
'path': '/'
},
'http://user%3Apw@www.example.com/' : {
'href': 'http://user:pw@www.example.com/',
'protocol': 'http:',
'slashes': true,
'auth': 'user:pw',
'host': 'www.example.com',
'hostname': 'www.example.com',
'pathname': '/',
'path': '/'
},
'http://x.com/path?that\'s#all, folks' : {
'href': 'http://x.com/path?that%27s#all,',
'protocol': 'http:',
Expand Down Expand Up @@ -324,7 +344,7 @@ var parseTests = {
'protocol' : 'http:',
'slashes': true,
'host' : '127.0.0.1:8080',
'auth' : 'atpass:foo%40bar',
'auth' : 'atpass:foo@bar',
'hostname' : '127.0.0.1',
'port' : '8080',
'pathname': '/path',
Expand Down

0 comments on commit 86f4846

Please sign in to comment.