Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v18.x backport] url: ensure getter access do not mutate observable symbols #48891

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {
ReflectOwnKeys,
RegExpPrototypeSymbolReplace,
SafeMap,
SafeWeakMap,
StringPrototypeCharAt,
StringPrototypeCharCodeAt,
StringPrototypeCodePointAt,
Expand Down Expand Up @@ -99,6 +100,12 @@ const FORWARD_SLASH = /\//g;
const context = Symbol('context');
const searchParams = Symbol('query');

/**
* We cannot use private fields without a breaking change, so a WeakMap is the alternative.
* @type {WeakMap<URL,URLSearchParams>}
*/
const internalSearchParams = new SafeWeakMap();

const updateActions = {
kProtocol: 0,
kHost: 1,
Expand Down Expand Up @@ -179,7 +186,7 @@ class URLContext {
}

function isURLSearchParams(self) {
return self && self[searchParams] && !self[searchParams][searchParams];
return self?.[searchParams];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return self?.[searchParams];
return Boolean(self?.[searchParams]);

}

class URLSearchParams {
Expand Down Expand Up @@ -672,11 +679,12 @@ class URL {
ctx.hash_start = hash_start;
ctx.scheme_type = scheme_type;

if (this[searchParams]) {
const alreadyInstantiatedSearchParams = internalSearchParams.get(this);
if (alreadyInstantiatedSearchParams) {
if (ctx.hasSearch) {
this[searchParams][searchParams] = parseParams(this.search);
alreadyInstantiatedSearchParams[searchParams] = parseParams(this.search);
} else {
this[searchParams][searchParams] = [];
alreadyInstantiatedSearchParams[searchParams] = [];
}
}
}
Expand Down Expand Up @@ -898,11 +906,15 @@ class URL {
get searchParams() {
if (!isURL(this))
throw new ERR_INVALID_THIS('URL');
if (this[searchParams] == null) {
this[searchParams] = new URLSearchParams(this.search);
this[searchParams][context] = this;
}
return this[searchParams];

const cachedValue = internalSearchParams.get(this);
if (cachedValue != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (cachedValue != null)
if (cachedValue !== undefined)

return cachedValue;

const value = new URLSearchParams(this.search);
value[context] = this;
internalSearchParams.set(this, value);
return value;
}

get hash() {
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-whatwg-url-custom-searchparams.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const normalizedValues = ['a', '1', 'true', 'undefined', 'null', '\uFFFD',
'[object Object]'];

const m = new URL('http://example.org');
const ownSymbolsBeforeGetterAccess = Object.getOwnPropertySymbols(m);
const sp = m.searchParams;
assert.deepStrictEqual(Object.getOwnPropertySymbols(m), ownSymbolsBeforeGetterAccess);

assert(sp);
assert.strictEqual(sp.toString(), '');
Expand Down
50 changes: 0 additions & 50 deletions test/pummel/test-net-bytes-per-incoming-chunk-overhead.js

This file was deleted.