Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ function isSpecial(scheme) {
);
}

/**
* Decode a URI component without throwing on malformed percent-encoding.
* url-parse is documented as a tolerant parser (see #195), so a stray `%`
* in the userinfo must not raise; the WHATWG URL parser tolerates the same
* input.
*
* @param {String} input Value to decode.
* @return {String} Decoded value, or the original input if decoding fails.
* @private
*/
function decode(input) {
try {
return decodeURIComponent(input);
} catch (e) {
return input;
}
}

/**
* @typedef ProtocolExtract
* @type Object
Expand Down Expand Up @@ -410,12 +428,12 @@ function Url(address, location, parser) {

if (~index) {
url.username = url.auth.slice(0, index);
url.username = encodeURIComponent(decodeURIComponent(url.username));
url.username = encodeURIComponent(decode(url.username));

url.password = url.auth.slice(index + 1);
url.password = encodeURIComponent(decodeURIComponent(url.password))
url.password = encodeURIComponent(decode(url.password))
} else {
url.username = encodeURIComponent(decodeURIComponent(url.auth));
url.username = encodeURIComponent(decode(url.auth));
}

url.auth = url.password ? url.username +':'+ url.password : url.username;
Expand Down Expand Up @@ -525,12 +543,12 @@ function set(part, value, fn) {

if (~index) {
url.username = value.slice(0, index);
url.username = encodeURIComponent(decodeURIComponent(url.username));
url.username = encodeURIComponent(decode(url.username));

url.password = value.slice(index + 1);
url.password = encodeURIComponent(decodeURIComponent(url.password));
url.password = encodeURIComponent(decode(url.password));
} else {
url.username = encodeURIComponent(decodeURIComponent(value));
url.username = encodeURIComponent(decode(value));
}
}

Expand Down
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,23 @@ describe('url-parse', function () {
assume(parsed.href).equals('http://user%40:pas%3As%40@www.example.com/');
});

it('does not throw on malformed percent-encoding in username/password', function () {
// decodeURIComponent('%') throws; the fallback keeps the raw value and
// re-encodes it, so it round-trips as a literal '%' (i.e. '%25') rather
// than propagating the URIError.
assume(function () { parse('http://%@example.com/'); }).does.not.throw();
assume(parse('http://%@example.com/').username).equals('%25');
assume(parse('http://%@example.com/').hostname).equals('example.com');

assume(function () { parse('http://user:%@example.com/'); }).does.not.throw();
assume(parse('http://user:%@example.com/').password).equals('%25');

assume(function () {
parse('http://example.com/').set('auth', '%');
}).does.not.throw();
assume(parse('http://example.com/').set('auth', '%').username).equals('%25');
});

it('adds @ to href if auth and host are empty', function () {
var parsed, i = 0;
var urls = [
Expand Down