diff --git a/src/node_url.cc b/src/node_url.cc index 6294cd03667980..ea56a415fe73aa 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -169,7 +169,10 @@ void BindingData::PathToFileURL(const FunctionCallbackInfo& args) { [[unlikely]] { CHECK(args[2]->IsString()); Utf8Value hostname(isolate, args[2]); - CHECK(out->set_hostname(hostname.ToStringView())); + // Ada should validate chars in hostname + if (!out->set_hostname(hostname.ToStringView())) { + return ThrowInvalidURL(realm->env(), input.ToStringView(), std::nullopt); + } } binding_data->UpdateComponents(out->get_components(), out->type); @@ -441,7 +444,10 @@ void BindingData::Update(const FunctionCallbackInfo& args) { std::string_view new_value_view = new_value.ToStringView(); auto out = ada::parse(input.ToStringView()); - CHECK(out); + // If the href cannot be re-parsed, return original url + if (!out) { + return args.GetReturnValue().Set(false); + } bool result{true}; diff --git a/test/parallel/test-url-format-whatwg.js b/test/parallel/test-url-format-whatwg.js index 12594335d6bd67..021cb0093c3024 100644 --- a/test/parallel/test-url-format-whatwg.js +++ b/test/parallel/test-url-format-whatwg.js @@ -154,4 +154,6 @@ test('should not crash on URLs with invalid IDN hostnames', () => { const u = new URL('ws:xn-\u022B'); // doesNotThrow url.format(u, { fragment: false, unicode: false, auth: false, search: false }); + // Same problem with re-parsing + url.port = 80; }); diff --git a/test/parallel/test-url-pathtofileurl.js b/test/parallel/test-url-pathtofileurl.js index 089232caeb3b2d..9dac35cfc4de0f 100644 --- a/test/parallel/test-url-pathtofileurl.js +++ b/test/parallel/test-url-pathtofileurl.js @@ -223,3 +223,10 @@ for (const { path, expected } of testCases) { }); } } + +// Regression for forbidden chars in UNC Windows +{ + assert.throws(() => url.pathToFileURL('\\\\%\\file', { windows: true }), { + code: 'ERR_INVALID_URL', + }); +}