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
@@ -36,6 +36,34 @@ const (
|
||||
termSweepInterval = 30 * time.Second
|
||||
)
|
||||
|
||||
// DeviceOwner reports the owner of a device. The web console's DeviceACL
|
||||
// implements it; declared here as an interface so the MCP package does not
|
||||
// import the web package.
|
||||
type DeviceOwner interface {
|
||||
Owner(deviceID string) string
|
||||
}
|
||||
|
||||
// deviceOwnerSnapshotter is an optional DeviceOwner capability: returning the
|
||||
// whole binding table lets a long operation (the audit scan) apply the policy
|
||||
// without a read-lock acquisition per record. Implemented by web.DeviceACL.
|
||||
type deviceOwnerSnapshotter interface {
|
||||
Owners() map[string]string
|
||||
}
|
||||
|
||||
// ownerChecker returns a predicate for "is this device public?" that borrows a
|
||||
// single snapshot when the ACL supports it, so scanning thousands of audit
|
||||
// records does not contend with an in-flight bind's write lock.
|
||||
func (s *Server) ownerChecker() func(deviceID string) bool {
|
||||
if s.cfg.DeviceACL == nil {
|
||||
return nil
|
||||
}
|
||||
if snap, ok := s.cfg.DeviceACL.(deviceOwnerSnapshotter); ok {
|
||||
owners := snap.Owners()
|
||||
return func(id string) bool { return owners[id] == "" }
|
||||
}
|
||||
return func(id string) bool { return s.cfg.DeviceACL.Owner(id) == "" }
|
||||
}
|
||||
|
||||
// Config is filled by cmd/main.go from the YAMA_MCP_* environment variables;
|
||||
// the package itself never reads the environment, so tests are deterministic.
|
||||
type Config struct {
|
||||
@@ -56,6 +84,12 @@ type Config struct {
|
||||
AuditPath string // "" = audit file disabled (YAMA_AUDIT_LOG=off)
|
||||
Port int // web/MCP listening port, for get_server_status
|
||||
Bind string // listening address, for get_server_status
|
||||
|
||||
// DeviceACL, when non-nil, hides devices bound to a web user. MCP only
|
||||
// operates public devices (product decision): a bound device is invisible
|
||||
// in list_hosts/get_host_detail and unreachable by ID in every device-
|
||||
// targeted tool, even if its ID is known.
|
||||
DeviceACL DeviceOwner
|
||||
}
|
||||
|
||||
// toolFunc implements one tools/call. It returns either a result value or a
|
||||
@@ -145,6 +179,36 @@ func New(cfg Config) *Server {
|
||||
// unknown path.
|
||||
func (s *Server) Enabled() bool { return s != nil && s.cfg.Token != "" }
|
||||
|
||||
// devicePrivate reports whether the device is bound to a web user. MCP only
|
||||
// operates public devices; bound devices are hidden and unreachable here even
|
||||
// though the endpoint's static token is effectively an admin credential.
|
||||
func (s *Server) devicePrivate(deviceID string) bool {
|
||||
return s.cfg.DeviceACL != nil && s.cfg.DeviceACL.Owner(deviceID) != ""
|
||||
}
|
||||
|
||||
// denyPrivateDevice returns the same error shape as an offline host so a
|
||||
// private device is indistinguishable from one that does not exist.
|
||||
func (s *Server) denyPrivateDevice(deviceID string) *rpcError {
|
||||
if s.devicePrivate(deviceID) {
|
||||
return errf(CodeOffline, "Host not found or offline: %s", deviceID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// publicDevices filters a device snapshot down to the ones MCP may expose.
|
||||
func (s *Server) publicDevices(devices []hub.DeviceInfo) []hub.DeviceInfo {
|
||||
if s.cfg.DeviceACL == nil {
|
||||
return devices
|
||||
}
|
||||
out := make([]hub.DeviceInfo, 0, len(devices))
|
||||
for _, d := range devices {
|
||||
if s.cfg.DeviceACL.Owner(d.ID) == "" {
|
||||
out = append(out, d)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Close releases resources held by long-lived tool state: the reaper stops and
|
||||
// every terminal session MCP owns is torn down and the shell told to exit.
|
||||
func (s *Server) Close() {
|
||||
@@ -344,6 +408,13 @@ func (s *Server) callTool(ctx context.Context, raw json.RawMessage) (result any,
|
||||
// a temporary state between milestones, not a client mistake.
|
||||
return nil, errf(CodeInternal, "tool %s is not implemented by this server build", params.Name)
|
||||
}
|
||||
// A device bound to a web user is invisible to MCP (product policy). Enforce
|
||||
// it once here for every single-host tool so a new tool cannot forget the
|
||||
// check; batch id[] spellings are checked by their per-host handler
|
||||
// (listFilesOne / hostMaps).
|
||||
if rerr := s.denyPrivateDevice(deviceID); rerr != nil {
|
||||
return nil, rerr
|
||||
}
|
||||
|
||||
// Global slot first: a call that would be rejected for concurrency must
|
||||
// not squat on a device lock while it fails.
|
||||
|
||||
Reference in new issue
Block a user