Skip to content

Commit c968a27

Browse files
fix: strip credentials from service-level API responses (Dokploy#4564)
* fix: strip credentials from service-level API responses Registry passwords and S3 destination credentials were being returned in service `.one` tRPC endpoints to any user with service-level read access. Reported by Nihon Kohden Corporation security team. - Strip registry `password` from `findApplicationById` via Drizzle `columns: { password: false }` - Strip destination `accessKey`/`secretAccessKey` from all DB service finders (postgres, mysql, mariadb, mongo, libsql, compose, backup, volume-backups) - Add `findRegistryByIdWithCredentials` for internal use only - Builders and upload utils now load registry credentials by ID at execution time - `createRollback` enriches `fullContext` with registry credentials before persisting to DB so rollback execution has what it needs - Remove `findApplicationByIdWithCredentials` and `ApplicationNestedWithCredentials` — no longer needed - Backup execution utils load full destination via `findDestinationById` at runtime instead of reading from the joined relation * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent f35f306 commit c968a27

22 files changed

Lines changed: 169 additions & 82 deletions

File tree

‎packages/server/src/services/application.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,22 @@ export const findApplicationById = async (applicationId: string) => {
9595
const application = await db.query.applications.findFirst({
9696
where: eq(applications.applicationId, applicationId),
9797
with: {
98-
environment: {
99-
with: {
100-
project: true,
101-
},
102-
},
98+
environment: { with: { project: true } },
10399
domains: true,
104100
deployments: true,
105101
mounts: true,
106102
redirects: true,
107103
security: true,
108104
ports: true,
109-
registry: true,
110105
gitlab: true,
111106
github: true,
112107
bitbucket: true,
113108
gitea: true,
114109
server: true,
115110
previewDeployments: true,
116-
buildRegistry: true,
117-
rollbackRegistry: true,
111+
registry: { columns: { password: false } },
112+
buildRegistry: { columns: { password: false } },
113+
rollbackRegistry: { columns: { password: false } },
118114
},
119115
});
120116
if (!application) {

‎packages/server/src/services/backup.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ export const findBackupById = async (backupId: string) => {
3434
mariadb: true,
3535
mongo: true,
3636
libsql: true,
37-
destination: true,
37+
destination: {
38+
columns: {
39+
accessKey: false,
40+
secretAccessKey: false,
41+
},
42+
},
3843
compose: true,
3944
},
4045
});
@@ -83,7 +88,12 @@ export const findBackupsByDbId = async (
8388
mariadb: true,
8489
mongo: true,
8590
libsql: true,
86-
destination: true,
91+
destination: {
92+
columns: {
93+
accessKey: false,
94+
secretAccessKey: false,
95+
},
96+
},
8797
},
8898
});
8999
return result || [];

‎packages/server/src/services/compose.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ export const findComposeById = async (composeId: string) => {
131131
server: true,
132132
backups: {
133133
with: {
134-
destination: true,
134+
destination: {
135+
columns: {
136+
accessKey: false,
137+
secretAccessKey: false,
138+
},
139+
},
135140
deployments: true,
136141
},
137142
},

‎packages/server/src/services/libsql.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ export const findLibsqlById = async (libsqlId: string) => {
6363
server: true,
6464
backups: {
6565
with: {
66-
destination: true,
66+
destination: {
67+
columns: {
68+
accessKey: false,
69+
secretAccessKey: false,
70+
},
71+
},
6772
deployments: true,
6873
},
6974
},

‎packages/server/src/services/mariadb.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ export const findMariadbById = async (mariadbId: string) => {
6868
server: true,
6969
backups: {
7070
with: {
71-
destination: true,
71+
destination: {
72+
columns: {
73+
accessKey: false,
74+
secretAccessKey: false,
75+
},
76+
},
7277
deployments: true,
7378
},
7479
},

‎packages/server/src/services/mongo.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ export const findMongoById = async (mongoId: string) => {
6363
server: true,
6464
backups: {
6565
with: {
66-
destination: true,
66+
destination: {
67+
columns: {
68+
accessKey: false,
69+
secretAccessKey: false,
70+
},
71+
},
6772
deployments: true,
6873
},
6974
},

‎packages/server/src/services/mysql.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ export const findMySqlById = async (mysqlId: string) => {
6666
server: true,
6767
backups: {
6868
with: {
69-
destination: true,
69+
destination: {
70+
columns: {
71+
accessKey: false,
72+
secretAccessKey: false,
73+
},
74+
},
7075
deployments: true,
7176
},
7277
},

‎packages/server/src/services/postgres.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ export const findPostgresById = async (postgresId: string) => {
7676
server: true,
7777
backups: {
7878
with: {
79-
destination: true,
79+
destination: {
80+
columns: {
81+
accessKey: false,
82+
secretAccessKey: false,
83+
},
84+
},
8085
deployments: true,
8186
},
8287
},

‎packages/server/src/services/registry.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ export const findRegistryById = async (registryId: string) => {
162162
return registryResponse;
163163
};
164164

165+
export const findRegistryByIdWithCredentials = async (registryId: string) => {
166+
const registryResponse = await db.query.registry.findFirst({
167+
where: eq(registry.registryId, registryId),
168+
});
169+
if (!registryResponse) {
170+
throw new TRPCError({
171+
code: "NOT_FOUND",
172+
message: "Registry not found",
173+
});
174+
}
175+
return registryResponse;
176+
};
177+
165178
export const findAllRegistryByOrganizationId = async (
166179
organizationId: string,
167180
) => {

‎packages/server/src/services/rollbacks.ts‎

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
deployments as deploymentsSchema,
88
rollbacks,
99
} from "../db/schema";
10-
import type { ApplicationNested } from "../utils/builders";
1110
import { getRegistryTag } from "../utils/cluster/upload";
1211
import {
1312
calculateResources,
@@ -23,7 +22,11 @@ import { findDeploymentById } from "./deployment";
2322
import type { Mount } from "./mount";
2423
import type { Port } from "./port";
2524
import type { Project } from "./project";
26-
import { type Registry, safeDockerLoginCommand } from "./registry";
25+
import {
26+
findRegistryByIdWithCredentials,
27+
type Registry,
28+
safeDockerLoginCommand,
29+
} from "./registry";
2730

2831
export const createRollback = async (
2932
input: z.infer<typeof createRollbackSchema>,
@@ -56,11 +59,29 @@ export const createRollback = async (
5659
...rest
5760
} = await findApplicationById(deployment.applicationId);
5861

62+
const registry = rest.registryId
63+
? await findRegistryByIdWithCredentials(rest.registryId)
64+
: rest.registry;
65+
const buildRegistry = rest.buildRegistryId
66+
? await findRegistryByIdWithCredentials(rest.buildRegistryId)
67+
: rest.buildRegistry;
68+
const rollbackRegistry = rest.rollbackRegistryId
69+
? await findRegistryByIdWithCredentials(rest.rollbackRegistryId)
70+
: rest.rollbackRegistry;
71+
72+
const fullContextWithCredentials = {
73+
...rest,
74+
registry,
75+
buildRegistry,
76+
rollbackRegistry,
77+
};
78+
5979
await tx
6080
.update(rollbacks)
6181
.set({
6282
image: tagImage,
63-
fullContext: rest,
83+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
84+
fullContext: fullContextWithCredentials as any,
6485
})
6586
.where(eq(rollbacks.rollbackId, rollback.rollbackId));
6687

@@ -162,7 +183,6 @@ export const rollback = async (rollbackId: string) => {
162183
if (!result.fullContext) {
163184
throw new Error("Rollback context not found");
164185
}
165-
// Use the full context for rollback
166186
await rollbackApplication(
167187
application.appName,
168188
result.image || "",
@@ -198,24 +218,25 @@ const rollbackApplication = async (
198218
};
199219
mounts: Mount[];
200220
ports: Port[];
201-
rollbackRegistry?: Registry;
221+
rollbackRegistry?: Registry | null;
202222
},
203223
) => {
204224
if (!fullContext) {
205225
throw new Error("Full context is required for rollback");
206226
}
207227

228+
const rollbackRegistry = fullContext.rollbackRegistry ?? undefined;
229+
208230
// Ensure Docker daemon is authenticated with the rollback registry
209231
// before updating the swarm service. The authconfig in CreateServiceOptions
210232
// alone is not sufficient — Docker Swarm also relies on the daemon's
211233
// cached credentials (~/.docker/config.json) to distribute auth to nodes.
212-
if (fullContext.rollbackRegistry) {
213-
await dockerLoginForRegistry(fullContext.rollbackRegistry, serverId);
234+
if (rollbackRegistry) {
235+
await dockerLoginForRegistry(rollbackRegistry, serverId);
214236
}
215237

216238
const docker = await getRemoteDocker(serverId);
217239

218-
// Use the same configuration as mechanizeDockerContainer
219240
const {
220241
env,
221242
mounts,
@@ -246,26 +267,26 @@ const rollbackApplication = async (
246267
UpdateConfig,
247268
Networks,
248269
Ulimits,
249-
} = generateConfigContainer(fullContext as ApplicationNested);
270+
} = generateConfigContainer(
271+
fullContext as Parameters<typeof generateConfigContainer>[0],
272+
);
250273

251274
const bindsMount = generateBindMounts(mounts);
252275
const envVariables = prepareEnvironmentVariables(
253276
env,
254277
fullContext.environment.project.env,
255278
);
256279

257-
// Build the full registry image path if rollbackRegistry is available
258-
// e.g., "appName:v5" -> "siumauricio/appName:v5" or "registry.com/prefix/appName:v5"
259280
let rollbackImage = image;
260-
if (fullContext.rollbackRegistry) {
261-
rollbackImage = getRegistryTag(fullContext.rollbackRegistry, image);
281+
if (rollbackRegistry) {
282+
rollbackImage = getRegistryTag(rollbackRegistry, image);
262283
}
263284

264285
const settings: CreateServiceOptions = {
265286
authconfig: {
266-
password: fullContext.rollbackRegistry?.password || "",
267-
username: fullContext.rollbackRegistry?.username || "",
268-
serveraddress: fullContext.rollbackRegistry?.registryUrl || "",
287+
password: rollbackRegistry?.password || "",
288+
username: rollbackRegistry?.username || "",
289+
serveraddress: rollbackRegistry?.registryUrl || "",
269290
},
270291
Name: appName,
271292
TaskTemplate: {

0 commit comments

Comments
 (0)