I have a root path represented by an std::filesystem::path
. I want to add some user provided filename to this path and make sure the resulting path is not out of the root directory.
For example:
std::filesystem::path root = "/foo/bar";
std::filesystem::path userFile = "ham/spam";
std::filesystem::path finalPath = root / userFile;
The final path is ok, it is inside /foo/bar
.
But if I give ../ham/spam
to the userFile
variable, this will result in a file outside the define rootPath
.
How can I check that the resulting file keeps inside its allowed boundaries ?
std::filesystem
library contained some function that computed a newabsolute
path, wouldn't it be useful? – Watkinsabsolute
(andcanonical
for that matter) both require the path to exist. Which it may not. – Rattlebrain