mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-30 15:18:46 +00:00
fix(security): escape file paths and remote schedule command in shell invocations
quote() the user-derived paths that reach the shell in file mounts (mount.ts, docker getCreateFileCommand), patch repo read (repoPath/filePath), certificate create/remove (certificatePath), and the remote scheduled command (containerId/shellType/command/logPath), so $(), backticks and traversal in these fields can no longer inject commands.
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
|||||||
import { removeDirectoryIfExistsContent } from "@dokploy/server/utils/filesystem/directory";
|
import { removeDirectoryIfExistsContent } from "@dokploy/server/utils/filesystem/directory";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
|
import { quote } from "shell-quote";
|
||||||
import { stringify } from "yaml";
|
import { stringify } from "yaml";
|
||||||
import type { z } from "zod";
|
import type { z } from "zod";
|
||||||
import { encodeBase64 } from "../utils/docker/utils";
|
import { encodeBase64 } from "../utils/docker/utils";
|
||||||
@@ -63,7 +64,7 @@ export const removeCertificateById = async (certificateId: string) => {
|
|||||||
const certDir = path.join(CERTIFICATES_PATH, certificate.certificatePath);
|
const certDir = path.join(CERTIFICATES_PATH, certificate.certificatePath);
|
||||||
|
|
||||||
if (certificate.serverId) {
|
if (certificate.serverId) {
|
||||||
await execAsyncRemote(certificate.serverId, `rm -rf ${certDir}`);
|
await execAsyncRemote(certificate.serverId, `rm -rf ${quote([certDir])}`);
|
||||||
} else {
|
} else {
|
||||||
await removeDirectoryIfExistsContent(certDir);
|
await removeDirectoryIfExistsContent(certDir);
|
||||||
}
|
}
|
||||||
@@ -108,10 +109,10 @@ const createCertificateFiles = async (certificate: Certificate) => {
|
|||||||
const certificateData = encodeBase64(certificate.certificateData);
|
const certificateData = encodeBase64(certificate.certificateData);
|
||||||
const privateKey = encodeBase64(certificate.privateKey);
|
const privateKey = encodeBase64(certificate.privateKey);
|
||||||
const command = `
|
const command = `
|
||||||
mkdir -p ${certDir};
|
mkdir -p ${quote([certDir])};
|
||||||
echo "${certificateData}" | base64 -d > "${crtPath}";
|
echo "${certificateData}" | base64 -d > ${quote([crtPath])};
|
||||||
echo "${privateKey}" | base64 -d > "${keyPath}";
|
echo "${privateKey}" | base64 -d > ${quote([keyPath])};
|
||||||
echo "${yamlConfig}" > "${configFile}";
|
echo "${yamlConfig}" > ${quote([configFile])};
|
||||||
`;
|
`;
|
||||||
|
|
||||||
await execAsyncRemote(certificate.serverId, command);
|
await execAsyncRemote(certificate.serverId, command);
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import {
|
|||||||
} from "@dokploy/server/utils/process/execAsync";
|
} from "@dokploy/server/utils/process/execAsync";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
import { eq, type SQL, sql } from "drizzle-orm";
|
import { eq, type SQL, sql } from "drizzle-orm";
|
||||||
|
import { quote } from "shell-quote";
|
||||||
import type { z } from "zod";
|
import type { z } from "zod";
|
||||||
|
|
||||||
export type Mount = typeof mounts.$inferSelect;
|
export type Mount = typeof mounts.$inferSelect;
|
||||||
@@ -317,7 +318,7 @@ export const updateFileMount = async (mountId: string) => {
|
|||||||
try {
|
try {
|
||||||
const serverId = await getServerId(mount);
|
const serverId = await getServerId(mount);
|
||||||
const encodedContent = encodeBase64(mount.content || "");
|
const encodedContent = encodeBase64(mount.content || "");
|
||||||
const command = `echo "${encodedContent}" | base64 -d > ${fullPath}`;
|
const command = `echo "${encodedContent}" | base64 -d > ${quote([fullPath])}`;
|
||||||
if (serverId) {
|
if (serverId) {
|
||||||
await execAsyncRemote(serverId, command);
|
await execAsyncRemote(serverId, command);
|
||||||
} else {
|
} else {
|
||||||
@@ -337,7 +338,7 @@ export const deleteFileMount = async (mountId: string) => {
|
|||||||
try {
|
try {
|
||||||
const serverId = await getServerId(mount);
|
const serverId = await getServerId(mount);
|
||||||
if (serverId) {
|
if (serverId) {
|
||||||
const command = `rm -rf ${fullPath}`;
|
const command = `rm -rf ${quote([fullPath])}`;
|
||||||
await execAsyncRemote(serverId, command);
|
await execAsyncRemote(serverId, command);
|
||||||
} else {
|
} else {
|
||||||
await removeFileOrDirectory(fullPath);
|
await removeFileOrDirectory(fullPath);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { paths } from "@dokploy/server/constants";
|
import { paths } from "@dokploy/server/constants";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
|
import { quote } from "shell-quote";
|
||||||
import { execAsync, execAsyncRemote } from "../utils/process/execAsync";
|
import { execAsync, execAsyncRemote } from "../utils/process/execAsync";
|
||||||
import { cloneBitbucketRepository } from "../utils/providers/bitbucket";
|
import { cloneBitbucketRepository } from "../utils/providers/bitbucket";
|
||||||
import { cloneGitRepository } from "../utils/providers/git";
|
import { cloneGitRepository } from "../utils/providers/git";
|
||||||
@@ -85,7 +86,7 @@ export const readPatchRepoDirectory = async (
|
|||||||
serverId?: string | null,
|
serverId?: string | null,
|
||||||
): Promise<DirectoryEntry[]> => {
|
): Promise<DirectoryEntry[]> => {
|
||||||
// Use git ls-tree to get tracked files only
|
// Use git ls-tree to get tracked files only
|
||||||
const command = `cd "${repoPath}" && git ls-tree -r --name-only HEAD`;
|
const command = `cd ${quote([repoPath])} && git ls-tree -r --name-only HEAD`;
|
||||||
|
|
||||||
let stdout: string;
|
let stdout: string;
|
||||||
try {
|
try {
|
||||||
@@ -168,7 +169,7 @@ export const readPatchRepoFile = async (
|
|||||||
const repoPath = join(PATCH_REPOS_PATH, type, application.appName);
|
const repoPath = join(PATCH_REPOS_PATH, type, application.appName);
|
||||||
const fullPath = join(repoPath, filePath);
|
const fullPath = join(repoPath, filePath);
|
||||||
|
|
||||||
const command = `cat "${fullPath}"`;
|
const command = `cat ${quote([fullPath])}`;
|
||||||
|
|
||||||
if (serverId) {
|
if (serverId) {
|
||||||
const result = await execAsyncRemote(serverId, command);
|
const result = await execAsyncRemote(serverId, command);
|
||||||
|
|||||||
@@ -712,14 +712,14 @@ export const getCreateFileCommand = (
|
|||||||
) => {
|
) => {
|
||||||
const fullPath = path.join(outputPath, filePath);
|
const fullPath = path.join(outputPath, filePath);
|
||||||
if (fullPath.endsWith(path.sep) || filePath.endsWith("/")) {
|
if (fullPath.endsWith(path.sep) || filePath.endsWith("/")) {
|
||||||
return `mkdir -p ${fullPath};`;
|
return `mkdir -p ${quote([fullPath])};`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const directory = path.dirname(fullPath);
|
const directory = path.dirname(fullPath);
|
||||||
const encodedContent = encodeBase64(content);
|
const encodedContent = encodeBase64(content);
|
||||||
return `
|
return `
|
||||||
mkdir -p ${directory};
|
mkdir -p ${quote([directory])};
|
||||||
echo "${encodedContent}" | base64 -d > "${fullPath}";
|
echo "${encodedContent}" | base64 -d > ${quote([fullPath])};
|
||||||
`;
|
`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
} from "@dokploy/server/services/deployment";
|
} from "@dokploy/server/services/deployment";
|
||||||
import { findScheduleById } from "@dokploy/server/services/schedule";
|
import { findScheduleById } from "@dokploy/server/services/schedule";
|
||||||
import { scheduledJobs, scheduleJob as scheduleJobNode } from "node-schedule";
|
import { scheduledJobs, scheduleJob as scheduleJobNode } from "node-schedule";
|
||||||
|
import { quote } from "shell-quote";
|
||||||
import { getComposeContainer, getServiceContainer } from "../docker/utils";
|
import { getComposeContainer, getServiceContainer } from "../docker/utils";
|
||||||
import { execAsyncRemote } from "../process/execAsync";
|
import { execAsyncRemote } from "../process/execAsync";
|
||||||
import { spawnAsync } from "../process/spawnAsync";
|
import { spawnAsync } from "../process/spawnAsync";
|
||||||
@@ -77,12 +78,12 @@ export const runCommand = async (scheduleId: string) => {
|
|||||||
serverId,
|
serverId,
|
||||||
`
|
`
|
||||||
set -e
|
set -e
|
||||||
echo "Running command: docker exec ${containerId} ${shellType} -c '${command}'" >> ${deployment.logPath};
|
echo "Running scheduled command" >> ${quote([deployment.logPath])};
|
||||||
docker exec ${containerId} ${shellType} -c '${command}' >> ${deployment.logPath} 2>> ${deployment.logPath} || {
|
docker exec ${quote([containerId])} ${quote([shellType])} -c ${quote([command])} >> ${quote([deployment.logPath])} 2>> ${quote([deployment.logPath])} || {
|
||||||
echo "❌ Command failed" >> ${deployment.logPath};
|
echo "❌ Command failed" >> ${quote([deployment.logPath])};
|
||||||
exit 1;
|
exit 1;
|
||||||
}
|
}
|
||||||
echo "✅ Command executed successfully" >> ${deployment.logPath};
|
echo "✅ Command executed successfully" >> ${quote([deployment.logPath])};
|
||||||
`,
|
`,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user