Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
restored_path.hpp
Go to the documentation of this file.
1#pragma once
2
19
21
22#include <filesystem>
23#include <optional>
24#include <system_error>
25
26namespace pulp::audio {
27
34inline std::optional<std::filesystem::path>
35confine_restored_path(const std::filesystem::path& path, const std::filesystem::path& root) {
36 namespace fs = std::filesystem;
37 if (path.empty() || root.empty())
38 return std::nullopt;
39 std::error_code ec;
40 const fs::path canonical_root = fs::weakly_canonical(root, ec);
41 if (ec || canonical_root.empty())
42 return std::nullopt;
43 const fs::path joined = path.is_absolute() ? path : canonical_root / path;
44 const fs::path canonical = fs::weakly_canonical(joined, ec);
45 if (ec || canonical.empty())
46 return std::nullopt;
47 if (!pulp::runtime::is_within_directory(canonical_root, canonical))
48 return std::nullopt;
49 return canonical;
50}
51
52} // namespace pulp::audio
Definition analyzer_provider.hpp:12
std::optional< std::filesystem::path > confine_restored_path(const std::filesystem::path &path, const std::filesystem::path &root)
Resolve a path restored from state against the directory the plugin is allowed to read from.
Definition restored_path.hpp:35
bool is_within_directory(const std::filesystem::path &directory, const std::filesystem::path &candidate)
True if candidate, compared lexically after normalization, is directory itself or lies beneath it.
Definition safe_relative_path.hpp:43
Screening for untrusted relative paths that will be joined beneath a destination directory: archive e...