mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-11 06:25:17 +00:00
fix: prevent to pass invalid docker container names
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import type http from "node:http";
|
import type http from "node:http";
|
||||||
import { findServerById, validateRequest } from "@dokploy/server";
|
import { findServerById, IS_CLOUD, validateRequest } from "@dokploy/server";
|
||||||
import { spawn } from "node-pty";
|
import { spawn } from "node-pty";
|
||||||
import { Client } from "ssh2";
|
import { Client } from "ssh2";
|
||||||
import { WebSocketServer } from "ws";
|
import { WebSocketServer } from "ws";
|
||||||
@@ -111,6 +111,11 @@ export const setupDockerContainerLogsWebSocketServer = (
|
|||||||
client.end();
|
client.end();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
if (IS_CLOUD) {
|
||||||
|
ws.send("This feature is not available in the cloud version.");
|
||||||
|
ws.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
const shell = getShell();
|
const shell = getShell();
|
||||||
const baseCommand = `docker ${runType === "swarm" ? "service" : "container"} logs --timestamps ${
|
const baseCommand = `docker ${runType === "swarm" ? "service" : "container"} logs --timestamps ${
|
||||||
runType === "swarm" ? "--raw" : ""
|
runType === "swarm" ? "--raw" : ""
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import type http from "node:http";
|
import type http from "node:http";
|
||||||
import { findServerById, validateRequest } from "@dokploy/server";
|
import { findServerById, IS_CLOUD, validateRequest } from "@dokploy/server";
|
||||||
import { spawn } from "node-pty";
|
import { spawn } from "node-pty";
|
||||||
import { Client } from "ssh2";
|
import { Client } from "ssh2";
|
||||||
import { WebSocketServer } from "ws";
|
import { WebSocketServer } from "ws";
|
||||||
import { getShell } from "./utils";
|
import { getShell, isValidContainerId, isValidShell } from "./utils";
|
||||||
|
|
||||||
export const setupDockerContainerTerminalWebSocketServer = (
|
export const setupDockerContainerTerminalWebSocketServer = (
|
||||||
server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>,
|
server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>,
|
||||||
@@ -39,6 +39,26 @@ export const setupDockerContainerTerminalWebSocketServer = (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!containerId) {
|
||||||
|
ws.close(4000, "containerId not provided");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Security: Validate containerId to prevent command injection
|
||||||
|
if (!isValidContainerId(containerId)) {
|
||||||
|
ws.close(4000, "Invalid container ID format");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Security: Validate shell to prevent command injection
|
||||||
|
if (activeWay && !isValidShell(activeWay)) {
|
||||||
|
ws.close(4000, "Invalid shell specified");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default to 'sh' if no shell specified
|
||||||
|
const shell = activeWay || "sh";
|
||||||
|
|
||||||
if (!user || !session) {
|
if (!user || !session) {
|
||||||
ws.close();
|
ws.close();
|
||||||
return;
|
return;
|
||||||
@@ -54,55 +74,61 @@ export const setupDockerContainerTerminalWebSocketServer = (
|
|||||||
let _stderr = "";
|
let _stderr = "";
|
||||||
conn
|
conn
|
||||||
.once("ready", () => {
|
.once("ready", () => {
|
||||||
conn.exec(
|
// Use array-style arguments to prevent shell injection
|
||||||
`docker exec -it -w / ${containerId} ${activeWay}`,
|
const dockerCommand = [
|
||||||
{ pty: true },
|
"docker",
|
||||||
(err, stream) => {
|
"exec",
|
||||||
if (err) {
|
"-it",
|
||||||
console.error("SSH exec error:", err);
|
"-w",
|
||||||
ws.close();
|
"/",
|
||||||
|
containerId,
|
||||||
|
shell,
|
||||||
|
].join(" ");
|
||||||
|
conn.exec(dockerCommand, { pty: true }, (err, stream) => {
|
||||||
|
if (err) {
|
||||||
|
console.error("SSH exec error:", err);
|
||||||
|
ws.close();
|
||||||
|
conn.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
stream
|
||||||
|
.on("close", (code: number, _signal: string) => {
|
||||||
|
ws.send(`\nContainer closed with code: ${code}\n`);
|
||||||
conn.end();
|
conn.end();
|
||||||
return;
|
})
|
||||||
}
|
.on("data", (data: string) => {
|
||||||
|
_stdout += data.toString();
|
||||||
|
ws.send(data.toString());
|
||||||
|
})
|
||||||
|
.stderr.on("data", (data) => {
|
||||||
|
_stderr += data.toString();
|
||||||
|
ws.send(data.toString());
|
||||||
|
console.error("Error: ", data.toString());
|
||||||
|
});
|
||||||
|
|
||||||
stream
|
ws.on("message", (message) => {
|
||||||
.on("close", (code: number, _signal: string) => {
|
try {
|
||||||
ws.send(`\nContainer closed with code: ${code}\n`);
|
let command: string | Buffer[] | Buffer | ArrayBuffer;
|
||||||
conn.end();
|
if (Buffer.isBuffer(message)) {
|
||||||
})
|
command = message.toString("utf8");
|
||||||
.on("data", (data: string) => {
|
} else {
|
||||||
_stdout += data.toString();
|
command = message;
|
||||||
ws.send(data.toString());
|
|
||||||
})
|
|
||||||
.stderr.on("data", (data) => {
|
|
||||||
_stderr += data.toString();
|
|
||||||
ws.send(data.toString());
|
|
||||||
console.error("Error: ", data.toString());
|
|
||||||
});
|
|
||||||
|
|
||||||
ws.on("message", (message) => {
|
|
||||||
try {
|
|
||||||
let command: string | Buffer[] | Buffer | ArrayBuffer;
|
|
||||||
if (Buffer.isBuffer(message)) {
|
|
||||||
command = message.toString("utf8");
|
|
||||||
} else {
|
|
||||||
command = message;
|
|
||||||
}
|
|
||||||
stream.write(command.toString());
|
|
||||||
} catch (error) {
|
|
||||||
// @ts-ignore
|
|
||||||
const errorMessage = error?.message as unknown as string;
|
|
||||||
ws.send(errorMessage);
|
|
||||||
}
|
}
|
||||||
});
|
stream.write(command.toString());
|
||||||
|
} catch (error) {
|
||||||
|
// @ts-ignore
|
||||||
|
const errorMessage = error?.message as unknown as string;
|
||||||
|
ws.send(errorMessage);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
ws.on("close", () => {
|
ws.on("close", () => {
|
||||||
stream.end();
|
stream.end();
|
||||||
// Ensure SSH connection is closed when WebSocket closes
|
// Ensure SSH connection is closed when WebSocket closes
|
||||||
conn.end();
|
conn.end();
|
||||||
});
|
});
|
||||||
},
|
});
|
||||||
);
|
|
||||||
})
|
})
|
||||||
.on("error", (err) => {
|
.on("error", (err) => {
|
||||||
console.error("SSH connection error:", err);
|
console.error("SSH connection error:", err);
|
||||||
@@ -119,10 +145,15 @@ export const setupDockerContainerTerminalWebSocketServer = (
|
|||||||
privateKey: server.sshKey?.privateKey,
|
privateKey: server.sshKey?.privateKey,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
if (IS_CLOUD) {
|
||||||
|
ws.send("This feature is not available in the cloud version.");
|
||||||
|
ws.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
const shell = getShell();
|
const shell = getShell();
|
||||||
const ptyProcess = spawn(
|
const ptyProcess = spawn(
|
||||||
shell,
|
"docker",
|
||||||
["-c", `docker exec -it -w / ${containerId} ${activeWay}`],
|
["exec", "-it", "-w", "/", containerId, shell],
|
||||||
{},
|
{},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
execAsync,
|
execAsync,
|
||||||
getHostSystemStats,
|
getHostSystemStats,
|
||||||
getLastAdvancedStatsFile,
|
getLastAdvancedStatsFile,
|
||||||
|
IS_CLOUD,
|
||||||
recordAdvancedStats,
|
recordAdvancedStats,
|
||||||
validateRequest,
|
validateRequest,
|
||||||
} from "@dokploy/server";
|
} from "@dokploy/server";
|
||||||
@@ -32,6 +33,12 @@ export const setupDockerStatsMonitoringSocketServer = (
|
|||||||
|
|
||||||
wssTerm.on("connection", async (ws, req) => {
|
wssTerm.on("connection", async (ws, req) => {
|
||||||
const url = new URL(req.url || "", `http://${req.headers.host}`);
|
const url = new URL(req.url || "", `http://${req.headers.host}`);
|
||||||
|
|
||||||
|
if (IS_CLOUD) {
|
||||||
|
ws.send("This feature is not available in the cloud version.");
|
||||||
|
ws.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
const appName = url.searchParams.get("appName");
|
const appName = url.searchParams.get("appName");
|
||||||
const appType = (url.searchParams.get("appType") || "application") as
|
const appType = (url.searchParams.get("appType") || "application") as
|
||||||
| "application"
|
| "application"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { spawn } from "node:child_process";
|
import { spawn } from "node:child_process";
|
||||||
import type http from "node:http";
|
import type http from "node:http";
|
||||||
import { findServerById, validateRequest } from "@dokploy/server";
|
import { findServerById, IS_CLOUD, validateRequest } from "@dokploy/server";
|
||||||
import { Client } from "ssh2";
|
import { Client } from "ssh2";
|
||||||
import { WebSocketServer } from "ws";
|
import { WebSocketServer } from "ws";
|
||||||
|
|
||||||
@@ -108,6 +108,11 @@ export const setupDeploymentLogsWebSocketServer = (
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
if (IS_CLOUD) {
|
||||||
|
ws.send("This feature is not available in the cloud version.");
|
||||||
|
ws.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
tailProcess = spawn("tail", ["-n", "+1", "-f", logPath]);
|
tailProcess = spawn("tail", ["-n", "+1", "-f", logPath]);
|
||||||
|
|
||||||
const stdout = tailProcess.stdout;
|
const stdout = tailProcess.stdout;
|
||||||
|
|||||||
@@ -1,9 +1,41 @@
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { execAsync, paths } from "@dokploy/server";
|
import { execAsync, IS_CLOUD, paths } from "@dokploy/server";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates that the container ID matches Docker's expected format.
|
||||||
|
* Docker container IDs are 64-character hex strings (or 12-char short form).
|
||||||
|
* Also allows container names: alphanumeric, underscores, hyphens, and dots.
|
||||||
|
*/
|
||||||
|
export const isValidContainerId = (id: string): boolean => {
|
||||||
|
// Match full ID (64 hex chars), short ID (12 hex chars), or container name
|
||||||
|
const hexPattern = /^[a-f0-9]{12,64}$/i;
|
||||||
|
const namePattern = /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/;
|
||||||
|
return hexPattern.test(id) || (namePattern.test(id) && id.length <= 128);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates that the shell is one of the allowed shells.
|
||||||
|
*/
|
||||||
|
export const isValidShell = (shell: string): boolean => {
|
||||||
|
const allowedShells = [
|
||||||
|
"sh",
|
||||||
|
"bash",
|
||||||
|
"zsh",
|
||||||
|
"ash",
|
||||||
|
"/bin/sh",
|
||||||
|
"/bin/bash",
|
||||||
|
"/bin/zsh",
|
||||||
|
"/bin/ash",
|
||||||
|
];
|
||||||
|
return allowedShells.includes(shell);
|
||||||
|
};
|
||||||
|
|
||||||
export const getShell = () => {
|
export const getShell = () => {
|
||||||
|
if (IS_CLOUD) {
|
||||||
|
return "CLOUD_VERSION";
|
||||||
|
}
|
||||||
switch (os.platform()) {
|
switch (os.platform()) {
|
||||||
case "win32":
|
case "win32":
|
||||||
return "powershell.exe";
|
return "powershell.exe";
|
||||||
|
|||||||
Reference in New Issue
Block a user