From 73d58b9f521a2cd9799880f20bb3945c67636762 Mon Sep 17 00:00:00 2001 From: Wirlaburla Date: Wed, 1 Jan 2025 14:12:17 -0600 Subject: [PATCH] Fixed traversing through symlinks with external symlinks on --- src/plugins/filer_local/filer_local.cpp | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/plugins/filer_local/filer_local.cpp b/src/plugins/filer_local/filer_local.cpp index 4f69913..c4554b5 100644 --- a/src/plugins/filer_local/filer_local.cpp +++ b/src/plugins/filer_local/filer_local.cpp @@ -106,10 +106,7 @@ public: if (fs::is_symlink(*target) && resolve_symlink) { fs::path resolved_sym = fs::weakly_canonical(*target); logger->print(LOGLEVEL_DEBUG, "Resolved symlink path: %s", resolved_sym.c_str()); - if (!resolve_ext_symlinks) { - if (!resolved_sym.string().starts_with(root.string())) - return false; - } else *target = resolved_sym; + return resolve_ext_symlinks ? true : resolved_sym.string().starts_with(root.string()); } else if (!(*target).string().starts_with(root.string())) return false; @@ -126,7 +123,7 @@ public: struct file_data fd; try { fs::path requested_path; - if (!resolvePath(dir, &requested_path)) { + if (!resolvePath(dir, &requested_path, false)) { fd.error = file_error{FilerStatusCodes::NoPermission, "Invalid Permissions"}; return fd; } @@ -148,18 +145,11 @@ public: return fd; } - // Make sure path is within root directory - fs::path rel_path = fs::relative(requested_path, root); - if (rel_path.string().find("..") == 0) { - fd.error = file_error{FilerStatusCodes::NoPermission, "Invalid Permissions"}; - return fd; - } - // Update current working directory relative to root - cwd = "/" + rel_path.string(); + cwd = "/" + fs::path(requested_path).lexically_relative(root).string(); + fd.path = strdup(requested_path.c_str()); fd.relpath = strdup(fs::relative(requested_path, root).c_str()); - } catch (const fs::filesystem_error& ex) { logger->print(LOGLEVEL_ERROR, "Path fallback: %s", ex.what()); fd.error = file_error{FilerStatusCodes::Exception, ex.what()}; @@ -395,6 +385,8 @@ public: char* line; fs::perms fperms = fs::symlink_status(p).permissions(); bool is_link = S_ISLNK(fstat.st_mode); + std::string link_path; + if (is_link) link_path = fs::read_symlink(p).lexically_relative(root).string(); asprintf( &line, "%c%c%c%c%c%c%c%c%c%c %4u %4u %4u %12u %s %s%s\r\n", @@ -414,7 +406,7 @@ public: fstat.st_size, timebuff, p.path().filename().c_str(), - is_link?(" -> " + fs::read_symlink(p).string()).c_str():"" + is_link?(" -> " + link_path).c_str():"" ); listStream << std::string(line);