diff --git a/server/internal/agentd/acl_test.go b/server/internal/agentd/acl_test.go index 5ae4159..a920b10 100644 --- a/server/internal/agentd/acl_test.go +++ b/server/internal/agentd/acl_test.go @@ -1,6 +1,7 @@ package agentd import ( + "bytes" "context" "encoding/json" "fmt" @@ -514,3 +515,92 @@ func TestACLExampleFileStaysValid(t *testing.T) { } } } + +// ─── C2: 手抖字段名(如 targets → targetz)不得静默关闸,也不得覆盖 last-good ────── + +// 字段名手抖(targetz)→ encoding/json 忽略未知字段,Targets 变 nil,active()=false。 +// 若不做特殊处理,旧代码会把这份"看似合法"的空配置当成新的 last-good 落盘, +// 把恢复用的快照也一起冲掉。正确行为:当作加载失败处理,回退 last-good,不覆盖磁盘。 +func TestACL_TypoFieldWithEnabledTrue_FallsBackWithoutOverwritingLastGood(t *testing.T) { + cfg := testConfig(t) + writeACL(t, cfg.ACLConfigPath, validACL) + sb := NewSingBox(cfg, nil) + sb.ApplyConfig(sampleSnapshot(&agentv1.Credential{DpUUID: "aaaa", Protocol: agentv1.ProtocolBoth}), true) + if _, err := sb.RenderConfig(); err != nil { + t.Fatal(err) + } + before, err := os.ReadFile(cfg.ACLLastGoodPath()) + if err != nil { + t.Fatalf("last-good 未在健康加载后落盘: %v", err) + } + + // "targets" 手抖成 "targetz":JSON 仍能解析,但 Targets 字段收不到值。 + writeACL(t, cfg.ACLConfigPath, `{"enabled":true,"allow_dp_uuids":["x"],"targetz":[{"domain":["a.com"]}]}`) + data, err := sb.RenderConfig() + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(data), "reject") { + t.Fatal("字段名手抖(targetz)后拒绝规则消失了 —— 这是静默关闸,私有服务已敞开") + } + after, err := os.ReadFile(cfg.ACLLastGoodPath()) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(before, after) { + t.Fatal("手抖配置覆盖了 acl.last-good.json —— 恢复用的快照被销毁了") + } +} + +// 显式 enabled:false 是唯一合法的关闭方式,渲染结果必须真的没有 reject 规则; +// 但它不该覆盖磁盘上已有的 last-good 快照——否则日后 acl.json 意外损坏/丢失时, +// 回退到的将是这份"已关闭"的快照而不是最近一次真正 active 的配置。 +func TestACL_ExplicitDisableDoesNotOverwriteLastGood(t *testing.T) { + cfg := testConfig(t) + writeACL(t, cfg.ACLConfigPath, validACL) + sb := NewSingBox(cfg, nil) + sb.ApplyConfig(sampleSnapshot(&agentv1.Credential{DpUUID: "aaaa", Protocol: agentv1.ProtocolBoth}), true) + if _, err := sb.RenderConfig(); err != nil { + t.Fatal(err) + } + before, err := os.ReadFile(cfg.ACLLastGoodPath()) + if err != nil { + t.Fatalf("last-good 未在健康加载后落盘: %v", err) + } + + writeACL(t, cfg.ACLConfigPath, `{"enabled": false, "allow_dp_uuids": ["x"], "targets": [{"domain":["a.com"]}]}`) + data, err := sb.RenderConfig() + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(data), "reject") { + t.Fatal("enabled=false 应真正关闭 ACL,不应再产出 reject 规则") + } + after, err := os.ReadFile(cfg.ACLLastGoodPath()) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(before, after) { + t.Fatal("显式 enabled=false 不该覆盖 last-good 快照,否则日后 acl.json 损坏就回不到真正 active 的配置了") + } +} + +// 白名单为空但 target 有效 → 是合法的"拒绝所有人"配置,不是手抖,必须仍被当作 +// last-good 持久化,且仍产出拒绝规则。防止 C2 修复对"零 target"的特判过度矫正到 +// "零白名单"上。 +func TestACL_EnabledWithEmptyAllowlistStillPersistsAndDenies(t *testing.T) { + cfg := testConfig(t) + writeACL(t, cfg.ACLConfigPath, `{"enabled": true, "allow_dp_uuids": [], "targets": [{"domain": ["brain.51yanmei.com"]}]}`) + sb := NewSingBox(cfg, nil) + sb.ApplyConfig(sampleSnapshot(&agentv1.Credential{DpUUID: "aaaa", Protocol: agentv1.ProtocolBoth}), true) + data, err := sb.RenderConfig() + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(data), "reject") { + t.Fatal("空白名单+有效 target 应仍产出拒绝规则(拒所有人),不该被当成手抖") + } + if _, err := os.Stat(cfg.ACLLastGoodPath()); err != nil { + t.Fatalf("空白名单(但有效 target)的配置应被当作合法 active 配置持久化为 last-good: %v", err) + } +} diff --git a/server/internal/agentd/singbox.go b/server/internal/agentd/singbox.go index 8c185b4..961bceb 100644 --- a/server/internal/agentd/singbox.go +++ b/server/internal/agentd/singbox.go @@ -342,11 +342,25 @@ func (s *SingBox) loadACL() *ACLConfig { acl, err := LoadACLConfig(s.cfg.ACLConfigPath) switch { case err == nil && acl != nil: - s.mu.Lock() - s.lastGoodACL = acl - s.mu.Unlock() - if perr := persistACL(s.cfg.ACLLastGoodPath(), acl); perr != nil { - logf("[acl] persist last-good to %s failed: %v", s.cfg.ACLLastGoodPath(), perr) + if acl.Enabled && len(acl.cleanTargets()) == 0 { + // enabled 却零 target = 几乎必然是字段名手抖(如 targets 打成 targetz)。 + // encoding/json 对未知字段静默无视,这份配置"看似合法"但毫无内容——当作 + // 加载失败,走下面的 last-good 兜底,绝不静默敞开、更不能拿它覆盖兜底快照。 + logf("[acl] ERROR %s parsed but enabled=true yields zero targets — treating as broken, falling back", s.cfg.ACLConfigPath) + break // 落到下方 last-good 兜底(Go 的 switch 内 break 只退出 switch) + } + if acl.active() { + // 只有 active 的配置才配当 last-good:否则一次手抖既关闸又冲掉兜底。 + s.mu.Lock() + s.lastGoodACL = acl + s.mu.Unlock() + if perr := persistACL(s.cfg.ACLLastGoodPath(), acl); perr != nil { + logf("[acl] persist last-good to %s failed: %v", s.cfg.ACLLastGoodPath(), perr) + } + } else { + // active() 为 false 但走到了这里,只可能是显式 enabled:false —— 合法关闭。 + // 直接返回这份(空规则的)配置,不 persist,保留此前的恢复能力。 + logf("[acl] gate explicitly disabled (enabled=false)") } return acl case err != nil: