Skip to content

Commit f8a3561

Browse files
fix(backup): redact S3 credentials from logs and error output (Dokploy#4648)
* fix(backup): redact S3 credentials from logs and error output (Dokploy#4621) S3 backup credentials (access key + secret) were logged in plaintext to Dokploy service stdout via logger.info in getBackupCommand() and console.error in keepLatestNBackups(). Any operator with access to service logs could recover S3 credentials. Added redactRcloneCredentials() pure function that masks --s3-access-key-id and --s3-secret-access-key values with [REDACTED]. Applied to both the structured logger call and the error handler. Closes Dokploy#4621 * fix(backups): redact sensitive information in error logs during web server backup process Updated error handling in the web server backup function to redact Rclone credentials from error messages before logging and notification. This change enhances security by preventing sensitive data exposure in logs. --------- Co-authored-by: Mauricio Siu <[email protected]>
1 parent 38de9ef commit f8a3561

5 files changed

Lines changed: 77 additions & 9 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { redactRcloneCredentials } from "@dokploy/server/utils/backups/redact";
2+
import { describe, expect, it } from "vitest";
3+
4+
describe("redactRcloneCredentials (#4621)", () => {
5+
it("should redact access key in rclone command", () => {
6+
const cmd =
7+
'rclone rcat --s3-access-key-id="AKIAIOSFODNN7EXAMPLE" --s3-secret-access-key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" :s3:bucket/file.gz';
8+
const redacted = redactRcloneCredentials(cmd);
9+
expect(redacted).not.toContain("AKIAIOSFODNN7EXAMPLE");
10+
expect(redacted).toContain('--s3-access-key-id="[REDACTED]"');
11+
});
12+
13+
it("should redact secret access key in rclone command", () => {
14+
const cmd =
15+
'rclone rcat --s3-access-key-id="key" --s3-secret-access-key="supersecret" :s3:bucket/file.gz';
16+
const redacted = redactRcloneCredentials(cmd);
17+
expect(redacted).not.toContain("supersecret");
18+
expect(redacted).toContain('--s3-secret-access-key="[REDACTED]"');
19+
});
20+
21+
it("should redact both credentials simultaneously", () => {
22+
const cmd =
23+
'rclone lsf --s3-access-key-id="AKIA123" --s3-secret-access-key="secret456" --s3-region="us-east-1" :s3:bucket/';
24+
const redacted = redactRcloneCredentials(cmd);
25+
expect(redacted).not.toContain("AKIA123");
26+
expect(redacted).not.toContain("secret456");
27+
expect(redacted).toContain('--s3-region="us-east-1"');
28+
});
29+
30+
it("should not modify non-credential flags", () => {
31+
const cmd =
32+
'rclone rcat --s3-region="eu-west-1" --s3-endpoint="https://s3.example.com" --s3-no-check-bucket :s3:bucket/file.gz';
33+
const redacted = redactRcloneCredentials(cmd);
34+
expect(redacted).toBe(cmd);
35+
});
36+
37+
it("should handle commands with no credentials", () => {
38+
const cmd = "rclone lsf :s3:bucket/";
39+
expect(redactRcloneCredentials(cmd)).toBe(cmd);
40+
});
41+
42+
it("should handle error strings containing credentials", () => {
43+
const errorStr =
44+
'Error: Command failed: rclone lsf --s3-access-key-id="MYKEY" --s3-secret-access-key="MYSECRET" :s3:bucket/';
45+
const redacted = redactRcloneCredentials(errorStr);
46+
expect(redacted).not.toContain("MYKEY");
47+
expect(redacted).not.toContain("MYSECRET");
48+
expect(redacted).toContain("[REDACTED]");
49+
});
50+
});

‎packages/server/src/utils/backups/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { startLogCleanup } from "../access-log/handler";
1111
import { cleanupAll } from "../docker/utils";
1212
import { sendDockerCleanupNotifications } from "../notifications/docker-cleanup";
1313
import { execAsync, execAsyncRemote } from "../process/execAsync";
14+
import { redactRcloneCredentials } from "./redact";
1415
import { getS3Credentials, normalizeS3Path, scheduleBackup } from "./utils";
1516

1617
export const initCronJobs = async () => {
@@ -153,6 +154,6 @@ export const keepLatestNBackups = async (
153154
await execAsync(rcloneCommand);
154155
}
155156
} catch (error) {
156-
console.error(error);
157+
console.error(redactRcloneCredentials(String(error)));
157158
}
158159
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Redacts S3 credentials from rclone command strings.
3+
*
4+
* Used to prevent credential leakage in structured logs and error output.
5+
* Matches the flag format produced by `getS3Credentials()`:
6+
* --s3-access-key-id="VALUE" and --s3-secret-access-key="VALUE"
7+
*/
8+
export const redactRcloneCredentials = (command: string): string => {
9+
return command
10+
.replace(/(--s3-access-key-id=)"[^"]*"/g, '$1"[REDACTED]"')
11+
.replace(/(--s3-secret-access-key=)"[^"]*"/g, '$1"[REDACTED]"');
12+
};

‎packages/server/src/utils/backups/utils.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { runMariadbBackup } from "./mariadb";
99
import { runMongoBackup } from "./mongo";
1010
import { runMySqlBackup } from "./mysql";
1111
import { runPostgresBackup } from "./postgres";
12+
import { redactRcloneCredentials } from "./redact";
1213
import { runWebServerBackup } from "./web-server";
1314

1415
export const scheduleBackup = (backup: BackupSchedule) => {
@@ -262,7 +263,7 @@ export const getBackupCommand = (
262263
{
263264
containerSearch,
264265
backupCommand,
265-
rcloneCommand,
266+
rcloneCommand: redactRcloneCredentials(rcloneCommand),
266267
logPath,
267268
},
268269
`Executing backup command: ${backup.databaseType} ${backup.backupType}`,

‎packages/server/src/utils/backups/web-server.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { findDestinationById } from "@dokploy/server/services/destination";
1212
import { sendDokployBackupNotifications } from "../notifications/dokploy-backup";
1313
import { execAsync } from "../process/execAsync";
14+
import { redactRcloneCredentials } from "./redact";
1415
import { getBackupTimestamp, getS3Credentials, normalizeS3Path } from "./utils";
1516

1617
function formatBytes(bytes?: number) {
@@ -113,20 +114,23 @@ export const runWebServerBackup = async (backup: BackupSchedule) => {
113114
try {
114115
await rm(tempDir, { recursive: true, force: true });
115116
} catch (cleanupError) {
116-
console.error("Cleanup error:", cleanupError);
117+
console.error(
118+
"Cleanup error:",
119+
redactRcloneCredentials(String(cleanupError)),
120+
);
117121
}
118122
}
119123
} catch (error) {
120-
console.error("Backup error:", error);
121-
writeStream.write("Backup error❌\n");
122-
writeStream.write(
123-
error instanceof Error ? error.message : "Unknown error\n",
124+
const safeErrorMessage = redactRcloneCredentials(
125+
error instanceof Error ? error.message : String(error),
124126
);
127+
console.error("Backup error:", redactRcloneCredentials(String(error)));
128+
writeStream.write("Backup error❌\n");
129+
writeStream.write(`${safeErrorMessage}\n`);
125130
writeStream.end();
126131
await sendDokployBackupNotifications({
127132
type: "error",
128-
// @ts-ignore
129-
errorMessage: error?.message || "Error message not provided",
133+
errorMessage: safeErrorMessage || "Error message not provided",
130134
backupSize: formatBytes(computedBackupSize),
131135
});
132136
await updateDeploymentStatus(deployment.deploymentId, "error");

0 commit comments

Comments
 (0)