Skip to content

Commit 7c4e235

Browse files
committed
fix: security issue gh closes #44
1 parent 4aa9088 commit 7c4e235

File tree

3 files changed

+351
-315
lines changed

3 files changed

+351
-315
lines changed

src/index.js renamed to src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function buildParam(key: string, value: any, traditional?: boolean): Array<strin
158158
* @param traditional Boolean Use the old URI template standard (RFC6570)
159159
* @returns The generated query string, excluding leading '?'.
160160
*/
161-
export function buildQueryString(params: Object, traditional?: Boolean): string {
161+
export function buildQueryString(params?: Object, traditional?: boolean): string {
162162
let pairs = [];
163163
let keys = Object.keys(params || {}).sort();
164164
for (let i = 0, len = keys.length; i < len; i++) {
@@ -203,16 +203,19 @@ function processScalarParam(existedParam: Object, value: Object): Object {
203203
* @param keys Collection of keys related to this parameter.
204204
* @param value Parameter value to append.
205205
*/
206-
function parseComplexParam(queryParams: Object, keys: Object, value: any): void {
206+
function parseComplexParam(queryParams: Object, keys: (string | number)[], value: any): void {
207207
let currentParams = queryParams;
208208
let keysLastIndex = keys.length - 1;
209209
for (let j = 0; j <= keysLastIndex; j++) {
210-
let key = keys[j] === '' ? currentParams.length : keys[j];
210+
let key = keys[j] === '' ? (currentParams as any).length : keys[j];
211+
if (key === '__proto__') {
212+
throw new Error('Prototype pollution detected.');
213+
}
211214
if (j < keysLastIndex) {
212215
// The value has to be an array or a false value
213216
// It can happen that the value is no array if the key was repeated with traditional style like `list=1&list[]=2`
214217
let prevValue = !currentParams[key] || typeof currentParams[key] === 'object' ? currentParams[key] : [currentParams[key]];
215-
currentParams = currentParams[key] = prevValue || (isNaN(keys[j + 1]) ? {} : []);
218+
currentParams = currentParams[key] = prevValue || (isNaN(keys[j + 1] as number) ? {} : []);
216219
} else {
217220
currentParams = currentParams[key] = value;
218221
}

test/path.spec.js

Lines changed: 0 additions & 311 deletions
This file was deleted.

0 commit comments

Comments
 (0)