BCS*_*BCS 2 .net relative-path path-manipulation
我有一个rootPath我信任的relativePath,而我不信任.我希望以这样的方式组合它们,以确保结果不足rootPath并且用户不能使用它..来回到起点.我确实希望相对路径允许以下内容:hello\..\world==world
要展开:使用Path.Combine,然后对结果调用GetFullPath并检查该结果是否以rootPath开头.
它不会保护你免受硬链接,但它应该捕捉简单的事情,如双点.
以上代码:
string Resolve(string fileName)
{
string root = FileRoot();
string ret = Path.GetFullPath(Path.Combine(root, fileName));
if (ret.StartsWith(root.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar)) return ret;
throw new ArgumentException("path resolved to out of accesable directroy");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
848 次 |
| 最近记录: |