From 39ebf4e8c770fb50d061ba0f68ab63c6c816eb04 Mon Sep 17 00:00:00 2001 From: leemour Date: Wed, 18 Nov 2026 11:47:39 +0100 Subject: [PATCH 1/2] feat(cli): the api group checks a key by a field's value, as WordPress publishes by status A manifest permission with values (cli-base's permissions.valueKeys) applies when the JSON body or the query holds one of them: posts.publish before a post whose status is publish or future, and none for a draft. Both places count, as WordPress reads a parameter from either; an HTTP call takes no body field as a flag, so these two are every way to send it. --dry-run and discovery show the key "with status publish or future". Co-Authored-By: Claude Opus 5.5 --- changes/feat-permission-values.md | 9 +++++++ src/cli/service-api.test.ts | 41 +++++++++++++++++++++++++++++-- src/cli/service-api.ts | 31 +++++++++++++++++------ 3 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 changes/feat-permission-values.md diff --git a/changes/feat-permission-values.md b/changes/feat-permission-values.md new file mode 100644 index 000000000..35284599b --- /dev/null +++ b/changes/feat-permission-values.md @@ -0,0 +1,9 @@ +### Added + +- **The `api` group checks a key by a field's value, as WordPress publishes by `status: "publish"`.** A manifest + permission with `values` (cli-base's `permissions.valueKeys`) applies when the call's JSON body or its query holds + one of those values: `{ key: "posts.publish", values: { status: ["publish", "future"] } }` asks for + `posts.publish` before `api post-wp-v2-posts --body '{"status":"publish"}'` and lets a draft through. Both places + count, because WordPress reads the same parameter from either. So a profile that denies publishing also stops an + agent that publishes through the raw `api` group. `--dry-run` and discovery show the key with + `with status publish or future`. diff --git a/src/cli/service-api.test.ts b/src/cli/service-api.test.ts index d771dab92..11aacd11d 100644 --- a/src/cli/service-api.test.ts +++ b/src/cli/service-api.test.ts @@ -65,6 +65,16 @@ const operations: Operation[] = [ parameters: [page("block_id")], permissions: [{ key: "documents.delete" }], }, + { + id: "create-post", + command: "create-post", + binding: { kind: "http", method: "POST", path: "/posts" }, + effect: "write", + tags: ["Posts"], + parameters: [query("status")], + request: { required: false, confidence: "contract", schema: "PatchPage" }, + permissions: [{ key: "posts.publish", values: { status: ["publish", "future"] } }], + }, { id: "agent-batch", command: "agent-batch", @@ -477,6 +487,25 @@ describe("the shared service api group", () => { expect(bare()).toEqual([{ method: "PATCH", url: "https://api.pages.test/v1/pages/p1", body: '{"in_trash":false}' }]) }) + it("**a write whose field holds a publishing value checks its key**, in the body or the query, as WordPress", async () => { + const { cli, bare } = fixture({ "posts.publish": "deny", api: "allow" }) + for (const call of [ + ["--body", '{"status":"publish","title":"t"}'], + ["--body", '{"status":"future"}'], + ["--status", "publish", "--body", '{"title":"t"}'], + ]) { + const refused = await cli("work", "api", "create-post", ...call, "--json") + expect(refused.error).toMatchObject({ code: "permission_error" }) + expect(refused.error?.message).toContain("posts.publish") + } + expect(bare()).toEqual([]) + const draft = await cli("work", "api", "create-post", "--body", '{"status":"draft","title":"t"}', "--json") + expect(draft.code).toBe(0) + expect(bare()).toEqual([ + { method: "POST", url: "https://api.pages.test/v1/posts", body: '{"status":"draft","title":"t"}' }, + ]) + }) + it("**a read whose query flag deletes goes through the guard**, as an upload link that overwrites", async () => { const { cli, bare } = fixture({ "documents.delete": "deny" }) const refused = await cli("work", "api", "upload-link", "--path", "/a.txt", "--overwrite", "true", "--json") @@ -510,12 +539,20 @@ describe("the shared service api group", () => { { key: "documents.delete", when: "with in_trash or archived set" }, ]) expect(alsoChecks(["api", "upload-link"])).toEqual([{ key: "documents.delete", when: "with overwrite set" }]) + expect(alsoChecks(["api", "create-post"])).toEqual([ + { key: "posts.publish", when: "with status publish or future" }, + ]) expect(alsoChecks(["api", "agent-batch"])).toEqual([ { key: "documents.delete", when: "an operation's action is delete" }, ]) expect(alsoChecks(["api", "retrieve-a-page"])).toEqual([]) - expect(defaults).toEqual({ "documents.delete": "ask", "documents.share": "ask", "api.delete-a-block": "ask" }) - expect(extra).toEqual(["documents.delete", "documents.share"]) + expect(defaults).toEqual({ + "documents.delete": "ask", + "posts.publish": "ask", + "documents.share": "ask", + "api.delete-a-block": "ask", + }) + expect(extra).toEqual(["documents.delete", "posts.publish", "documents.share"]) }) it("**--all reads every page** by the manifest's cursor, offset or link, in one run", async () => { diff --git a/src/cli/service-api.ts b/src/cli/service-api.ts index fe00d2738..ccbce0e99 100644 --- a/src/cli/service-api.ts +++ b/src/cli/service-api.ts @@ -113,14 +113,30 @@ const destructive = (operation: ManifestOperation) => const flagsOf = ({ body = [], query = [] }: OperationPermission) => [...body, ...query] +const valuesOf = ({ values = {} }: OperationPermission) => Object.entries(values) + +/** Always, unless its flags or values name when; `with status publish or future`, `with archived set`. */ +const whenOf = (permission: OperationPermission): string | undefined => { + const flags = flagsOf(permission) + const values = valuesOf(permission).map(([field, listed]) => `${field} ${listed.join(" or ")}`) + if (flags.length === 0 && values.length === 0) return undefined + return `with ${[...(flags.length > 0 ? [`${flags.join(" or ")} set`] : []), ...values].join(" or ")}` +} + +/** A body field or query parameter holding one of the values: WordPress publishes from either. */ +const holds = (value: unknown, listed: readonly string[]) => typeof value === "string" && listed.includes(value) + /** A flag counts unless it is absent or plainly false: a service may read `1` or `True` as true. */ const set = (value: unknown) => value !== undefined && value !== null && value !== false && value !== "false" -/** Whether a manifest key applies to this call: always, or when one of its flags is set. */ +/** Whether a manifest key applies to this call: always, or when one of its flags is set or a field holds a value. */ const applies = (permission: OperationPermission, body: unknown, input: ApiCommandInput): boolean => - flagsOf(permission).length === 0 || + whenOf(permission) === undefined || (permission.body ?? []).some((field) => isObject(body) && set(body[field])) || - (permission.query ?? []).some((name) => set(input.query[name])) + (permission.query ?? []).some((name) => set(input.query[name])) || + valuesOf(permission).some( + ([field, listed]) => (isObject(body) && holds(body[field], listed)) || holds(input.query[field], listed), + ) /** The keys one call checks beside `api.`: the manifest's that apply, and the CLI's from the body. */ export const serviceApiKeys = ( @@ -155,11 +171,10 @@ export const serviceApiPermissions = ( const checks = new Map() for (const operation of input.manifest.operations) checks.set(operation.command, [ - ...permissionsOf(operation).map((permission) => - flagsOf(permission).length === 0 - ? permission.key - : { key: permission.key, when: `with ${flagsOf(permission).join(" or ")} set` }, - ), + ...permissionsOf(operation).map((permission) => { + const when = whenOf(permission) + return when === undefined ? permission.key : { key: permission.key, when } + }), ...(input.bodyPermissions?.[operation.command] ?? []).map(({ key, when }) => ({ key, when })), ]) const keys = [...new Set([...checks.values()].flat().map((one) => (typeof one === "string" ? one : one.key)))] From 52ca45082c43ecdbfd2a75b7549ede9ab97110c3 Mon Sep 17 00:00:00 2001 From: leemour Date: Wed, 18 Nov 2026 11:59:37 +0100 Subject: [PATCH 2/2] chore(deps): cli-base 0.118.0, with the manifest's permission values Co-Authored-By: Claude Opus 5.5 --- changes/feat-permission-values.md | 5 +++++ package.json | 4 ++-- pnpm-lock.yaml | 16 ++++++++-------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/changes/feat-permission-values.md b/changes/feat-permission-values.md index 35284599b..c82fd8d13 100644 --- a/changes/feat-permission-values.md +++ b/changes/feat-permission-values.md @@ -7,3 +7,8 @@ count, because WordPress reads the same parameter from either. So a profile that denies publishing also stops an agent that publishes through the raw `api` group. `--dry-run` and discovery show the key with `with status publish or future`. + +### Changed — may break callers + +- **cli-core needs `@wirecat/cli-base` 0.118.0**, which carries the manifest's `values` and WordPress's + `wpJsonToOpenApi`. A CLI that pins an older cli-base raises it with this cli-core. diff --git a/package.json b/package.json index 0609799af..17ee4936c 100644 --- a/package.json +++ b/package.json @@ -318,7 +318,7 @@ "@types/pngjs": "6.0.5", "@types/ssh2": "1.15.5", "@vitest/coverage-v8": "^5.0.5", - "@wirecat/cli-base": "0.117.0", + "@wirecat/cli-base": "0.118.0", "@wirecat/cli-core-legacy-tags": "npm:@wirecat/cli-core@0.414.0", "ajv": "8.17.1", "commander": "15.0.0", @@ -342,7 +342,7 @@ }, "peerDependencies": { "@napi-rs/canvas": ">=0.1.69 <2", - "@wirecat/cli-base": ">=0.117.0 <1.0.0", + "@wirecat/cli-base": ">=0.118.0 <1.0.0", "commander": ">=15", "mysql2": "^3.24.6", "pg": "^8.23.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2af298b0c..6a6035187 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -192,11 +192,11 @@ importers: specifier: ^5.0.5 version: 5.0.5(vitest@5.0.5) '@wirecat/cli-base': - specifier: 0.117.0 - version: 0.117.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5) + specifier: 0.118.0 + version: 0.118.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5) '@wirecat/cli-core-legacy-tags': specifier: npm:@wirecat/cli-core@0.414.0 - version: '@wirecat/cli-core@0.414.0(@napi-rs/canvas@1.0.13)(@wirecat/cli-base@0.117.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5))(commander@15.0.0)(typescript@7.0.2)(zod@4.6.7)' + version: '@wirecat/cli-core@0.414.0(@napi-rs/canvas@1.0.13)(@wirecat/cli-base@0.118.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5))(commander@15.0.0)(typescript@7.0.2)(zod@4.6.7)' ajv: specifier: 8.17.1 version: 8.17.1 @@ -1394,8 +1394,8 @@ packages: '@wasm-audio-decoders/opus-ml@0.0.3': resolution: {integrity: sha512-SEeTmk0eb3q5etJA/NHCRWW6BivTx9tqvNTmG0kg2ynuATEQRUZnwQE6Ibvl1BBvqIGHqI57+qyJMWmJBwb5XQ==} - '@wirecat/cli-base@0.117.0': - resolution: {integrity: sha512-EvvT9thrZihJc+s2taT1rwww5k1bITBG/7CtucjSMsNi1Y89JRARWVSa94/QR/o7002761+b1v5TXoyMJGFTdg==} + '@wirecat/cli-base@0.118.0': + resolution: {integrity: sha512-J++xrZeoL7BBZ7yZOQW1l/L0ScXnTzuwvy693gR+9ig7WXQl1b0QRqxWT6B6/I0RZHMsKs+lWb3P6ZK4XmhFDw==} engines: {node: '>=22'} hasBin: true peerDependencies: @@ -3890,7 +3890,7 @@ snapshots: dependencies: '@wasm-audio-decoders/common': 9.0.7 - '@wirecat/cli-base@0.117.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5)': + '@wirecat/cli-base@0.118.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5)': dependencies: '@valibot/to-json-schema': 1.8.0(valibot@1.5.0(typescript@7.0.2)) cli-table3: 0.6.5 @@ -3916,7 +3916,7 @@ snapshots: '@wirecat/cli-core-sqlite@1.1.0': {} - '@wirecat/cli-core@0.414.0(@napi-rs/canvas@1.0.13)(@wirecat/cli-base@0.117.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5))(commander@15.0.0)(typescript@7.0.2)(zod@4.6.7)': + '@wirecat/cli-core@0.414.0(@napi-rs/canvas@1.0.13)(@wirecat/cli-base@0.118.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5))(commander@15.0.0)(typescript@7.0.2)(zod@4.6.7)': dependencies: '@anthropic-ai/sdk': 0.131.0(zod@4.6.7) '@bomb.sh/tab': 0.0.22(commander@15.0.0) @@ -3924,7 +3924,7 @@ snapshots: '@modelcontextprotocol/server': 2.3.0 '@resvg/resvg-js': 2.6.2 '@valibot/to-json-schema': 1.8.0(valibot@1.5.0(typescript@7.0.2)) - '@wirecat/cli-base': 0.117.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5) + '@wirecat/cli-base': 0.118.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5) '@wirecat/cli-core-onnx': 1.0.1 '@wirecat/cli-core-sqlite': 1.0.1 '@xmldom/xmldom': 0.9.12