Skip to content

Commit ac57e22

Browse files
committed
fix: allow docker compose command chaining
1 parent 53feb9d commit ac57e22

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

‎packages/server/src/utils/builders/compose.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ Compose Type: ${composeType} ✅`;
7070
// Shell control characters that must never appear in a user-provided compose
7171
// command: they would let it break out of the `docker ${command}` invocation
7272
// into arbitrary host commands. A normal docker compose CLI line never needs them.
73-
const UNSAFE_COMPOSE_COMMAND = /[;&|`$(){}<>\n\\]/;
73+
// Removed '&' from the blocklist to allow '&&' chaining
74+
const UNSAFE_COMPOSE_COMMAND = /[;|`$(){}<>\n\\]/;
7475

7576
const sanitizeCommand = (command: string) => {
7677
const sanitizedCommand = command.trim();
@@ -81,8 +82,24 @@ const sanitizeCommand = (command: string) => {
8182
);
8283
}
8384

84-
const parts = sanitizedCommand.split(/\s+/);
85+
if (sanitizedCommand.includes("&")) {
86+
// Block single '&' (e.g., backgrounding tasks) or malformed chains like '&&&'
87+
if (/(?<!&)&(?!&)/.test(sanitizedCommand) || sanitizedCommand.includes("&&&")) {
88+
throw new Error("Single '&' is not allowed. Use '&&' for chaining.");
89+
}
90+
91+
// Split by '&&' and check that every chained command (skipping the first one) is safe
92+
const chains = sanitizedCommand.split("&&").map((cmd) => cmd.trim());
93+
const isSafeChain = chains.slice(1).every((cmd) =>
94+
cmd.startsWith("docker compose ") || cmd.startsWith("docker-compose ")
95+
);
8596

97+
if (!isSafeChain) {
98+
throw new Error("Chained commands must strictly start with 'docker compose '");
99+
}
100+
}
101+
102+
const parts = sanitizedCommand.split(/\s+/);
86103
const restCommand = parts.map((arg) => arg.replace(/^"(.*)"$/, "$1"));
87104

88105
return restCommand.join(" ");

0 commit comments

Comments
 (0)