Răsfoiți Sursa

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 <yuji.hirose.bug@gmail.com>
metsw24-max 1 zi în urmă
părinte
comite
654fd3546c
2 a modificat fișierele cu 72 adăugiri și 3 ștergeri
  1. 29 3
      httplib.h
  2. 43 0
      test/test.cc

+ 29 - 3
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, &params);
+  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<DWORD>(wbuf.size()), FILE_NAME_NORMALIZED);
+  if (len >= wbuf.size()) {
+    wbuf.resize(len);
+    len = ::GetFinalPathNameByHandleW(
+        h, &wbuf[0], static_cast<DWORD>(wbuf.size()), FILE_NAME_NORMALIZED);
+  }
+  if (len == 0 || len >= wbuf.size()) { return false; }
+
+  auto n = ::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), static_cast<int>(len),
+                                 nullptr, 0, nullptr, nullptr);
+  if (n <= 0) { return false; }
+  resolved.assign(static_cast<size_t>(n), '\0');
+  ::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), static_cast<int>(len),
+                        &resolved[0], n, nullptr, nullptr);
 #elif defined(PATH_MAX)
   char buf[PATH_MAX];
   if (realpath(path, buf) == nullptr) { return false; }

+ 43 - 0
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