url: handle unparsable serialized URLs in setters

Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/64651
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
This commit is contained in:
Matteo Collina authored and Antoine du Hamel committed 2026-08-27 12:03:01 +02:00
1 parent 8c11beae27
commit 9ffc0da90c
2 files changed
+31 -1

No files matched your search

+5 -1
View File
@@ -443,8 +443,12 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
Utf8Value new_value(isolate, args[2].As<String>());
std::string_view new_value_view = new_value.ToStringView();
// A serialized URL is not always reparsable: the IDNA encoder can emit a
// host label that the decoder rejects. Fail the update instead of crashing.
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());
CHECK(out);
if (!out) {
return args.GetReturnValue().Set(false);
}
bool result{true};
@@ -39,6 +39,32 @@ const additionalTestCases =
}
}
// The parser can produce a serialization it rejects when parsing it back: a
// Unicode host encodes to an `xn--xn--` label that the punycode decoder turns
// down. Setters reparse `href`, so the failure must not take the process down.
// Implementations backed by ICU accept that label, and ada does too as of
// https://github.com/ada-url/idna/pull/72, so this URL round-trips once that
// lands here and the setters below apply as usual.
test(function() {
const url = new URL('http:\u{1F600}xn-');
const setters = {
hostname: 'example.com',
host: 'example.com:8080',
protocol: 'https:',
pathname: '/path',
search: '?search',
hash: '#hash',
port: '8080',
username: 'username',
password: 'password',
};
for (const [property, value] of Object.entries(setters)) {
url[property] = value;
assert_equals(typeof url.href, 'string', `Setting ${property} does not crash`);
}
}, 'URL: setting properties with an unparsable serialized URL');
{
const url = new URL('http://example.com/');
const obj = {