Skip to content

Commit

Permalink
fix some cases of work with malformed URI sequences in URLSearchParams,
Browse files Browse the repository at this point in the history
close #525
  • Loading branch information
zloirock committed Apr 5, 2019
1 parent f813c4e commit a1eafbd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/core-js/modules/web.url-search-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,27 @@ var getInternalParamsState = InternalStateModule.getterFor(URL_SEARCH_PARAMS);
var getInternalIteratorState = InternalStateModule.getterFor(URL_SEARCH_PARAMS_ITERATOR);

var plus = /\+/g;
var sequences = Array(4);

var percentSequence = function (bytes) {
return sequences[bytes - 1] || (sequences[bytes - 1] = RegExp('((?:%[\\da-f]{2}){' + bytes + '})', 'gi'));
};

var percentDecode = function (sequence) {
try {
return decodeURIComponent(sequence);
} catch (error) {
return sequence;
}
};

var deserialize = function (it) {
return decodeURIComponent(it.replace(plus, ' '));
var result = it.replace(plus, ' ');
var bytes = 4;
while (bytes) {
result = result.replace(percentSequence(bytes--), percentDecode);
}
return result;
};

var find = /[!'()~]|%20/g;
Expand Down
1 change: 1 addition & 0 deletions tests/pure/web.url-search-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ QUnit.test('URLSearchParams', assert => {
assert.same(params.get('query'), '+15555555555', 'parse encoded +');

const testData = [
{ input: '?a=%', output: [['a', '%']], name: 'handling %' },
{ input: { '+': '%C2' }, output: [['+', '%C2']], name: 'object with +' },
{ input: { c: 'x', a: '?' }, output: [['c', 'x'], ['a', '?']], name: 'object with two keys' },
{ input: [['c', 'x'], ['a', '?']], output: [['c', 'x'], ['a', '?']], name: 'array with two keys' },
Expand Down
1 change: 1 addition & 0 deletions tests/tests/web.url-search-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ QUnit.test('URLSearchParams', assert => {
assert.same(params.get('query'), '+15555555555', 'parse encoded +');

const testData = [
{ input: '?a=%', output: [['a', '%']], name: 'handling %' },
{ input: { '+': '%C2' }, output: [['+', '%C2']], name: 'object with +' },
{ input: { c: 'x', a: '?' }, output: [['c', 'x'], ['a', '?']], name: 'object with two keys' },
{ input: [['c', 'x'], ['a', '?']], output: [['c', 'x'], ['a', '?']], name: 'array with two keys' },
Expand Down

0 comments on commit a1eafbd

Please sign in to comment.