CLI: validate container hints

This commit is contained in:
Mariano Belinky
2026-04-08 17:52:47 +02:00
parent aabc93bfc8
commit 68b7ffd59e
2 changed files with 11 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ const PROFILE_FLAG_RE = /(?:^|\s)--profile(?:\s|=|$)/;
const DEV_FLAG_RE = /(?:^|\s)--dev(?:\s|$)/;
const UPDATE_COMMAND_RE =
/^(?:pnpm|npm|bunx|npx)\s+openclaw\b.*(?:^|\s)update(?:\s|$)|^openclaw\b.*(?:^|\s)update(?:\s|$)/;
const CONTAINER_HINT_RE = /^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,127}$/;
export function formatCliCommand(
command: string,
@@ -14,7 +15,8 @@ export function formatCliCommand(
): string {
const cliName = resolveCliName();
const normalizedCommand = replaceCliName(command, cliName);
const container = env.OPENCLAW_CONTAINER_HINT?.trim();
const rawContainer = env.OPENCLAW_CONTAINER_HINT?.trim();
const container = rawContainer && CONTAINER_HINT_RE.test(rawContainer) ? rawContainer : undefined;
const profile = normalizeProfileName(env.OPENCLAW_PROFILE);
if (!container && !profile) {
return normalizedCommand;

View File

@@ -214,6 +214,14 @@ describe("formatCliCommand", () => {
).toBe("openclaw --container demo gateway status --deep");
});
it("ignores unsafe container hints", () => {
expect(
formatCliCommand("openclaw gateway status --deep", {
OPENCLAW_CONTAINER_HINT: "demo; rm -rf /",
}),
).toBe("openclaw gateway status --deep");
});
it("preserves both --container and --profile hints", () => {
expect(
formatCliCommand("openclaw doctor", {