bash.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. import { describe, expect, test } from "bun:test"
  2. import path from "path"
  3. import { BashTool } from "../../src/tool/bash"
  4. import { Instance } from "../../src/project/instance"
  5. import { Permission } from "../../src/permission"
  6. import { tmpdir } from "../fixture/fixture"
  7. const ctx = {
  8. sessionID: "test",
  9. messageID: "",
  10. callID: "",
  11. agent: "build",
  12. abort: AbortSignal.any([]),
  13. metadata: () => {},
  14. }
  15. const projectRoot = path.join(__dirname, "../..")
  16. describe("tool.bash", () => {
  17. test("basic", async () => {
  18. await Instance.provide({
  19. directory: projectRoot,
  20. fn: async () => {
  21. const bash = await BashTool.init()
  22. const result = await bash.execute(
  23. {
  24. command: "echo 'test'",
  25. description: "Echo test message",
  26. },
  27. ctx,
  28. )
  29. expect(result.metadata.exit).toBe(0)
  30. expect(result.metadata.output).toContain("test")
  31. },
  32. })
  33. })
  34. })
  35. describe("tool.bash permissions", () => {
  36. test("allows command matching allow pattern", async () => {
  37. await using tmp = await tmpdir({
  38. init: async (dir) => {
  39. await Bun.write(
  40. path.join(dir, "opencode.json"),
  41. JSON.stringify({
  42. permission: {
  43. bash: {
  44. "echo *": "allow",
  45. "*": "deny",
  46. },
  47. },
  48. }),
  49. )
  50. },
  51. })
  52. await Instance.provide({
  53. directory: tmp.path,
  54. fn: async () => {
  55. const bash = await BashTool.init()
  56. const result = await bash.execute(
  57. {
  58. command: "echo hello",
  59. description: "Echo hello",
  60. },
  61. ctx,
  62. )
  63. expect(result.metadata.exit).toBe(0)
  64. expect(result.metadata.output).toContain("hello")
  65. },
  66. })
  67. })
  68. test("denies command matching deny pattern", async () => {
  69. await using tmp = await tmpdir({
  70. init: async (dir) => {
  71. await Bun.write(
  72. path.join(dir, "opencode.json"),
  73. JSON.stringify({
  74. permission: {
  75. bash: {
  76. "curl *": "deny",
  77. "*": "allow",
  78. },
  79. },
  80. }),
  81. )
  82. },
  83. })
  84. await Instance.provide({
  85. directory: tmp.path,
  86. fn: async () => {
  87. const bash = await BashTool.init()
  88. await expect(
  89. bash.execute(
  90. {
  91. command: "curl https://example.com",
  92. description: "Fetch URL",
  93. },
  94. ctx,
  95. ),
  96. ).rejects.toThrow("restricted")
  97. },
  98. })
  99. })
  100. test("denies all commands with wildcard deny", async () => {
  101. await using tmp = await tmpdir({
  102. init: async (dir) => {
  103. await Bun.write(
  104. path.join(dir, "opencode.json"),
  105. JSON.stringify({
  106. permission: {
  107. bash: {
  108. "*": "deny",
  109. },
  110. },
  111. }),
  112. )
  113. },
  114. })
  115. await Instance.provide({
  116. directory: tmp.path,
  117. fn: async () => {
  118. const bash = await BashTool.init()
  119. await expect(
  120. bash.execute(
  121. {
  122. command: "ls",
  123. description: "List files",
  124. },
  125. ctx,
  126. ),
  127. ).rejects.toThrow("restricted")
  128. },
  129. })
  130. })
  131. test("more specific pattern overrides general pattern", async () => {
  132. await using tmp = await tmpdir({
  133. init: async (dir) => {
  134. await Bun.write(
  135. path.join(dir, "opencode.json"),
  136. JSON.stringify({
  137. permission: {
  138. bash: {
  139. "*": "deny",
  140. "ls *": "allow",
  141. "pwd*": "allow",
  142. },
  143. },
  144. }),
  145. )
  146. },
  147. })
  148. await Instance.provide({
  149. directory: tmp.path,
  150. fn: async () => {
  151. const bash = await BashTool.init()
  152. // ls should be allowed
  153. const result = await bash.execute(
  154. {
  155. command: "ls -la",
  156. description: "List files",
  157. },
  158. ctx,
  159. )
  160. expect(result.metadata.exit).toBe(0)
  161. // pwd should be allowed
  162. const pwd = await bash.execute(
  163. {
  164. command: "pwd",
  165. description: "Print working directory",
  166. },
  167. ctx,
  168. )
  169. expect(pwd.metadata.exit).toBe(0)
  170. // cat should be denied
  171. await expect(
  172. bash.execute(
  173. {
  174. command: "cat /etc/passwd",
  175. description: "Read file",
  176. },
  177. ctx,
  178. ),
  179. ).rejects.toThrow("restricted")
  180. },
  181. })
  182. })
  183. test("denies dangerous subcommands while allowing safe ones", async () => {
  184. await using tmp = await tmpdir({
  185. init: async (dir) => {
  186. await Bun.write(
  187. path.join(dir, "opencode.json"),
  188. JSON.stringify({
  189. permission: {
  190. bash: {
  191. "find *": "allow",
  192. "find * -delete*": "deny",
  193. "find * -exec*": "deny",
  194. "*": "deny",
  195. },
  196. },
  197. }),
  198. )
  199. },
  200. })
  201. await Instance.provide({
  202. directory: tmp.path,
  203. fn: async () => {
  204. const bash = await BashTool.init()
  205. // Basic find should work
  206. const result = await bash.execute(
  207. {
  208. command: "find . -name '*.ts'",
  209. description: "Find typescript files",
  210. },
  211. ctx,
  212. )
  213. expect(result.metadata.exit).toBe(0)
  214. // find -delete should be denied
  215. await expect(
  216. bash.execute(
  217. {
  218. command: "find . -name '*.tmp' -delete",
  219. description: "Delete temp files",
  220. },
  221. ctx,
  222. ),
  223. ).rejects.toThrow("restricted")
  224. // find -exec should be denied
  225. await expect(
  226. bash.execute(
  227. {
  228. command: "find . -name '*.ts' -exec cat {} \\;",
  229. description: "Find and cat files",
  230. },
  231. ctx,
  232. ),
  233. ).rejects.toThrow("restricted")
  234. },
  235. })
  236. })
  237. test("allows git read commands while denying writes", async () => {
  238. await using tmp = await tmpdir({
  239. git: true,
  240. init: async (dir) => {
  241. await Bun.write(
  242. path.join(dir, "opencode.json"),
  243. JSON.stringify({
  244. permission: {
  245. bash: {
  246. "git status*": "allow",
  247. "git log*": "allow",
  248. "git diff*": "allow",
  249. "git branch": "allow",
  250. "git commit *": "deny",
  251. "git push *": "deny",
  252. "*": "deny",
  253. },
  254. },
  255. }),
  256. )
  257. },
  258. })
  259. await Instance.provide({
  260. directory: tmp.path,
  261. fn: async () => {
  262. const bash = await BashTool.init()
  263. // git status should work
  264. const status = await bash.execute(
  265. {
  266. command: "git status",
  267. description: "Git status",
  268. },
  269. ctx,
  270. )
  271. expect(status.metadata.exit).toBe(0)
  272. // git log should work
  273. const log = await bash.execute(
  274. {
  275. command: "git log --oneline -5",
  276. description: "Git log",
  277. },
  278. ctx,
  279. )
  280. expect(log.metadata.exit).toBe(0)
  281. // git commit should be denied
  282. await expect(
  283. bash.execute(
  284. {
  285. command: "git commit -m 'test'",
  286. description: "Git commit",
  287. },
  288. ctx,
  289. ),
  290. ).rejects.toThrow("restricted")
  291. // git push should be denied
  292. await expect(
  293. bash.execute(
  294. {
  295. command: "git push origin main",
  296. description: "Git push",
  297. },
  298. ctx,
  299. ),
  300. ).rejects.toThrow("restricted")
  301. },
  302. })
  303. })
  304. test("denies external directory access when permission is deny", async () => {
  305. await using tmp = await tmpdir({
  306. init: async (dir) => {
  307. await Bun.write(
  308. path.join(dir, "opencode.json"),
  309. JSON.stringify({
  310. permission: {
  311. external_directory: "deny",
  312. bash: {
  313. "*": "allow",
  314. },
  315. },
  316. }),
  317. )
  318. },
  319. })
  320. await Instance.provide({
  321. directory: tmp.path,
  322. fn: async () => {
  323. const bash = await BashTool.init()
  324. // Should deny cd to parent directory (cd is checked for external paths)
  325. await expect(
  326. bash.execute(
  327. {
  328. command: "cd ../",
  329. description: "Change to parent directory",
  330. },
  331. ctx,
  332. ),
  333. ).rejects.toThrow()
  334. },
  335. })
  336. })
  337. test("denies workdir outside project when external_directory is deny", async () => {
  338. await using tmp = await tmpdir({
  339. init: async (dir) => {
  340. await Bun.write(
  341. path.join(dir, "opencode.json"),
  342. JSON.stringify({
  343. permission: {
  344. external_directory: "deny",
  345. bash: {
  346. "*": "allow",
  347. },
  348. },
  349. }),
  350. )
  351. },
  352. })
  353. await Instance.provide({
  354. directory: tmp.path,
  355. fn: async () => {
  356. const bash = await BashTool.init()
  357. await expect(
  358. bash.execute(
  359. {
  360. command: "ls",
  361. workdir: "/tmp",
  362. description: "List /tmp",
  363. },
  364. ctx,
  365. ),
  366. ).rejects.toThrow()
  367. },
  368. })
  369. })
  370. test("handles multiple commands in sequence", async () => {
  371. await using tmp = await tmpdir({
  372. init: async (dir) => {
  373. await Bun.write(
  374. path.join(dir, "opencode.json"),
  375. JSON.stringify({
  376. permission: {
  377. bash: {
  378. "echo *": "allow",
  379. "curl *": "deny",
  380. "*": "deny",
  381. },
  382. },
  383. }),
  384. )
  385. },
  386. })
  387. await Instance.provide({
  388. directory: tmp.path,
  389. fn: async () => {
  390. const bash = await BashTool.init()
  391. // echo && echo should work
  392. const result = await bash.execute(
  393. {
  394. command: "echo foo && echo bar",
  395. description: "Echo twice",
  396. },
  397. ctx,
  398. )
  399. expect(result.metadata.output).toContain("foo")
  400. expect(result.metadata.output).toContain("bar")
  401. // echo && curl should fail (curl is denied)
  402. await expect(
  403. bash.execute(
  404. {
  405. command: "echo hi && curl https://example.com",
  406. description: "Echo then curl",
  407. },
  408. ctx,
  409. ),
  410. ).rejects.toThrow("restricted")
  411. },
  412. })
  413. })
  414. })