添加GC-SAVEDOG项目,降低隐患

This commit is contained in:
2509_92447106 committed 2026-11-14 19:42:02 +08:00
1 parent 791db4fe12
commit 2f3a8682c9
54 files changed
+3642 -320

No files matched your search

+9 -23
View File
@@ -1,36 +1,22 @@
using System;
using System.IO;
using System.IO.Compression;
namespace GC.Services;
/// <summary>
/// 安全解压:拒绝 zip slip(条目名含 ../、绝对路径、盘符),
/// 并在解压前逐条校验目标路径完整落在目标目录内。
/// 安全解压的统一入口(实现在 <see cref="SafeArchive"/>)。
/// 保留这个名字是因为模型源导入 / 启动器更新两处都在用,
/// 真正的校验逻辑集中在一处,避免两套实现走偏。
/// </summary>
public static class ZipSafety
{
public static void ExtractToDirectory(string zipPath, string destination)
{
var destFull = Path.GetFullPath(destination)
.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
using (var archive = ZipFile.OpenRead(zipPath))
{
foreach (var entry in archive.Entries)
{
var name = entry.FullName.Replace('\\', '/');
if (name.StartsWith('/') || name.Contains("..", StringComparison.Ordinal) ||
(name.Length >= 2 && name[1] == ':'))
throw new InvalidOperationException($"压缩包含非法路径条目: {entry.FullName}");
var target = Path.GetFullPath(Path.Combine(destFull, name));
if (!target.StartsWith(destFull, StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException($"压缩包条目越界: {entry.FullName}");
}
}
// 校验通过后交给框架解压(保留 overwrite 语义)
ZipFile.ExtractToDirectory(zipPath, destination, true);
if (!File.Exists(zipPath))
throw new FileNotFoundException("压缩包不存在", zipPath);
SafeArchive.ExtractToDirectory(zipPath, destination);
}
/// <summary>只检查不落盘:给下载完成后的预检用。</summary>
public static SafeArchive.Inspection Inspect(string zipPath) => SafeArchive.Inspect(zipPath);
}