回跳目标健壮性:IsAllowedReturnTarget 拒绝含控制字符(0x00-0x1F/0x7F)的畸形地址,防 Uri 解析/302 序列化异常导致 500
build / build-push (push) Successful in 27s

This commit is contained in:
wanghu
2026-09-04 11:53:57 +08:00
parent 986b24180a
commit 70ccd1cb8e
@@ -269,6 +269,9 @@ namespace YhCore.WebApi.Auth
/// 2. 完整 http(s) URL:host 必须与 service 根 host(AppBaseUrl 或当前请求)一致——供前端页完整
/// 地址(yschreferer 携带,含 #/ 路由片段)作回跳目标,杜绝开放重定向到任意外站。
/// 其余情况(null/空、非 http(s) 协议、外站 host)一律拒绝。
/// 防御:含控制字符(0x00-0x1F / 0x7F)的目标一律拒绝——hex 解码产物(平台 char 维度 hex 对
/// 非 ASCII 码点有损,可能还原出控制字符)或手工构造的畸形 URL,Uri 解析/302 Location 序列化
/// 都可能抛异常导致 500,此处前置拦截并安全降级(调用方回根路径或原 URL)。
/// </summary>
private static bool IsAllowedReturnTarget(string url, string allowedHost)
{
@@ -276,6 +279,13 @@ namespace YhCore.WebApi.Auth
{
return false;
}
foreach (var ch in url)
{
if (ch < 0x20 || ch == 0x7f)
{
return false;
}
}
if (url[0] == '/')
{
return !url.StartsWith("//", StringComparison.Ordinal)