permission-service.test.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. import { describe, expect, it } from "vitest";
  2. import { OFFICIAL_CUA_PERMISSION_RULE_TOOL_NAME } from "@zcode/shared";
  3. import {
  4. PermissionService,
  5. type PermissionContext,
  6. type PermissionToolCapability,
  7. } from "../src/permission/service.js";
  8. const readCapability: PermissionToolCapability = {
  9. destructive: false,
  10. needsApproval: false,
  11. readOnly: true,
  12. riskLevel: "low",
  13. sideEffectScope: "none",
  14. };
  15. const writeCapability: PermissionToolCapability = {
  16. destructive: false,
  17. needsApproval: true,
  18. readOnly: false,
  19. riskLevel: "medium",
  20. sideEffectScope: "workspace",
  21. };
  22. const editCapability: PermissionToolCapability = {
  23. destructive: false,
  24. needsApproval: true,
  25. permission: {
  26. denyPriority: "beforeAsk",
  27. needsApproval: true,
  28. permission: "edit",
  29. reason: "Edit modifies file contents",
  30. riskLevel: "medium",
  31. sideEffectScope: "workspace",
  32. },
  33. readOnly: false,
  34. riskLevel: "medium",
  35. sideEffectScope: "workspace",
  36. };
  37. const webFetchCapability: PermissionToolCapability = {
  38. destructive: false,
  39. needsApproval: true,
  40. permission: {
  41. denyPriority: "beforeAsk",
  42. needsApproval: true,
  43. permission: "webfetch",
  44. reason: "Fetch a URL",
  45. riskLevel: "medium",
  46. sideEffectScope: "network",
  47. },
  48. readOnly: true,
  49. riskLevel: "medium",
  50. sideEffectScope: "network",
  51. };
  52. const sessionMutationCapability: PermissionToolCapability = {
  53. destructive: false,
  54. needsApproval: false,
  55. permission: {
  56. denyPriority: "beforeAsk",
  57. needsApproval: false,
  58. permission: "target.update",
  59. reason: "Update session-local goal state",
  60. riskLevel: "low",
  61. sideEffectScope: "session",
  62. },
  63. readOnly: false,
  64. riskLevel: "low",
  65. sideEffectScope: "session",
  66. };
  67. const planModeSessionControlCapability: PermissionToolCapability = {
  68. allowedInPlanMode: true,
  69. destructive: false,
  70. needsApproval: false,
  71. permission: {
  72. denyPriority: "beforeAsk",
  73. needsApproval: false,
  74. permission: "agent.message.respond",
  75. reason: "Respond to the parent runtime queue",
  76. riskLevel: "low",
  77. sideEffectScope: "session",
  78. },
  79. readOnly: false,
  80. riskLevel: "low",
  81. sideEffectScope: "session",
  82. };
  83. const planModeTransitionCapability: PermissionToolCapability = {
  84. destructive: false,
  85. needsApproval: true,
  86. permission: {
  87. denyPriority: "beforeAsk",
  88. needsApproval: true,
  89. permission: "plan.exit",
  90. reason: "Exit plan mode after user approval",
  91. riskLevel: "low",
  92. sideEffectScope: "session",
  93. },
  94. readOnly: false,
  95. requiresUserInteraction: true,
  96. riskLevel: "low",
  97. sideEffectScope: "session",
  98. };
  99. const mcpCapability: PermissionToolCapability = {
  100. destructive: false,
  101. needsApproval: true,
  102. permission: {
  103. denyPriority: "beforeAsk",
  104. needsApproval: true,
  105. permission: "mcp",
  106. reason: "MCP tool executes through an external server",
  107. riskLevel: "medium",
  108. sideEffectScope: "network",
  109. },
  110. readOnly: false,
  111. riskLevel: "medium",
  112. sideEffectScope: "network",
  113. };
  114. const destructiveMcpCapability: PermissionToolCapability = {
  115. ...mcpCapability,
  116. destructive: true,
  117. permission: {
  118. ...mcpCapability.permission!,
  119. riskLevel: "high",
  120. },
  121. riskLevel: "high",
  122. };
  123. const fakeMcpNameCapability: PermissionToolCapability = {
  124. destructive: false,
  125. needsApproval: true,
  126. readOnly: false,
  127. riskLevel: "medium",
  128. sideEffectScope: "network",
  129. };
  130. describe("PermissionService mode policy", () => {
  131. it("treats WebSearch as read-only when resolving fallback capability", () => {
  132. const service = new PermissionService();
  133. const decision = service.checkPermission({
  134. input: { query: "latest zcode" },
  135. mode: "build",
  136. riskLevel: "low",
  137. toolName: "WebSearch",
  138. });
  139. expect(decision).toMatchObject({
  140. allowed: true,
  141. decision: "allow",
  142. riskLevel: "low",
  143. ruleId: "mode.build.readOnly",
  144. sideEffectScope: "none",
  145. });
  146. });
  147. it("allows read-only tools in plan mode", () => {
  148. const service = new PermissionService();
  149. const decision = service.checkPermission(
  150. {
  151. input: {},
  152. mode: "plan",
  153. riskLevel: "low",
  154. toolName: "Read",
  155. },
  156. readCapability,
  157. );
  158. expect(decision).toMatchObject({
  159. allowed: true,
  160. decision: "allow",
  161. ruleId: "mode.plan.readOnly",
  162. });
  163. });
  164. it("does not let allowedTools bypass plan mode write protection", () => {
  165. const service = new PermissionService({
  166. allowedTools: new Set(["Write"]),
  167. autoApproveHighRisk: false,
  168. disallowedTools: new Set(),
  169. allowMediumRiskInAutoMode: false,
  170. });
  171. const decision = service.checkPermission(
  172. {
  173. input: {},
  174. mode: "plan",
  175. riskLevel: "medium",
  176. toolName: "Write",
  177. },
  178. writeCapability,
  179. );
  180. expect(decision).toMatchObject({
  181. allowed: false,
  182. decision: "deny",
  183. ruleId: "mode.plan.nonReadOnly",
  184. });
  185. });
  186. it("denies non-read-only session mutations in plan mode", () => {
  187. const service = new PermissionService();
  188. const decision = service.checkPermission(
  189. {
  190. input: { todos: [] },
  191. mode: "plan",
  192. riskLevel: "low",
  193. toolName: "TodoWrite",
  194. },
  195. sessionMutationCapability,
  196. );
  197. expect(decision).toMatchObject({
  198. allowed: false,
  199. decision: "deny",
  200. ruleId: "mode.plan.nonReadOnly",
  201. });
  202. });
  203. it("allows an explicit non-read-only session capability in plan mode", () => {
  204. const service = new PermissionService();
  205. const decision = service.checkPermission(
  206. {
  207. input: { summary: "progress", message: "still working" },
  208. mode: "plan",
  209. riskLevel: "low",
  210. toolName: "RespondToCoordinator",
  211. },
  212. planModeSessionControlCapability,
  213. );
  214. expect(decision).toMatchObject({
  215. allowed: true,
  216. decision: "allow",
  217. ruleId: "mode.plan.explicitSessionCapability",
  218. sideEffectScope: "session",
  219. });
  220. });
  221. it("does not let explicit session capability bypass plan-mode deny or ask rules", () => {
  222. const context = {
  223. input: { summary: "progress", message: "still working" },
  224. mode: "plan" as const,
  225. riskLevel: "low" as const,
  226. toolName: "RespondToCoordinator",
  227. };
  228. const disallowed = new PermissionService({
  229. allowedTools: new Set(),
  230. autoApproveHighRisk: false,
  231. disallowedTools: new Set(["RespondToCoordinator"]),
  232. allowMediumRiskInAutoMode: false,
  233. });
  234. expect(disallowed.checkPermission(context, planModeSessionControlCapability)).toMatchObject({
  235. decision: "deny",
  236. ruleId: "rule.disallowedTools",
  237. });
  238. const service = new PermissionService();
  239. expect(
  240. service.checkPermission(context, planModeSessionControlCapability, {
  241. version: 1,
  242. deny: [{ toolName: "RespondToCoordinator" }],
  243. }),
  244. ).toMatchObject({ decision: "deny", ruleId: "rule.project.deny" });
  245. expect(
  246. service.checkPermission(context, planModeSessionControlCapability, {
  247. version: 1,
  248. ask: [{ toolName: "RespondToCoordinator" }],
  249. }),
  250. ).toMatchObject({ decision: "ask", ruleId: "rule.project.ask" });
  251. });
  252. it("rejects invalid explicit plan-mode capabilities", () => {
  253. const service = new PermissionService();
  254. const context = {
  255. input: {},
  256. mode: "plan" as const,
  257. riskLevel: "low" as const,
  258. toolName: "RespondToCoordinator",
  259. };
  260. for (const capability of [
  261. { ...planModeSessionControlCapability, destructive: true },
  262. {
  263. ...planModeSessionControlCapability,
  264. needsApproval: true,
  265. permission: {
  266. ...planModeSessionControlCapability.permission!,
  267. needsApproval: true,
  268. },
  269. },
  270. {
  271. ...planModeSessionControlCapability,
  272. permission: {
  273. ...planModeSessionControlCapability.permission!,
  274. sideEffectScope: "network" as const,
  275. },
  276. sideEffectScope: "network" as const,
  277. },
  278. ]) {
  279. expect(service.checkPermission(context, capability)).toMatchObject({
  280. decision: "deny",
  281. ruleId: "mode.plan.nonReadOnly",
  282. });
  283. }
  284. });
  285. it("allows non-destructive MCP tools in plan mode without requiring readOnly metadata", () => {
  286. const service = new PermissionService();
  287. const decision = service.checkPermission(
  288. {
  289. input: { query: "current simulator state" },
  290. mode: "plan",
  291. riskLevel: "medium",
  292. toolName: "mcp__ios_simulator__status",
  293. },
  294. mcpCapability,
  295. );
  296. expect(decision).toMatchObject({
  297. allowed: true,
  298. decision: "allow",
  299. ruleId: "mode.plan.mcp",
  300. sideEffectScope: "network",
  301. });
  302. });
  303. it("denies destructive MCP tools in plan mode", () => {
  304. const service = new PermissionService();
  305. const decision = service.checkPermission(
  306. {
  307. input: { target: "device" },
  308. mode: "plan",
  309. riskLevel: "high",
  310. toolName: "mcp__device__reset",
  311. },
  312. destructiveMcpCapability,
  313. );
  314. expect(decision).toMatchObject({
  315. allowed: false,
  316. decision: "deny",
  317. ruleId: "mode.plan.nonReadOnly",
  318. });
  319. });
  320. it("does not treat an mcp-prefixed tool name as MCP without the mcp permission declaration", () => {
  321. const service = new PermissionService();
  322. const decision = service.checkPermission(
  323. {
  324. input: {},
  325. mode: "plan",
  326. riskLevel: "medium",
  327. toolName: "mcp__fake__write",
  328. },
  329. fakeMcpNameCapability,
  330. );
  331. expect(decision).toMatchObject({
  332. allowed: false,
  333. decision: "deny",
  334. ruleId: "mode.plan.nonReadOnly",
  335. });
  336. });
  337. it("lets project deny rules override the plan-mode MCP exception", () => {
  338. const service = new PermissionService();
  339. const decision = service.checkPermission(
  340. {
  341. input: { query: "secret" },
  342. mode: "plan",
  343. riskLevel: "medium",
  344. toolName: "mcp__external__search",
  345. },
  346. mcpCapability,
  347. {
  348. version: 1,
  349. deny: [{ toolName: "mcp__external__search" }],
  350. },
  351. );
  352. expect(decision).toMatchObject({
  353. allowed: false,
  354. decision: "deny",
  355. ruleId: "rule.project.deny",
  356. });
  357. });
  358. it("lets project ask rules override the plan-mode MCP exception", () => {
  359. const service = new PermissionService();
  360. const decision = service.checkPermission(
  361. {
  362. input: { query: "maybe external" },
  363. mode: "plan",
  364. riskLevel: "medium",
  365. toolName: "mcp__external__search",
  366. },
  367. mcpCapability,
  368. {
  369. version: 1,
  370. ask: [{ toolName: "mcp__external__search" }],
  371. },
  372. );
  373. expect(decision).toMatchObject({
  374. allowed: false,
  375. decision: "ask",
  376. ruleId: "rule.project.ask",
  377. });
  378. });
  379. it("denies ExitPlanMode outside plan mode before asking for approval", () => {
  380. const service = new PermissionService();
  381. const decision = service.checkPermission(
  382. {
  383. input: { plan: "Implement the approved steps." },
  384. mode: "build",
  385. riskLevel: "low",
  386. toolName: "ExitPlanMode",
  387. },
  388. planModeTransitionCapability,
  389. );
  390. expect(decision).toMatchObject({
  391. allowed: false,
  392. decision: "deny",
  393. ruleId: "mode.plan.exitOnly",
  394. });
  395. });
  396. it("asks for ExitPlanMode approval while plan mode is active", () => {
  397. const service = new PermissionService();
  398. const decision = service.checkPermission(
  399. {
  400. input: { plan: "Implement the approved steps." },
  401. mode: "plan",
  402. riskLevel: "low",
  403. toolName: "ExitPlanMode",
  404. },
  405. planModeTransitionCapability,
  406. );
  407. expect(decision).toMatchObject({
  408. allowed: false,
  409. decision: "ask",
  410. ruleId: "tool.userInteraction",
  411. });
  412. });
  413. it("asks for ExitPlanMode approval even when plan mode was entered from yolo", () => {
  414. const service = new PermissionService();
  415. const decision = service.checkPermission(
  416. {
  417. input: { plan: "Implement the approved steps." },
  418. mode: "plan",
  419. prePlanMode: "yolo",
  420. riskLevel: "low",
  421. toolName: "ExitPlanMode",
  422. },
  423. planModeTransitionCapability,
  424. );
  425. expect(decision).toMatchObject({
  426. allowed: false,
  427. decision: "ask",
  428. ruleId: "tool.userInteraction",
  429. });
  430. });
  431. it("allows EnterPlanMode without approval", () => {
  432. const service = new PermissionService();
  433. const decision = service.checkPermission(
  434. {
  435. input: {},
  436. mode: "build",
  437. riskLevel: "low",
  438. toolName: "EnterPlanMode",
  439. },
  440. {
  441. ...planModeTransitionCapability,
  442. permission: {
  443. ...planModeTransitionCapability.permission!,
  444. permission: "plan.enter",
  445. reason: "Enter plan mode from yolo",
  446. },
  447. },
  448. );
  449. expect(decision).toMatchObject({
  450. allowed: true,
  451. decision: "allow",
  452. ruleId: "tool.plan.enter",
  453. });
  454. });
  455. it("asks for side-effecting tools in build mode", () => {
  456. const service = new PermissionService();
  457. const decision = service.checkPermission(
  458. {
  459. input: {},
  460. mode: "build",
  461. riskLevel: "medium",
  462. toolName: "Write",
  463. },
  464. writeCapability,
  465. );
  466. expect(decision).toMatchObject({
  467. allowed: false,
  468. decision: "ask",
  469. escalated: true,
  470. ruleId: "mode.build.sideEffect",
  471. });
  472. });
  473. it("allows file edit tools in edit mode", () => {
  474. const service = new PermissionService();
  475. const decision = service.checkPermission(
  476. {
  477. input: { file_path: "demo.txt" },
  478. mode: "edit",
  479. riskLevel: "medium",
  480. toolName: "Edit",
  481. },
  482. editCapability,
  483. );
  484. expect(decision).toMatchObject({
  485. allowed: true,
  486. decision: "allow",
  487. escalated: false,
  488. ruleId: "mode.edit.fileEdit",
  489. });
  490. });
  491. it("keeps non-edit side effects on build-mode approval behavior in edit mode", () => {
  492. const service = new PermissionService();
  493. const decision = service.checkPermission(
  494. {
  495. input: { command: "npm run build" },
  496. mode: "edit",
  497. riskLevel: "high",
  498. toolName: "Bash",
  499. },
  500. {
  501. destructive: true,
  502. needsApproval: true,
  503. readOnly: false,
  504. riskLevel: "high",
  505. sideEffectScope: "workspace",
  506. },
  507. );
  508. expect(decision).toMatchObject({
  509. allowed: false,
  510. decision: "ask",
  511. escalated: true,
  512. ruleId: "mode.build.highRisk",
  513. });
  514. });
  515. it("lets project ask rules override edit mode auto-approval", () => {
  516. const service = new PermissionService();
  517. const decision = service.checkPermission(
  518. {
  519. input: { file_path: "demo.txt" },
  520. mode: "edit",
  521. riskLevel: "medium",
  522. toolName: "Edit",
  523. },
  524. editCapability,
  525. {
  526. version: 1,
  527. ask: [{ toolName: "Edit" }],
  528. },
  529. );
  530. expect(decision).toMatchObject({
  531. allowed: false,
  532. decision: "ask",
  533. ruleId: "rule.project.ask",
  534. });
  535. });
  536. it("lets project allow rules stay explicit in edit mode", () => {
  537. const service = new PermissionService();
  538. const decision = service.checkPermission(
  539. {
  540. input: { file_path: "demo.txt" },
  541. mode: "edit",
  542. riskLevel: "medium",
  543. toolName: "Write",
  544. },
  545. writeCapability,
  546. {
  547. version: 1,
  548. allow: [{ toolName: "Write" }],
  549. },
  550. );
  551. expect(decision).toMatchObject({
  552. allowed: true,
  553. decision: "allow",
  554. ruleId: "rule.project.allow",
  555. });
  556. });
  557. it("allows low-risk session-local state updates in build mode without approval", () => {
  558. const service = new PermissionService();
  559. const decision = service.checkPermission(
  560. {
  561. input: { todos: [] },
  562. mode: "build",
  563. riskLevel: "low",
  564. toolName: "TodoWrite",
  565. },
  566. sessionMutationCapability,
  567. );
  568. expect(decision).toMatchObject({
  569. allowed: true,
  570. decision: "allow",
  571. ruleId: "mode.build.sessionState",
  572. });
  573. });
  574. it("allows explicitly allowed tools in build mode", () => {
  575. const service = new PermissionService({
  576. allowedTools: new Set(["Write"]),
  577. autoApproveHighRisk: false,
  578. disallowedTools: new Set(),
  579. allowMediumRiskInAutoMode: false,
  580. });
  581. const decision = service.checkPermission(
  582. {
  583. input: {},
  584. mode: "build",
  585. riskLevel: "medium",
  586. toolName: "Write",
  587. },
  588. writeCapability,
  589. );
  590. expect(decision).toMatchObject({
  591. allowed: true,
  592. decision: "allow",
  593. ruleId: "rule.allowedTools",
  594. });
  595. });
  596. it("allows side-effecting tools that match project permission rules", () => {
  597. const service = new PermissionService();
  598. const decision = service.checkPermission(
  599. {
  600. input: { file_path: "demo.txt" },
  601. mode: "build",
  602. riskLevel: "medium",
  603. toolName: "Write",
  604. },
  605. writeCapability,
  606. {
  607. version: 1,
  608. allow: [{ toolName: "Write" }],
  609. },
  610. );
  611. expect(decision).toMatchObject({
  612. allowed: true,
  613. decision: "allow",
  614. ruleId: "rule.project.allow",
  615. });
  616. });
  617. it("shares an official CUA project rule only with authority-verified CUA tools", () => {
  618. const service = new PermissionService();
  619. const rules = {
  620. allow: [{ toolName: OFFICIAL_CUA_PERMISSION_RULE_TOOL_NAME }],
  621. version: 1 as const,
  622. };
  623. const context = {
  624. input: { app_ref: { bundle_id: "com.apple.TextEdit" } },
  625. mode: "build" as const,
  626. riskLevel: "medium" as const,
  627. toolName: "mcp__computer-use__left_click",
  628. };
  629. const trusted = service.checkPermission(
  630. context,
  631. {
  632. ...mcpCapability,
  633. permissionCapabilityGroup: "official_cua",
  634. } as PermissionToolCapability,
  635. rules as never,
  636. );
  637. const sameNameButUntrusted = service.checkPermission(context, mcpCapability, rules as never);
  638. expect(trusted).toMatchObject({
  639. allowed: true,
  640. decision: "allow",
  641. ruleId: "rule.project.allow",
  642. });
  643. expect(sameNameButUntrusted).toMatchObject({
  644. allowed: false,
  645. decision: "ask",
  646. ruleId: "mode.build.sideEffect",
  647. });
  648. });
  649. it("does not fall back to exact tool-name matching when a capability group loses authority", () => {
  650. const service = new PermissionService();
  651. const context = {
  652. input: {},
  653. mode: "build" as const,
  654. riskLevel: "medium" as const,
  655. toolName: "mcp__computer-use__get_app_state",
  656. };
  657. const rules = {
  658. allow: [{ toolName: OFFICIAL_CUA_PERMISSION_RULE_TOOL_NAME }],
  659. version: 1 as const,
  660. };
  661. expect(service.checkPermission(context, mcpCapability, rules as never)).toMatchObject({
  662. allowed: false,
  663. decision: "ask",
  664. ruleId: "mode.build.sideEffect",
  665. });
  666. });
  667. it("does not let project allow rules bypass plan mode write protection", () => {
  668. const service = new PermissionService();
  669. const decision = service.checkPermission(
  670. {
  671. input: { file_path: "demo.txt" },
  672. mode: "plan",
  673. riskLevel: "medium",
  674. toolName: "Write",
  675. },
  676. writeCapability,
  677. {
  678. version: 1,
  679. allow: [{ toolName: "Write" }],
  680. },
  681. );
  682. expect(decision).toMatchObject({
  683. allowed: false,
  684. decision: "deny",
  685. ruleId: "mode.plan.nonReadOnly",
  686. });
  687. });
  688. it("matches project command prefix rules", () => {
  689. const service = new PermissionService();
  690. const decision = service.checkPermission(
  691. {
  692. input: { command: "npm run test -- --watch=false" },
  693. mode: "build",
  694. riskLevel: "high",
  695. toolName: "Bash",
  696. },
  697. {
  698. destructive: true,
  699. needsApproval: true,
  700. readOnly: false,
  701. riskLevel: "high",
  702. sideEffectScope: "workspace",
  703. },
  704. {
  705. version: 1,
  706. allow: [{ toolName: "Bash", ruleContent: "npm run:*" }],
  707. },
  708. );
  709. expect(decision).toMatchObject({
  710. allowed: true,
  711. decision: "allow",
  712. ruleId: "rule.project.allow",
  713. });
  714. });
  715. it("matches WebFetch project rules by normalized domain", () => {
  716. const service = new PermissionService();
  717. const decision = service.checkPermission(
  718. {
  719. input: { url: " https://EXAMPLE.com/docs?token=redacted " },
  720. mode: "build",
  721. riskLevel: "medium",
  722. toolName: "WebFetch",
  723. },
  724. webFetchCapability,
  725. {
  726. version: 1,
  727. allow: [{ toolName: "WebFetch", ruleContent: "domain:example.com" }],
  728. },
  729. );
  730. expect(decision).toMatchObject({
  731. allowed: true,
  732. decision: "allow",
  733. ruleId: "rule.project.allow",
  734. });
  735. });
  736. it("does not match WebFetch project rules against full URLs", () => {
  737. const service = new PermissionService();
  738. const decision = service.checkPermission(
  739. {
  740. input: { url: "http://example.com/docs" },
  741. mode: "build",
  742. riskLevel: "medium",
  743. toolName: "WebFetch",
  744. },
  745. webFetchCapability,
  746. {
  747. version: 1,
  748. deny: [{ toolName: "WebFetch", ruleContent: "https://example.com/docs" }],
  749. },
  750. );
  751. expect(decision).toMatchObject({
  752. allowed: false,
  753. decision: "ask",
  754. ruleId: "mode.build.sideEffect",
  755. });
  756. });
  757. it("allows preapproved WebFetch URLs after project deny and ask rules are checked", () => {
  758. const service = new PermissionService();
  759. const decision = service.checkPermission(
  760. {
  761. input: { url: "https://docs.python.org/3/library/pathlib.html" },
  762. mode: "build",
  763. riskLevel: "medium",
  764. toolName: "WebFetch",
  765. },
  766. webFetchCapability,
  767. {
  768. version: 1,
  769. },
  770. );
  771. expect(decision).toMatchObject({
  772. allowed: true,
  773. decision: "allow",
  774. ruleId: "tool.webfetch.preapproved",
  775. });
  776. });
  777. it("does not auto-allow removed vendor WebFetch URLs", () => {
  778. const service = new PermissionService();
  779. for (const url of [
  780. "https://platform.claude.com/docs",
  781. "https://code.claude.com/docs",
  782. "https://github.com/anthropics/example",
  783. ]) {
  784. const decision = service.checkPermission(
  785. {
  786. input: { url },
  787. mode: "build",
  788. riskLevel: "medium",
  789. toolName: "WebFetch",
  790. },
  791. webFetchCapability,
  792. {
  793. version: 1,
  794. },
  795. );
  796. expect(decision, url).toMatchObject({
  797. allowed: false,
  798. decision: "ask",
  799. ruleId: "mode.build.sideEffect",
  800. });
  801. }
  802. });
  803. it("lets WebFetch project ask rules override preapproved URL auto-allow", () => {
  804. const service = new PermissionService();
  805. const decision = service.checkPermission(
  806. {
  807. input: { url: "https://docs.python.org/3/library/pathlib.html" },
  808. mode: "build",
  809. riskLevel: "medium",
  810. toolName: "WebFetch",
  811. },
  812. webFetchCapability,
  813. {
  814. version: 1,
  815. ask: [{ toolName: "WebFetch", ruleContent: "domain:docs.python.org" }],
  816. },
  817. );
  818. expect(decision).toMatchObject({
  819. allowed: false,
  820. decision: "ask",
  821. ruleId: "rule.project.ask",
  822. });
  823. });
  824. it("lets WebFetch project deny domain rules override allow rules", () => {
  825. const service = new PermissionService();
  826. const decision = service.checkPermission(
  827. {
  828. input: { url: "https://example.com/docs" },
  829. mode: "build",
  830. riskLevel: "medium",
  831. toolName: "WebFetch",
  832. },
  833. webFetchCapability,
  834. {
  835. version: 1,
  836. allow: [{ toolName: "WebFetch", ruleContent: "domain:*.example.com" }],
  837. deny: [{ toolName: "WebFetch", ruleContent: "domain:example.com" }],
  838. },
  839. );
  840. expect(decision).toMatchObject({
  841. allowed: false,
  842. decision: "deny",
  843. ruleId: "rule.project.deny",
  844. });
  845. });
  846. it("lets yolo bypass disallowed tool rules", () => {
  847. const service = new PermissionService({
  848. allowedTools: new Set(),
  849. autoApproveHighRisk: false,
  850. disallowedTools: new Set(["Bash"]),
  851. allowMediumRiskInAutoMode: false,
  852. });
  853. const decision = service.checkPermission(
  854. {
  855. input: { command: "rm -rf build" },
  856. mode: "yolo",
  857. riskLevel: "high",
  858. toolName: "Bash",
  859. },
  860. {
  861. destructive: true,
  862. needsApproval: true,
  863. readOnly: false,
  864. riskLevel: "high",
  865. sideEffectScope: "system",
  866. },
  867. );
  868. expect(decision).toMatchObject({
  869. allowed: true,
  870. decision: "allow",
  871. ruleId: "mode.yolo",
  872. });
  873. });
  874. it("does not let yolo bypass user interaction tools", () => {
  875. const service = new PermissionService();
  876. const decision = service.checkPermission(
  877. {
  878. input: {
  879. questions: [
  880. {
  881. header: "Choice",
  882. question: "Which option should we use?",
  883. options: [
  884. { label: "A", description: "Use A" },
  885. { label: "B", description: "Use B" },
  886. ],
  887. },
  888. ],
  889. },
  890. mode: "yolo",
  891. riskLevel: "low",
  892. toolName: "AskUserQuestion",
  893. },
  894. {
  895. destructive: false,
  896. needsApproval: true,
  897. readOnly: true,
  898. requiresUserInteraction: true,
  899. riskLevel: "low",
  900. sideEffectScope: "userInteraction",
  901. },
  902. );
  903. expect(decision).toMatchObject({
  904. allowed: false,
  905. decision: "ask",
  906. ruleId: "tool.userInteraction",
  907. });
  908. });
  909. it("keeps auto mode reserved and non-executable", () => {
  910. const service = new PermissionService();
  911. const decision = service.checkPermission(
  912. {
  913. input: {},
  914. mode: "auto",
  915. riskLevel: "low",
  916. toolName: "Read",
  917. },
  918. readCapability,
  919. );
  920. expect(decision).toMatchObject({
  921. allowed: false,
  922. decision: "deny",
  923. ruleId: "mode.auto.unimplemented",
  924. });
  925. });
  926. });
  927. // ── alwaysAsk:模式无关的确认(docs/dynamic-workflow/launch.md「Why the window always appears」)──
  928. const alwaysAskCapability: PermissionToolCapability = {
  929. destructive: false,
  930. needsApproval: true,
  931. permission: {
  932. alwaysAsk: true,
  933. denyPriority: "beforeAsk",
  934. needsApproval: true,
  935. permission: "createWorkflow",
  936. reason: "createWorkflow.runConfirmation",
  937. riskLevel: "low",
  938. sideEffectScope: "none",
  939. },
  940. readOnly: true,
  941. riskLevel: "low",
  942. sideEffectScope: "none",
  943. };
  944. describe("PermissionService alwaysAsk", () => {
  945. function check(mode: PermissionContext["mode"], service = new PermissionService()) {
  946. return service.checkPermission(
  947. { input: { script: "return 1;" }, mode, riskLevel: "low", toolName: "CreateWorkflow" },
  948. alwaysAskCapability,
  949. );
  950. }
  951. // 宽松度设置一律压不过 alwaysAsk:yolo 直通、plan 的 readOnly 直通、edit/build 都要问。
  952. for (const mode of ["yolo", "plan", "build", "edit", "default"] as const) {
  953. it(`asks in ${mode} mode`, () => {
  954. expect(check(mode)).toMatchObject({
  955. allowed: false,
  956. alwaysAsk: true,
  957. decision: "ask",
  958. ruleId: "tool.alwaysAsk",
  959. });
  960. });
  961. }
  962. // 反向边界:alwaysAsk 只压过"放行"分支,压不过"阻断"分支。
  963. it("still yields to an explicitly disallowed tool", () => {
  964. const service = new PermissionService({
  965. allowedTools: new Set(),
  966. allowMediumRiskInAutoMode: false,
  967. autoApproveHighRisk: false,
  968. disallowedTools: new Set(["CreateWorkflow"]),
  969. });
  970. expect(check("yolo", service)).toMatchObject({
  971. allowed: false,
  972. decision: "deny",
  973. ruleId: "rule.disallowedTools",
  974. });
  975. });
  976. it("still yields to a project deny rule", () => {
  977. const service = new PermissionService();
  978. const decision = service.checkPermission(
  979. { input: { script: "return 1;" }, mode: "yolo", riskLevel: "low", toolName: "CreateWorkflow" },
  980. alwaysAskCapability,
  981. { deny: [{ toolName: "CreateWorkflow" }], version: 1 },
  982. );
  983. expect(decision).toMatchObject({
  984. allowed: false,
  985. decision: "deny",
  986. ruleId: "rule.project.deny",
  987. });
  988. });
  989. it("still yields to auto mode being unimplemented", () => {
  990. expect(check("auto")).toMatchObject({
  991. allowed: false,
  992. decision: "deny",
  993. ruleId: "mode.auto.unimplemented",
  994. });
  995. });
  996. it("leaves tools without the flag on their existing mode semantics", () => {
  997. expect(
  998. new PermissionService().checkPermission(
  999. { input: {}, mode: "yolo", riskLevel: "low", toolName: "Read" },
  1000. readCapability,
  1001. ),
  1002. ).toMatchObject({ allowed: true, decision: "allow", ruleId: "mode.yolo" });
  1003. });
  1004. });
  1005. // ── 会话免确认(docs/dynamic-workflow/launch.md「Always allow in this session」)──
  1006. // 「Always allow in this session」把 alwaysAsk gate 在一个 PermissionService 实例(= 一个会话)内
  1007. // 关掉;阻断分支仍然优先,普通工具的模式语义不认会话规则。
  1008. describe("PermissionService session grant", () => {
  1009. const sessionGrant = [
  1010. {
  1011. behavior: "allow" as const,
  1012. rules: [{ toolName: "CreateWorkflow" }],
  1013. type: "addRules" as const,
  1014. },
  1015. ];
  1016. function check(mode: PermissionContext["mode"], service: PermissionService, toolName = "CreateWorkflow") {
  1017. return service.checkPermission(
  1018. { input: { script: "return 2;" }, mode, riskLevel: "low", toolName },
  1019. alwaysAskCapability,
  1020. );
  1021. }
  1022. for (const mode of ["yolo", "plan", "build", "edit", "default"] as const) {
  1023. it(`lifts the alwaysAsk gate in ${mode} mode once the session grant is in place`, () => {
  1024. const service = new PermissionService();
  1025. expect(check(mode, service).decision).toBe("ask");
  1026. service.grantSessionPermission(sessionGrant);
  1027. expect(check(mode, service)).toMatchObject({
  1028. allowed: true,
  1029. decision: "allow",
  1030. ruleId: "rule.session.allow",
  1031. });
  1032. });
  1033. }
  1034. it("is scoped to the granted tool name", () => {
  1035. const service = new PermissionService();
  1036. service.grantSessionPermission(sessionGrant);
  1037. expect(check("build", service, "SaveWorkflow")).toMatchObject({
  1038. decision: "ask",
  1039. ruleId: "tool.alwaysAsk",
  1040. });
  1041. });
  1042. it("does not survive into a fresh service instance (session boundary)", () => {
  1043. const granted = new PermissionService();
  1044. granted.grantSessionPermission(sessionGrant);
  1045. expect(check("build", granted).decision).toBe("allow");
  1046. expect(check("build", new PermissionService()).decision).toBe("ask");
  1047. });
  1048. it("still yields to an explicitly disallowed tool", () => {
  1049. const service = new PermissionService({
  1050. allowedTools: new Set(),
  1051. allowMediumRiskInAutoMode: false,
  1052. autoApproveHighRisk: false,
  1053. disallowedTools: new Set(["CreateWorkflow"]),
  1054. });
  1055. service.grantSessionPermission(sessionGrant);
  1056. expect(check("yolo", service)).toMatchObject({
  1057. decision: "deny",
  1058. ruleId: "rule.disallowedTools",
  1059. });
  1060. });
  1061. it("still yields to a project deny rule", () => {
  1062. const service = new PermissionService();
  1063. service.grantSessionPermission(sessionGrant);
  1064. expect(
  1065. service.checkPermission(
  1066. { input: { script: "return 2;" }, mode: "yolo", riskLevel: "low", toolName: "CreateWorkflow" },
  1067. alwaysAskCapability,
  1068. { deny: [{ toolName: "CreateWorkflow" }], version: 1 },
  1069. ),
  1070. ).toMatchObject({ decision: "deny", ruleId: "rule.project.deny" });
  1071. });
  1072. // 会话规则只服务 alwaysAsk gate:plan 模式下的只读约束不能被一条会话规则绕开。
  1073. it("leaves tools without the alwaysAsk flag on their mode semantics", () => {
  1074. const service = new PermissionService();
  1075. service.grantSessionPermission([
  1076. { behavior: "allow", rules: [{ toolName: "Write" }], type: "addRules" },
  1077. ]);
  1078. expect(
  1079. service.checkPermission(
  1080. { input: { file_path: "/w/a.ts" }, mode: "plan", riskLevel: "medium", toolName: "Write" },
  1081. writeCapability,
  1082. ).decision,
  1083. ).not.toBe("allow");
  1084. });
  1085. });
  1086. // ── 本会话 run 的修订免确认(docs/dynamic-workflow/launch.md「Amending this session's runs」)──
  1087. // 钥匙是 resolveInput 回填的 `predecessor`(journal 的 parent_session_id / stopReason),不是内存表:
  1088. // 本会话发起且非用户停下 → 放行;别的会话的 run、用户停过的 run、事实块缺席、其他工具都照常 ask;
  1089. // 阻断分支仍优先。没有可播种、可撤销的状态,所以新实例上同样成立。
  1090. describe("PermissionService workflow owner rule", () => {
  1091. const owned = {
  1092. run_id: "dwfrun-a",
  1093. script: "return 3;",
  1094. predecessor: { status: "running", owned_by_this_session: true },
  1095. };
  1096. function checkAmend(
  1097. service: PermissionService,
  1098. input: Record<string, unknown>,
  1099. mode: PermissionContext["mode"] = "build",
  1100. toolName = "AmendWorkflow",
  1101. ) {
  1102. return service.checkPermission(
  1103. { input, mode, riskLevel: "low", toolName },
  1104. alwaysAskCapability,
  1105. );
  1106. }
  1107. for (const mode of ["yolo", "plan", "build", "edit", "default"] as const) {
  1108. it(`lifts the gate in ${mode} mode for an amend of a run this session started`, () => {
  1109. expect(checkAmend(new PermissionService(), owned, mode)).toMatchObject({
  1110. allowed: true,
  1111. decision: "allow",
  1112. ruleId: "rule.session.workflowOwner",
  1113. });
  1114. });
  1115. }
  1116. it("keeps asking for another session's run, a user-stopped run, and a missing fact block", () => {
  1117. const service = new PermissionService();
  1118. expect(
  1119. checkAmend(service, {
  1120. ...owned,
  1121. predecessor: { status: "completed", owned_by_this_session: false },
  1122. }),
  1123. ).toMatchObject({ decision: "ask", ruleId: "tool.alwaysAsk" });
  1124. expect(
  1125. checkAmend(service, {
  1126. ...owned,
  1127. predecessor: { status: "stopped", stop_reason: "user", owned_by_this_session: true },
  1128. }),
  1129. ).toMatchObject({ decision: "ask", ruleId: "tool.alwaysAsk" });
  1130. expect(checkAmend(service, { run_id: "dwfrun-a", script: "return 3;" })).toMatchObject({
  1131. decision: "ask",
  1132. ruleId: "tool.alwaysAsk",
  1133. });
  1134. });
  1135. it("allows an own run the model stopped, or that settled on its own", () => {
  1136. const service = new PermissionService();
  1137. for (const stop_reason of ["model", "provider", "interrupted", "superseded"] as const) {
  1138. expect(
  1139. checkAmend(service, {
  1140. ...owned,
  1141. predecessor: { status: "stopped", stop_reason, owned_by_this_session: true },
  1142. }).decision,
  1143. ).toBe("allow");
  1144. }
  1145. expect(
  1146. checkAmend(service, {
  1147. ...owned,
  1148. predecessor: { status: "completed", owned_by_this_session: true },
  1149. }).decision,
  1150. ).toBe("allow");
  1151. });
  1152. it("is scoped to AmendWorkflow", () => {
  1153. const service = new PermissionService();
  1154. expect(checkAmend(service, owned, "build", "CreateWorkflow")).toMatchObject({
  1155. decision: "ask",
  1156. ruleId: "tool.alwaysAsk",
  1157. });
  1158. expect(checkAmend(service, owned, "build", "SaveWorkflow")).toMatchObject({
  1159. decision: "ask",
  1160. ruleId: "tool.alwaysAsk",
  1161. });
  1162. });
  1163. it("still yields to an explicitly disallowed tool and to a project deny rule", () => {
  1164. const disallowed = new PermissionService({
  1165. allowedTools: new Set(),
  1166. allowMediumRiskInAutoMode: false,
  1167. autoApproveHighRisk: false,
  1168. disallowedTools: new Set(["AmendWorkflow"]),
  1169. });
  1170. expect(checkAmend(disallowed, owned, "yolo")).toMatchObject({
  1171. decision: "deny",
  1172. ruleId: "rule.disallowedTools",
  1173. });
  1174. const denied = new PermissionService();
  1175. expect(
  1176. denied.checkPermission(
  1177. { input: owned, mode: "yolo", riskLevel: "low", toolName: "AmendWorkflow" },
  1178. alwaysAskCapability,
  1179. { deny: [{ toolName: "AmendWorkflow" }], version: 1 },
  1180. ),
  1181. ).toMatchObject({ decision: "deny", ruleId: "rule.project.deny" });
  1182. });
  1183. });