Skip to content

Commit

Permalink
ftp_server: remove leading absolute path slashes
Browse files Browse the repository at this point in the history
QGC requests paths with a leading /. If we just remove it, that works
inside of the defined root path.

Signed-off-by: Julian Oes <[email protected]>
  • Loading branch information
julianoes committed Nov 2, 2023
1 parent eb3deb9 commit 62874de
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/mavsdk/core/mavlink_ftp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,22 @@ MavlinkFtpServer::_path_from_string(const std::string& payload_path)
return ServerResult::ERR_FAIL;
}

fs::path combined_path = (fs::path(_root_dir) / payload_path).lexically_normal();
// Take a copy before we mess with it.
auto temp_path = payload_path;

// We strip leading slashes (like absolute paths).
if (temp_path.size() >= 1 && temp_path[0] == '/') {
temp_path = temp_path.substr(1, temp_path.size());
}

fs::path combined_path = (fs::path(_root_dir) / temp_path).lexically_normal();

// Check whether the combined path is inside the root dir.
// From: https://stackoverflow.com/a/61125335/8548472
auto ret = std::mismatch(_root_dir.begin(), _root_dir.end(), combined_path.string().begin());
if (ret.first != _root_dir.end()) {
LogWarn() << "Not inside root dir";
LogWarn() << "Not inside root dir: " << combined_path.string()
<< ", root dir: " << _root_dir;
return ServerResult::ERR_FAIL;
}

Expand Down

0 comments on commit 62874de

Please sign in to comment.