bash-command-permission-policy.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { describe, expect, it } from "vitest";
  2. import type { PermissionRuleset } from "@zcode/contracts";
  3. import { PermissionService } from "../src/permission/service.js";
  4. import { createBashPermissionRulePolicy } from "../src/tool/handlers/bash-command-permission-policy.js";
  5. import { bashToolEntry } from "../src/tool/handlers/bash.js";
  6. describe("Bash command permission rule policy", () => {
  7. it.each(["cmd", "legacy-shell"] as const)(
  8. "only suggests and accepts exact command grants for %s",
  9. (dialect) => {
  10. const command = "npm run test # & python script.py";
  11. const policy = createBashPermissionRulePolicy(command, {
  12. bashShellSelection: { dialect, source: "legacy-fallback", display: { name: dialect } },
  13. });
  14. expect(policy.suggestedPermissionUpdates).toEqual([
  15. {
  16. behavior: "allow",
  17. type: "addRules",
  18. rules: [{ toolName: "Bash", ruleContent: command }],
  19. },
  20. ]);
  21. for (const ruleContent of ["npm run:*", "npm *", "npm run test"]) {
  22. expect(policy.evaluateRules("allow", [{ toolName: "Bash", ruleContent }])).toBe(false);
  23. }
  24. expect(policy.evaluateRules("allow", [{ toolName: "Bash", ruleContent: command }])).toBe(
  25. true,
  26. );
  27. expect(policy.evaluateRules("allow", [{ toolName: "Bash" }])).toBe(true);
  28. for (const behavior of ["deny", "ask"] as const) {
  29. expect(policy.evaluateRules(behavior, [{ toolName: "Bash", ruleContent: command }])).toBe(
  30. true,
  31. );
  32. expect(
  33. policy.evaluateRules(behavior, [{ toolName: "Bash", ruleContent: "npm run:*" }]),
  34. ).toBe(true);
  35. }
  36. },
  37. );
  38. it.each([
  39. ["pnpm run lint --fix", "pnpm run lint:*"],
  40. ["pnpm --dir /tmp/project run lint --fix", "pnpm run lint:*"],
  41. ["git -C packages/app push origin main", "git push:*"],
  42. ["python3 -m pytest tests/unit", "python3 -m pytest:*"],
  43. ["sudo -u build git push origin main", "sudo git push:*"],
  44. ["docker compose up --detach", "docker compose up:*"],
  45. ["FOO=bar pnpm run lint --fix", "FOO=bar pnpm run lint:*"],
  46. ])("resolves a stable prefix for %s", (command, expectedRuleContent) => {
  47. expect(ruleContents(command)).toEqual([expectedRuleContent]);
  48. });
  49. it.each([
  50. "curl https://example.com/archive.tgz",
  51. "rm -rf ./dist",
  52. "pnpm run lint > lint.txt",
  53. "pnpm run $(node choose-script.js)",
  54. "FOO=$BAR pnpm run lint",
  55. ])("falls back to an exact rule for %s", (command) => {
  56. expect(ruleContents(command)).toEqual([command]);
  57. });
  58. it("suggests independent compound rules and omits readonly invocations", () => {
  59. expect(ruleContents("ls && pnpm run lint --fix && git push origin main")).toEqual([
  60. "pnpm run lint:*",
  61. "git push:*",
  62. ]);
  63. });
  64. it("falls back to the whole exact command above the five-rule limit", () => {
  65. const command = ["a", "b", "c", "d", "e", "f"]
  66. .map((script) => `pnpm run ${script}`)
  67. .join(" && ");
  68. expect(ruleContents(command)).toEqual([command]);
  69. });
  70. it("requires every permission-requiring invocation to be allowed", () => {
  71. const command = "npm run test && rm -rf ./out";
  72. const partialRules: PermissionRuleset = {
  73. allow: [{ ruleContent: "npm run test:*", toolName: "Bash" }],
  74. version: 1,
  75. };
  76. expect(check(command, partialRules).decision).toBe("ask");
  77. const fullRules: PermissionRuleset = {
  78. allow: [
  79. { ruleContent: "npm run test:*", toolName: "Bash" },
  80. { ruleContent: "rm -rf ./out", toolName: "Bash" },
  81. ],
  82. version: 1,
  83. };
  84. expect(check(command, fullRules).decision).toBe("allow");
  85. });
  86. it("matches a persisted stable prefix after stripping transient global flags", () => {
  87. expect(
  88. check("pnpm --dir /tmp/project run lint -- --second", {
  89. allow: [{ ruleContent: "pnpm run lint:*", toolName: "Bash" }],
  90. version: 1,
  91. }).decision,
  92. ).toBe("allow");
  93. expect(
  94. check("git -C packages/app push origin main", {
  95. allow: [{ ruleContent: "git push:*", toolName: "Bash" }],
  96. version: 1,
  97. }).decision,
  98. ).toBe("allow");
  99. });
  100. it("applies deny and ask when any invocation matches", () => {
  101. const command = "npm run test && rm -rf ./out";
  102. expect(
  103. check(command, {
  104. deny: [{ ruleContent: "rm:*", toolName: "Bash" }],
  105. version: 1,
  106. }).decision,
  107. ).toBe("deny");
  108. expect(
  109. check(command, {
  110. ask: [{ ruleContent: "npm run:*", toolName: "Bash" }],
  111. version: 1,
  112. }).decision,
  113. ).toBe("ask");
  114. });
  115. it("keeps legacy exact, wildcard, prefix, and tool-only behavior without compound bypass", () => {
  116. expect(
  117. check("npm run test && rm -rf ./out", {
  118. allow: [{ ruleContent: "npm run test && rm -rf ./out", toolName: "Bash" }],
  119. version: 1,
  120. }).decision,
  121. ).toBe("allow");
  122. expect(
  123. check(" pnpm run lint --fix ", {
  124. allow: [{ ruleContent: " pnpm run lint --fix ", toolName: "Bash" }],
  125. version: 1,
  126. }).decision,
  127. ).toBe("allow");
  128. expect(
  129. check("npm run test --watch", {
  130. allow: [{ ruleContent: "npm run:*", toolName: "Bash" }],
  131. version: 1,
  132. }).decision,
  133. ).toBe("allow");
  134. expect(
  135. check("npm run test && rm -rf ./out", {
  136. allow: [{ ruleContent: "npm run:*", toolName: "Bash" }],
  137. version: 1,
  138. }).decision,
  139. ).toBe("ask");
  140. expect(
  141. check("pnpm run lint --fix", {
  142. allow: [{ ruleContent: "pnpm *", toolName: "Bash" }],
  143. version: 1,
  144. }).decision,
  145. ).toBe("allow");
  146. expect(
  147. check("npm run test && rm -rf ./out", {
  148. allow: [{ toolName: "Bash" }],
  149. version: 1,
  150. }).decision,
  151. ).toBe("allow");
  152. });
  153. it("only lets an unsafe command match its complete exact rule", () => {
  154. const rules: PermissionRuleset = {
  155. allow: [
  156. { ruleContent: "pnpm run:*", toolName: "Bash" },
  157. { ruleContent: "pnpm run lint > lint.txt", toolName: "Bash" },
  158. ],
  159. version: 1,
  160. };
  161. expect(check("pnpm run lint > lint.txt", rules).decision).toBe("allow");
  162. expect(check("pnpm run lint > other.txt", rules).decision).toBe("ask");
  163. });
  164. });
  165. function ruleContents(command: string): Array<string | undefined> {
  166. const policy = createBashPermissionRulePolicy(command);
  167. return policy.suggestedPermissionUpdates.flatMap((update) =>
  168. update.rules.map((rule) => rule.ruleContent),
  169. );
  170. }
  171. function check(command: string, ruleset: PermissionRuleset) {
  172. const service = new PermissionService();
  173. return service.checkPermission(
  174. {
  175. input: { command },
  176. mode: "build",
  177. riskLevel: bashToolEntry.metadata.riskLevel,
  178. toolName: "Bash",
  179. },
  180. {
  181. ...bashToolEntry.metadata,
  182. permission: bashToolEntry.permission,
  183. },
  184. ruleset,
  185. createBashPermissionRulePolicy(command),
  186. );
  187. }