Feature: hide user-bound devices from MCP
设备绑定到 Web 用户后,MCP 端点(静态 token,等同管理员凭证)仍能按 ID 操作,与"私人设备只归 owner"的模型冲突。按产品决定采用最严格策略:MCP 只面向公共设备,绑定设备对 MCP 一律不可见、不可达,即使知道 ID 也一样。 - list_hosts / get_host_detail / get_server_status 先过滤绑定设备再计数与 分页;离线列表用完整在线集合作差,避免在线私人设备以离线条目复现。 - 所有单主机工具在 callTool 统一拒绝(get_screenshot / list_files / read_file / terminal_exec / terminal_close 及将来新增工具),批量 ids 由 逐台执行处拒绝;后续新工具不会再漏。 - 拒绝返回与离线主机相同的 -32002 "Host not found or offline",不泄露设备 是否存在。 - get_audit_log 过滤 device_id / device / devices 中任意一个指向绑定设备的 记录;owner 快照一次取用,不逐条加锁。 - cmd 把 Web 的 DeviceACL 注入 MCP;未启用 ACL 时行为完全不变。 验证:go build/test/vet 全绿;新增 MCP 侧列表隐藏、按 ID 拒绝、批量拒绝、 状态计数、审计过滤(三种字段写法)测试。 Model: deepseek-flash Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
1 parent
46434b46d0
commit
b704258ba4
6 files changed
+286
-12
No files matched your search
@@ -0,0 +1,185 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// fakeOwners stands in for web.DeviceACL: a device with a non-empty owner is
|
||||
// bound to that web user and must be invisible to MCP.
|
||||
type fakeOwners map[string]string
|
||||
|
||||
func (f fakeOwners) Owner(deviceID string) string { return f[deviceID] }
|
||||
|
||||
func bindOwners(s *Server, owners fakeOwners) { s.cfg.DeviceACL = owners }
|
||||
|
||||
func hasID(hosts []map[string]any, id string) bool {
|
||||
for _, h := range hosts {
|
||||
if h["id"] == id {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func wantNoID(t *testing.T, hosts []map[string]any, id string) {
|
||||
t.Helper()
|
||||
if hasID(hosts, id) {
|
||||
t.Fatalf("ids = %v, must not contain bound device %s", hostIDs(hosts), id)
|
||||
}
|
||||
}
|
||||
|
||||
// TestACLListHostsHidesBoundDevices covers the listing paths: online and
|
||||
// offline entries whose owner is set are dropped before paging and counting,
|
||||
// so they affect neither the page nor total/totalAll.
|
||||
func TestACLListHostsHidesBoundDevices(t *testing.T) {
|
||||
f := newLocalFixture(t)
|
||||
bindOwners(f.srv, fakeOwners{"1001": "alice", "2001": "alice"})
|
||||
|
||||
_, sc := mustCallLocal(t, f.srv, "list_hosts", `{}`)
|
||||
hosts := hostList(t, sc)
|
||||
wantIDs(t, hosts, "1002", "1003")
|
||||
wantNoID(t, hosts, "1001")
|
||||
if got := intField(t, sc, "total"); got != 2 {
|
||||
t.Fatalf("total = %d, want 2 public hosts", got)
|
||||
}
|
||||
if got := intField(t, sc, "totalAll"); got != 2 {
|
||||
t.Fatalf("totalAll = %d, want 2 public hosts", got)
|
||||
}
|
||||
|
||||
_, sc = mustCallLocal(t, f.srv, "list_hosts", `{"include_offline":true}`)
|
||||
hosts = hostList(t, sc)
|
||||
wantNoID(t, hosts, "1001")
|
||||
wantNoID(t, hosts, "2001") // bound device that is now offline
|
||||
if !hasID(hosts, "2002") {
|
||||
t.Fatalf("ids = %v, want unbound offline device 2002", hostIDs(hosts))
|
||||
}
|
||||
if got := intField(t, sc, "total"); got != 3 {
|
||||
t.Fatalf("total = %d, want 3 public hosts (2 online + 1 offline)", got)
|
||||
}
|
||||
|
||||
// Filtering by the bound device's own name or group must not reveal it
|
||||
// through an empty-but-otherwise-valid result either: the filters apply to
|
||||
// the public snapshot, so the answer is simply nothing.
|
||||
for _, q := range []string{`{"name":"ALPHA"}`, `{"query":"ALPHA"}`, `{"group":"Legacy"}`} {
|
||||
if _, sc = mustCallLocal(t, f.srv, "list_hosts", q); len(hostList(t, sc)) != 0 {
|
||||
t.Fatalf("%s returned bound hosts: %v", q, hostIDs(hostList(t, sc)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestACLDeniesBoundDeviceByID checks the per-device tools: a bound device is
|
||||
// answered exactly like an offline host, so MCP callers cannot tell it exists
|
||||
// at all (policy C: bound devices are unreachable even by id).
|
||||
func TestACLDeniesBoundDeviceByID(t *testing.T) {
|
||||
f := newLocalFixture(t)
|
||||
bindOwners(f.srv, fakeOwners{"1001": "alice"})
|
||||
|
||||
single := []struct{ tool, args string }{
|
||||
{"get_host_detail", `{"id":"1001"}`},
|
||||
{"get_screenshot", `{"id":"1001"}`},
|
||||
{"list_files", `{"id":"1001"}`},
|
||||
{"read_file", `{"id":"1001","path":"/etc/hostname"}`},
|
||||
{"terminal_exec", `{"id":"1001","command":"dir"}`},
|
||||
}
|
||||
for _, c := range single {
|
||||
if _, _, rerr := callLocal(t, f.srv, c.tool, c.args); rerr == nil || rerr.Code != CodeOffline {
|
||||
t.Fatalf("%s on bound device = %+v, want -32002", c.tool, rerr)
|
||||
}
|
||||
}
|
||||
|
||||
// The batch form reports a per-id failure, not a leak.
|
||||
text, sc := mustCallLocal(t, f.srv, "get_host_detail", `{"ids":["1001","1002"]}`)
|
||||
if got := intField(t, sc, "succeeded"); got != 1 {
|
||||
t.Fatalf("succeeded = %d, want only the public host", got)
|
||||
}
|
||||
if got := intField(t, sc, "failed"); got != 1 {
|
||||
t.Fatalf("failed = %d, want the bound host", got)
|
||||
}
|
||||
if strings.Contains(text, "ALPHA") {
|
||||
t.Fatalf("text leaks the bound host: %q", text)
|
||||
}
|
||||
first := sc["results"].([]any)[0].(map[string]any)
|
||||
if ok, _ := first["ok"].(bool); ok {
|
||||
t.Fatalf("bound id reported ok: %v", first)
|
||||
}
|
||||
|
||||
// Files batch listing: every bound id fails, public ids still succeed.
|
||||
_, sc = mustCallLocal(t, f.srv, "list_files", `{"ids":["1001"]}`)
|
||||
raw, _ := json.Marshal(sc["results"])
|
||||
if !strings.Contains(string(raw), `"ok":false`) || strings.Contains(string(raw), "ALPHA") {
|
||||
t.Fatalf("list_files batch = %s, want one opaque failure", raw)
|
||||
}
|
||||
|
||||
// The gate is the owner map, not the device's online state: an unbound id
|
||||
// must still pass it (the later "is it online" check rejects 9999).
|
||||
if f.srv.denyPrivateDevice("1002") != nil {
|
||||
t.Fatal("unbound device denied")
|
||||
}
|
||||
if f.srv.denyPrivateDevice("9999") != nil {
|
||||
t.Fatal("unknown device denied as private")
|
||||
}
|
||||
}
|
||||
|
||||
// TestACLServerStatusCounts verifies the status tool counts only public
|
||||
// devices, so bound hosts do not inflate the fleet summary.
|
||||
func TestACLServerStatusCounts(t *testing.T) {
|
||||
f := newLocalFixture(t)
|
||||
bindOwners(f.srv, fakeOwners{"1001": "alice", "2001": "alice"})
|
||||
|
||||
_, sc := mustCallLocal(t, f.srv, "get_server_status", `{}`)
|
||||
hosts, ok := sc["hosts"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("hosts = %v", sc["hosts"])
|
||||
}
|
||||
if got := intField(t, hosts, "online"); got != 2 {
|
||||
t.Fatalf("online = %d, want 2 public online hosts", got)
|
||||
}
|
||||
if got := intField(t, hosts, "offline"); got != 1 {
|
||||
t.Fatalf("offline = %d, want 1 public offline host", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestACLAuditHidesBoundDeviceRecords checks that audit records carrying a
|
||||
// bound device_id are filtered out of get_audit_log, including through
|
||||
// keyword/type filters, while the pre-filter total stays honest.
|
||||
func TestACLAuditHidesBoundDeviceRecords(t *testing.T) {
|
||||
path := writeAuditFile(t,
|
||||
`{"audit":true,"time":"2026-11-12T10:00:00Z","level":"info","event":"web.login.success","actor":"admin","ip":"1.2.3.4"}`,
|
||||
`{"audit":true,"time":"2026-11-12T10:01:00Z","level":"info","event":"device.online","device_id":"1001","name":"ALPHA"}`,
|
||||
`{"audit":true,"time":"2026-11-12T10:02:00Z","level":"info","event":"device.online","device_id":"1002","name":"BETA"}`,
|
||||
// MCP tool calls spell the target as "device".
|
||||
`{"audit":true,"time":"2026-11-12T10:03:00Z","level":"info","event":"mcp.tool.call","device":"1001","args":"{}"}`,
|
||||
// Bind/unbind records carry a devices array.
|
||||
`{"audit":true,"time":"2026-11-12T10:04:00Z","level":"info","event":"admin.device.bind","devices":["1001","1002"],"owner":"alice"}`,
|
||||
)
|
||||
s := New(Config{Token: "test-token", AuditPath: path})
|
||||
bindOwners(s, fakeOwners{"1001": "alice"})
|
||||
|
||||
text, sc := mustCallLocal(t, s, "get_audit_log", `{}`)
|
||||
// Public: login, device.online 1002. Hidden: device_id 1001, MCP device
|
||||
// 1001, and the bind record because its devices array names 1001.
|
||||
if got := intField(t, sc, "total"); got != 2 {
|
||||
t.Fatalf("total = %d, want the two public records", got)
|
||||
}
|
||||
if got := intField(t, sc, "totalAll"); got != 5 {
|
||||
t.Fatalf("totalAll = %d, want unfiltered 5", got)
|
||||
}
|
||||
for _, leak := range []string{"ALPHA", `"device_id":"1001"`, `"device":"1001"`, `"devices":["1001"`} {
|
||||
if strings.Contains(text, leak) {
|
||||
t.Fatalf("text leaks bound-device record %s: %q", leak, text)
|
||||
}
|
||||
}
|
||||
|
||||
// A keyword carried only by the bound device's record finds nothing.
|
||||
if _, sc = mustCallLocal(t, s, "get_audit_log", `{"keyword":"ALPHA"}`); intField(t, sc, "total") != 0 {
|
||||
t.Fatalf("keyword ALPHA total = %d, want 0", intField(t, sc, "total"))
|
||||
}
|
||||
|
||||
// Without an owner map (MCP-only deployments) nothing is filtered.
|
||||
plain := New(Config{Token: "test-token", AuditPath: path})
|
||||
if _, sc = mustCallLocal(t, plain, "get_audit_log", `{}`); intField(t, sc, "total") != 5 {
|
||||
t.Fatalf("no-ACL total = %d, want 5", intField(t, sc, "total"))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user