From 4922daf4e7792c712a286c5bda41c0de1e6e630e Mon Sep 17 00:00:00 2001 From: yunyingk Date: Tue, 29 Sep 2026 22:04:35 +0800 Subject: [PATCH] Require --yes for user deletion, password reset, and batch cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rules delete / documents delete / webhook delete all gate behind --yes, but users delete, users reset-password and simulation clean reached the API with no confirmation at all — a bare invocation deleted live data. Add the same confirmationRequired gate (exit 10, no API call) the other destructive commands use, and update the help registry plus the skill reference so the usage lines show --yes. --- skills/seal-use/references/cli.md | 8 +- src/cli/commands/simulation.test.ts | 114 ++++++++++++++++++++++ src/cli/commands/simulation.ts | 10 +- src/cli/commands/users.test.ts | 144 ++++++++++++++++++++++++++++ src/cli/commands/users.ts | 21 +++- src/cli/help.ts | 6 +- 6 files changed, 292 insertions(+), 11 deletions(-) create mode 100644 src/cli/commands/simulation.test.ts create mode 100644 src/cli/commands/users.test.ts diff --git a/skills/seal-use/references/cli.md b/skills/seal-use/references/cli.md index 2c6a4dd..a1c369d 100644 --- a/skills/seal-use/references/cli.md +++ b/skills/seal-use/references/cli.md @@ -113,9 +113,9 @@ seal-use users create --name --phone [--dept ] [--corp [--corp ] seal-use users delegation [--note ] [--corp ] seal-use users activate [--corp ] -seal-use users reset-password [--corp ] +seal-use users reset-password --yes [--corp ] seal-use users get [--corp ] -seal-use users delete [--corp ] +seal-use users delete --yes [--corp ] seal-use contacts departments [] [--corp ] seal-use contacts children [--corp ] seal-use contacts users [--corp ] @@ -182,7 +182,7 @@ seal-use simulation create --mode material_pack --package-ids seal-use simulation status --corp seal-use simulation wait [--timeout 10m] [--poll-interval 2s] --corp -seal-use simulation clean --corp +seal-use simulation clean --yes --corp seal-use simulation specifications [--integration ] --corp seal-use simulation approval-definitions --corp seal-use simulation list [--limit 50] --corp @@ -356,7 +356,7 @@ Approval runs, simulations, and Langfuse bridge: - `seal-use simulation status --corp ` performs one non-blocking status check and returns `QUEUED`/`RUNNING`/`COMPLETED`/`FAILED`-style state plus counters. - `seal-use simulation approval-definitions --corp ` lists available Feishu approval definitions so an `approvalCode` can be selected before creating a batch. - `seal-use simulation wait --corp ` -- `seal-use simulation clean --corp ` +- `seal-use simulation clean --yes --corp ` - `seal-use simulation list --corp ` - `seal-use simulation get --corp ` - `seal-use simulation batch-records --corp ` diff --git a/src/cli/commands/simulation.test.ts b/src/cli/commands/simulation.test.ts new file mode 100644 index 0000000..fa5d969 --- /dev/null +++ b/src/cli/commands/simulation.test.ts @@ -0,0 +1,114 @@ +import { afterEach, describe, expect, test } from "bun:test"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { runCli } from "../runner.js"; + +const ORIGINAL_ENV = { + enterprises: process.env.SEAL_USE_ENTERPRISES_DIR, + config: process.env.SEAL_USE_CONFIG_FILE, + tokenStore: process.env.SEAL_USE_TOKEN_STORE, + usageFile: process.env.SEAL_USE_USAGE_FILE +}; +const ORIGINAL_FETCH = globalThis.fetch; +const tempDirs: string[] = []; + +afterEach(() => { + restoreEnv("SEAL_USE_ENTERPRISES_DIR", ORIGINAL_ENV.enterprises); + restoreEnv("SEAL_USE_CONFIG_FILE", ORIGINAL_ENV.config); + restoreEnv("SEAL_USE_TOKEN_STORE", ORIGINAL_ENV.tokenStore); + restoreEnv("SEAL_USE_USAGE_FILE", ORIGINAL_ENV.usageFile); + globalThis.fetch = ORIGINAL_FETCH; + + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } +}); + +function restoreEnv(name: string, value: string | undefined) { + if (value === undefined) delete process.env[name]; + else process.env[name] = value; +} + +function setupMockEnv() { + const root = mkdtempSync(join(tmpdir(), "seal-use-sim-cli-")); + const enterprises = join(root, "enterprises"); + tempDirs.push(root); + process.env.SEAL_USE_ENTERPRISES_DIR = enterprises; + process.env.SEAL_USE_CONFIG_FILE = join(root, "config.json"); + process.env.SEAL_USE_TOKEN_STORE = join(root, "tokens.json"); + process.env.SEAL_USE_USAGE_FILE = join(root, "usage.json"); + mkdirSync(enterprises, { recursive: true }); + + writeFileSync( + join(enterprises, "sim-corp.json"), + JSON.stringify( + { + id: "sim-corp", + name: "Sim Corp", + environment: "test", + seal: { + url: "https://sim-corp.sealai.cc", + endpoints: { approvalStylePreferences: "api/v1/agent/ai-approval/config" } + }, + source: { type: "seal", token: "mock-token", sealUrl: "https://sim-corp.sealai.cc" } + }, + null, + 2 + ) + ); + writeFileSync( + join(root, "config.json"), + JSON.stringify({ defaultCorpId: "sim-corp", stateful: true }, null, 2) + ); +} + +function trackMutationRequests() { + const mutations: string[] = []; + globalThis.fetch = (async (input: RequestInfo | URL) => { + const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; + mutations.push(url); + return new Response(JSON.stringify({ data: { success: true } }), { + status: 200, + headers: { "Content-Type": "application/json" } + }); + }) as typeof fetch; + return mutations; +} + +describe("simulation command confirmation gates", () => { + test("simulation clean refuses without --yes and never reaches the API", async () => { + setupMockEnv(); + const mutations = trackMutationRequests(); + + const result = await runCli(["simulation", "clean", "batch-123"]); + + expect(result.isError).toBe(true); + expect(result.exitCode).toBe(10); + expect(result.stderr).toContain("Refusing to delete simulation batch 'batch-123' without --yes."); + expect(result.stderr).toContain("seal-use simulation clean batch-123"); + expect(mutations).toEqual([]); + }); + + test("simulation delete alias also requires --yes", async () => { + setupMockEnv(); + const mutations = trackMutationRequests(); + + const result = await runCli(["simulation", "delete", "batch-123"]); + + expect(result.isError).toBe(true); + expect(result.exitCode).toBe(10); + expect(mutations).toEqual([]); + }); + + test("simulation clean runs with --yes", async () => { + setupMockEnv(); + const mutations = trackMutationRequests(); + + const result = await runCli(["simulation", "clean", "batch-123", "--yes"]); + + expect(result.isError).toBe(false); + expect(result.exitCode).toBe(0); + expect(mutations.some((url) => url.includes("batch-123"))).toBe(true); + }); +}); diff --git a/src/cli/commands/simulation.ts b/src/cli/commands/simulation.ts index 9ce6c4d..4edd3f4 100644 --- a/src/cli/commands/simulation.ts +++ b/src/cli/commands/simulation.ts @@ -1,6 +1,7 @@ import { existsSync, readFileSync } from "node:fs"; import type { CorpConfig } from "../../core/config/types.js"; import type { createSealClient } from "../../core/http/factory.js"; +import { confirmationRequired } from "../errors.js"; import { runTool, runToolResult } from "../helpers.js"; import { booleanOption, @@ -151,7 +152,14 @@ export async function handleSimulationCommand( if (action === "clean" || action === "delete") { const batchId = maybeName ?? stringOption(args.options.batchId) ?? stringOption(args.options["batch-id"]); - if (!batchId) throw new Error("Usage: seal-use simulation clean "); + if (!batchId) throw new Error("Usage: seal-use simulation clean --yes"); + const confirmed = booleanOption(args.options.yes) === true || booleanOption(args.options.confirm) === true; + if (!confirmed) { + throw confirmationRequired( + `Refusing to delete simulation batch '${batchId}' without --yes.`, + `seal-use simulation clean ${batchId} --corp ${corp.id} --yes` + ); + } await runTool("seal_simulation_batch_clean", client, corp, { batchId }, outputOptions(args.options)); return; } diff --git a/src/cli/commands/users.test.ts b/src/cli/commands/users.test.ts new file mode 100644 index 0000000..cae7f78 --- /dev/null +++ b/src/cli/commands/users.test.ts @@ -0,0 +1,144 @@ +import { afterEach, describe, expect, test } from "bun:test"; +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { runCli } from "../runner.js"; + +const ORIGINAL_ENV = { + enterprises: process.env.SEAL_USE_ENTERPRISES_DIR, + config: process.env.SEAL_USE_CONFIG_FILE, + tokenStore: process.env.SEAL_USE_TOKEN_STORE, + usageFile: process.env.SEAL_USE_USAGE_FILE +}; +const ORIGINAL_FETCH = globalThis.fetch; +const tempDirs: string[] = []; + +afterEach(() => { + restoreEnv("SEAL_USE_ENTERPRISES_DIR", ORIGINAL_ENV.enterprises); + restoreEnv("SEAL_USE_CONFIG_FILE", ORIGINAL_ENV.config); + restoreEnv("SEAL_USE_TOKEN_STORE", ORIGINAL_ENV.tokenStore); + restoreEnv("SEAL_USE_USAGE_FILE", ORIGINAL_ENV.usageFile); + globalThis.fetch = ORIGINAL_FETCH; + + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } +}); + +function restoreEnv(name: string, value: string | undefined) { + if (value === undefined) delete process.env[name]; + else process.env[name] = value; +} + +function setupMockEnv() { + const root = mkdtempSync(join(tmpdir(), "seal-use-users-cli-")); + const enterprises = join(root, "enterprises"); + tempDirs.push(root); + process.env.SEAL_USE_ENTERPRISES_DIR = enterprises; + process.env.SEAL_USE_CONFIG_FILE = join(root, "config.json"); + process.env.SEAL_USE_TOKEN_STORE = join(root, "tokens.json"); + process.env.SEAL_USE_USAGE_FILE = join(root, "usage.json"); + mkdirSync(enterprises, { recursive: true }); + + writeFileSync( + join(enterprises, "users-corp.json"), + JSON.stringify( + { + id: "users-corp", + name: "Users Corp", + environment: "test", + seal: { + url: "https://users-corp.sealai.cc", + endpoints: { approvalStylePreferences: "api/v1/agent/ai-approval/config" } + }, + source: { type: "seal", token: "mock-token", sealUrl: "https://users-corp.sealai.cc" } + }, + null, + 2 + ) + ); + + writeFileSync( + join(root, "config.json"), + JSON.stringify({ defaultCorpId: "users-corp", stateful: true }, null, 2) + ); +} + +/** Counts requests that would actually delete or reset a user. */ +function trackUserMutationRequests() { + const mutations: string[] = []; + globalThis.fetch = (async (input: RequestInfo | URL) => { + const url = typeof input === "string" + ? input + : input instanceof URL + ? input.toString() + : input.url; + mutations.push(url); + // Endpoint-shaped payloads: reset-password is validated against + // SealUserResetPasswordResultSchema ({ password }). + const payload = url.includes("reset-password") + ? { data: { password: "mock-reset-password" } } + : { data: { success: true } }; + return new Response(JSON.stringify(payload), { + status: 200, + headers: { "Content-Type": "application/json" } + }); + }) as typeof fetch; + return mutations; +} + +describe("users command confirmation gates", () => { + test("users delete refuses without --yes and never reaches the API", async () => { + setupMockEnv(); + const mutations = trackUserMutationRequests(); + + const result = await runCli(["users", "delete", "user-123"]); + + expect(result.isError).toBe(true); + expect(result.exitCode).toBe(10); + expect(result.stderr).toContain("Refusing to delete user 'user-123' without --yes."); + expect(result.stderr).toContain("seal-use users delete user-123"); + expect(mutations).toEqual([]); + }); + + test("users delete runs with --yes", async () => { + setupMockEnv(); + trackUserMutationRequests(); + + const result = await runCli(["users", "delete", "user-123", "--yes"]); + + expect(result.isError).toBe(false); + expect(result.exitCode).toBe(0); + }); + + test("users reset-password refuses without --yes and never reaches the API", async () => { + setupMockEnv(); + const mutations = trackUserMutationRequests(); + + const result = await runCli(["users", "reset-password", "user-123"]); + + expect(result.isError).toBe(true); + expect(result.exitCode).toBe(10); + expect(result.stderr).toContain("Refusing to reset password of user 'user-123' without --yes."); + expect(mutations).toEqual([]); + }); + + test("users reset-password runs with --confirm", async () => { + setupMockEnv(); + trackUserMutationRequests(); + + const result = await runCli(["users", "reset-password", "user-123", "--confirm"]); + + expect(result.isError).toBe(false); + expect(result.exitCode).toBe(0); + }); + + test("usage line mentions --yes when the id is missing", async () => { + setupMockEnv(); + + const result = await runCli(["users", "delete"]); + + expect(result.isError).toBe(true); + expect(result.stderr).toContain("--yes"); + }); +}); diff --git a/src/cli/commands/users.ts b/src/cli/commands/users.ts index 9136b01..0c0cfe1 100644 --- a/src/cli/commands/users.ts +++ b/src/cli/commands/users.ts @@ -1,7 +1,8 @@ import type { CorpConfig } from "../../core/config/types.js"; import type { createSealClient } from "../../core/http/factory.js"; +import { confirmationRequired } from "../errors.js"; import { runTool } from "../helpers.js"; -import { stringOption } from "../parser.js"; +import { booleanOption, stringOption } from "../parser.js"; import { outputOptions } from "../printer.js"; import type { ParsedArgs } from "../types.js"; @@ -24,7 +25,14 @@ export async function handleUsersCommand( if (action === "reset-password") { const userId = maybeName ?? stringOption(args.options.userId); - if (!userId) throw new Error("Usage: seal-use users reset-password "); + if (!userId) throw new Error("Usage: seal-use users reset-password --yes"); + const confirmed = booleanOption(args.options.yes) === true || booleanOption(args.options.confirm) === true; + if (!confirmed) { + throw confirmationRequired( + `Refusing to reset password of user '${userId}' without --yes.`, + `seal-use users reset-password ${userId} --corp ${corp.id} --yes` + ); + } await runTool("seal_user_reset_password", client, corp, { userId }, outputOptions(args.options)); return; } @@ -44,7 +52,14 @@ export async function handleUsersCommand( if (action === "delete") { const userId = maybeName ?? stringOption(args.options.userId); - if (!userId) throw new Error("Usage: seal-use users delete "); + if (!userId) throw new Error("Usage: seal-use users delete --yes"); + const confirmed = booleanOption(args.options.yes) === true || booleanOption(args.options.confirm) === true; + if (!confirmed) { + throw confirmationRequired( + `Refusing to delete user '${userId}' without --yes.`, + `seal-use users delete ${userId} --corp ${corp.id} --yes` + ); + } await runTool("seal_user_delete", client, corp, { userId }, outputOptions(args.options)); return; } diff --git a/src/cli/help.ts b/src/cli/help.ts index aa9ba81..48dd1f7 100644 --- a/src/cli/help.ts +++ b/src/cli/help.ts @@ -623,7 +623,7 @@ export const HELP_REGISTRY: AreaHelp[] = [ { name: "clean", summary: "Clean simulation batch records", - usage: ["seal-use simulation clean [--corp ]"] + usage: ["seal-use simulation clean --yes [--corp ]"] }, { name: "specifications", @@ -866,7 +866,7 @@ export const HELP_REGISTRY: AreaHelp[] = [ { name: "reset-password", summary: "Reset user password", - usage: ["seal-use users reset-password [--corp ]"] + usage: ["seal-use users reset-password --yes [--corp ]"] }, { name: "get", @@ -876,7 +876,7 @@ export const HELP_REGISTRY: AreaHelp[] = [ { name: "delete", summary: "Delete user", - usage: ["seal-use users delete [--corp ]"] + usage: ["seal-use users delete --yes [--corp ]"] } ] },