11import {
22 containerRestart ,
3+ findServerById ,
34 getConfig ,
45 getContainers ,
56 getContainersByAppLabel ,
@@ -9,6 +10,9 @@ import {
910} from "@dokploy/server" ;
1011import { z } from "zod" ;
1112import { createTRPCRouter , protectedProcedure } from "../trpc" ;
13+ import { TRPCError } from "@trpc/server" ;
14+
15+ export const containerIdRegex = / ^ [ a - z A - Z 0 - 9 . \- _ ] + $ / ;
1216
1317export const dockerRouter = createTRPCRouter ( {
1418 getContainers : protectedProcedure
@@ -17,14 +21,23 @@ export const dockerRouter = createTRPCRouter({
1721 serverId : z . string ( ) . optional ( ) ,
1822 } ) ,
1923 )
20- . query ( async ( { input } ) => {
24+ . query ( async ( { input, ctx } ) => {
25+ if ( input . serverId ) {
26+ const server = await findServerById ( input . serverId ) ;
27+ if ( server . organizationId !== ctx . session ?. activeOrganizationId ) {
28+ throw new TRPCError ( { code : "UNAUTHORIZED" } ) ;
29+ }
30+ }
2131 return await getContainers ( input . serverId ) ;
2232 } ) ,
2333
2434 restartContainer : protectedProcedure
2535 . input (
2636 z . object ( {
27- containerId : z . string ( ) . min ( 1 ) ,
37+ containerId : z
38+ . string ( )
39+ . min ( 1 )
40+ . regex ( containerIdRegex , "Invalid container id." ) ,
2841 } ) ,
2942 )
3043 . mutation ( async ( { input } ) => {
@@ -34,11 +47,20 @@ export const dockerRouter = createTRPCRouter({
3447 getConfig : protectedProcedure
3548 . input (
3649 z . object ( {
37- containerId : z . string ( ) . min ( 1 ) ,
50+ containerId : z
51+ . string ( )
52+ . min ( 1 )
53+ . regex ( containerIdRegex , "Invalid container id." ) ,
3854 serverId : z . string ( ) . optional ( ) ,
3955 } ) ,
4056 )
41- . query ( async ( { input } ) => {
57+ . query ( async ( { input, ctx } ) => {
58+ if ( input . serverId ) {
59+ const server = await findServerById ( input . serverId ) ;
60+ if ( server . organizationId !== ctx . session ?. activeOrganizationId ) {
61+ throw new TRPCError ( { code : "UNAUTHORIZED" } ) ;
62+ }
63+ }
4264 return await getConfig ( input . containerId , input . serverId ) ;
4365 } ) ,
4466
@@ -48,11 +70,17 @@ export const dockerRouter = createTRPCRouter({
4870 appType : z
4971 . union ( [ z . literal ( "stack" ) , z . literal ( "docker-compose" ) ] )
5072 . optional ( ) ,
51- appName : z . string ( ) . min ( 1 ) ,
73+ appName : z . string ( ) . min ( 1 ) . regex ( containerIdRegex , "Invalid app name." ) ,
5274 serverId : z . string ( ) . optional ( ) ,
5375 } ) ,
5476 )
55- . query ( async ( { input } ) => {
77+ . query ( async ( { input, ctx } ) => {
78+ if ( input . serverId ) {
79+ const server = await findServerById ( input . serverId ) ;
80+ if ( server . organizationId !== ctx . session ?. activeOrganizationId ) {
81+ throw new TRPCError ( { code : "UNAUTHORIZED" } ) ;
82+ }
83+ }
5684 return await getContainersByAppNameMatch (
5785 input . appName ,
5886 input . appType ,
@@ -63,12 +91,18 @@ export const dockerRouter = createTRPCRouter({
6391 getContainersByAppLabel : protectedProcedure
6492 . input (
6593 z . object ( {
66- appName : z . string ( ) . min ( 1 ) ,
94+ appName : z . string ( ) . min ( 1 ) . regex ( containerIdRegex , "Invalid app name." ) ,
6795 serverId : z . string ( ) . optional ( ) ,
6896 type : z . enum ( [ "standalone" , "swarm" ] ) ,
6997 } ) ,
7098 )
71- . query ( async ( { input } ) => {
99+ . query ( async ( { input, ctx } ) => {
100+ if ( input . serverId ) {
101+ const server = await findServerById ( input . serverId ) ;
102+ if ( server . organizationId !== ctx . session ?. activeOrganizationId ) {
103+ throw new TRPCError ( { code : "UNAUTHORIZED" } ) ;
104+ }
105+ }
72106 return await getContainersByAppLabel (
73107 input . appName ,
74108 input . type ,
@@ -79,22 +113,34 @@ export const dockerRouter = createTRPCRouter({
79113 getStackContainersByAppName : protectedProcedure
80114 . input (
81115 z . object ( {
82- appName : z . string ( ) . min ( 1 ) ,
116+ appName : z . string ( ) . min ( 1 ) . regex ( containerIdRegex , "Invalid app name." ) ,
83117 serverId : z . string ( ) . optional ( ) ,
84118 } ) ,
85119 )
86- . query ( async ( { input } ) => {
120+ . query ( async ( { input, ctx } ) => {
121+ if ( input . serverId ) {
122+ const server = await findServerById ( input . serverId ) ;
123+ if ( server . organizationId !== ctx . session ?. activeOrganizationId ) {
124+ throw new TRPCError ( { code : "UNAUTHORIZED" } ) ;
125+ }
126+ }
87127 return await getStackContainersByAppName ( input . appName , input . serverId ) ;
88128 } ) ,
89129
90130 getServiceContainersByAppName : protectedProcedure
91131 . input (
92132 z . object ( {
93- appName : z . string ( ) . min ( 1 ) ,
133+ appName : z . string ( ) . min ( 1 ) . regex ( containerIdRegex , "Invalid app name." ) ,
94134 serverId : z . string ( ) . optional ( ) ,
95135 } ) ,
96136 )
97- . query ( async ( { input } ) => {
137+ . query ( async ( { input, ctx } ) => {
138+ if ( input . serverId ) {
139+ const server = await findServerById ( input . serverId ) ;
140+ if ( server . organizationId !== ctx . session ?. activeOrganizationId ) {
141+ throw new TRPCError ( { code : "UNAUTHORIZED" } ) ;
142+ }
143+ }
98144 return await getServiceContainersByAppName ( input . appName , input . serverId ) ;
99145 } ) ,
100146} ) ;
0 commit comments