mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-21 03:18:47 +00:00
feat: add organization-level authorization checks to WebSocket servers
- Implemented checks in the WebSocket server setups for Docker container logs, terminal, and deployment logs to ensure users can only access resources associated with their active organization. - Enhanced security by closing WebSocket connections if the organization ID does not match the session's active organization ID.
This commit is contained in:
@@ -11,20 +11,6 @@ import { audit } from "@/server/api/utils/audit";
|
|||||||
import { getLocalServerIp } from "@/server/wss/terminal";
|
import { getLocalServerIp } from "@/server/wss/terminal";
|
||||||
import { createTRPCRouter, withPermission } from "../trpc";
|
import { createTRPCRouter, withPermission } from "../trpc";
|
||||||
|
|
||||||
const assertServerBelongsToOrg = async (
|
|
||||||
serverId: string | undefined,
|
|
||||||
activeOrganizationId: string,
|
|
||||||
) => {
|
|
||||||
if (!serverId) return;
|
|
||||||
const targetServer = await findServerById(serverId);
|
|
||||||
if (targetServer.organizationId !== activeOrganizationId) {
|
|
||||||
throw new TRPCError({
|
|
||||||
code: "UNAUTHORIZED",
|
|
||||||
message: "You don't have access to this server.",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const clusterRouter = createTRPCRouter({
|
export const clusterRouter = createTRPCRouter({
|
||||||
getNodes: withPermission("server", "read")
|
getNodes: withPermission("server", "read")
|
||||||
.input(
|
.input(
|
||||||
@@ -33,10 +19,15 @@ export const clusterRouter = createTRPCRouter({
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.query(async ({ input, ctx }) => {
|
.query(async ({ input, ctx }) => {
|
||||||
await assertServerBelongsToOrg(
|
if (input.serverId) {
|
||||||
input.serverId,
|
const targetServer = await findServerById(input.serverId);
|
||||||
ctx.session.activeOrganizationId,
|
if (targetServer.organizationId !== ctx.session.activeOrganizationId) {
|
||||||
);
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "You don't have access to this server.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
const docker = await getRemoteDocker(input.serverId);
|
const docker = await getRemoteDocker(input.serverId);
|
||||||
const workers: DockerNode[] = await docker.listNodes();
|
const workers: DockerNode[] = await docker.listNodes();
|
||||||
return workers;
|
return workers;
|
||||||
@@ -50,10 +41,15 @@ export const clusterRouter = createTRPCRouter({
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
await assertServerBelongsToOrg(
|
if (input.serverId) {
|
||||||
input.serverId,
|
const targetServer = await findServerById(input.serverId);
|
||||||
ctx.session.activeOrganizationId,
|
if (targetServer.organizationId !== ctx.session.activeOrganizationId) {
|
||||||
);
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "You don't have access to this server.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const drainCommand = `docker node update --availability drain ${input.nodeId}`;
|
const drainCommand = `docker node update --availability drain ${input.nodeId}`;
|
||||||
const removeCommand = `docker node rm ${input.nodeId} --force`;
|
const removeCommand = `docker node rm ${input.nodeId} --force`;
|
||||||
@@ -88,10 +84,15 @@ export const clusterRouter = createTRPCRouter({
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.query(async ({ input, ctx }) => {
|
.query(async ({ input, ctx }) => {
|
||||||
await assertServerBelongsToOrg(
|
if (input.serverId) {
|
||||||
input.serverId,
|
const targetServer = await findServerById(input.serverId);
|
||||||
ctx.session.activeOrganizationId,
|
if (targetServer.organizationId !== ctx.session.activeOrganizationId) {
|
||||||
);
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "You don't have access to this server.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
const docker = await getRemoteDocker(input.serverId);
|
const docker = await getRemoteDocker(input.serverId);
|
||||||
const result = await docker.swarmInspect();
|
const result = await docker.swarmInspect();
|
||||||
const docker_version = await docker.version();
|
const docker_version = await docker.version();
|
||||||
@@ -115,10 +116,15 @@ export const clusterRouter = createTRPCRouter({
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.query(async ({ input, ctx }) => {
|
.query(async ({ input, ctx }) => {
|
||||||
await assertServerBelongsToOrg(
|
if (input.serverId) {
|
||||||
input.serverId,
|
const targetServer = await findServerById(input.serverId);
|
||||||
ctx.session.activeOrganizationId,
|
if (targetServer.organizationId !== ctx.session.activeOrganizationId) {
|
||||||
);
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "You don't have access to this server.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
const docker = await getRemoteDocker(input.serverId);
|
const docker = await getRemoteDocker(input.serverId);
|
||||||
const result = await docker.swarmInspect();
|
const result = await docker.swarmInspect();
|
||||||
const docker_version = await docker.version();
|
const docker_version = await docker.version();
|
||||||
|
|||||||
Reference in New Issue
Block a user