Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes to mounting path validation (fix #3733) #3908

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions include/multipass/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ QString backend_directory_path(const Path& path, const QString& subdirectory);
std::string filename_for(const std::string& path);
std::string contents_of(const multipass::Path& file_path);
bool invalid_target_path(const QString& target_path);
std::string make_abspath(const QString& input_path);
QTemporaryFile create_temp_file_with_path(const QString& filename_template);
void remove_directories(const std::vector<QString>& dirs);

Expand Down
4 changes: 2 additions & 2 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1928,7 +1928,7 @@ try // clang-format on
const auto q_target_path = path_entry.target_path().empty()
? MP_UTILS.default_mount_target(QString::fromStdString(request->source_path()))
: QDir::cleanPath(QString::fromStdString(path_entry.target_path()));
const auto target_path = q_target_path.toStdString();
const auto target_path = mp::utils::make_abspath(q_target_path);

auto it = operative_instances.find(name);
if (it == operative_instances.end())
Expand All @@ -1938,7 +1938,7 @@ try // clang-format on
}
auto& vm = it->second;

if (mp::utils::invalid_target_path(q_target_path))
if (mp::utils::invalid_target_path(QString::fromStdString(target_path)))
{
add_fmt_to(errors, "unable to mount to \"{}\"", target_path);
continue;
Expand Down
13 changes: 13 additions & 0 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <array>
#include <cassert>
#include <cctype>
#include <filesystem>
#include <fstream>
#include <optional>
#include <random>
Expand Down Expand Up @@ -150,6 +151,18 @@ bool mp::utils::valid_hostname(const std::string& name_string)
return matcher.match(QString::fromStdString(name_string)).hasMatch();
}

std::string mp::utils::make_abspath(const QString& input_path)
{
const fs::path base_path("/home/ubuntu"); // Base path for relative paths
const fs::path path_obj = input_path.toStdString(); // Convert QString to std::string

if (path_obj.is_absolute())
{
return path_obj.lexically_normal().string(); // Return as string if already absolute
}
return (fs::absolute(base_path / path_obj)).lexically_normal().string(); // Convert to absolute path
}

bool mp::utils::invalid_target_path(const QString& target_path)
{
QString sanitized_path{QDir::cleanPath(target_path)};
Expand Down