permission-service.test.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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 let project allow rules bypass plan mode write protection", () => {
  650. const service = new PermissionService();
  651. const decision = service.checkPermission(
  652. {
  653. input: { file_path: "demo.txt" },
  654. mode: "plan",
  655. riskLevel: "medium",
  656. toolName: "Write",
  657. },
  658. writeCapability,
  659. {
  660. version: 1,
  661. allow: [{ toolName: "Write" }],
  662. },
  663. );
  664. expect(decision).toMatchObject({
  665. allowed: false,
  666. decision: "deny",
  667. ruleId: "mode.plan.nonReadOnly",
  668. });
  669. });
  670. it("matches project command prefix rules", () => {
  671. const service = new PermissionService();
  672. const decision = service.checkPermission(
  673. {
  674. input: { command: "npm run test -- --watch=false" },
  675. mode: "build",
  676. riskLevel: "high",
  677. toolName: "Bash",
  678. },
  679. {
  680. destructive: true,
  681. needsApproval: true,
  682. readOnly: false,
  683. riskLevel: "high",
  684. sideEffectScope: "workspace",
  685. },
  686. {
  687. version: 1,
  688. allow: [{ toolName: "Bash", ruleContent: "npm run:*" }],
  689. },
  690. );
  691. expect(decision).toMatchObject({
  692. allowed: true,
  693. decision: "allow",
  694. ruleId: "rule.project.allow",
  695. });
  696. });
  697. it("matches WebFetch project rules by normalized domain", () => {
  698. const service = new PermissionService();
  699. const decision = service.checkPermission(
  700. {
  701. input: { url: " https://EXAMPLE.com/docs?token=redacted " },
  702. mode: "build",
  703. riskLevel: "medium",
  704. toolName: "WebFetch",
  705. },
  706. webFetchCapability,
  707. {
  708. version: 1,
  709. allow: [{ toolName: "WebFetch", ruleContent: "domain:example.com" }],
  710. },
  711. );
  712. expect(decision).toMatchObject({
  713. allowed: true,
  714. decision: "allow",
  715. ruleId: "rule.project.allow",
  716. });
  717. });
  718. it("does not match WebFetch project rules against full URLs", () => {
  719. const service = new PermissionService();
  720. const decision = service.checkPermission(
  721. {
  722. input: { url: "http://example.com/docs" },
  723. mode: "build",
  724. riskLevel: "medium",
  725. toolName: "WebFetch",
  726. },
  727. webFetchCapability,
  728. {
  729. version: 1,
  730. deny: [{ toolName: "WebFetch", ruleContent: "https://example.com/docs" }],
  731. },
  732. );
  733. expect(decision).toMatchObject({
  734. allowed: false,
  735. decision: "ask",
  736. ruleId: "mode.build.sideEffect",
  737. });
  738. });
  739. it("allows preapproved WebFetch URLs after project deny and ask rules are checked", () => {
  740. const service = new PermissionService();
  741. const decision = service.checkPermission(
  742. {
  743. input: { url: "https://docs.python.org/3/library/pathlib.html" },
  744. mode: "build",
  745. riskLevel: "medium",
  746. toolName: "WebFetch",
  747. },
  748. webFetchCapability,
  749. {
  750. version: 1,
  751. },
  752. );
  753. expect(decision).toMatchObject({
  754. allowed: true,
  755. decision: "allow",
  756. ruleId: "tool.webfetch.preapproved",
  757. });
  758. });
  759. it("does not auto-allow removed vendor WebFetch URLs", () => {
  760. const service = new PermissionService();
  761. for (const url of [
  762. "https://platform.claude.com/docs",
  763. "https://code.claude.com/docs",
  764. "https://github.com/anthropics/example",
  765. ]) {
  766. const decision = service.checkPermission(
  767. {
  768. input: { url },
  769. mode: "build",
  770. riskLevel: "medium",
  771. toolName: "WebFetch",
  772. },
  773. webFetchCapability,
  774. {
  775. version: 1,
  776. },
  777. );
  778. expect(decision, url).toMatchObject({
  779. allowed: false,
  780. decision: "ask",
  781. ruleId: "mode.build.sideEffect",
  782. });
  783. }
  784. });
  785. it("lets WebFetch project ask rules override preapproved URL auto-allow", () => {
  786. const service = new PermissionService();
  787. const decision = service.checkPermission(
  788. {
  789. input: { url: "https://docs.python.org/3/library/pathlib.html" },
  790. mode: "build",
  791. riskLevel: "medium",
  792. toolName: "WebFetch",
  793. },
  794. webFetchCapability,
  795. {
  796. version: 1,
  797. ask: [{ toolName: "WebFetch", ruleContent: "domain:docs.python.org" }],
  798. },
  799. );
  800. expect(decision).toMatchObject({
  801. allowed: false,
  802. decision: "ask",
  803. ruleId: "rule.project.ask",
  804. });
  805. });
  806. it("lets WebFetch project deny domain rules override allow rules", () => {
  807. const service = new PermissionService();
  808. const decision = service.checkPermission(
  809. {
  810. input: { url: "https://example.com/docs" },
  811. mode: "build",
  812. riskLevel: "medium",
  813. toolName: "WebFetch",
  814. },
  815. webFetchCapability,
  816. {
  817. version: 1,
  818. allow: [{ toolName: "WebFetch", ruleContent: "domain:*.example.com" }],
  819. deny: [{ toolName: "WebFetch", ruleContent: "domain:example.com" }],
  820. },
  821. );
  822. expect(decision).toMatchObject({
  823. allowed: false,
  824. decision: "deny",
  825. ruleId: "rule.project.deny",
  826. });
  827. });
  828. it("lets yolo bypass disallowed tool rules", () => {
  829. const service = new PermissionService({
  830. allowedTools: new Set(),
  831. autoApproveHighRisk: false,
  832. disallowedTools: new Set(["Bash"]),
  833. allowMediumRiskInAutoMode: false,
  834. });
  835. const decision = service.checkPermission(
  836. {
  837. input: { command: "rm -rf build" },
  838. mode: "yolo",
  839. riskLevel: "high",
  840. toolName: "Bash",
  841. },
  842. {
  843. destructive: true,
  844. needsApproval: true,
  845. readOnly: false,
  846. riskLevel: "high",
  847. sideEffectScope: "system",
  848. },
  849. );
  850. expect(decision).toMatchObject({
  851. allowed: true,
  852. decision: "allow",
  853. ruleId: "mode.yolo",
  854. });
  855. });
  856. it("does not let yolo bypass user interaction tools", () => {
  857. const service = new PermissionService();
  858. const decision = service.checkPermission(
  859. {
  860. input: {
  861. questions: [
  862. {
  863. header: "Choice",
  864. question: "Which option should we use?",
  865. options: [
  866. { label: "A", description: "Use A" },
  867. { label: "B", description: "Use B" },
  868. ],
  869. },
  870. ],
  871. },
  872. mode: "yolo",
  873. riskLevel: "low",
  874. toolName: "AskUserQuestion",
  875. },
  876. {
  877. destructive: false,
  878. needsApproval: true,
  879. readOnly: true,
  880. requiresUserInteraction: true,
  881. riskLevel: "low",
  882. sideEffectScope: "userInteraction",
  883. },
  884. );
  885. expect(decision).toMatchObject({
  886. allowed: false,
  887. decision: "ask",
  888. ruleId: "tool.userInteraction",
  889. });
  890. });
  891. it("keeps auto mode reserved and non-executable", () => {
  892. const service = new PermissionService();
  893. const decision = service.checkPermission(
  894. {
  895. input: {},
  896. mode: "auto",
  897. riskLevel: "low",
  898. toolName: "Read",
  899. },
  900. readCapability,
  901. );
  902. expect(decision).toMatchObject({
  903. allowed: false,
  904. decision: "deny",
  905. ruleId: "mode.auto.unimplemented",
  906. });
  907. });
  908. });
  909. // ── alwaysAsk:模式无关的确认(docs/dynamic-workflow/launch.md「Why the window always appears」)──
  910. const alwaysAskCapability: PermissionToolCapability = {
  911. destructive: false,
  912. needsApproval: true,
  913. permission: {
  914. alwaysAsk: true,
  915. denyPriority: "beforeAsk",
  916. needsApproval: true,
  917. permission: "createWorkflow",
  918. reason: "createWorkflow.runConfirmation",
  919. riskLevel: "low",
  920. sideEffectScope: "none",
  921. },
  922. readOnly: true,
  923. riskLevel: "low",
  924. sideEffectScope: "none",
  925. };
  926. describe("PermissionService alwaysAsk", () => {
  927. function check(mode: PermissionContext["mode"], service = new PermissionService()) {
  928. return service.checkPermission(
  929. { input: { script: "return 1;" }, mode, riskLevel: "low", toolName: "CreateWorkflow" },
  930. alwaysAskCapability,
  931. );
  932. }
  933. // 宽松度设置一律压不过 alwaysAsk:yolo 直通、plan 的 readOnly 直通、edit/build 都要问。
  934. for (const mode of ["yolo", "plan", "build", "edit", "default"] as const) {
  935. it(`asks in ${mode} mode`, () => {
  936. expect(check(mode)).toMatchObject({
  937. allowed: false,
  938. alwaysAsk: true,
  939. decision: "ask",
  940. ruleId: "tool.alwaysAsk",
  941. });
  942. });
  943. }
  944. // 反向边界:alwaysAsk 只压过"放行"分支,压不过"阻断"分支。
  945. it("still yields to an explicitly disallowed tool", () => {
  946. const service = new PermissionService({
  947. allowedTools: new Set(),
  948. allowMediumRiskInAutoMode: false,
  949. autoApproveHighRisk: false,
  950. disallowedTools: new Set(["CreateWorkflow"]),
  951. });
  952. expect(check("yolo", service)).toMatchObject({
  953. allowed: false,
  954. decision: "deny",
  955. ruleId: "rule.disallowedTools",
  956. });
  957. });
  958. it("still yields to a project deny rule", () => {
  959. const service = new PermissionService();
  960. const decision = service.checkPermission(
  961. {
  962. input: { script: "return 1;" },
  963. mode: "yolo",
  964. riskLevel: "low",
  965. toolName: "CreateWorkflow",
  966. },
  967. alwaysAskCapability,
  968. { deny: [{ toolName: "CreateWorkflow" }], version: 1 },
  969. );
  970. expect(decision).toMatchObject({
  971. allowed: false,
  972. decision: "deny",
  973. ruleId: "rule.project.deny",
  974. });
  975. });
  976. it("still yields to auto mode being unimplemented", () => {
  977. expect(check("auto")).toMatchObject({
  978. allowed: false,
  979. decision: "deny",
  980. ruleId: "mode.auto.unimplemented",
  981. });
  982. });
  983. it("leaves tools without the flag on their existing mode semantics", () => {
  984. expect(
  985. new PermissionService().checkPermission(
  986. { input: {}, mode: "yolo", riskLevel: "low", toolName: "Read" },
  987. readCapability,
  988. ),
  989. ).toMatchObject({ allowed: true, decision: "allow", ruleId: "mode.yolo" });
  990. });
  991. });
  992. // ── 会话免确认(docs/dynamic-workflow/launch.md「Always allow in this session」)──
  993. // 「Always allow in this session」把 alwaysAsk gate 在一个 PermissionService 实例(= 一个会话)内
  994. // 关掉;阻断分支仍然优先,普通工具的模式语义不认会话规则。
  995. describe("PermissionService session grant", () => {
  996. const sessionGrant = [
  997. {
  998. behavior: "allow" as const,
  999. rules: [{ toolName: "CreateWorkflow" }],
  1000. type: "addRules" as const,
  1001. },
  1002. ];
  1003. function check(
  1004. mode: PermissionContext["mode"],
  1005. service: PermissionService,
  1006. toolName = "CreateWorkflow",
  1007. ) {
  1008. return service.checkPermission(
  1009. { input: { script: "return 2;" }, mode, riskLevel: "low", toolName },
  1010. alwaysAskCapability,
  1011. );
  1012. }
  1013. for (const mode of ["yolo", "plan", "build", "edit", "default"] as const) {
  1014. it(`lifts the alwaysAsk gate in ${mode} mode once the session grant is in place`, () => {
  1015. const service = new PermissionService();
  1016. expect(check(mode, service).decision).toBe("ask");
  1017. service.grantSessionPermission(sessionGrant);
  1018. expect(check(mode, service)).toMatchObject({
  1019. allowed: true,
  1020. decision: "allow",
  1021. ruleId: "rule.session.allow",
  1022. });
  1023. });
  1024. }
  1025. it("is scoped to the granted tool name", () => {
  1026. const service = new PermissionService();
  1027. service.grantSessionPermission(sessionGrant);
  1028. expect(check("build", service, "SaveWorkflow")).toMatchObject({
  1029. decision: "ask",
  1030. ruleId: "tool.alwaysAsk",
  1031. });
  1032. });
  1033. it("does not survive into a fresh service instance (session boundary)", () => {
  1034. const granted = new PermissionService();
  1035. granted.grantSessionPermission(sessionGrant);
  1036. expect(check("build", granted).decision).toBe("allow");
  1037. expect(check("build", new PermissionService()).decision).toBe("ask");
  1038. });
  1039. it("still yields to an explicitly disallowed tool", () => {
  1040. const service = new PermissionService({
  1041. allowedTools: new Set(),
  1042. allowMediumRiskInAutoMode: false,
  1043. autoApproveHighRisk: false,
  1044. disallowedTools: new Set(["CreateWorkflow"]),
  1045. });
  1046. service.grantSessionPermission(sessionGrant);
  1047. expect(check("yolo", service)).toMatchObject({
  1048. decision: "deny",
  1049. ruleId: "rule.disallowedTools",
  1050. });
  1051. });
  1052. it("still yields to a project deny rule", () => {
  1053. const service = new PermissionService();
  1054. service.grantSessionPermission(sessionGrant);
  1055. expect(
  1056. service.checkPermission(
  1057. {
  1058. input: { script: "return 2;" },
  1059. mode: "yolo",
  1060. riskLevel: "low",
  1061. toolName: "CreateWorkflow",
  1062. },
  1063. alwaysAskCapability,
  1064. { deny: [{ toolName: "CreateWorkflow" }], version: 1 },
  1065. ),
  1066. ).toMatchObject({ decision: "deny", ruleId: "rule.project.deny" });
  1067. });
  1068. // 会话规则只服务 alwaysAsk gate:plan 模式下的只读约束不能被一条会话规则绕开。
  1069. it("leaves tools without the alwaysAsk flag on their mode semantics", () => {
  1070. const service = new PermissionService();
  1071. service.grantSessionPermission([
  1072. { behavior: "allow", rules: [{ toolName: "Write" }], type: "addRules" },
  1073. ]);
  1074. expect(
  1075. service.checkPermission(
  1076. { input: { file_path: "/w/a.ts" }, mode: "plan", riskLevel: "medium", toolName: "Write" },
  1077. writeCapability,
  1078. ).decision,
  1079. ).not.toBe("allow");
  1080. });
  1081. });
  1082. // ── 本会话 run 的修订免确认(docs/dynamic-workflow/launch.md「Amending this session's runs」)──
  1083. // 钥匙是 resolveInput 回填的 `predecessor`(journal 的 parent_session_id / stopReason),不是内存表:
  1084. // 本会话发起且非用户停下 → 放行;别的会话的 run、用户停过的 run、事实块缺席、其他工具都照常 ask;
  1085. // 阻断分支仍优先。没有可播种、可撤销的状态,所以新实例上同样成立。
  1086. describe("PermissionService workflow owner rule", () => {
  1087. const owned = {
  1088. run_id: "dwfrun-a",
  1089. script: "return 3;",
  1090. predecessor: { status: "running", owned_by_this_session: true },
  1091. };
  1092. function checkAmend(
  1093. service: PermissionService,
  1094. input: Record<string, unknown>,
  1095. mode: PermissionContext["mode"] = "build",
  1096. toolName = "AmendWorkflow",
  1097. ) {
  1098. return service.checkPermission(
  1099. { input, mode, riskLevel: "low", toolName },
  1100. alwaysAskCapability,
  1101. );
  1102. }
  1103. for (const mode of ["yolo", "plan", "build", "edit", "default"] as const) {
  1104. it(`lifts the gate in ${mode} mode for an amend of a run this session started`, () => {
  1105. expect(checkAmend(new PermissionService(), owned, mode)).toMatchObject({
  1106. allowed: true,
  1107. decision: "allow",
  1108. ruleId: "rule.session.workflowOwner",
  1109. });
  1110. });
  1111. }
  1112. it("keeps asking for another session's run, a user-stopped run, and a missing fact block", () => {
  1113. const service = new PermissionService();
  1114. expect(
  1115. checkAmend(service, {
  1116. ...owned,
  1117. predecessor: { status: "completed", owned_by_this_session: false },
  1118. }),
  1119. ).toMatchObject({ decision: "ask", ruleId: "tool.alwaysAsk" });
  1120. expect(
  1121. checkAmend(service, {
  1122. ...owned,
  1123. predecessor: { status: "stopped", stop_reason: "user", owned_by_this_session: true },
  1124. }),
  1125. ).toMatchObject({ decision: "ask", ruleId: "tool.alwaysAsk" });
  1126. expect(checkAmend(service, { run_id: "dwfrun-a", script: "return 3;" })).toMatchObject({
  1127. decision: "ask",
  1128. ruleId: "tool.alwaysAsk",
  1129. });
  1130. });
  1131. it("allows an own run the model stopped, or that settled on its own", () => {
  1132. const service = new PermissionService();
  1133. for (const stop_reason of ["model", "provider", "interrupted", "superseded"] as const) {
  1134. expect(
  1135. checkAmend(service, {
  1136. ...owned,
  1137. predecessor: { status: "stopped", stop_reason, owned_by_this_session: true },
  1138. }).decision,
  1139. ).toBe("allow");
  1140. }
  1141. expect(
  1142. checkAmend(service, {
  1143. ...owned,
  1144. predecessor: { status: "completed", owned_by_this_session: true },
  1145. }).decision,
  1146. ).toBe("allow");
  1147. });
  1148. it("is scoped to AmendWorkflow", () => {
  1149. const service = new PermissionService();
  1150. expect(checkAmend(service, owned, "build", "CreateWorkflow")).toMatchObject({
  1151. decision: "ask",
  1152. ruleId: "tool.alwaysAsk",
  1153. });
  1154. expect(checkAmend(service, owned, "build", "SaveWorkflow")).toMatchObject({
  1155. decision: "ask",
  1156. ruleId: "tool.alwaysAsk",
  1157. });
  1158. });
  1159. it("still yields to an explicitly disallowed tool and to a project deny rule", () => {
  1160. const disallowed = new PermissionService({
  1161. allowedTools: new Set(),
  1162. allowMediumRiskInAutoMode: false,
  1163. autoApproveHighRisk: false,
  1164. disallowedTools: new Set(["AmendWorkflow"]),
  1165. });
  1166. expect(checkAmend(disallowed, owned, "yolo")).toMatchObject({
  1167. decision: "deny",
  1168. ruleId: "rule.disallowedTools",
  1169. });
  1170. const denied = new PermissionService();
  1171. expect(
  1172. denied.checkPermission(
  1173. { input: owned, mode: "yolo", riskLevel: "low", toolName: "AmendWorkflow" },
  1174. alwaysAskCapability,
  1175. { deny: [{ toolName: "AmendWorkflow" }], version: 1 },
  1176. ),
  1177. ).toMatchObject({ decision: "deny", ruleId: "rule.project.deny" });
  1178. });
  1179. });