C ++检查路径是否在给定目录之外

C++ check if path is outside a given directory

检查给定路径 A 是否在另一条路径 B 之外的最简单方法是什么? 即:判断foo/../../bar/是否在foo/.

之外

像这样的东西应该有用。另请注意,这两条路径都应该存在。

#include <filesystem>
#include <algorithm>
#include <iterator>
#include <cassert>

bool isSafePath(const std::filesystem::path &root, const std::filesystem::path &child) {
    auto const normRoot = std::filesystem::canonical(root);
    auto const normChild = std::filesystem::canonical(child);
    
    auto itr = std::search(normChild.begin(), normChild.end(), 
                           normRoot.begin(), normRoot.end());
    
    return itr == normChild.begin();
}

int main(int argc, char **argv)
{
    assert(isSafePath("www/root/nvevg", "www/root/nvevg/../../../www/root/nvevg/index.html"));
    assert(isSafePath("www/root/nvevg", "www/root/nvevg/../../../www/root/nvevg"));
    assert(isSafePath("/home/nvevg/projects/davshare/apps/", "/home/nvevg/projects/davshare/apps/../apps/CMakeLists.txt"));
    
    assert(not isSafePath("/home/nvevg/projects/davshare/apps/", "/home/nvevg/projects/davshare/apps/../../../../../etc/shadow"));
    assert(not isSafePath("/home/nvevg/projects/davshare/apps/", "/home/nvevg/projects/davshare/apps/../CMakeLists.txt"));
    assert(not isSafePath("www/root/nvevg", "www/root/nvevg/../../../www/root/"));
    assert(not isSafePath("www/root/nvevg", "www/root/nvevg/../../../www/"));
    assert(not isSafePath("www/root/nvevg", "www/root/nvevg/../../../../../../../../../etc/fstab"));
    
    return 0;
}

有一个函数 returns 传递的两个函数之间的相对路径称为 relative。您可以检查结果路径是否以 ..

开头
bool isSubPath(const std::string& base, const std::string& destination)
{
    std::string relative = std::filesystem::relative(destination, base);
    // size check for "." result
    // if path starts with ".." it's not subdir
    return relative.size() == 1 || relative[0] != '.' && relative[1] != '.';
}