Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,12 @@ std::optional<std::string> FileURLToPath(Environment* env,
return "\\\\" + ada::idna::to_unicode(hostname) + decoded_pathname;
}

if (decoded_pathname.size() < 3) {
THROW_ERR_INVALID_FILE_URL_PATH(env->isolate(),
"File URL path must be absolute");
return std::nullopt;
}

char letter = decoded_pathname[1] | 0x20;
char sep = decoded_pathname[2];

Expand Down
24 changes: 24 additions & 0 deletions test/cctest/test_path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "v8.h"

using node::BufferValue;
using node::NormalizeFileURLOrPath;
using node::PathResolve;
using node::ToNamespacedPath;

Expand Down Expand Up @@ -93,3 +94,26 @@ TEST_F(PathTest, ToNamespacedPath) {
EXPECT_EQ(data.ToStringView(), "hello world"); // Input should not be mutated
#endif
}

#ifdef _WIN32
TEST_F(PathTest, NormalizeShortFileURLPath) {
const v8::HandleScope handle_scope(isolate_);
Argv argv;
Env env{handle_scope, argv, node::EnvironmentFlags::kNoBrowserGlobals};
v8::TryCatch try_catch(isolate_);

EXPECT_EQ(NormalizeFileURLOrPath(*env, "file:///"), "");
ASSERT_TRUE(try_catch.HasCaught());

v8::Local<v8::Value> exception = try_catch.Exception();
ASSERT_TRUE(exception->IsObject());
v8::Local<v8::Value> code;
ASSERT_TRUE(exception.As<v8::Object>()
->Get((*env)->context(),
v8::String::NewFromUtf8Literal(isolate_, "code"))
.ToLocal(&code));
ASSERT_TRUE(code->IsString());
node::Utf8Value code_value(isolate_, code);
EXPECT_EQ(code_value.ToStringView(), "ERR_INVALID_FILE_URL_PATH");
}
#endif