From 654fd3546c62f14178e2e8f02dcab2e6a4fcca28 Mon Sep 17 00:00:00 2001 From: metsw24-max Date: Tue, 17 Nov 2026 07:49:39 +0530 Subject: [PATCH] resolve junctions and symlinks in canonicalize_path on Windows (#2670) * resolve junctions and symlinks in canonicalize_path on Windows * Shorten the canonicalize_path comment --------- Co-authored-by: yhirose --- httplib.h | 32 +++++++++++++++++++++++++++++--- test/test.cc | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 56fc405f..6d1d8e0f 100644 --- a/httplib.h +++ b/httplib.h @@ -6111,9 +6111,35 @@ inline bool is_valid_path(const std::string &path) { inline bool canonicalize_path(const char *path, std::string &resolved) { #if defined(_WIN32) - char buf[_MAX_PATH]; - if (_fullpath(buf, path, _MAX_PATH) == nullptr) { return false; } - resolved = buf; + // Resolve junctions and symlinks, as realpath() does + auto wpath = u8string_to_wstring(path); + if (wpath.empty()) { return false; } + + CREATEFILE2_EXTENDED_PARAMETERS params{}; + params.dwSize = sizeof(params); + params.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS; // Needed to open a directory + auto h = ::CreateFile2(wpath.c_str(), 0, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + OPEN_EXISTING, ¶ms); + if (h == INVALID_HANDLE_VALUE) { return false; } + auto guard = scope_exit([&]() { ::CloseHandle(h); }); + + std::wstring wbuf(MAX_PATH, L'\0'); + auto len = ::GetFinalPathNameByHandleW( + h, &wbuf[0], static_cast(wbuf.size()), FILE_NAME_NORMALIZED); + if (len >= wbuf.size()) { + wbuf.resize(len); + len = ::GetFinalPathNameByHandleW( + h, &wbuf[0], static_cast(wbuf.size()), FILE_NAME_NORMALIZED); + } + if (len == 0 || len >= wbuf.size()) { return false; } + + auto n = ::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), static_cast(len), + nullptr, 0, nullptr, nullptr); + if (n <= 0) { return false; } + resolved.assign(static_cast(n), '\0'); + ::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), static_cast(len), + &resolved[0], n, nullptr, nullptr); #elif defined(PATH_MAX) char buf[PATH_MAX]; if (realpath(path, buf) == nullptr) { return false; } diff --git a/test/test.cc b/test/test.cc index 8c4f44b0..de594452 100644 --- a/test/test.cc +++ b/test/test.cc @@ -30712,6 +30712,49 @@ TEST(SymlinkTest, SymlinkEscapeFromBaseDirectory) { ASSERT_TRUE(res) << "Error: " << to_string(res.error()); EXPECT_EQ(StatusCode::Forbidden_403, res->status); } +#else +TEST(SymlinkTest, JunctionEscapeFromBaseDirectory) { + auto secret_dir = std::string("junction_test_secret"); + auto served_dir = std::string("junction_test_served"); + auto secret_file = secret_dir + "\\secret.txt"; + auto junction_path = served_dir + "\\escape"; + + ::CreateDirectoryA(secret_dir.c_str(), nullptr); + ::CreateDirectoryA(served_dir.c_str(), nullptr); + + { + std::ofstream ofs(secret_file); + ofs << "SECRET_DATA"; + } + + // A junction needs no privilege to create, unlike a symlink + auto cmd = "mklink /J " + junction_path + " " + secret_dir + " >nul"; + ASSERT_EQ(0, std::system(cmd.c_str())); + + auto se = detail::scope_exit([&] { + ::RemoveDirectoryA(junction_path.c_str()); + std::remove(secret_file.c_str()); + ::RemoveDirectoryA(served_dir.c_str()); + ::RemoveDirectoryA(secret_dir.c_str()); + }); + + Server svr; + svr.set_mount_point("/", served_dir); + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se2 = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + }); + svr.wait_until_ready(); + + Client cli("localhost", PORT); + + // Junction pointing outside base dir should be blocked + auto res = cli.Get("/escape/secret.txt"); + ASSERT_TRUE(res) << "Error: " << to_string(res.error()); + EXPECT_EQ(StatusCode::Forbidden_403, res->status); +} #endif // Sends `outer_head` (with every "{len}" replaced by the length of an embedded