6f232d4043
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Successful in 25s
ci-pangolin / Cleartext Scan — Android 禁明文 (push) Successful in 19s
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (push) Successful in 18s
ci-pangolin / Golden — 视觉回归 (全量:components/auth/desktop/tablet) (push) Failing after 13m6s
ci-pangolin / Go — integration (mysql/redis testcontainers) (push) Failing after 13m16s
ci-pangolin / E2E Smoke — L4 进程级端到端 (push) Failing after 13m25s
ci-pangolin / Go — build + test (push) Failing after 13m35s
ci-pangolin / DS-flow — 原型/跨端同源/代码色单源闸 (push) Failing after 13m45s
ci-pangolin / Codegen Drift — token 生成物未漂移 (push) Failing after 13m54s
ci-pangolin / Flutter — analyze + test (push) Failing after 14m5s
ci-pangolin / OpenAPI Sync Check (push) Failing after 14m16s
ci-pangolin / Lint — shellcheck (push) Failing after 14m27s
分支审核发现两处 Important,合并前修复。
① [安全] direct 的 ip_cidr 用户规则可自伤式旁路整条隧道:
Validate 原先只校 CIDR 语法。用户提交 ip_cidr=0.0.0.0/0 action=direct
(或 172.16.0.0/12,含隧道 DNS 172.19.0.2)会并入 TUN 入站
route_exclude_address(OS/auto_route 层,位于系统强制层之下),被排除的
流量根本不进 sing-box → hijack-dns 与整条隧道被静默旁路,违反「系统层
用户不可越」铁律。
- Validate: direct 的 ip_cidr 拒绝 catch-all(/0)及与保留段 172.16.0.0/12
重叠(写入闸)。
- clientconfig 渲染层:新增 routing.SafeToExclude 守卫,只有安全的 direct
ip_cidr 才并入 route_exclude_address(纵深防护,兜底写入闸之前的历史坏行)。
- 测试 TestValidateDirectIPCIDRReservedGuard 钉死:拒 catch-all/隧道段重叠、
放行 proxy catch-all 与不重叠 direct。
② [健壮性] 客户端保存失败静默回滚 + 抛未捕获异步异常 + 对话框无字段校验:
_persist 失败会 rethrow(约定调用方 catch),但屏幕层所有回调
(setMode/setBuiltin/addRule/removeRule/reorder/resetToDefault)均未 catch,
规则闪现即消失、无提示,且 rethrow 变 zone 未处理异常。
- 新增 _guardSave 守卫:await + 失败弹 SnackBar(AuthApiException 显服务端
双语文案含校验错,其余回退通用「保存失败」),包裹全部变更类回调。
- 添加规则对话框:_valueError 字段级预校验(ip_cidr 用 InternetAddress
校验、geo 白名单仅 cn),非法即禁用保存并内联红字提示;语义级(保留段)
仍由服务端权威判定经 SnackBar 呈现。
- l10n 单源新增 routingSaveFailed / routingRuleValueInvalid(6 语),regen。
go test ./... 全绿;flutter analyze 无 error;flutter test 265 全过无 golden 回归;
codegen 幂等、原型 i18n 无漂移。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A79VtQA1BwTuQN1ThpvYpo
117 lines
4.0 KiB
Go
117 lines
4.0 KiB
Go
package routing
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
// TestValidateEmptyErrorsSerializeAsArray guards the JSON shape API clients
|
|
// depend on: a valid profile's Validate() must marshal to `[]`, not `null`.
|
|
func TestValidateEmptyErrorsSerializeAsArray(t *testing.T) {
|
|
raw, err := json.Marshal(Default().Validate())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(raw) != "[]" {
|
|
t.Fatalf("want `[]`, got %s", raw)
|
|
}
|
|
}
|
|
|
|
func TestValidate(t *testing.T) {
|
|
ok := Default()
|
|
ok.Rules = []Rule{{Type: "domain_suffix", Value: "example.com", Action: "direct", Enabled: true}}
|
|
e := ok.Validate()
|
|
if len(e) != 0 {
|
|
t.Fatalf("valid profile got errors %v", e)
|
|
}
|
|
if e == nil {
|
|
t.Fatal("Validate must return a non-nil empty slice (serializes to [] not null)")
|
|
}
|
|
|
|
bad := Default()
|
|
bad.Mode = "weird" // 非法 mode
|
|
bad.Final = "reject" // final 只能 proxy|direct
|
|
bad.Rules = []Rule{
|
|
{Type: "ip_cidr", Value: "not-a-cidr", Action: "proxy", Enabled: true}, // CIDR 非法
|
|
{Type: "geosite", Value: "netflix", Action: "direct", Enabled: true}, // 不在白名单(仅 cn)
|
|
{Type: "bogus", Value: "x", Action: "direct", Enabled: true}, // type 非法
|
|
}
|
|
errs := bad.Validate()
|
|
if len(errs) < 5 {
|
|
t.Fatalf("want >=5 field errors, got %d: %v", len(errs), errs)
|
|
}
|
|
}
|
|
|
|
func TestValidateDirectIPCIDRReservedGuard(t *testing.T) {
|
|
// direct 的 ip_cidr 会并入 TUN route_exclude_address(系统层之下),catch-all 或
|
|
// 与隧道/DNS 保留段(172.16.0.0/12)重叠会静默旁路 hijack-dns/隧道 → 必须拒绝。
|
|
rejected := []Rule{
|
|
{Type: "ip_cidr", Value: "0.0.0.0/0", Action: "direct", Enabled: true},
|
|
{Type: "ip_cidr", Value: "::/0", Action: "direct", Enabled: true},
|
|
{Type: "ip_cidr", Value: "172.16.0.0/12", Action: "direct", Enabled: true},
|
|
{Type: "ip_cidr", Value: "172.19.0.0/16", Action: "direct", Enabled: true}, // 含隧道 DNS 172.19.0.2
|
|
}
|
|
for _, r := range rejected {
|
|
p := Default()
|
|
p.Rules = []Rule{r}
|
|
if errs := p.Validate(); len(errs) == 0 {
|
|
t.Errorf("direct ip_cidr %q 应被拒,却无报错", r.Value)
|
|
}
|
|
}
|
|
// proxy/reject 不入排除表故不受限;不与保留段重叠的 direct 允许。
|
|
allowed := []Rule{
|
|
{Type: "ip_cidr", Value: "0.0.0.0/0", Action: "proxy", Enabled: true},
|
|
{Type: "ip_cidr", Value: "10.0.0.0/8", Action: "direct", Enabled: true},
|
|
{Type: "ip_cidr", Value: "8.8.8.8/32", Action: "direct", Enabled: true},
|
|
}
|
|
for _, r := range allowed {
|
|
p := Default()
|
|
p.Rules = []Rule{r}
|
|
if errs := p.Validate(); len(errs) != 0 {
|
|
t.Errorf("ip_cidr %q action=%s 应允许,却报错 %v", r.Value, r.Action, errs)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateCountLimit(t *testing.T) {
|
|
p := Default()
|
|
for i := 0; i < 201; i++ {
|
|
p.Rules = append(p.Rules, Rule{Type: "domain", Value: "a.com", Action: "proxy", Enabled: true})
|
|
}
|
|
if e := p.Validate(); len(e) == 0 {
|
|
t.Fatal("want count-limit error")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeDedupAndTrim(t *testing.T) {
|
|
p := Default()
|
|
p.Rules = []Rule{
|
|
{Type: "domain", Value: " a.com ", Action: "proxy", Enabled: true},
|
|
{Type: "domain", Value: "a.com", Action: "proxy", Enabled: false},
|
|
{Type: "domain", Value: "b.com", Action: "proxy", Enabled: true},
|
|
}
|
|
p.Normalize()
|
|
if len(p.Rules) != 2 {
|
|
t.Fatalf("want 2 rules after dedup, got %d: %v", len(p.Rules), p.Rules)
|
|
}
|
|
if p.Rules[0].Value != "a.com" || p.Rules[1].Value != "b.com" {
|
|
t.Fatalf("want first-occurrence order preserved, got %v", p.Rules)
|
|
}
|
|
}
|
|
|
|
// TestNormalizeDedupNoSeparatorCollision guards against the historical
|
|
// string-concatenation dedup key ("type|value|action"): two distinct rules
|
|
// whose Value contains "|" could concatenate to the same string even though
|
|
// (type, value, action) differ. The struct-keyed dedup must tell them apart.
|
|
func TestNormalizeDedupNoSeparatorCollision(t *testing.T) {
|
|
p := Default()
|
|
p.Rules = []Rule{
|
|
{Type: "domain", Value: "a|b", Action: "proxy", Enabled: true},
|
|
{Type: "domain", Value: "a", Action: "b|proxy", Enabled: true},
|
|
}
|
|
p.Normalize()
|
|
if len(p.Rules) != 2 {
|
|
t.Fatalf("want 2 distinct rules preserved (no false collision), got %d: %v", len(p.Rules), p.Rules)
|
|
}
|
|
}
|