From 1ec691e8910c06104834b43b75dad8d11379c986 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Fri, 3 Apr 2026 08:32:55 +0200 Subject: [PATCH 1/2] url: process crash via malformed UNC hostname in pathToFileURL() Fixes: https://github.com/nodejs/node/issues/62546 --- src/node_url.cc | 5 ++++- test/parallel/test-url-pathtofileurl.js | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/node_url.cc b/src/node_url.cc index 6294cd03667980..3b450521a9644a 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())); + if (!out->set_hostname(hostname.ToStringView())) { + return ThrowInvalidURL( + realm->env(), input.ToStringView(), std::string(hostname.ToStringView())); + } } binding_data->UpdateComponents(out->get_components(), out->type); diff --git a/test/parallel/test-url-pathtofileurl.js b/test/parallel/test-url-pathtofileurl.js index 089232caeb3b2d..01d27f2b736d75 100644 --- a/test/parallel/test-url-pathtofileurl.js +++ b/test/parallel/test-url-pathtofileurl.js @@ -21,6 +21,14 @@ const url = require('url'); assert.ok(fileURL.includes('%25')); } +{ + assert.throws(() => { + url.pathToFileURL('\\\\exa mple\\share\\file.txt', { windows: true }); + }, { + code: 'ERR_INVALID_URL', + }); +} + { if (isWindows) { // UNC path: \\server\share\resource From 0c39aeff6894e31af6471d7b2590ad7a0bbf6e08 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Fri, 3 Apr 2026 15:57:34 +0200 Subject: [PATCH 2/2] fixed linter error. --- src/node_url.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/node_url.cc b/src/node_url.cc index 3b450521a9644a..68dd40452eeec3 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -93,7 +93,6 @@ constexpr auto lookup_table = []() consteval { case CHAR: \ result[i] = {{'%', HEX_DIGIT_2, HEX_DIGIT_1, 0}}; \ break; - ENCODE_CHAR('\0', '0', '0') // '\0' == 0x00 ENCODE_CHAR('\t', '0', '9') // '\t' == 0x09 ENCODE_CHAR('\n', '0', 'A') // '\n' == 0x0A @@ -170,8 +169,9 @@ void BindingData::PathToFileURL(const FunctionCallbackInfo& args) { CHECK(args[2]->IsString()); Utf8Value hostname(isolate, args[2]); if (!out->set_hostname(hostname.ToStringView())) { - return ThrowInvalidURL( - realm->env(), input.ToStringView(), std::string(hostname.ToStringView())); + return ThrowInvalidURL(realm->env(), + input.ToStringView(), + std::string(hostname.ToStringView())); } } @@ -426,9 +426,9 @@ void BindingData::Parse(const FunctionCallbackInfo& args) { } void BindingData::Update(const FunctionCallbackInfo& args) { - CHECK(args[0]->IsString()); // href - CHECK(args[1]->IsNumber()); // action type - CHECK(args[2]->IsString()); // new value + CHECK(args[0]->IsString()); // href + CHECK(args[1]->IsNumber()); // action type + CHECK(args[2]->IsString()); // new value Realm* realm = Realm::GetCurrent(args); BindingData* binding_data = realm->GetBindingData();