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 <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 4b52727 commit e727eb0

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

‎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-
constvisited={};
247+
constvisited=newSafeMap();
247248
constkeys=ReflectOwnKeys(init);
248249
for(leti=0;i<keys.length;i++){
249250
constkey=keys[i];
@@ -252,14 +253,15 @@ class URLSearchParams {
252253
consttypedKey=toUSVString(key);
253254
consttypedValue=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+
constkeyIdx=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=newURLSearchParams({hasOwnProperty: 1});
42+
assert.strictEqual(params.get('hasOwnProperty'),'1');
43+
assert.strictEqual(params.toString(),'hasOwnProperty=1');
4144
assert.throws(()=>newURLSearchParams([[1]]),tupleError);
4245
assert.throws(()=>newURLSearchParams([[1,2,3]]),tupleError);
46+
assert.throws(()=>newURLSearchParams({[Symbol('test')]: 42}),
47+
TypeError);
4348
assert.throws(()=>newURLSearchParams({[Symbol.iterator]: 42}),
4449
iterableError);
4550
assert.throws(()=>newURLSearchParams([{}]),tupleError);

0 commit comments

Comments
 (0)