Skip to content

Commit 9a2354d

Browse files
TimothyGuMoLow
authored andcommitted
url: do not use object as hashmap
Fixes cases like new URLSearchParams({ hasOwnProperty: 1 }). PR-URL: #47415 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 89a5d04 commit 9a2354d

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/internal/url.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const {
2121
ReflectGetOwnPropertyDescriptor,
2222
ReflectOwnKeys,
2323
RegExpPrototypeSymbolReplace,
24+
SafeMap,
2425
StringPrototypeCharAt,
2526
StringPrototypeCharCodeAt,
2627
StringPrototypeCodePointAt,
@@ -225,7 +226,7 @@ class URLSearchParams {
225226
} else {
226227
// Record<USVString, USVString>
227228
// Need to use reflection APIs for full spec compliance.
228-
const visited = {};
229+
const visited = new SafeMap();
229230
const keys = ReflectOwnKeys(init);
230231
for (let i = 0; i < keys.length; i++) {
231232
const key = keys[i];
@@ -234,14 +235,15 @@ class URLSearchParams {
234235
const typedKey = toUSVString(key);
235236
const typedValue = toUSVString(init[key]);
236237

237-
// Two different key may result same after `toUSVString()`, we only
238-
// leave the later one. Refers to WPT.
239-
if (visited[typedKey] !== undefined) {
240-
this[searchParams][visited[typedKey]] = typedValue;
238+
// Two different keys may become the same USVString after normalization.
239+
// In that case, we retain the later one. Refer to WPT.
240+
const keyIdx = visited.get(typedKey);
241+
if (keyIdx !== undefined) {
242+
this[searchParams][keyIdx] = typedValue;
241243
} else {
242-
visited[typedKey] = ArrayPrototypePush(this[searchParams],
243-
typedKey,
244-
typedValue) - 1;
244+
visited.set(typedKey, ArrayPrototypePush(this[searchParams],
245+
typedKey,
246+
typedValue) - 1);
245247
}
246248
}
247249
}

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

+5
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)