Skip to content

Commit 0f29561

Browse files
Tristan Bergerindutny
authored andcommitted
querystring: fix unescape override
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>
1 parent f39e608 commit 0f29561

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/querystring.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ QueryString.unescapeBuffer = function(s, decodeSpaces) {
105105

106106

107107
QueryString.unescape = function(s, decodeSpaces) {
108-
return QueryString.unescapeBuffer(s, decodeSpaces).toString();
108+
try {
109+
return decodeURIComponent(s);
110+
} catch (e) {
111+
return QueryString.unescapeBuffer(s, decodeSpaces).toString();
112+
}
109113
};
110114

111115

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

196-
try {
197-
k = decodeURIComponent(kstr);
198-
v = decodeURIComponent(vstr);
199-
} catch (e) {
200-
k = QueryString.unescape(kstr, true);
201-
v = QueryString.unescape(vstr, true);
202-
}
200+
k = QueryString.unescape(kstr, true);
201+
v = QueryString.unescape(vstr, true);
203202

204203
if (!hasOwnProperty(obj, k)) {
205204
obj[k] = v;

test/simple/test-querystring.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,11 @@ assert.equal(0xeb, b[16]);
229229
assert.equal(0xd8, b[17]);
230230
assert.equal(0xa2, b[18]);
231231
assert.equal(0xe6, b[19]);
232+
233+
// test overriding .unescape
234+
var prevUnescape = qs.unescape;
235+
qs.unescape = function (str) {
236+
return str.replace(/o/g, '_');
237+
};
238+
assert.deepEqual(qs.parse('foo=bor'), {f__: 'b_r'});
239+
qs.unescape = prevUnescape;

0 commit comments

Comments
 (0)