fix(security): 加固模型安装与本地目录源,防止同机后端伪装
- ModelInstaller: 模型 Id 限定为单段安全名称,拒绝路径分隔符/绝对路径/ 符号链接,源目录枚举跳过重解析点,防止写到模型库外 - DirectoryRemote: 清单验签失败如实记录;包 URL 仅允许 http(s) 公网地址, 拦截 file:/私有 IP/环回/元数据地址(SSRF);SafePath 逐级解析符号链接 并确认仍在根目录内 - 本机后端健康检查改为一次性会话令牌双向校验(/api/health 返回指纹), 同机伪造服务无法通过身份确认;所有 /api 请求需带令牌,封面图走 query 只含 7 个安全文件,基于最新 origin2/main(含已合入的 #32 课件)。
This commit is contained in:
1 parent
1dd3b40cb6
commit
46ed84475d
7 files changed
+305
-34
No files matched your search
@@ -169,6 +169,35 @@ public static class BackendServer
|
||||
await next();
|
||||
});
|
||||
|
||||
// 会话令牌校验:启动器拉起时经 GC_UI_TOKEN 注入一次性令牌,前端每个 /api 请求
|
||||
// 都必须带回。这样同机其他进程即使占了端口、伪造 /api/health 返回 200,也过不了
|
||||
// 这一关——它拿不到令牌。独立进程 / 调试模式未设置令牌时放行,保持兼容。
|
||||
var sessionToken = Environment.GetEnvironmentVariable(GcUiContract.TokenEnvVar);
|
||||
if (!string.IsNullOrEmpty(sessionToken))
|
||||
{
|
||||
var expected = System.Text.Encoding.UTF8.GetBytes(sessionToken);
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
if (context.Request.Path.StartsWithSegments("/api"))
|
||||
{
|
||||
// 正常请求从请求头取;封面图由 Image.network 发起、无法加自定义头,
|
||||
// 用同名 query 参数携带(GET 只读资源,参数不会落到日志正文)。
|
||||
var supplied = context.Request.Headers[GcUiContract.TokenHeader].ToString();
|
||||
if (string.IsNullOrEmpty(supplied))
|
||||
supplied = context.Request.Query[GcUiContract.TokenHeader].ToString();
|
||||
var actual = System.Text.Encoding.UTF8.GetBytes(supplied);
|
||||
// 长度不等时 CryptographicOperations 仍安全比较,但先比对长度省一次分支泄漏。
|
||||
if (actual.Length != expected.Length ||
|
||||
!System.Security.Cryptography.CryptographicOperations.FixedTimeEquals(actual, expected))
|
||||
{
|
||||
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
||||
return;
|
||||
}
|
||||
}
|
||||
await next();
|
||||
});
|
||||
}
|
||||
|
||||
// ---- 健康检查 -------------------------------------------------------------
|
||||
|
||||
app.MapGet("/api/health", () => Results.Ok(new HealthDto(
|
||||
@@ -176,7 +205,8 @@ public static class BackendServer
|
||||
LauncherRuntimeInfo.CurrentLauncherVersion,
|
||||
LauncherRuntimeInfo.CurrentRid,
|
||||
host.IsAvailable,
|
||||
host.LastError)));
|
||||
host.LastError,
|
||||
GcUiContract.DeriveSessionProof(sessionToken))));
|
||||
|
||||
// ---- 已安装模型 -----------------------------------------------------------
|
||||
|
||||
|
||||
Reference in new issue
Block a user