skillhub/openspec/changes/split-api-token-skill-capabilities/specs/api-token-capabilities/spec.md
小妍儿 ✨ 3ee80f996b
feat(auth): split API token skill capabilities (#1049) (#1078)
* docs(openspec): propose split API token skill capabilities

Define five token capabilities (read, submit, publish-private, manage,
delete), the route mapping, combined-operation limits, token minting
limits, legacy token and CLI compatibility, and negative test design.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <xiaoyaner0201@users.noreply.github.com>

* docs(openspec): clarify token compatibility and bundle snapshot boundaries

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* docs(auth): inventory Bundle authorization provenance

Record persisted evidence and fail-closed recovery rules before implementing snapshots.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* feat(auth): split token capabilities and classify route policies

Preserve Suite deletion authorization and verify legacy route compatibility against the original ordered catalog.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* feat(auth): enforce token ceilings on combined skill operations

Persist Bundle initiator capability snapshots and fail closed on unknown historical authority during recovery and retry.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* feat(auth): bound token issuance and expose effective capabilities

Validate requested scopes and expiry before rotation or extension. Expose capability discovery and machine-readable authorization errors without broadening token privileges.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* feat(tokens): add Web capability presets and CLI authorization guidance

Issue candidate-only login tokens, display effective capabilities and legacy compatibility, and require explicit delete authorization without automatic reissuance. Verify real HTTP candidate and legacy publication flows.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* docs(auth): document token capabilities and upgrade boundaries

Describe bounded issuance, compatibility aliases, CLI delete authorization, administrator upload changes, fail-closed Bundle recovery and the omitted-scopes deprecation schedule.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* docs(auth): align capability action boundaries

Document manage authorization for review withdrawal and keep DTO imports formatted with backend conventions.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* fix(publish): limit visibility ceiling check to token calls

Session uploads keep their previous path, and a missing locked row no longer raises a null pointer before the existing visibility checks run.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* fix(auth): preserve rerelease ceilings and explain bundle blocks

Carry token capabilities through rerelease and resolve visibility under the publication lock. Localize permanent Bundle authorization failures and hide ineffective retry actions.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* fix(auth): classify main's public routes and renumber bundle snapshot migration

main added public GET site-presentation routes and its own V122 migration. Record the new routes in the catalog baseline as unchanged public routes and move the Bundle authorization snapshot to V123.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* test(site): pin presentation upgrade check to V122

The upgrade test counted every migration after V121; with the Bundle snapshot now at V123 it saw two. Target V122 so it keeps checking only the presentation seed.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* fix(auth): compare token expiry ceilings at stored precision

Token expiry is stored at microsecond precision. On Linux CI Instant.now() carries nanoseconds, so a child token asking for exactly its parent's expiry compared as later and was rejected. Truncate requested expiry to microseconds before the ceiling check.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* fix(auth): renumber bundle snapshot migration after main's V123

main 22409aba added V123__homepage_recommendations. Move the Bundle authorization snapshot to V124 and record main's public site-recommendations routes in the token catalog baseline as unchanged public routes.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* test(auth): keep built-in skill sync out of token flow side-effect checks

Since main 22409aba the built-in skill sync can publish after startup, asynchronously, and its storage writes landed inside the 'denied rerelease writes nothing' assertion on CI. Disable built-in sync for this test class, as other integration tests already do.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* fix(auth): block bundle recovery with unknown authorization

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
(cherry picked from commit cb8fb05d77d4b455032cbc2d3a1fdc2dd7bce894)
Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>

* feat(web): redesign token credentials and compact permission controls

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
(cherry picked from commit acd3615ee57a4bb432980601bb883a1a86906d1a)
Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>

* chore(web): regenerate API types from integrated backend

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>

* fix(web): complete token locales and static analytics markers

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>

* test(auth): preserve operation-level bundle authorization errors

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>

---------

Signed-off-by: 千乘妍 (Xiaoyaner) <xiaoyaner0201@users.noreply.github.com>
Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>
Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
Co-authored-by: 千乘妍 (Xiaoyaner) <xiaoyaner0201@users.noreply.github.com>
Co-authored-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
2026-11-16 11:02:38 +08:00

154 lines
9.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## ADDED Requirements
### Requirement: REQ-ATC-01 Token 技能能力 SHALL 分为 5 类并按交集生效
系统 SHALL 提供 `skill:read`、`skill:submit`、`skill:publish-private`、`skill:manage`、`skill:delete` 五类技能能力。
Token 调用的有效权限 SHALL 为账号/空间权限、Token 有效能力、资源状态与治理规则三者的交集。授予能力 SHALL 不使普通成员
获得管理员权限,也 SHALL 不扩大可访问的资源范围。
#### Scenario: 候选提交 Token
- **WHEN** 空间 MEMBER 的 Token 只有 `skill:read + skill:submit`
- **THEN** 上传 PRIVATE 技能包成功并保持为未发布候选
- **AND** 对该候选调用 `submit-review`(`NAMESPACE_ONLY` 或 `PUBLIC`)成功,进入现有扫描与审核流程
- **AND** 调用 `confirm-publish` 返回 403 `error.apiToken.scope.missing`(参数 `skill:publish-private`),候选状态不变
- **AND** 调用 `visibility`、`archive`、`yank`、标签写入或删除接口均被拒绝,资源状态不变
#### Scenario: 私有自动发布 Token
- **WHEN** Token 有 `skill:read + skill:submit + skill:publish-private`
- **THEN** 上传 PRIVATE 后调用 `confirm-publish` 成功,版本成为 `PUBLISHED`,不生成审核通过记录
#### Scenario: 空间角色不扩大 Token 能力
- **WHEN** 空间 OWNER 使用只有 `skill:read + skill:submit` 的 Token
- **THEN** `confirm-publish` 与管理类接口仍被拒绝
### Requirement: REQ-ATC-02 每个 Token 可写入口 SHALL 有明确能力归属
`RouteSecurityPolicyRegistry` 中每条要求技能写入能力的 Token 策略 SHALL 映射到 design D2 中的唯一能力。
`/api/v1`、`/api/web`、`/api/cli/v1` 与 ClawHub 兼容入口中执行同一操作的路由 SHALL 要求相同能力。
新增的技能写入路由未归类时,目录完整性测试 SHALL 失败。
#### Scenario: 等价入口一致
- **WHEN** 同一 Token 分别调用 `POST /api/v1/skills/{ns}/{slug}/confirm-publish` 与 `POST /api/web/skills/{ns}/{slug}/confirm-publish`
- **THEN** 两者返回相同的授权结果与错误码
#### Scenario: 订阅路由不被删除通配命中
- **WHEN** `skill:read` Token 调用 `DELETE /api/v1/skills/{id}/subscription`
- **THEN** 请求按订阅路由授权,不要求 `skill:delete`
### Requirement: REQ-ATC-03 组合操作 SHALL 按实际执行的能力检查
组合操作 SHALL 对其实际执行的每个状态转换检查对应能力,任何权限不足 SHALL 在产生副作用之前拒绝。
#### Scenario: 上传改变已有技能可见性
- **WHEN** 同 slug 已有技能为 `NAMESPACE_ONLY`,只有 `skill:read + skill:submit` 的 Token 以 `PRIVATE` 上传新版本
- **THEN** 请求返回 403 `error.apiToken.scope.missing`(参数 `skill:manage`)
- **AND** 不创建 SkillVersion,技能可见性与 `latestVersionId` 不变,空间成员访问不受影响
#### Scenario: 同时具备提交与管理
- **WHEN** Token 有 `skill:submit + skill:manage`,对已有技能以不同可见性上传
- **THEN** 按现有规则处理:缩小立即生效,扩大进入审核
#### Scenario: 重新发布不能绕过私有发布
- **WHEN** 只有 `skill:submit` 的 Token 对 PRIVATE 技能执行 `rerelease`
- **THEN** 新版本落在 `UPLOADED`,不成为 `PUBLISHED`
#### Scenario: 平台管理员 Token 不走直发捷径
- **WHEN** SUPER_ADMIN 账号的 `skill:read + skill:submit` Token 以 `PRIVATE` 上传
- **THEN** 版本落在 `UPLOADED`,没有 `skill:publish-private` 时不能发布
- **AND** 同一账号的会话上传行为不变
#### Scenario: Bundle 内部私有发布
- **WHEN** Bundle 操作的发起 Token 有效能力不含 `skill:publish-private`,成员为 PRIVATE
- **THEN** 成员标记为 `BLOCKED_RETRYABLE` 并说明缺少的能力,不调用 `confirmPublish`,不创建部分 Suite 草稿
#### Scenario: Bundle 新操作缺失能力快照
- **WHEN** Token 发起 Bundle 确认,但操作记录未能持久化发起方式和有效能力快照
- **THEN** 在创建操作和成员副作用前拒绝,不把空快照按旧版全能力处理
#### Scenario: Bundle 历史空快照来源不明
- **WHEN** 迁移前的 Bundle 操作快照为空,且无法由关联原操作 ID 的记录证明当时的发起方式及授权
- **THEN** 恢复执行保持 `BLOCKED_RETRYABLE` 并标明授权来源不明,不调用 `confirmPublish`,不创建 Suite 草稿
- **AND** 不能仅凭迁移前创建时间、当前 actor 的管理员身份或当前 Token 推断全能力;需以明确授权另建操作
#### Scenario: Bundle 可证历史操作的恢复
- **WHEN** 空快照操作能由原操作 ID 关联记录证实当时使用带旧 `skill:publish` 的 Token 或会话及其授权
- **THEN** 恢复时分别按旧 scope 展开能力或沿用会话权限,不能由恢复 worker 的权限扩张
#### Scenario: Bundle 失败重试不能提权
- **WHEN** 缺少 `skill:publish-private` 的 Bundle 操作在失败后由更高权限的账号或 Token 请求重试
- **THEN** 重试仍按操作记录的原快照拒绝 PRIVATE 发布,不补写新能力、不调用 `confirmPublish`,不创建 Suite 草稿
### Requirement: REQ-ATC-04 Token 签发、轮换与续期 SHALL 不超过调用 Token 的能力上限
经 Token 调用创建、同名轮换或续期 Token 时,新 Token 的有效能力 SHALL 是调用 Token 有效能力的子集,
到期时间 SHALL 不晚于调用 Token。比较 SHALL 在别名展开后进行,省略 scopes SHALL 按默认值展开后比较。
未知 scope SHALL 被拒绝且不写入。
#### Scenario: 受限 Token 签出更高权限
- **WHEN** 只有 `token:manage` 的 Token 请求创建带 `skill:delete` 的 Token
- **THEN** 返回 403 `error.apiToken.scope.escalation`,不创建 Token,同名旧 Token 不被吊销
#### Scenario: 省略 scopes 不能绕过
- **WHEN** 只有 `token:manage + skill:read` 的 Token 省略 scopes 创建 Token
- **THEN** 按默认值展开后比较,超出时返回 403
#### Scenario: 未知 scope
- **WHEN** 请求包含 `no-such:scope`
- **THEN** 返回 400 `error.apiToken.scope.unknown`
#### Scenario: 会话创建
- **WHEN** 用户通过会话登录创建任意合法组合
- **THEN** 只受账号权限约束
### Requirement: REQ-ATC-05 读取能力 SHALL 支持查询本人空间、候选与 Token 能力
`skill:read` Token SHALL 能查询本人所属命名空间及本人角色(包括空空间)、本人有权查看的未发布候选及状态,
以及当前 Token 的有效能力、兼容状态和到期时间。查询当前 Token 能力 SHALL 不需要 `token:manage`,
SHALL 不返回其他凭据或其哈希、前缀以外的标识。
#### Scenario: 空空间角色
- **WHEN** 账号是某空间 ADMIN 且该空间没有技能
- **THEN** `GET /api/v1/me/namespaces` 返回该空间及 `currentUserRole = ADMIN`
#### Scenario: 当前 Token 能力
- **WHEN** 旧版 `skill:read + skill:publish` Token 调用 `GET /api/cli/v1/auth/whoami`
- **THEN** 响应 `capabilities` 含 `skill:read`、`skill:submit`、`skill:publish-private`、`skill:manage`,`legacyCompat = true`
### Requirement: REQ-ATC-06 存量 Token 与旧 CLI SHALL 保持兼容
存量 Token 的存储 SHALL 不被改写,原 scope 的路由授权 SHALL 保持一致并标注旧版兼容;例外是
SUPER_ADMIN 的 Token 调用不再因管理员身份在上传 PRIVATE 时自动发布,须显式 `confirm-publish`,而会话调用
SHALL 保持原有直发行为。新建入口的宽默认值 SHALL 标注弃用并有退出计划。
CLI 登录 SHALL 签发 `skill:read + skill:submit`;旧 CLI 的 publish、预检与送审 SHALL 继续可用;删除 SHALL 需要显式授权,
系统 SHALL 不通过自动换发更宽 Token 恢复删除能力。
#### Scenario: 升级后的存量普通 Token
- **WHEN** 升级前签发的非 SUPER_ADMIN `skill:read + skill:publish` Token 调用 `confirm-publish`
- **THEN** 按旧 scope 展开后的 `skill:publish-private` 授权,结果与升级前一致,存储 scope 不变
#### Scenario: SUPER_ADMIN 存量 Token 的直发例外
- **WHEN** SUPER_ADMIN 账号使用升级前带 `skill:publish` 的 Token 上传 PRIVATE 技能
- **THEN** 上传后版本为 `UPLOADED`,不再自动发布;显式 `confirm-publish` 可按展开后的能力使其成为 `PUBLISHED`
#### Scenario: SUPER_ADMIN 会话调用保持原行为
- **WHEN** 同一 SUPER_ADMIN 账号使用会话上传 PRIVATE 技能
- **THEN** 保持升级前的直发流程,不要求 Token 能力
#### Scenario: SUPER_ADMIN 新候选 Token 无法直发
- **WHEN** SUPER_ADMIN 账号使用新 `skill:read + skill:submit` Token 上传 PRIVATE 技能并尝试 `confirm-publish`
- **THEN** 上传后版本为 `UPLOADED`,确认请求返回 403 `error.apiToken.scope.missing`(参数 `skill:publish-private`),状态不变
#### Scenario: 旧 CLI 使用新登录 Token
- **WHEN** 旧版 CLI 重新登录后执行 `publish` 与送审
- **THEN** 均成功
- **AND** 执行 `remove` 时返回 `error.apiToken.scope.missing`(参数 `skill:delete`),远端技能不变
### Requirement: REQ-ATC-07 拒绝原因 SHALL 使用稳定错误码
缺少能力 SHALL 返回 `error.apiToken.scope.missing` 并以新能力名为参数;路由不对 Token 开放 SHALL 返回
`error.apiToken.endpoint.unsupported`;签发越权与未知 scope SHALL 分别使用 `error.apiToken.scope.escalation`、
`error.apiToken.scope.unknown`。调用方 SHALL 能据此判断授权,而不需要通过尝试写操作探测。
#### Scenario: 服务层拒绝沿用同一错误码
- **WHEN** 上传因缺少 `skill:manage` 在服务层被拒绝
- **THEN** 响应错误码与路由层缺少能力时相同