From 9ffc0da90c7acd6916be95157dcbd81f4e783ce8 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 5 Aug 2026 11:19:53 +0100 Subject: [PATCH] url: handle unparsable serialized URLs in setters Signed-off-by: Matteo Collina PR-URL: https://github.com/nodejs/node/pull/64651 Reviewed-By: James M Snell Reviewed-By: Yagiz Nizipli Reviewed-By: Filip Skokan --- src/node_url.cc | 6 ++++- .../test-whatwg-url-custom-setters.js | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/node_url.cc b/src/node_url.cc index 5f5241f6c67..05c4b10dab7 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -443,8 +443,12 @@ void BindingData::Update(const FunctionCallbackInfo& args) { Utf8Value new_value(isolate, args[2].As()); 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(input.ToStringView()); - CHECK(out); + if (!out) { + return args.GetReturnValue().Set(false); + } bool result{true}; diff --git a/test/parallel/test-whatwg-url-custom-setters.js b/test/parallel/test-whatwg-url-custom-setters.js index b98bf5d8d3b..061ccf6f448 100644 --- a/test/parallel/test-whatwg-url-custom-setters.js +++ b/test/parallel/test-whatwg-url-custom-setters.js @@ -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 = {