Skip to content

Commit e727eb0

Browse files
TimothyGuRafaelGSS
authored andcommitted
url: do not use object as hashmap
Fixes cases like new URLSearchParams({ hasOwnProperty: 1 }). PR-URL: #47415 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 4b52727 commit e727eb0

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/internal/url.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {
2020
ReflectGetOwnPropertyDescriptor,
2121
ReflectOwnKeys,
2222
RegExpPrototypeSymbolReplace,
23+
SafeMap,
2324
StringPrototypeCharAt,
2425
StringPrototypeCharCodeAt,
2526
StringPrototypeCodePointAt,
@@ -243,7 +244,7 @@ class URLSearchParams {
243244
} else {
244245
// Record<USVString, USVString>
245246
// Need to use reflection APIs for full spec compliance.
246-
const visited = {};
247+
const visited = new SafeMap();
247248
const keys = ReflectOwnKeys(init);
248249
for (let i = 0; i < keys.length; i++) {
249250
const key = keys[i];
@@ -252,14 +253,15 @@ class URLSearchParams {
252253
const typedKey = toUSVString(key);
253254
const typedValue = toUSVString(init[key]);
254255

255-
// Two different key may result same after `toUSVString()`, we only
256-
// leave the later one. Refers to WPT.
257-
if (visited[typedKey] !== undefined) {
258-
this[searchParams][visited[typedKey]] = typedValue;
256+
// Two different keys may become the same USVString after normalization.
257+
// In that case, we retain the later one. Refer to WPT.
258+
const keyIdx = visited.get(typedKey);
259+
if (keyIdx !== undefined) {
260+
this[searchParams][keyIdx] = typedValue;
259261
} else {
260-
visited[typedKey] = ArrayPrototypePush(this[searchParams],
261-
typedKey,
262-
typedValue) - 1;
262+
visited.set(typedKey, ArrayPrototypePush(this[searchParams],
263+
typedKey,
264+
typedValue) - 1);
263265
}
264266
}
265267
}

test/parallel/test-whatwg-url-custom-searchparams-constructor.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ function makeIterableFunc(array) {
3838
makeIterableFunc([['key', 'val'], ['key2', 'val2']].map(makeIterableFunc))
3939
);
4040
assert.strictEqual(params.toString(), 'key=val&key2=val2');
41+
params = new URLSearchParams({ hasOwnProperty: 1 });
42+
assert.strictEqual(params.get('hasOwnProperty'), '1');
43+
assert.strictEqual(params.toString(), 'hasOwnProperty=1');
4144
assert.throws(() => new URLSearchParams([[1]]), tupleError);
4245
assert.throws(() => new URLSearchParams([[1, 2, 3]]), tupleError);
46+
assert.throws(() => new URLSearchParams({ [Symbol('test')]: 42 }),
47+
TypeError);
4348
assert.throws(() => new URLSearchParams({ [Symbol.iterator]: 42 }),
4449
iterableError);
4550
assert.throws(() => new URLSearchParams([{}]), tupleError);

0 commit comments

Comments
 (0)