From a1fcb5241aced8d3d23dfb422761b88fca41350c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Mon, 29 Jun 2026 07:42:59 +0800 Subject: [PATCH] PathUtils.IsParentOf --- Shared/Utils/DirectoryUtils.cs | 5 ++--- Shared/Utils/FileUtils.cs | 8 +++----- Shared/Utils/PathUtils.cs | 9 +++++++++ Tests/General/ProgressTest.cs | 6 +++--- Tests/General/WorkerTest.cs | 2 +- Tests/Utils/FileUtilsTest.cs | 8 ++------ 6 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Shared/Utils/DirectoryUtils.cs b/Shared/Utils/DirectoryUtils.cs index 9dce17c..2e24fc6 100644 --- a/Shared/Utils/DirectoryUtils.cs +++ b/Shared/Utils/DirectoryUtils.cs @@ -169,7 +169,7 @@ public static class DirectoryUtils { folder = PathUtils.ForCompare(folder); if (folder == PathUtils.ForCompare(Path.GetPathRoot(folder))) throw new UnauthorizedAccessException($"不应操作磁盘根目录:{folder}"); - if (criticalFolders.Value.Any(f => f.StartsWithF(folder, ignoreCase: true))) + if (criticalFolders.Value.Any(f => PathUtils.IsParentOf(f, folder))) throw new UnauthorizedAccessException($"不应操作文件夹:{folder}"); } private static readonly Lazy> criticalFolders = new(() => new(new[] { @@ -179,7 +179,6 @@ public static class DirectoryUtils { Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Environment.GetFolderPath(Environment.SpecialFolder.Windows), Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads") // 下载文件夹没有 SpecialFolder 枚举 - }.Where(f => f.Contains(Path.VolumeSeparatorChar)) // 当缺少某个文件夹时,GetFolderPath 会返回空字符串(#8636) - .Select(f => PathUtils.ForCompare(f)))); + }.Where(f => f.Contains(Path.VolumeSeparatorChar)))); // 当缺少某个文件夹时,GetFolderPath 会返回空字符串(#8636) } diff --git a/Shared/Utils/FileUtils.cs b/Shared/Utils/FileUtils.cs index 5ac6f0b..480be2e 100644 --- a/Shared/Utils/FileUtils.cs +++ b/Shared/Utils/FileUtils.cs @@ -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) {} /// /// 将指定文件夹的内容打包为 zip 文件。 diff --git a/Shared/Utils/PathUtils.cs b/Shared/Utils/PathUtils.cs index 0616003..02d4753 100644 --- a/Shared/Utils/PathUtils.cs +++ b/Shared/Utils/PathUtils.cs @@ -178,6 +178,15 @@ public static class PathUtils { #endregion + /// + /// 判断 是否为 的父路径,或两者是否相同。 + /// + public static bool IsParentOf(string parentPath, string childPath) { + parentPath = PathUtils.ForCompare(parentPath); + childPath = PathUtils.ForCompare(childPath); + return childPath.StartsWithF(parentPath + @"\") || childPath == parentPath; + } + } /// diff --git a/Tests/General/ProgressTest.cs b/Tests/General/ProgressTest.cs index 9c6e403..2443269 100644 --- a/Tests/General/ProgressTest.cs +++ b/Tests/General/ProgressTest.cs @@ -6,7 +6,7 @@ public class ProgressTest : TestBase { public async Task ProgressChanged_主进度实际改变时触发() { var progress = new ProgressProvider(); int changedCount = 0; - progress.ProgressChanged += () => changedCount++; + progress.ProgressChanged += _ => changedCount++; progress.Set(0.2); progress.Set(0.2); @@ -21,7 +21,7 @@ public class ProgressTest : TestBase { public async Task ProgressChanged_子进度改变时向父级传播() { var progress = new ProgressProvider(); int changedCount = 0; - progress.ProgressChanged += () => changedCount++; + progress.ProgressChanged += _ => changedCount++; var sub = progress.SplitBy(0.5).Single(); sub.Set(0.5); @@ -34,7 +34,7 @@ public class ProgressTest : TestBase { public async Task ProgressChanged_不会触发Observe() { var progress = new ProgressProvider(); int changedCount = 0; - progress.ProgressChanged += () => changedCount++; + progress.ProgressChanged += _ => changedCount++; progress.Set(0.5); progress.Set(0.75, skiped: true); diff --git a/Tests/General/WorkerTest.cs b/Tests/General/WorkerTest.cs index 8bbcff7..b010097 100644 --- a/Tests/General/WorkerTest.cs +++ b/Tests/General/WorkerTest.cs @@ -32,7 +32,7 @@ public class WorkerTest : TestBase { if (currentRun == 1) { firstStarted.Set(); while (true) { - ct.ThrowIfCancellationRequested(); + ct?.ThrowIfCancellationRequested(); Thread.Sleep(10); } } diff --git a/Tests/Utils/FileUtilsTest.cs b/Tests/Utils/FileUtilsTest.cs index b38c61e..3993adc 100644 --- a/Tests/Utils/FileUtilsTest.cs +++ b/Tests/Utils/FileUtilsTest.cs @@ -30,14 +30,10 @@ public class FileUtilsTest : TestWithFolder { [Test] [Arguments("Corrupted.zip")] [Arguments("Not zip.zip")] - public void 解压_ReadBad(string testFile) - => Assert.Throws(() => FileUtils.ExtractToDirectory(GetTestFile(testFile), tempFolder)); - - [Test] [Arguments("DotDot ZipSlip.zip")] [Arguments("AbsPath ZipSlip.zip")] - public void 解压_ZipSlip(string testFile) - => Assert.Throws(() => FileUtils.ExtractToDirectory(GetTestFile(testFile), tempFolder)); + public void 解压_ReadBad(string testFile) + => Assert.Throws(() => FileUtils.ExtractToDirectory(GetTestFile(testFile), tempFolder)); #endregion