|
| 1 | +import { describe, it, expect, mock } from "bun:test"; |
| 2 | +import { runWorkspaceScript } from "./scriptRunner"; |
| 3 | +import type { Runtime } from "@/node/runtime/Runtime"; |
| 4 | + |
| 5 | +// Mock discovery utils to control paths |
| 6 | +void mock.module("@/utils/scripts/discovery", () => ({ |
| 7 | + getScriptPath: (wsPath: string, scriptName: string) => { |
| 8 | + if (wsPath.includes("/")) { |
| 9 | + return `${wsPath}/.cmux/scripts/${scriptName}`; |
| 10 | + } |
| 11 | + return `${wsPath}\\.cmux\\scripts\\${scriptName}`; |
| 12 | + }, |
| 13 | + getScriptsDir: (wsPath: string) => { |
| 14 | + if (wsPath.includes("/")) { |
| 15 | + return `${wsPath}/.cmux/scripts`; |
| 16 | + } |
| 17 | + return `${wsPath}\\.cmux\\scripts`; |
| 18 | + }, |
| 19 | +})); |
| 20 | + |
| 21 | +// Mock createBashTool |
| 22 | +void mock.module("@/node/services/tools/bash", () => ({ |
| 23 | + createBashTool: () => ({ |
| 24 | + execute: mock().mockResolvedValue({ |
| 25 | + success: true, |
| 26 | + exitCode: 0, |
| 27 | + output: "mock output", |
| 28 | + error: "", |
| 29 | + }), |
| 30 | + }), |
| 31 | +})); |
| 32 | + |
| 33 | +// Mock helpers |
| 34 | +void mock.module("@/node/utils/runtime/helpers", () => ({ |
| 35 | + execBuffered: mock().mockResolvedValue({ exitCode: 0, stdout: "/tmp/mock", stderr: "" }), |
| 36 | + writeFileString: mock().mockResolvedValue(undefined), |
| 37 | + readFileString: mock().mockResolvedValue(""), |
| 38 | +})); |
| 39 | + |
| 40 | +describe("runWorkspaceScript path handling", () => { |
| 41 | + const mockRuntime = { |
| 42 | + stat: mock().mockResolvedValue({ isDirectory: false }), |
| 43 | + normalizePath: mock(), |
| 44 | + }; |
| 45 | + |
| 46 | + it("should handle POSIX paths correctly (SSH scenario)", async () => { |
| 47 | + // SSH runtime semantics: paths are POSIX |
| 48 | + const workspacePath = "/home/user/workspace"; |
| 49 | + const scriptName = "deploy.sh"; |
| 50 | + |
| 51 | + // normalizePath just returns the path as-is for POSIX on POSIX |
| 52 | + // But key is that it doesn't convert to backslashes |
| 53 | + mockRuntime.normalizePath.mockImplementation((p: string) => p); |
| 54 | + |
| 55 | + const result = await runWorkspaceScript( |
| 56 | + mockRuntime as unknown as Runtime, |
| 57 | + workspacePath, |
| 58 | + scriptName, |
| 59 | + [] |
| 60 | + ); |
| 61 | + |
| 62 | + expect(result.success).toBe(true); |
| 63 | + expect(mockRuntime.normalizePath).toHaveBeenCalledTimes(2); |
| 64 | + // Verify it didn't fail with escape error |
| 65 | + }); |
| 66 | + |
| 67 | + it("should handle Windows paths correctly", async () => { |
| 68 | + const workspacePath = "C:\\Users\\user\\workspace"; |
| 69 | + const scriptName = "deploy.bat"; |
| 70 | + |
| 71 | + mockRuntime.normalizePath.mockImplementation((p: string) => p); |
| 72 | + |
| 73 | + const result = await runWorkspaceScript( |
| 74 | + mockRuntime as unknown as Runtime, |
| 75 | + workspacePath, |
| 76 | + scriptName, |
| 77 | + [] |
| 78 | + ); |
| 79 | + |
| 80 | + expect(result.success).toBe(true); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should detect path escape attempts", async () => { |
| 84 | + const workspacePath = "/home/user/workspace"; |
| 85 | + const scriptName = "evil.sh"; |
| 86 | + |
| 87 | + // Mock getScriptPath returning something outside |
| 88 | + // We can't easily mock the module conditionally inside test, |
| 89 | + // but we can rely on normalizePath to simulate the escape check failure |
| 90 | + |
| 91 | + // Simulate a case where script path is outside scripts dir |
| 92 | + mockRuntime.normalizePath.mockImplementation((p: string, _base: string) => { |
| 93 | + if (p.includes("scripts")) { |
| 94 | + if (p.endsWith("scripts")) return "/home/user/workspace/.cmux/scripts"; |
| 95 | + return "/home/user/workspace/evil.sh"; // Escaped! |
| 96 | + } |
| 97 | + return p; |
| 98 | + }); |
| 99 | + |
| 100 | + const result = await runWorkspaceScript( |
| 101 | + mockRuntime as unknown as Runtime, |
| 102 | + workspacePath, |
| 103 | + scriptName, |
| 104 | + [] |
| 105 | + ); |
| 106 | + |
| 107 | + expect(result.success).toBe(false); |
| 108 | + if (!result.success) { |
| 109 | + expect(result.error).toContain("Script path escapes scripts directory"); |
| 110 | + } |
| 111 | + }); |
| 112 | +}); |
0 commit comments