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

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