Scope command audit storage and queries to the requesting owner #104

Closed
weichen wants to merge 0 commits from feature/command-audit-owner-scoping into main
Owner

问题

GET /api/commands/audit 无租户过滤:store.ListAudits 无 owner 条件、command_audit 表无 owner 列——多用户认证档下任何登录用户可读全租户命令审计(命令 ID、入口、会话 ID、参数摘要、状态)。发现与引证见 Dev 仓任务卡(feature/command-audit-owner-scoping)与 RDM 仓 U0 提案 §5 C7。

修复(红测先行)

  • 迁移 v18 command_audit_owner:加 owner 列(两方言)+ 存量回填 COALESCE(payload.actor,'')(修复前 Actor 是唯一归属线索)+ 索引 (owner, conversation_id, time);down 先删索引再 DROP COLUMN(sqlite 被索引列直接 DROP 会报错)。
  • AppendAudit(owner, env) / ListAudits(owner, conversationID, limit) 按租户写入与过滤;owner 为空=无作用域(纵深防御)。
  • cmdbus.AuditStore 端口与 auditReceipt 传 req.Owner;server.listAudits 传认证注入的 owner;测试假体同步。
  • actor 为空的存量行 owner 留空:不再经 API 返回,数据保留表内可直查(取舍记录在任务卡)。

验证(SELF;CI 为权威 GATE)

红:a7cc69b FAIL(alice 查到 bob 行);绿:e0b1c27 go test ./... 32/32 包 ok(含 TestCommandAuditOwnerScoping 与 v18 回填测试)。既有迁移测试期望更新至 v18。

关联

Dev gitlink bump PR 随任务卡推进(本 PR 为组件侧)。不合入,待评审。

## 问题 `GET /api/commands/audit` 无租户过滤:`store.ListAudits` 无 owner 条件、`command_audit` 表无 owner 列——多用户认证档下任何登录用户可读**全租户**命令审计(命令 ID、入口、会话 ID、参数摘要、状态)。发现与引证见 Dev 仓任务卡(feature/command-audit-owner-scoping)与 RDM 仓 U0 提案 §5 C7。 ## 修复(红测先行) - 迁移 v18 `command_audit_owner`:加 `owner` 列(两方言)+ 存量回填 `COALESCE(payload.actor,'')`(修复前 Actor 是唯一归属线索)+ 索引 `(owner, conversation_id, time)`;down 先删索引再 DROP COLUMN(sqlite 被索引列直接 DROP 会报错)。 - `AppendAudit(owner, env)` / `ListAudits(owner, conversationID, limit)` 按租户写入与过滤;owner 为空=无作用域(纵深防御)。 - `cmdbus.AuditStore` 端口与 `auditReceipt` 传 `req.Owner`;`server.listAudits` 传认证注入的 owner;测试假体同步。 - actor 为空的存量行 owner 留空:不再经 API 返回,数据保留表内可直查(取舍记录在任务卡)。 ## 验证(SELF;CI 为权威 GATE) 红:`a7cc69b` FAIL(alice 查到 bob 行);绿:`e0b1c27` `go test ./...` 32/32 包 ok(含 `TestCommandAuditOwnerScoping` 与 v18 回填测试)。既有迁移测试期望更新至 v18。 ## 关联 Dev gitlink bump PR 随任务卡推进(本 PR 为组件侧)。不合入,待评审。
weichen added 2 commits 2026-10-08 14:02:55 +00:00
Why: GET /api/commands/audit returns every tenant's audit rows; the regression test pins the desired owner-scoped behavior before the fix.
Evidence: go test ./internal/server -run TestCommandAuditOwnerScoping FAIL (owner alice sees bob's row; output recorded on the task card).
Impact: Test only; suite red on this commit by design.
Refs: Task card R1; U0 proposal section 5 C7 security finding.
Scope command audit storage and queries to the requesting owner
ci / vet-and-test (pull_request) Successful in 42s
e0b1c27f1f
Why: GET /api/commands/audit leaked every tenant's audit rows (command ids, entrances, conversation ids, param digests) to any authenticated user; owner was never recorded nor filtered.
Evidence: Red run at a7cc69b FAIL (alice sees bob's row); after fix go test ./... 32/32 packages ok, including TestCommandAuditOwnerScoping and the v18 backfill test (rows recorded on the task card).
Impact: v18 migration adds command_audit.owner with actor-based backfill (empty-actor rows stay invisible via API, kept in-table); AppendAudit/ListAudits gain owner params; cmdbus port and fakes updated; handler passes authenticated owner. No public extension surface change.
Refs: Task card R1; U0 proposal section 5 C7 finding.
weichen closed this pull request 2026-10-08 15:42:31 +00:00

Pull request closed

This pull request cannot be reopened because the branch was deleted.
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: weichen/Agent-System-Backend#104