Files
dokploy/apps/dokploy/server/api/routers/swarm.ts
Mauricio Siu 5563699f71 fix(security): enforce org-scope on swarm reads and escape nodeId
swarm.getNodes/getNodeInfo/getNodeApps/getAppInfos accepted a caller-supplied
serverId and ran remote Docker Swarm reads against it with no organization
check, exposing another tenant's swarm topology cross-org (getContainerStats
already had the check; the others did not). getNodeInfo additionally
interpolated nodeId unescaped into 'docker node inspect ${nodeId}'.

Adds an org-ownership assertion to the four unscoped handlers and passes nodeId
through shell-quote in getNodeInfo.

Closes GHSA-jj6h-388v-9rwm
2026-07-19 21:20:47 -06:00

109 lines
2.8 KiB
TypeScript

import {
findServerById,
getAllContainerStats,
getApplicationInfo,
getNodeApplications,
getNodeInfo,
getSwarmNodes,
} from "@dokploy/server";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { createTRPCRouter, withPermission } from "../trpc";
import { containerIdRegex } from "./docker";
// Ensures a caller-supplied serverId belongs to the caller's active
// organization before any Swarm read runs against it. Without this, the
// server-scoped read procedures were reachable cross-organization (IDOR).
const assertServerInActiveOrg = async (
serverId: string | undefined,
activeOrganizationId: string | undefined,
) => {
if (!serverId) return;
const server = await findServerById(serverId);
if (server.organizationId !== activeOrganizationId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this server",
});
}
};
export const swarmRouter = createTRPCRouter({
getNodes: withPermission("server", "read")
.input(
z.object({
serverId: z.string().optional(),
}),
)
.query(async ({ input, ctx }) => {
await assertServerInActiveOrg(
input.serverId,
ctx.session?.activeOrganizationId,
);
return await getSwarmNodes(input.serverId);
}),
getNodeInfo: withPermission("server", "read")
.input(z.object({ nodeId: z.string(), serverId: z.string().optional() }))
.query(async ({ input, ctx }) => {
await assertServerInActiveOrg(
input.serverId,
ctx.session?.activeOrganizationId,
);
return await getNodeInfo(input.nodeId, input.serverId);
}),
getNodeApps: withPermission("server", "read")
.input(
z.object({
serverId: z.string().optional(),
}),
)
.query(async ({ input, ctx }) => {
await assertServerInActiveOrg(
input.serverId,
ctx.session?.activeOrganizationId,
);
return getNodeApplications(input.serverId);
}),
getAppInfos: withPermission("server", "read")
.meta({
openapi: {
path: "/drop-deployment",
method: "POST",
override: true,
enabled: false,
},
})
.input(
z.object({
appName: z
.string()
.min(1)
.regex(containerIdRegex, "Invalid app name.")
.array(),
serverId: z.string().optional(),
}),
)
.query(async ({ input, ctx }) => {
await assertServerInActiveOrg(
input.serverId,
ctx.session?.activeOrganizationId,
);
return await getApplicationInfo(input.appName, input.serverId);
}),
getContainerStats: withPermission("server", "read")
.input(
z.object({
serverId: z.string().optional(),
}),
)
.query(async ({ input, ctx }) => {
if (input.serverId) {
const server = await findServerById(input.serverId);
if (server.organizationId !== ctx.session?.activeOrganizationId) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
}
return await getAllContainerStats(input.serverId);
}),
});