Skip to content

url: call toString before valueOf when stringifying

The ES addition operator calls the ToPrimitive() abstract operation without hint String, leading a subsequent OrdinaryToPrimitive() to call valueOf() first on an object rather than the desired toString().

Instead, use template literals which directly call ToString() abstract operation, per Web IDL spec.

This issue is particularly visible in the example below:

const { URLSearchParams } = url;
const searchParams = new URLSearchParams();
searchParams.set('name', {
  toString() { return 'toString'; },
  valueOf() { return 'valueOf'; }
});
console.log(searchParams.get('name'));

In the browser, the above prints toString, while in current master it prints valueOf.

Fixes: b610a4db "url: enforce valid UTF-8 in WHATWG parser" Refs: https://github.com/nodejs/node/commit/b610a4db1c2919f88711962f5797f25ecb1cd36b#commitcomment-21200056 Refs: https://tc39.github.io/ecma262/#sec-addition-operator-plus-runtime-semantics-evaluation Refs: https://tc39.github.io/ecma262/#sec-template-literals-runtime-semantics-evaluation

/cc @nodejs/url

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

url

Merge request reports

Loading