permission-service.test.ts 36 KB

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