PathUtils.IsParentOf
This commit is contained in:
1 parent
0a3b21505a
commit
a1fcb5241a
6 files changed
+20
-18
No files matched your search
@@ -1,4 +1,3 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.IO.Compression;
|
||||
|
||||
namespace MeloongCore;
|
||||
@@ -359,15 +358,14 @@ public static class FileUtils {
|
||||
if (totalCount > 0) progressHandler?.Invoke((double) doneCount / totalCount);
|
||||
if (string.IsNullOrEmpty(entry.Name)) continue; // 跳过文件夹条目(ZipArchive 会将文件夹也作为一个 entry,但它们的 Name 为空)
|
||||
// ZipSlip 修复
|
||||
string outputFilePath = PathUtils.ForCompare(Path.Combine(outputDirectory, entry.FullName));
|
||||
if (!outputFilePath.StartsWithF(PathUtils.AddSlashSuffix(PathUtils.ForCompare(outputDirectory)), ignoreCase:true))
|
||||
throw new ZipSlipException($"Zip 文件项 {entry.FullName} 的路径在压缩包之外,这可能导致安全问题");
|
||||
string outputFilePath = Path.Combine(outputDirectory, entry.FullName);
|
||||
if (!PathUtils.IsParentOf(outputDirectory, outputFilePath))
|
||||
throw new InvalidDataException($"Zip 文件项 {entry.FullName} 的路径在压缩包之外,这可能导致安全问题");
|
||||
// 实际的解压
|
||||
using var entryStream = entry.Open();
|
||||
FileUtils.Write(outputFilePath, entryStream);
|
||||
}
|
||||
}
|
||||
public class ZipSlipException(string message) : Exception(message) {}
|
||||
|
||||
/// <summary>
|
||||
/// 将指定文件夹的内容打包为 zip 文件。
|
||||
|
||||
Reference in new issue
Block a user