Skip to content

Commit 8bdc77a

Browse files
papandreouljharb
authored andcommitted
Throw if the charset is not one of the two supported values or undefined
ljharb#268 (comment)
1 parent 2be2fb9 commit 8bdc77a

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/parse.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ var parseValues = function parseQueryStringValues(str, options) {
4545
var skipIndex = -1; // Keep track of where the utf8 sentinel was found
4646
var i;
4747

48+
if (charset !== undefined && charset !== 'utf-8' && charset !== 'iso-8859-1') {
49+
throw new Error('The charset option must be either utf-8, iso-8859-1, or undefined');
50+
}
4851
if (options.utf8Sentinel) {
4952
for (i = 0; i < parts.length; ++i) {
5053
if (parts[i].indexOf('utf8=') === 0) {

test/parse.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,13 @@ test('parse()', function (t) {
577577
st.end();
578578
});
579579

580+
t.test('throws if an invalid charset is specified', function (st) {
581+
st['throws'](function () {
582+
qs.parse('a=b', { charset: 'foobar' });
583+
}, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
584+
st.end();
585+
});
586+
580587
t.test('parses an iso-8859-1 string if asked to', function (st) {
581588
st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
582589
st.end();

0 commit comments

Comments
 (0)