| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338 |
- import { describe, expect, it } from "vitest";
- import { OFFICIAL_CUA_PERMISSION_RULE_TOOL_NAME } from "@zcode/shared";
- import {
- PermissionService,
- type PermissionContext,
- type PermissionToolCapability,
- } from "../src/permission/service.js";
- const readCapability: PermissionToolCapability = {
- destructive: false,
- needsApproval: false,
- readOnly: true,
- riskLevel: "low",
- sideEffectScope: "none",
- };
- const writeCapability: PermissionToolCapability = {
- destructive: false,
- needsApproval: true,
- readOnly: false,
- riskLevel: "medium",
- sideEffectScope: "workspace",
- };
- const editCapability: PermissionToolCapability = {
- destructive: false,
- needsApproval: true,
- permission: {
- denyPriority: "beforeAsk",
- needsApproval: true,
- permission: "edit",
- reason: "Edit modifies file contents",
- riskLevel: "medium",
- sideEffectScope: "workspace",
- },
- readOnly: false,
- riskLevel: "medium",
- sideEffectScope: "workspace",
- };
- const webFetchCapability: PermissionToolCapability = {
- destructive: false,
- needsApproval: true,
- permission: {
- denyPriority: "beforeAsk",
- needsApproval: true,
- permission: "webfetch",
- reason: "Fetch a URL",
- riskLevel: "medium",
- sideEffectScope: "network",
- },
- readOnly: true,
- riskLevel: "medium",
- sideEffectScope: "network",
- };
- const sessionMutationCapability: PermissionToolCapability = {
- destructive: false,
- needsApproval: false,
- permission: {
- denyPriority: "beforeAsk",
- needsApproval: false,
- permission: "target.update",
- reason: "Update session-local goal state",
- riskLevel: "low",
- sideEffectScope: "session",
- },
- readOnly: false,
- riskLevel: "low",
- sideEffectScope: "session",
- };
- const planModeSessionControlCapability: PermissionToolCapability = {
- allowedInPlanMode: true,
- destructive: false,
- needsApproval: false,
- permission: {
- denyPriority: "beforeAsk",
- needsApproval: false,
- permission: "agent.message.respond",
- reason: "Respond to the parent runtime queue",
- riskLevel: "low",
- sideEffectScope: "session",
- },
- readOnly: false,
- riskLevel: "low",
- sideEffectScope: "session",
- };
- const planModeTransitionCapability: PermissionToolCapability = {
- destructive: false,
- needsApproval: true,
- permission: {
- denyPriority: "beforeAsk",
- needsApproval: true,
- permission: "plan.exit",
- reason: "Exit plan mode after user approval",
- riskLevel: "low",
- sideEffectScope: "session",
- },
- readOnly: false,
- requiresUserInteraction: true,
- riskLevel: "low",
- sideEffectScope: "session",
- };
- const mcpCapability: PermissionToolCapability = {
- destructive: false,
- needsApproval: true,
- permission: {
- denyPriority: "beforeAsk",
- needsApproval: true,
- permission: "mcp",
- reason: "MCP tool executes through an external server",
- riskLevel: "medium",
- sideEffectScope: "network",
- },
- readOnly: false,
- riskLevel: "medium",
- sideEffectScope: "network",
- };
- const destructiveMcpCapability: PermissionToolCapability = {
- ...mcpCapability,
- destructive: true,
- permission: {
- ...mcpCapability.permission!,
- riskLevel: "high",
- },
- riskLevel: "high",
- };
- const fakeMcpNameCapability: PermissionToolCapability = {
- destructive: false,
- needsApproval: true,
- readOnly: false,
- riskLevel: "medium",
- sideEffectScope: "network",
- };
- describe("PermissionService mode policy", () => {
- it("treats WebSearch as read-only when resolving fallback capability", () => {
- const service = new PermissionService();
- const decision = service.checkPermission({
- input: { query: "latest zcode" },
- mode: "build",
- riskLevel: "low",
- toolName: "WebSearch",
- });
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- riskLevel: "low",
- ruleId: "mode.build.readOnly",
- sideEffectScope: "none",
- });
- });
- it("allows read-only tools in plan mode", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: {},
- mode: "plan",
- riskLevel: "low",
- toolName: "Read",
- },
- readCapability,
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "mode.plan.readOnly",
- });
- });
- it("does not let allowedTools bypass plan mode write protection", () => {
- const service = new PermissionService({
- allowedTools: new Set(["Write"]),
- autoApproveHighRisk: false,
- disallowedTools: new Set(),
- allowMediumRiskInAutoMode: false,
- });
- const decision = service.checkPermission(
- {
- input: {},
- mode: "plan",
- riskLevel: "medium",
- toolName: "Write",
- },
- writeCapability,
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "deny",
- ruleId: "mode.plan.nonReadOnly",
- });
- });
- it("denies non-read-only session mutations in plan mode", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { todos: [] },
- mode: "plan",
- riskLevel: "low",
- toolName: "TodoWrite",
- },
- sessionMutationCapability,
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "deny",
- ruleId: "mode.plan.nonReadOnly",
- });
- });
- it("allows an explicit non-read-only session capability in plan mode", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { summary: "progress", message: "still working" },
- mode: "plan",
- riskLevel: "low",
- toolName: "RespondToCoordinator",
- },
- planModeSessionControlCapability,
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "mode.plan.explicitSessionCapability",
- sideEffectScope: "session",
- });
- });
- it("does not let explicit session capability bypass plan-mode deny or ask rules", () => {
- const context = {
- input: { summary: "progress", message: "still working" },
- mode: "plan" as const,
- riskLevel: "low" as const,
- toolName: "RespondToCoordinator",
- };
- const disallowed = new PermissionService({
- allowedTools: new Set(),
- autoApproveHighRisk: false,
- disallowedTools: new Set(["RespondToCoordinator"]),
- allowMediumRiskInAutoMode: false,
- });
- expect(disallowed.checkPermission(context, planModeSessionControlCapability)).toMatchObject({
- decision: "deny",
- ruleId: "rule.disallowedTools",
- });
- const service = new PermissionService();
- expect(
- service.checkPermission(context, planModeSessionControlCapability, {
- version: 1,
- deny: [{ toolName: "RespondToCoordinator" }],
- }),
- ).toMatchObject({ decision: "deny", ruleId: "rule.project.deny" });
- expect(
- service.checkPermission(context, planModeSessionControlCapability, {
- version: 1,
- ask: [{ toolName: "RespondToCoordinator" }],
- }),
- ).toMatchObject({ decision: "ask", ruleId: "rule.project.ask" });
- });
- it("rejects invalid explicit plan-mode capabilities", () => {
- const service = new PermissionService();
- const context = {
- input: {},
- mode: "plan" as const,
- riskLevel: "low" as const,
- toolName: "RespondToCoordinator",
- };
- for (const capability of [
- { ...planModeSessionControlCapability, destructive: true },
- {
- ...planModeSessionControlCapability,
- needsApproval: true,
- permission: {
- ...planModeSessionControlCapability.permission!,
- needsApproval: true,
- },
- },
- {
- ...planModeSessionControlCapability,
- permission: {
- ...planModeSessionControlCapability.permission!,
- sideEffectScope: "network" as const,
- },
- sideEffectScope: "network" as const,
- },
- ]) {
- expect(service.checkPermission(context, capability)).toMatchObject({
- decision: "deny",
- ruleId: "mode.plan.nonReadOnly",
- });
- }
- });
- it("allows non-destructive MCP tools in plan mode without requiring readOnly metadata", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { query: "current simulator state" },
- mode: "plan",
- riskLevel: "medium",
- toolName: "mcp__ios_simulator__status",
- },
- mcpCapability,
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "mode.plan.mcp",
- sideEffectScope: "network",
- });
- });
- it("denies destructive MCP tools in plan mode", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { target: "device" },
- mode: "plan",
- riskLevel: "high",
- toolName: "mcp__device__reset",
- },
- destructiveMcpCapability,
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "deny",
- ruleId: "mode.plan.nonReadOnly",
- });
- });
- it("does not treat an mcp-prefixed tool name as MCP without the mcp permission declaration", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: {},
- mode: "plan",
- riskLevel: "medium",
- toolName: "mcp__fake__write",
- },
- fakeMcpNameCapability,
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "deny",
- ruleId: "mode.plan.nonReadOnly",
- });
- });
- it("lets project deny rules override the plan-mode MCP exception", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { query: "secret" },
- mode: "plan",
- riskLevel: "medium",
- toolName: "mcp__external__search",
- },
- mcpCapability,
- {
- version: 1,
- deny: [{ toolName: "mcp__external__search" }],
- },
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "deny",
- ruleId: "rule.project.deny",
- });
- });
- it("lets project ask rules override the plan-mode MCP exception", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { query: "maybe external" },
- mode: "plan",
- riskLevel: "medium",
- toolName: "mcp__external__search",
- },
- mcpCapability,
- {
- version: 1,
- ask: [{ toolName: "mcp__external__search" }],
- },
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "ask",
- ruleId: "rule.project.ask",
- });
- });
- it("denies ExitPlanMode outside plan mode before asking for approval", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { plan: "Implement the approved steps." },
- mode: "build",
- riskLevel: "low",
- toolName: "ExitPlanMode",
- },
- planModeTransitionCapability,
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "deny",
- ruleId: "mode.plan.exitOnly",
- });
- });
- it("asks for ExitPlanMode approval while plan mode is active", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { plan: "Implement the approved steps." },
- mode: "plan",
- riskLevel: "low",
- toolName: "ExitPlanMode",
- },
- planModeTransitionCapability,
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "ask",
- ruleId: "tool.userInteraction",
- });
- });
- it("asks for ExitPlanMode approval even when plan mode was entered from yolo", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { plan: "Implement the approved steps." },
- mode: "plan",
- prePlanMode: "yolo",
- riskLevel: "low",
- toolName: "ExitPlanMode",
- },
- planModeTransitionCapability,
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "ask",
- ruleId: "tool.userInteraction",
- });
- });
- it("allows EnterPlanMode without approval", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: {},
- mode: "build",
- riskLevel: "low",
- toolName: "EnterPlanMode",
- },
- {
- ...planModeTransitionCapability,
- permission: {
- ...planModeTransitionCapability.permission!,
- permission: "plan.enter",
- reason: "Enter plan mode from yolo",
- },
- },
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "tool.plan.enter",
- });
- });
- it("asks for side-effecting tools in build mode", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: {},
- mode: "build",
- riskLevel: "medium",
- toolName: "Write",
- },
- writeCapability,
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "ask",
- escalated: true,
- ruleId: "mode.build.sideEffect",
- });
- });
- it("allows file edit tools in edit mode", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { file_path: "demo.txt" },
- mode: "edit",
- riskLevel: "medium",
- toolName: "Edit",
- },
- editCapability,
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- escalated: false,
- ruleId: "mode.edit.fileEdit",
- });
- });
- it("keeps non-edit side effects on build-mode approval behavior in edit mode", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { command: "npm run build" },
- mode: "edit",
- riskLevel: "high",
- toolName: "Bash",
- },
- {
- destructive: true,
- needsApproval: true,
- readOnly: false,
- riskLevel: "high",
- sideEffectScope: "workspace",
- },
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "ask",
- escalated: true,
- ruleId: "mode.build.highRisk",
- });
- });
- it("lets project ask rules override edit mode auto-approval", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { file_path: "demo.txt" },
- mode: "edit",
- riskLevel: "medium",
- toolName: "Edit",
- },
- editCapability,
- {
- version: 1,
- ask: [{ toolName: "Edit" }],
- },
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "ask",
- ruleId: "rule.project.ask",
- });
- });
- it("lets project allow rules stay explicit in edit mode", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { file_path: "demo.txt" },
- mode: "edit",
- riskLevel: "medium",
- toolName: "Write",
- },
- writeCapability,
- {
- version: 1,
- allow: [{ toolName: "Write" }],
- },
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "rule.project.allow",
- });
- });
- it("allows low-risk session-local state updates in build mode without approval", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { todos: [] },
- mode: "build",
- riskLevel: "low",
- toolName: "TodoWrite",
- },
- sessionMutationCapability,
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "mode.build.sessionState",
- });
- });
- it("allows explicitly allowed tools in build mode", () => {
- const service = new PermissionService({
- allowedTools: new Set(["Write"]),
- autoApproveHighRisk: false,
- disallowedTools: new Set(),
- allowMediumRiskInAutoMode: false,
- });
- const decision = service.checkPermission(
- {
- input: {},
- mode: "build",
- riskLevel: "medium",
- toolName: "Write",
- },
- writeCapability,
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "rule.allowedTools",
- });
- });
- it("allows side-effecting tools that match project permission rules", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { file_path: "demo.txt" },
- mode: "build",
- riskLevel: "medium",
- toolName: "Write",
- },
- writeCapability,
- {
- version: 1,
- allow: [{ toolName: "Write" }],
- },
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "rule.project.allow",
- });
- });
- it("shares an official CUA project rule only with authority-verified CUA tools", () => {
- const service = new PermissionService();
- const rules = {
- allow: [{ toolName: OFFICIAL_CUA_PERMISSION_RULE_TOOL_NAME }],
- version: 1 as const,
- };
- const context = {
- input: { app_ref: { bundle_id: "com.apple.TextEdit" } },
- mode: "build" as const,
- riskLevel: "medium" as const,
- toolName: "mcp__computer-use__left_click",
- };
- const trusted = service.checkPermission(
- context,
- {
- ...mcpCapability,
- permissionCapabilityGroup: "official_cua",
- } as PermissionToolCapability,
- rules as never,
- );
- const sameNameButUntrusted = service.checkPermission(context, mcpCapability, rules as never);
- expect(trusted).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "rule.project.allow",
- });
- expect(sameNameButUntrusted).toMatchObject({
- allowed: false,
- decision: "ask",
- ruleId: "mode.build.sideEffect",
- });
- });
- it("does not fall back to exact tool-name matching when a capability group loses authority", () => {
- const service = new PermissionService();
- const context = {
- input: {},
- mode: "build" as const,
- riskLevel: "medium" as const,
- toolName: "mcp__computer-use__get_app_state",
- };
- const rules = {
- allow: [{ toolName: OFFICIAL_CUA_PERMISSION_RULE_TOOL_NAME }],
- version: 1 as const,
- };
- expect(service.checkPermission(context, mcpCapability, rules as never)).toMatchObject({
- allowed: false,
- decision: "ask",
- ruleId: "mode.build.sideEffect",
- });
- });
- it("does not let project allow rules bypass plan mode write protection", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { file_path: "demo.txt" },
- mode: "plan",
- riskLevel: "medium",
- toolName: "Write",
- },
- writeCapability,
- {
- version: 1,
- allow: [{ toolName: "Write" }],
- },
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "deny",
- ruleId: "mode.plan.nonReadOnly",
- });
- });
- it("matches project command prefix rules", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { command: "npm run test -- --watch=false" },
- mode: "build",
- riskLevel: "high",
- toolName: "Bash",
- },
- {
- destructive: true,
- needsApproval: true,
- readOnly: false,
- riskLevel: "high",
- sideEffectScope: "workspace",
- },
- {
- version: 1,
- allow: [{ toolName: "Bash", ruleContent: "npm run:*" }],
- },
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "rule.project.allow",
- });
- });
- it("matches WebFetch project rules by normalized domain", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { url: " https://EXAMPLE.com/docs?token=redacted " },
- mode: "build",
- riskLevel: "medium",
- toolName: "WebFetch",
- },
- webFetchCapability,
- {
- version: 1,
- allow: [{ toolName: "WebFetch", ruleContent: "domain:example.com" }],
- },
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "rule.project.allow",
- });
- });
- it("does not match WebFetch project rules against full URLs", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { url: "http://example.com/docs" },
- mode: "build",
- riskLevel: "medium",
- toolName: "WebFetch",
- },
- webFetchCapability,
- {
- version: 1,
- deny: [{ toolName: "WebFetch", ruleContent: "https://example.com/docs" }],
- },
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "ask",
- ruleId: "mode.build.sideEffect",
- });
- });
- it("allows preapproved WebFetch URLs after project deny and ask rules are checked", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { url: "https://docs.python.org/3/library/pathlib.html" },
- mode: "build",
- riskLevel: "medium",
- toolName: "WebFetch",
- },
- webFetchCapability,
- {
- version: 1,
- },
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "tool.webfetch.preapproved",
- });
- });
- it("does not auto-allow removed vendor WebFetch URLs", () => {
- const service = new PermissionService();
- for (const url of [
- "https://platform.claude.com/docs",
- "https://code.claude.com/docs",
- "https://github.com/anthropics/example",
- ]) {
- const decision = service.checkPermission(
- {
- input: { url },
- mode: "build",
- riskLevel: "medium",
- toolName: "WebFetch",
- },
- webFetchCapability,
- {
- version: 1,
- },
- );
- expect(decision, url).toMatchObject({
- allowed: false,
- decision: "ask",
- ruleId: "mode.build.sideEffect",
- });
- }
- });
- it("lets WebFetch project ask rules override preapproved URL auto-allow", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { url: "https://docs.python.org/3/library/pathlib.html" },
- mode: "build",
- riskLevel: "medium",
- toolName: "WebFetch",
- },
- webFetchCapability,
- {
- version: 1,
- ask: [{ toolName: "WebFetch", ruleContent: "domain:docs.python.org" }],
- },
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "ask",
- ruleId: "rule.project.ask",
- });
- });
- it("lets WebFetch project deny domain rules override allow rules", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: { url: "https://example.com/docs" },
- mode: "build",
- riskLevel: "medium",
- toolName: "WebFetch",
- },
- webFetchCapability,
- {
- version: 1,
- allow: [{ toolName: "WebFetch", ruleContent: "domain:*.example.com" }],
- deny: [{ toolName: "WebFetch", ruleContent: "domain:example.com" }],
- },
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "deny",
- ruleId: "rule.project.deny",
- });
- });
- it("lets yolo bypass disallowed tool rules", () => {
- const service = new PermissionService({
- allowedTools: new Set(),
- autoApproveHighRisk: false,
- disallowedTools: new Set(["Bash"]),
- allowMediumRiskInAutoMode: false,
- });
- const decision = service.checkPermission(
- {
- input: { command: "rm -rf build" },
- mode: "yolo",
- riskLevel: "high",
- toolName: "Bash",
- },
- {
- destructive: true,
- needsApproval: true,
- readOnly: false,
- riskLevel: "high",
- sideEffectScope: "system",
- },
- );
- expect(decision).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "mode.yolo",
- });
- });
- it("does not let yolo bypass user interaction tools", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: {
- questions: [
- {
- header: "Choice",
- question: "Which option should we use?",
- options: [
- { label: "A", description: "Use A" },
- { label: "B", description: "Use B" },
- ],
- },
- ],
- },
- mode: "yolo",
- riskLevel: "low",
- toolName: "AskUserQuestion",
- },
- {
- destructive: false,
- needsApproval: true,
- readOnly: true,
- requiresUserInteraction: true,
- riskLevel: "low",
- sideEffectScope: "userInteraction",
- },
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "ask",
- ruleId: "tool.userInteraction",
- });
- });
- it("keeps auto mode reserved and non-executable", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- {
- input: {},
- mode: "auto",
- riskLevel: "low",
- toolName: "Read",
- },
- readCapability,
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "deny",
- ruleId: "mode.auto.unimplemented",
- });
- });
- });
- // ── alwaysAsk:模式无关的确认(docs/dynamic-workflow/launch.md「Why the window always appears」)──
- const alwaysAskCapability: PermissionToolCapability = {
- destructive: false,
- needsApproval: true,
- permission: {
- alwaysAsk: true,
- denyPriority: "beforeAsk",
- needsApproval: true,
- permission: "createWorkflow",
- reason: "createWorkflow.runConfirmation",
- riskLevel: "low",
- sideEffectScope: "none",
- },
- readOnly: true,
- riskLevel: "low",
- sideEffectScope: "none",
- };
- describe("PermissionService alwaysAsk", () => {
- function check(mode: PermissionContext["mode"], service = new PermissionService()) {
- return service.checkPermission(
- { input: { script: "return 1;" }, mode, riskLevel: "low", toolName: "CreateWorkflow" },
- alwaysAskCapability,
- );
- }
- // 宽松度设置一律压不过 alwaysAsk:yolo 直通、plan 的 readOnly 直通、edit/build 都要问。
- for (const mode of ["yolo", "plan", "build", "edit", "default"] as const) {
- it(`asks in ${mode} mode`, () => {
- expect(check(mode)).toMatchObject({
- allowed: false,
- alwaysAsk: true,
- decision: "ask",
- ruleId: "tool.alwaysAsk",
- });
- });
- }
- // 反向边界:alwaysAsk 只压过"放行"分支,压不过"阻断"分支。
- it("still yields to an explicitly disallowed tool", () => {
- const service = new PermissionService({
- allowedTools: new Set(),
- allowMediumRiskInAutoMode: false,
- autoApproveHighRisk: false,
- disallowedTools: new Set(["CreateWorkflow"]),
- });
- expect(check("yolo", service)).toMatchObject({
- allowed: false,
- decision: "deny",
- ruleId: "rule.disallowedTools",
- });
- });
- it("still yields to a project deny rule", () => {
- const service = new PermissionService();
- const decision = service.checkPermission(
- { input: { script: "return 1;" }, mode: "yolo", riskLevel: "low", toolName: "CreateWorkflow" },
- alwaysAskCapability,
- { deny: [{ toolName: "CreateWorkflow" }], version: 1 },
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "deny",
- ruleId: "rule.project.deny",
- });
- });
- it("still yields to auto mode being unimplemented", () => {
- expect(check("auto")).toMatchObject({
- allowed: false,
- decision: "deny",
- ruleId: "mode.auto.unimplemented",
- });
- });
- it("leaves tools without the flag on their existing mode semantics", () => {
- expect(
- new PermissionService().checkPermission(
- { input: {}, mode: "yolo", riskLevel: "low", toolName: "Read" },
- readCapability,
- ),
- ).toMatchObject({ allowed: true, decision: "allow", ruleId: "mode.yolo" });
- });
- });
- // ── 会话免确认(docs/dynamic-workflow/launch.md「Always allow in this session」)──
- // 「Always allow in this session」把 alwaysAsk gate 在一个 PermissionService 实例(= 一个会话)内
- // 关掉;阻断分支仍然优先,普通工具的模式语义不认会话规则。
- describe("PermissionService session grant", () => {
- const sessionGrant = [
- {
- behavior: "allow" as const,
- rules: [{ toolName: "CreateWorkflow" }],
- type: "addRules" as const,
- },
- ];
- function check(mode: PermissionContext["mode"], service: PermissionService, toolName = "CreateWorkflow") {
- return service.checkPermission(
- { input: { script: "return 2;" }, mode, riskLevel: "low", toolName },
- alwaysAskCapability,
- );
- }
- for (const mode of ["yolo", "plan", "build", "edit", "default"] as const) {
- it(`lifts the alwaysAsk gate in ${mode} mode once the session grant is in place`, () => {
- const service = new PermissionService();
- expect(check(mode, service).decision).toBe("ask");
- service.grantSessionPermission(sessionGrant);
- expect(check(mode, service)).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "rule.session.allow",
- });
- });
- }
- it("is scoped to the granted tool name", () => {
- const service = new PermissionService();
- service.grantSessionPermission(sessionGrant);
- expect(check("build", service, "SaveWorkflow")).toMatchObject({
- decision: "ask",
- ruleId: "tool.alwaysAsk",
- });
- });
- it("does not survive into a fresh service instance (session boundary)", () => {
- const granted = new PermissionService();
- granted.grantSessionPermission(sessionGrant);
- expect(check("build", granted).decision).toBe("allow");
- expect(check("build", new PermissionService()).decision).toBe("ask");
- });
- it("still yields to an explicitly disallowed tool", () => {
- const service = new PermissionService({
- allowedTools: new Set(),
- allowMediumRiskInAutoMode: false,
- autoApproveHighRisk: false,
- disallowedTools: new Set(["CreateWorkflow"]),
- });
- service.grantSessionPermission(sessionGrant);
- expect(check("yolo", service)).toMatchObject({
- decision: "deny",
- ruleId: "rule.disallowedTools",
- });
- });
- it("still yields to a project deny rule", () => {
- const service = new PermissionService();
- service.grantSessionPermission(sessionGrant);
- expect(
- service.checkPermission(
- { input: { script: "return 2;" }, mode: "yolo", riskLevel: "low", toolName: "CreateWorkflow" },
- alwaysAskCapability,
- { deny: [{ toolName: "CreateWorkflow" }], version: 1 },
- ),
- ).toMatchObject({ decision: "deny", ruleId: "rule.project.deny" });
- });
- // 会话规则只服务 alwaysAsk gate:plan 模式下的只读约束不能被一条会话规则绕开。
- it("leaves tools without the alwaysAsk flag on their mode semantics", () => {
- const service = new PermissionService();
- service.grantSessionPermission([
- { behavior: "allow", rules: [{ toolName: "Write" }], type: "addRules" },
- ]);
- expect(
- service.checkPermission(
- { input: { file_path: "/w/a.ts" }, mode: "plan", riskLevel: "medium", toolName: "Write" },
- writeCapability,
- ).decision,
- ).not.toBe("allow");
- });
- });
- // ── 本会话 run 的修订免确认(docs/dynamic-workflow/launch.md「Amending this session's runs」)──
- // 钥匙是 resolveInput 回填的 `predecessor`(journal 的 parent_session_id / stopReason),不是内存表:
- // 本会话发起且非用户停下 → 放行;别的会话的 run、用户停过的 run、事实块缺席、其他工具都照常 ask;
- // 阻断分支仍优先。没有可播种、可撤销的状态,所以新实例上同样成立。
- describe("PermissionService workflow owner rule", () => {
- const owned = {
- run_id: "dwfrun-a",
- script: "return 3;",
- predecessor: { status: "running", owned_by_this_session: true },
- };
- function checkAmend(
- service: PermissionService,
- input: Record<string, unknown>,
- mode: PermissionContext["mode"] = "build",
- toolName = "AmendWorkflow",
- ) {
- return service.checkPermission(
- { input, mode, riskLevel: "low", toolName },
- alwaysAskCapability,
- );
- }
- for (const mode of ["yolo", "plan", "build", "edit", "default"] as const) {
- it(`lifts the gate in ${mode} mode for an amend of a run this session started`, () => {
- expect(checkAmend(new PermissionService(), owned, mode)).toMatchObject({
- allowed: true,
- decision: "allow",
- ruleId: "rule.session.workflowOwner",
- });
- });
- }
- it("keeps asking for another session's run, a user-stopped run, and a missing fact block", () => {
- const service = new PermissionService();
- expect(
- checkAmend(service, {
- ...owned,
- predecessor: { status: "completed", owned_by_this_session: false },
- }),
- ).toMatchObject({ decision: "ask", ruleId: "tool.alwaysAsk" });
- expect(
- checkAmend(service, {
- ...owned,
- predecessor: { status: "stopped", stop_reason: "user", owned_by_this_session: true },
- }),
- ).toMatchObject({ decision: "ask", ruleId: "tool.alwaysAsk" });
- expect(checkAmend(service, { run_id: "dwfrun-a", script: "return 3;" })).toMatchObject({
- decision: "ask",
- ruleId: "tool.alwaysAsk",
- });
- });
- it("allows an own run the model stopped, or that settled on its own", () => {
- const service = new PermissionService();
- for (const stop_reason of ["model", "provider", "interrupted", "superseded"] as const) {
- expect(
- checkAmend(service, {
- ...owned,
- predecessor: { status: "stopped", stop_reason, owned_by_this_session: true },
- }).decision,
- ).toBe("allow");
- }
- expect(
- checkAmend(service, {
- ...owned,
- predecessor: { status: "completed", owned_by_this_session: true },
- }).decision,
- ).toBe("allow");
- });
- it("is scoped to AmendWorkflow", () => {
- const service = new PermissionService();
- expect(checkAmend(service, owned, "build", "CreateWorkflow")).toMatchObject({
- decision: "ask",
- ruleId: "tool.alwaysAsk",
- });
- expect(checkAmend(service, owned, "build", "SaveWorkflow")).toMatchObject({
- decision: "ask",
- ruleId: "tool.alwaysAsk",
- });
- });
- it("still yields to an explicitly disallowed tool and to a project deny rule", () => {
- const disallowed = new PermissionService({
- allowedTools: new Set(),
- allowMediumRiskInAutoMode: false,
- autoApproveHighRisk: false,
- disallowedTools: new Set(["AmendWorkflow"]),
- });
- expect(checkAmend(disallowed, owned, "yolo")).toMatchObject({
- decision: "deny",
- ruleId: "rule.disallowedTools",
- });
- const denied = new PermissionService();
- expect(
- denied.checkPermission(
- { input: owned, mode: "yolo", riskLevel: "low", toolName: "AmendWorkflow" },
- alwaysAskCapability,
- { deny: [{ toolName: "AmendWorkflow" }], version: 1 },
- ),
- ).toMatchObject({ decision: "deny", ruleId: "rule.project.deny" });
- });
- });
|