Skip to content

Commit 6215c7f

Browse files
committed
fix: redact S3 credentials in backup logs regardless of shell quoting
1 parent a0e0574 commit 6215c7f

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎apps/dokploy/__test__/backups/redact-credentials.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import type { Destination } from "@dokploy/server/services/destination";
12
import { redactRcloneCredentials } from "@dokploy/server/utils/backups/redact";
3+
import { getS3Credentials } from "@dokploy/server/utils/backups/utils";
24
import { describe, expect, it } from "vitest";
35

46
describe("redactRcloneCredentials (#4621)", () => {
@@ -48,3 +50,48 @@ describe("redactRcloneCredentials (#4621)", () => {
4850
expect(redacted).toContain("[REDACTED]");
4951
});
5052
});
53+
54+
describe("redactRcloneCredentials with getS3Credentials output (#5519)", () => {
55+
const buildCommand = (accessKey: string, secretAccessKey: string) =>
56+
`rclone rcat ${getS3Credentials({
57+
accessKey,
58+
secretAccessKey,
59+
region: "us-west-001",
60+
endpoint: "https://s3.us-west-001.backblazeb2.com",
61+
provider: "Other",
62+
} as Destination).join(" ")} :s3:bucket/file.gz`;
63+
64+
it.each([
65+
[
66+
"plain alphanumeric",
67+
"001aaaabbbbccccdd0000000001",
68+
"K001FAKEfakeFAKEfake",
69+
],
70+
[
71+
"containing slashes",
72+
"AKIAIOSFODNN7EXAMPLE",
73+
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
74+
],
75+
["containing spaces", "key with space", "secret with space"],
76+
["containing quotes", "it's-a-key", `say "hi" it's`],
77+
["containing shell metacharacters", "key$HOME", "sec;ret`id`&|\\x"],
78+
])("redacts credentials %s", (_, accessKey, secretAccessKey) => {
79+
const redacted = redactRcloneCredentials(
80+
buildCommand(accessKey, secretAccessKey),
81+
);
82+
expect(redacted).toContain(
83+
'--s3-access-key-id="[REDACTED]" --s3-secret-access-key="[REDACTED]" --s3-region=us-west-001',
84+
);
85+
expect(redacted).not.toContain(accessKey);
86+
expect(redacted).not.toContain(secretAccessKey);
87+
expect(redacted).toContain(":s3:bucket/file.gz");
88+
});
89+
90+
it("redacts credentials embedded in an error string", () => {
91+
const errorStr = `Error: Command failed: ${buildCommand("MYKEY", "MY/SECRET")}`;
92+
const redacted = redactRcloneCredentials(errorStr);
93+
expect(redacted).not.toContain("MYKEY");
94+
expect(redacted).not.toContain("MY/SECRET");
95+
expect(redacted).toContain("[REDACTED]");
96+
});
97+
});
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
// A shell word as produced by shell-quote: bare, 'single' or "double" quoted.
2+
const SHELL_WORD = String.raw`(?:[^\s'"\\]|\\.|'[^']*'|"(?:[^"\\]|\\.)*")+`;
3+
4+
const ACCESS_KEY_PATTERN = new RegExp(
5+
`(--s3-access-key-id=)${SHELL_WORD}`,
6+
"g",
7+
);
8+
const SECRET_KEY_PATTERN = new RegExp(
9+
`(--s3-secret-access-key=)${SHELL_WORD}`,
10+
"g",
11+
);
12+
113
/**
214
* Redacts S3 credentials from rclone command strings.
315
*
416
* Used to prevent credential leakage in structured logs and error output.
517
* Matches the flag format produced by `getS3Credentials()`:
6-
* --s3-access-key-id="VALUE" and --s3-secret-access-key="VALUE"
18+
* --s3-access-key-id=VALUE and --s3-secret-access-key=VALUE (shell-quoted)
719
*/
820
export const redactRcloneCredentials = (command: string): string => {
921
return command
10-
.replace(/(--s3-access-key-id=)"[^"]*"/g, '$1"[REDACTED]"')
11-
.replace(/(--s3-secret-access-key=)"[^"]*"/g, '$1"[REDACTED]"');
22+
.replace(ACCESS_KEY_PATTERN, '$1"[REDACTED]"')
23+
.replace(SECRET_KEY_PATTERN, '$1"[REDACTED]"');
1224
};

0 commit comments

Comments
 (0)