Skip to content

test(approval): 补齐有界授权作用域匹配与消费核验的单元测试 - #521

Merged
Qiyuanqiii merged 3 commits into
CWNU-Open-Source-Community:mainfrom
qazxswedc-ui:test/approval-bounded-grant-coverage
Oct 30, 2026
Merged

Qiyuanqiii merged 3 commits into
CWNU-Open-Source-Community:mainfrom
qazxswedc-ui:test/approval-bounded-grant-coverage

Conversation

@qazxswedc-ui

@qazxswedc-ui qazxswedc-ui commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Summary

  • 为 internal/approval 补齐审批账本核心安全原语的单元测试。仅新增 internal/approval/bounded_grant_test.go,无任何生产代码改动。
  • 覆盖此前无测试的关键边界:
    • boundedGrantPermission 权限白名单(command_runtime/command_process × ask/auto/full,以及 workspace_access 历史路径);
    • MatchesBoundedGrantScope 全字段绑定校验(17 个绑定字段逐一漂移拒绝);
    • CheckBoundedConsumption 消费核验:作用域漂移、消费绑定篡改、显式撤销否决、now 越过过期时间、use ordinal 超限,以及"额度耗尽型撤销"(approval_store)的合法终局用例;
    • SessionGrant.Validate 有界/遗留(legacy)一致性、TTL ≤ 15 分钟、MaxUses ≤ 8 的双侧边界(含 MaxUses=8 与 ExpiresAt 恰为 +15min 的接受侧)、撤销元数据一致性等路径;
    • CreateGrantRequest.Normalize / RevokeGrantRequest.Normalize 规范化与边界;
    • GrantConsumption.Validate 与 GrantConsumptionFingerprint 逐字段绑定差分、Record.Validate 决策状态、DecisionRequest.Normalize deny 路径,以及 RecordFingerprint、ComputerUseAppScope.Fingerprint 等指纹辅助函数的参数绑定。
  • 包语句覆盖率 24.9% → 87.6%(基线在 merge_base b58f898 实测)。新增文件含 14 个测试函数;internal/approval 全包共 144 个用例(18 个顶层 + 126 个表驱动子用例)。
  • 与 test(privatefile, workspaceidentity): 补齐凭据原子写与路径排除边界的单元测试 #517(test(privatefile, workspaceidentity))互补,不触碰其文件。

Validation

  • go test -count=1 ./internal/approval/ → ok cyberagent-workbench/internal/approval(Go 1.26.10,与 go.mod 一致)
  • go vet ./internal/approval/...
  • gofmt 全包干净
  • go test -count=1 ./...(全仓回归:本 PR 仅新增一个测试文件,只影响 internal/approval 包编译;全量回归交由 CI 执行。本地 Windows 无 cgo,-race 亦由 CI Linux 侧覆盖)

Surface governance

  • No Surface is added, promoted, downgraded, deprecated, or removed.
    N/A — no Surface change(纯测试新增)。

Audit

  • No credentials or local runtime data are included. — 测试全部使用合成 fixture,不含真实凭据、路径或运行数据。
  • Policy, workspace, sandbox, and persistence boundaries were reviewed. — 仅新增测试断言既有安全边界(有界授权作用域与消费核验),未改变任何策略、沙箱或持久化行为。
  • README.md or project memory was updated when behavior or progress changed. — N/A:无行为变化,纯测试新增。

internal/approval 是审批账本的核心安全原语,但 bounded_grant.go 与
SessionGrant/CreateGrantRequest/RevokeGrantRequest 的校验矩阵此前没有
单元测试(包覆盖率 24.9%)。本 PR 仅新增测试,无生产代码改动:

- boundedGrantPermission 权限白名单(command_runtime/command_process
  × ask/auto/full,及 workspace_access 历史路径)
- MatchesBoundedGrantScope 全字段绑定校验(逐字段漂移拒绝)
- CheckBoundedConsumption 消费核验:作用域漂移、显式撤销否决、过期、
  ordinal 超用、以及"额度耗尽型撤销"的合法终局用例
- SessionGrant.Validate 有界/遗留(legacy)一致性、TTL≤15min、
  MaxUses≤8、撤销元数据一致性等边界
- GrantConsumption/Record/DecisionRequest 校验与指纹辅助函数绑定

包覆盖率 24.9% → 87.6%。与 CWNU-Open-Source-Community#517(privatefile/workspaceidentity)互补,
不触碰其文件。
@qazxswedc-ui

Copy link
Copy Markdown
Member Author

OpenCodeReview delegate 模式评审报告

  • 工具:OpenCodeReview CLI v1.13.3(delegate 模式,宿主 agent 执行评审,未消耗额外 LLM key)
  • 评审对象:internal/approval/bounded_grant_test.go(+678/-0,本 PR 唯一变更文件)
  • 说明:OCR 默认 path filter 排除 *_test.go 且 v1.13.3 不可配置;因本 PR 为纯测试 PR,文件选择步骤显式纳入该文件,规则解析与 diff 确定性步骤仍由 OCR 完成。

结论:APPROVE(1 处文档准确性问题已当场修正)

核查记录(非局部论断均已建立调用点证据)

  1. "额度耗尽型撤销的合法终局"用例(CheckBoundedConsumption 返回 nil 的分支):
    • 消费写入:internal/store/approval_grants.go:405-436,useOrdinal = MaxUses - UsesRemaining + 1,耗尽时置 Status=GrantRevoked / RevokedBy=approval_store / RevocationReason="bounded grant uses exhausted"——与测试 fixture 完全一致;
    • explicitlyRevoked 来源:internal/store/command_approval_grants.go:121-123 查 approval_grant_operations 的 action='revoke' 行;该行仅在显式操作员撤销路径写入(approval_grants.go:300),额度耗尽路径不写——因此耗尽后 replay 确实到达 nil 分支,测试与生产语义一致。
  2. 边界值方向:TTL 恰 15 分钟(接受)、MaxUses=8(接受)/9(拒绝)、DecidedAt == CreatedAt(接受)/早于(拒绝)、ExpiresAt == CreatedAt(拒绝)——均与源码一致。
  3. 测试有效性:变异用例在改字段后均重算绑定指纹,失败来自 CheckBoundedConsumption 自身的比较逻辑而非 Validate,不存在"把实现抄一遍当期望值"的同义反复;时间全部使用相对偏移,无 sleep,确定性可复现。
  4. 工具与静态检查覆盖项(go vet/gofmt/编译器可判定的问题)未重复报告;测试代码无并发、无资源泄漏面。
  5. 发现并已修正:PR 描述中"约 70 个子用例"与实际不符(go test -v 实测 127 个用例 / 115 个子用例),PR 描述已更新。

未覆盖说明

ParseComputerUseTarget 依赖 internal/computeruse 的 ParseAppID 语义,建议后续随该包一起补测;本 PR 未触碰。

独立评审(与 internal/store 调用方逐行核对)提出的三项加固:
- GrantConsumptionFingerprint 逐字段绑定差分测试,消除 fixture 与
  期望同源导致的永真盲区(该指纹守护持久化消费行的防篡改绑定)
- SessionGrant.Validate 接受侧上边界:MaxUses=8、ExpiresAt 恰为
  +15min(防 > → >= 型 off-by-one 回归)
- CheckBoundedConsumption 增加 now 越过 ExpiresAt 的拒绝路径;
  ComputerUseAppScope 增加 4 个字段的漂移差分
@qazxswedc-ui

Copy link
Copy Markdown
Member Author

评审后续:独立评审通过 + 三项加固(b983db5)

在 OpenCodeReview delegate 模式评审(见上条评论)之外,又完成了一轮独立评审:以 internal/store 真实调用方逐行核对的方式复核了全部断言语义,结论 APPROVE、无 P1。依据评审发现,追加一个提交(b983db5,+62/-2):

  1. GrantConsumptionFingerprint 逐字段绑定差分测试(9 个字段):此前该函数的接受用例因 fixture 与期望同源而无法测出公式丢字段的回归;它守护持久化消费行的防篡改绑定,现已与其他指纹函数同强度覆盖。
  2. 接受侧上边界:MaxUses=8 被接受、SessionGrant.Validate 中 ExpiresAt 恰为 CreatedAt+15min 被接受——堵住 > → >= 型 off-by-one 回归不可检测的盲区(此前仅测了拒绝侧)。
  3. CheckBoundedConsumption 的 now 越过 ExpiresAt 拒绝路径,以及 ComputerUseAppScope.Fingerprint 对 AppID/ServerID/PermissionGeneration/DescriptorFingerprint 的漂移差分。

更正:此前评论中"12 个测试函数、约 70 个子用例"计数有误(少报)。实测:新增文件 14 个测试函数;全包 144 个用例(18 顶层 + 126 子用例),覆盖率 24.9% → 87.6%(基线在 merge_base b58f898 实测),PR 描述已同步更正。

已知未覆盖(留作后续):ParseComputerUseTarget 依赖 internal/computeruse 的 ParseAppID 语义,建议随该包一起补测。

The pending rejection cases started from an approved record, so each kept
another invalid field and could pass after a guard was removed. Build a
valid pending record first, then mutate one decision field at a time.

Add the equality boundaries for both expiry comparisons, since the
implementation uses strict Before checks. Split record fingerprint drift
per decision field, and cover all fourteen ComputerUseAppScope bindings.
@Qiyuanqiii
Qiyuanqiii merged commit 988a7a1 into CWNU-Open-Source-Community:main Oct 30, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants