GeezerVison is showing github.com — url: create URLSearchParams collision map lazily · HoonDongKang/node@0e3c6d5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/url.js +15-3Lines changed: 15 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -447,7 +447,9 @@ class URLSearchParams { | 447 | 447 | } else { | 448 | 448 | // Record<USVString, USVString> | 449 | 449 | // Need to use reflection APIs for full spec compliance. | 450 | | - const visited = new SafeMap(); | | 450 | + // Object keys are unique, but USVString normalization may merge | | 451 | + // distinct keys. Lazily track normalized keys if a key changes. | | 452 | + let visited; | 451 | 453 | const keys = ReflectOwnKeys(init); | 452 | 454 | for (let i = 0; i < keys.length; i++) { | 453 | 455 | const key = keys[i]; | @@ -456,8 +458,18 @@ class URLSearchParams { | 456 | 458 | const typedKey = toUSVString(key); | 457 | 459 | const typedValue = toUSVString(init[key]); | 458 | 460 |
| 459 | | - // Two different keys may become the same USVString after normalization. | 460 | | - // In that case, we retain the later one. Refer to WPT. | | 461 | + if (visited === undefined && typedKey === key) { | | 462 | + ArrayPrototypePush(this.#searchParams, typedKey, typedValue); | | 463 | + continue; | | 464 | + } | | 465 | + | | 466 | + if (visited === undefined) { | | 467 | + visited = new SafeMap(); | | 468 | + for (let j = 0; j < this.#searchParams.length; j += 2) { | | 469 | + visited.set(this.#searchParams[j], j + 1); | | 470 | + } | | 471 | + } | | 472 | + | 461 | 473 | const keyIdx = visited.get(typedKey); | 462 | 474 | if (keyIdx !== undefined) { | 463 | 475 | this.#searchParams[keyIdx] = typedValue; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-whatwg-url-custom-searchparams-constructor.js +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,6 +47,19 @@ function makeIterableFunc(array) { | 47 | 47 | params = new URLSearchParams({ hasOwnProperty: 1 }); | 48 | 48 | assert.strictEqual(params.get('hasOwnProperty'), '1'); | 49 | 49 | assert.strictEqual(params.toString(), 'hasOwnProperty=1'); | | 50 | + // A malformed key can collide with a valid key collected before the map | | 51 | + // exists. | | 52 | + params = new URLSearchParams({ | | 53 | + 'before': '0', | | 54 | + '\uFFFDx': 'first', | | 55 | + '\uD835x': 'last', | | 56 | + 'after': '3' | | 57 | + }); | | 58 | + assert.deepStrictEqual([...params], [ | | 59 | + ['before', '0'], | | 60 | + ['\uFFFDx', 'last'], | | 61 | + ['after', '3'], | | 62 | + ]); | 50 | 63 | assert.throws(() => new URLSearchParams([[1]]), tupleError); | 51 | 64 | assert.throws(() => new URLSearchParams([[1, 2, 3]]), tupleError); | 52 | 65 | assert.throws(() => new URLSearchParams({ [Symbol('test')]: 42 }), | |
|