Skip to content

Commit

Permalink
querystring: fix unescape override
Browse files Browse the repository at this point in the history
Documentation states that `querystring.unescape` may be overridden to
replace unescaper during parsing. However, the function was only
being used as a fallback for when the native decoder throws (on a
malformed URL). This patch moves the call to the native function and
the try/catch around it into querystring.unescape then has the parser
always invoke it, so that an override will always be used.

Fixes nodejs#4055

Reviewed-By: Fedor Indutny <fedor@indutny.com>
  • Loading branch information
Tristan Berger authored and mscdex committed Dec 25, 2014
1 parent e3689e3 commit 7ad4e0c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/querystring.js
Expand Up @@ -105,7 +105,11 @@ QueryString.unescapeBuffer = function(s, decodeSpaces) {


QueryString.unescape = function(s, decodeSpaces) {
return QueryString.unescapeBuffer(s, decodeSpaces).toString();
try {
return decodeURIComponent(s);
} catch (e) {
return QueryString.unescapeBuffer(s, decodeSpaces).toString();
}
};


Expand Down Expand Up @@ -193,13 +197,8 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
vstr = '';
}

try {
k = decodeURIComponent(kstr);
v = decodeURIComponent(vstr);
} catch (e) {
k = QueryString.unescape(kstr, true);
v = QueryString.unescape(vstr, true);
}
k = QueryString.unescape(kstr, true);
v = QueryString.unescape(vstr, true);

if (!hasOwnProperty(obj, k)) {
obj[k] = v;
Expand Down
8 changes: 8 additions & 0 deletions test/simple/test-querystring.js
Expand Up @@ -229,3 +229,11 @@ assert.equal(0xeb, b[16]);
assert.equal(0xd8, b[17]);
assert.equal(0xa2, b[18]);
assert.equal(0xe6, b[19]);

// test overriding .unescape
var prevUnescape = qs.unescape;
qs.unescape = function (str) {
return str.replace(/o/g, '_');
};
assert.deepEqual(qs.parse('foo=bor'), {f__: 'b_r'});
qs.unescape = prevUnescape;

0 comments on commit 7ad4e0c

Please sign in to comment.