-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: redact private keys from logged command errors #4602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
weibeu
wants to merge
1
commit into
Dokploy:canary
Choose a base branch
from
weibeu:fix/redact-secrets-in-logs
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+81
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { redactSecrets } from "@dokploy/server/utils/process/redactSecrets"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| // All key material below is synthetic: these base64 strings decode to the | ||
| // literal text "synthetic-test-not-a-real-...-key" and are not real keys. | ||
|
|
||
| describe("redactSecrets", () => { | ||
| it("redacts a PEM private key block written to /tmp/id_rsa", () => { | ||
| const secret = "c3ludGhldGljLXRlc3Qtbm90LWEtcmVhbC1wcml2YXRlLWtleQ=="; | ||
| const command = | ||
| `echo "-----BEGIN OPENSSH PRIVATE KEY-----\n${secret}\n-----END OPENSSH PRIVATE KEY-----" > /tmp/id_rsa;` + | ||
| "chmod 600 /tmp/id_rsa;git clone --branch main --depth 1 git@example.com:org/repo /code"; | ||
|
|
||
| const redacted = redactSecrets(command); | ||
|
|
||
| expect(redacted).not.toContain(secret); | ||
| expect(redacted).toContain("[REDACTED PRIVATE KEY]"); | ||
| expect(redacted).toContain("chmod 600 /tmp/id_rsa"); | ||
| expect(redacted).toContain("git clone --branch main"); | ||
| }); | ||
|
|
||
| it("redacts a base64 key piped to base64 -d", () => { | ||
| const secret = "c3ludGhldGljLXRlc3Qtbm90LWEtcmVhbC1jZXJ0LWtleQ=="; | ||
| const command = `echo "${secret}" | base64 -d > "/etc/dokploy/cert.key";`; | ||
|
|
||
| const redacted = redactSecrets(command); | ||
|
|
||
| expect(redacted).not.toContain(secret); | ||
| expect(redacted).toContain('echo "[REDACTED]" | base64 -d'); | ||
| }); | ||
|
|
||
| it("leaves commands without secrets untouched", () => { | ||
| const command = | ||
| "git clone --branch main --depth 1 git@github.com:org/repo.git /tmp/code"; | ||
|
|
||
| expect(redactSecrets(command)).toBe(command); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Dokploy embeds some secrets directly into the shell commands it runs: the | ||
| // SSH key written to /tmp/id_rsa when cloning over SSH, and the base64 TLS key | ||
| // piped to `base64 -d` when provisioning certificates on a remote server. When | ||
| // such a command fails, its ExecError (command/stdout/stderr) is logged, which | ||
| // would otherwise persist the secret in plain text. These helpers strip that | ||
| // material before it can reach the logs. | ||
|
|
||
| const PRIVATE_KEY_BLOCK = | ||
| /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----/g; | ||
|
|
||
| const BASE64_DECODE_PIPE = /echo "[A-Za-z0-9+/=]+"\s*\|\s*base64 -d/g; | ||
|
|
||
| export const redactSecrets = (value: string): string => | ||
| value | ||
| .replace(PRIVATE_KEY_BLOCK, "[REDACTED PRIVATE KEY]") | ||
| .replace(BASE64_DECODE_PIPE, 'echo "[REDACTED]" | base64 -d'); | ||
|
|
||
| // Node's child_process errors repeat the failed command on `message`, `stack` | ||
| // and `cmd`, so redact those too when wrapping an original error. | ||
| export const redactErrorSecrets = <T extends Error>(error: T): T => { | ||
| const candidate = error as T & { cmd?: string }; | ||
| candidate.message = redactSecrets(candidate.message); | ||
| if (candidate.stack) { | ||
| candidate.stack = redactSecrets(candidate.stack); | ||
| } | ||
| if (candidate.cmd) { | ||
| candidate.cmd = redactSecrets(candidate.cmd); | ||
| } | ||
| return candidate; | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a local
execAsynccommand fails after writing a secret to stdout or stderr, the promisifiedchild_process.execerror passed asoriginalErrorcarries enumerablestdoutandstderrproperties.ExecErrornow redacts its own top-level output fields, butconsole.log(new ExecError(...))will still printoriginalError.stdout/stderr, so a failed command that emits a PEM or base64 key before exiting can still leak the same secret this change is trying to remove.Useful? React with 👍 / 👎.