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,7 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -29,10 +32,22 @@ public sealed class DirectoryRemote : IModelRemote
|
||||
var file = File.Exists(primary) ? primary : fallback;
|
||||
if (!File.Exists(file)) return Task.FromResult<RepoVersion?>(null);
|
||||
|
||||
// 离线目录同样验签:U 盘 / 共享盘里的清单也可能是被替换过的
|
||||
// 离线目录同样验签:U 盘 / 共享盘里的清单也可能是被替换过的。
|
||||
// 验签失败或无签名时直接拒绝:本地源是最高风险场景(用户可能插了恶意 U 盘),
|
||||
// 不能把"签名无效但内容可读"的清单交给上层——那是安全盲点。
|
||||
var text = File.ReadAllText(file);
|
||||
_rawManifest = text;
|
||||
var version = ManifestLoader.Parse(text, out _);
|
||||
var version = ManifestLoader.Parse(text, out var verification);
|
||||
if (verification.Trust is ManifestTrust.Invalid)
|
||||
{
|
||||
Console.Error.WriteLine($"[directory] 清单验签失败: {verification.Error}");
|
||||
return Task.FromResult<RepoVersion?>(null);
|
||||
}
|
||||
if (verification.Trust == ManifestTrust.Unsigned)
|
||||
{
|
||||
Console.Error.WriteLine($"[directory] 清单未签名,拒绝加载");
|
||||
return Task.FromResult<RepoVersion?>(null);
|
||||
}
|
||||
if (version != null && !string.IsNullOrWhiteSpace(version.Launcher.Version))
|
||||
_launcherVersion = version.Launcher.Version.Trim();
|
||||
return Task.FromResult(version);
|
||||
@@ -57,6 +72,8 @@ public sealed class DirectoryRemote : IModelRemote
|
||||
if (depth > 8) return;
|
||||
foreach (var sub in Directory.GetDirectories(dir))
|
||||
{
|
||||
// 符号链接目录可能指向根目录之外:既不递归进去,也不作为条目暴露。
|
||||
if (IsReparsePoint(sub)) continue;
|
||||
var name = Path.GetFileName(sub);
|
||||
if (name.StartsWith('.')) continue;
|
||||
var rel = Relative(baseDir, sub);
|
||||
@@ -71,6 +88,7 @@ public sealed class DirectoryRemote : IModelRemote
|
||||
}
|
||||
foreach (var file in Directory.GetFiles(dir))
|
||||
{
|
||||
if (IsReparsePoint(file)) continue;
|
||||
var name = Path.GetFileName(file);
|
||||
if (name.StartsWith('.')) continue;
|
||||
result.Add(new RepoEntry
|
||||
@@ -106,16 +124,81 @@ public sealed class DirectoryRemote : IModelRemote
|
||||
|
||||
var url = package.Url;
|
||||
if (string.IsNullOrWhiteSpace(url)) return null;
|
||||
// 离线 U 盘源与 ./build.sh update-test 都会用本地 zip 路径,必须保持兼容。
|
||||
|
||||
// 清单里的 URL 属于外部输入,必须约束来源:
|
||||
// · file:// 和绝对路径只允许读 _root 之内,杜绝任意本地文件读取;
|
||||
// · 网络 URL 仅允许 http(s),且目标不能是环回/私有/链路本地/元数据地址,防 SSRF。
|
||||
if (url!.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
|
||||
return await File.ReadAllBytesAsync(new Uri(url).LocalPath, ct);
|
||||
if (Path.IsPathRooted(url) && File.Exists(url))
|
||||
return await File.ReadAllBytesAsync(url, ct);
|
||||
if (Uri.TryCreate(url, UriKind.Absolute, out _))
|
||||
{
|
||||
var local = new Uri(url).LocalPath;
|
||||
return await File.ReadAllBytesAsync(SafePath(Path.GetRelativePath(_root, local)), ct);
|
||||
}
|
||||
if (Path.IsPathRooted(url))
|
||||
{
|
||||
if (File.Exists(url)) return await File.ReadAllBytesAsync(SafePath(Path.GetRelativePath(_root, url)), ct);
|
||||
return null;
|
||||
}
|
||||
if (IsAllowedHttpUrl(url))
|
||||
return await Http.GetByteArrayAsync(url, ct);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 仅允许 http/https:挡住 file: 导致的任意本地文件读取和 gopher: 之类的协议滥用。
|
||||
/// IP 字面量直接判公网;主机名(清单里的正常发布 URL)允许,但拒绝 localhost 与
|
||||
/// 常见本地后缀,避免指向本机。域名解析到内网的情况由 HttpClient 侧的实际连接地址
|
||||
/// 兜底——本地目录源的主路径本就是本地文件,网络只是兜底。
|
||||
/// </summary>
|
||||
private static bool IsAllowedHttpUrl(string url)
|
||||
{
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) return false;
|
||||
if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps) return false;
|
||||
|
||||
if (uri.HostNameType == UriHostNameType.IPv4 || uri.HostNameType == UriHostNameType.IPv6)
|
||||
return IsPublicAddress(uri.Host);
|
||||
|
||||
var host = uri.Host;
|
||||
if (string.IsNullOrEmpty(host)) return false;
|
||||
if (host.Equals("localhost", StringComparison.OrdinalIgnoreCase)) return false;
|
||||
// 仅允许含点、且各段是字母数字/连字符的常规域名;拒绝 .local/.internal 等本地域。
|
||||
if (host.EndsWith(".local", StringComparison.OrdinalIgnoreCase) ||
|
||||
host.EndsWith(".localhost", StringComparison.OrdinalIgnoreCase) ||
|
||||
host.EndsWith(".internal", StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
foreach (var label in host.Split('.'))
|
||||
if (label.Length == 0 || label[0] == '-' || label[^1] == '-' ||
|
||||
!label.All(c => char.IsAsciiLetterOrDigit(c) || c == '-'))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsPublicAddress(string host)
|
||||
{
|
||||
if (!IPAddress.TryParse(host, out var ip)) return false;
|
||||
if (IPAddress.IsLoopback(ip) || IPAddress.Any.Equals(ip) || IPAddress.IPv6Any.Equals(ip)) return false;
|
||||
|
||||
if (ip.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
var b = ip.GetAddressBytes();
|
||||
if (b[0] == 10 || b[0] == 127 || b[0] == 0) return false; // 私有 / 环回 / 本网
|
||||
if (b[0] == 169 && b[1] == 254) return false; // 链路本地 / 云元数据
|
||||
if (b[0] == 172 && b[1] >= 16 && b[1] <= 31) return false; // 私有
|
||||
if (b[0] == 192 && b[1] == 168) return false; // 私有
|
||||
if (b[0] == 100 && b[1] >= 64 && b[1] <= 127) return false; // CGNAT (IPv4 共享地址)
|
||||
if (b[0] == 192 && b[1] == 0) return false; // IETF 协议分配
|
||||
if (b[0] == 198 && (b[1] == 18 || b[1] == 19)) return false; // 基准测试
|
||||
if (b[0] >= 224) return false; // 组播 / 保留
|
||||
}
|
||||
else if (ip.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
if (ip.IsIPv6LinkLocal || ip.IsIPv6SiteLocal || ip.IsIPv6Multicast) return false;
|
||||
var b = ip.GetAddressBytes();
|
||||
if (b[0] == 0xfd || b[0] == 0xfc) return false; // 唯一本地地址
|
||||
if (ip.IsIPv4MappedToIPv6) return IsPublicAddress(ip.MapToIPv4().ToString());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>在 dist/<系统>-<架构>/ 里找这个平台的启动器包;找不到返回 null。</summary>
|
||||
private string? FindInDist(RepoPackage package)
|
||||
{
|
||||
@@ -137,6 +220,7 @@ public sealed class DirectoryRemote : IModelRemote
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>把相对路径解析成 _root 内的真实路径:拒绝越界,且逐级拒绝符号链接。</summary>
|
||||
private string SafePath(string relative)
|
||||
{
|
||||
var full = Path.GetFullPath(Path.Combine(_root, relative));
|
||||
@@ -144,9 +228,38 @@ public sealed class DirectoryRemote : IModelRemote
|
||||
if (!full.Equals(_root, StringComparison.OrdinalIgnoreCase) &&
|
||||
!full.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
||||
throw new InvalidOperationException($"路径越界: {relative}");
|
||||
|
||||
// 词法校验之后还要解析符号链接:root 内的链接可能指向根目录之外。
|
||||
// 逐级检查 _root 到 full 的每一段,任何重解析点都直接拒绝。
|
||||
var rel = Path.GetRelativePath(_root, full);
|
||||
if (rel != "." && rel != "")
|
||||
{
|
||||
var cursor = _root;
|
||||
foreach (var segment in rel.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
if (segment == "..") throw new InvalidOperationException($"路径越界: {relative}");
|
||||
cursor = Path.Combine(cursor, segment);
|
||||
if (IsReparsePoint(cursor))
|
||||
throw new InvalidOperationException($"路径含符号链接,已拒绝: {relative}");
|
||||
}
|
||||
}
|
||||
return full;
|
||||
}
|
||||
|
||||
private static bool IsReparsePoint(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(path) && !Directory.Exists(path)) return false;
|
||||
return (File.GetAttributes(path) & FileAttributes.ReparsePoint) != 0;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 取不到属性时按不可信处理,避免绕过符号链接检查。
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static string Relative(string baseDir, string file) =>
|
||||
Path.GetRelativePath(baseDir, file).Replace('\\', '/');
|
||||
}
|
||||
Reference in new issue
Block a user