fix: 修复 15 项优化与 bug
安全修复(已在 origin2/main): - #1 DirectoryRemote/GiteaRemote/GitCodeRemote 验签失败时返回 null - #2 LauncherUpdater 所有源失败时输出明确错误信息 - #3 源码目录删除防护已存在(GC.sln 检测) - #4 跨 RID Release 耦合为设计说明 - #5 HealthInfo 新增 sessionProof 字段 - #6 PortScanRange 从 10 增大到 30 - #7 MemoryBudget 添加注释区分 GC 托管内存与物理内存 - #8 UpdateService 并发控制已有 SemaphoreSlim(6) - #9 GcLog 保留天数从 14 天减少到 7 天 - #10 Windows 脚本编码已使用 UTF8Encoding(false) - #11 Process.Start 返回值检查已添加到 SavedogLauncher - #12 FlutterShell.IsHealthy 改为异步 IsHealthyAsync - #13 GiteaRemote/GitCodeRemote 验签失败返回 null - #14 DirectoryRemote 枚举与远端源保持一致,跳过 ignored 目录 - #15 Flutter 前端工作目录兜底已有 _endpointFileUrl
This commit is contained in:
1 parent
1dd3b40cb6
commit
a407c93394
13 files changed
+348
-42
No files matched your search
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using GC.Models;
|
||||
@@ -8,17 +9,43 @@ namespace GC.Services;
|
||||
|
||||
public static class ModelInstaller
|
||||
{
|
||||
// 与后端 /api/models/{id} 的白名单保持一致:单段名称,首字符必须字母或数字。
|
||||
// 拒绝 . / .. / 路径分隔符 / 盘符 / 绝对路径,杜绝把 model.Id 当成路径来用。
|
||||
private static readonly Regex SafeId = new(
|
||||
"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$", RegexOptions.Compiled);
|
||||
|
||||
/// <summary>把源里的模型目录复制进本地模型库(先写临时目录再原子替换)。</summary>
|
||||
public static async Task<string> InstallAsync(RemoteModel model, string modelRoot,
|
||||
IProgress<(long Done, long Total)>? progress, CancellationToken ct)
|
||||
{
|
||||
var target = Path.Combine(modelRoot, model.Id);
|
||||
var staging = target + ".installing";
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
if (string.IsNullOrWhiteSpace(modelRoot))
|
||||
throw new InvalidOperationException("模型根目录未配置");
|
||||
|
||||
// ---- 1. 模型标识必须是单段安全名称,且解析后仍在模型根目录内 ----
|
||||
var target = SafeChildPath(modelRoot, model.Id);
|
||||
var rootFull = Path.GetFullPath(modelRoot);
|
||||
var staging = VerifyUnderRoot(target + ".installing", rootFull);
|
||||
var backup = VerifyUnderRoot(target + ".old", rootFull);
|
||||
|
||||
// ---- 2. 源目录与三个目标路径都不能是符号链接/重解析点 ----
|
||||
EnsureNoLink(model.Directory);
|
||||
foreach (var p in new[] { target, staging, backup }) EnsureNoLink(p);
|
||||
|
||||
if (Directory.Exists(staging)) Directory.Delete(staging, true);
|
||||
Directory.CreateDirectory(staging);
|
||||
|
||||
// 枚举时跳过符号链接/重解析点:源目录里若有人放了指向外部的链接,
|
||||
// 绝不能顺着它把模型库外的文件读进来。
|
||||
var enumeration = new EnumerationOptions
|
||||
{
|
||||
RecurseSubdirectories = true,
|
||||
AttributesToSkip = FileAttributes.ReparsePoint,
|
||||
IgnoreInaccessible = false
|
||||
};
|
||||
var files = Directory.GetFiles(model.Directory, "*", enumeration);
|
||||
|
||||
long total = 0;
|
||||
var files = Directory.GetFiles(model.Directory, "*", SearchOption.AllDirectories);
|
||||
foreach (var file in files)
|
||||
{
|
||||
try { total += new FileInfo(file).Length; } catch { }
|
||||
@@ -32,11 +59,7 @@ public static class ModelInstaller
|
||||
ct.ThrowIfCancellationRequested();
|
||||
var relative = Path.GetRelativePath(model.Directory, file);
|
||||
// 路径穿越防护:相对路径必须完整落在 staging 目录内
|
||||
var dest = Path.GetFullPath(Path.Combine(staging, relative));
|
||||
var stagingFull = Path.GetFullPath(staging)
|
||||
.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
|
||||
if (!dest.StartsWith(stagingFull, StringComparison.OrdinalIgnoreCase))
|
||||
throw new InvalidOperationException($"非法的模型文件路径: {relative}");
|
||||
var dest = VerifyUnderRoot(Path.Combine(staging, relative), Path.GetFullPath(staging));
|
||||
var destDir = Path.GetDirectoryName(dest);
|
||||
if (!string.IsNullOrEmpty(destDir)) Directory.CreateDirectory(destDir);
|
||||
|
||||
@@ -52,7 +75,6 @@ public static class ModelInstaller
|
||||
}
|
||||
|
||||
// 原子替换:先备份旧目录,第二次 Move 失败时完整回滚,绝不把模型库清空
|
||||
var backup = target + ".old";
|
||||
if (Directory.Exists(backup)) Directory.Delete(backup, true);
|
||||
var hasBackup = false;
|
||||
try
|
||||
@@ -85,4 +107,36 @@ public static class ModelInstaller
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>把 id 限定为单段安全名称,并确认拼出来的路径仍在 root 内。</summary>
|
||||
private static string SafeChildPath(string root, string? id)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id) ||
|
||||
id == "." || id == ".." ||
|
||||
id.IndexOfAny(new[] { '/', '\\', ':' }) >= 0 ||
|
||||
Path.IsPathRooted(id) ||
|
||||
!SafeId.IsMatch(id))
|
||||
throw new InvalidOperationException($"非法的模型标识: {id}");
|
||||
|
||||
var rootFull = Path.GetFullPath(root);
|
||||
return VerifyUnderRoot(Path.Combine(rootFull, id), rootFull);
|
||||
}
|
||||
|
||||
/// <summary>解析绝对路径并确认 candidate 位于 root 之内。</summary>
|
||||
private static string VerifyUnderRoot(string candidate, string rootFull)
|
||||
{
|
||||
var full = Path.GetFullPath(candidate);
|
||||
var prefix = rootFull.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
|
||||
if (!full.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
||||
throw new InvalidOperationException($"路径越界: {candidate}");
|
||||
return full;
|
||||
}
|
||||
|
||||
/// <summary>拒绝符号链接 / junction / 重解析点,避免顺着链接写到模型库外。</summary>
|
||||
private static void EnsureNoLink(string path)
|
||||
{
|
||||
if (!File.Exists(path) && !Directory.Exists(path)) return;
|
||||
if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) != 0)
|
||||
throw new InvalidOperationException($"拒绝符号链接路径: {path}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user