Skip to content

Commit 2a12b7e

Browse files
committed
use more stringent version of encodeURIComponent
1 parent 7bee64c commit 2a12b7e

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
'use strict';
22

3+
// more stringent version of encodeURIComponent
4+
// (from https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent)
5+
function fixedEncodeURIComponent(str) {
6+
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
7+
return '%' + c.charCodeAt(0).toString(16);
8+
});
9+
}
10+
311
exports.extract = function (maybeUrl) {
412
return maybeUrl.split('?')[1] || '';
513
};
@@ -43,10 +51,10 @@ exports.stringify = function (obj) {
4351

4452
if (Array.isArray(val)) {
4553
return val.sort().map(function (val2) {
46-
return encodeURIComponent(key) + '=' + encodeURIComponent(val2);
54+
return fixedEncodeURIComponent(key) + '=' + fixedEncodeURIComponent(val2);
4755
}).join('&');
4856
}
4957

50-
return encodeURIComponent(key) + '=' + encodeURIComponent(val);
58+
return fixedEncodeURIComponent(key) + '=' + fixedEncodeURIComponent(val);
5159
}).join('&') : '';
5260
};

test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ describe('.stringify()', function () {
6262

6363
it('URI encode', function () {
6464
assert.strictEqual(qs.stringify({'foo bar': 'baz faz'}), 'foo%20bar=baz%20faz');
65+
assert.strictEqual(qs.stringify({'foo bar': "baz'faz"}), 'foo%20bar=baz%27faz');
6566
});
6667

6768
it('handle array value', function () {
@@ -88,4 +89,4 @@ describe('.extract()', function () {
8889
qs.extract(undefined);
8990
}, TypeError);
9091
});
91-
});
92+
});

0 commit comments

Comments
 (0)