From 5dd9fbc58b1e2379c4222c6fde4e6487a61ecc82 Mon Sep 17 00:00:00 2001 From: Samuel K <69881238+skevetter@users.noreply.github.com> Date: Sun, 27 Sep 2026 21:50:21 -0600 Subject: [PATCH 1/4] fix(secrets): stop masking low-entropy env values as literal substrings Redaction replaces a secret value as a literal substring wherever it appears, with no minimum length. A one-character value under a credential-bearing key therefore masked that character everywhere in captured subprocess output: "v1.2.3" became "v***.2.3" and a docker volume mountpoint became a path that does not exist, so writing the agent binary into the volume failed with ENOENT. Skip values below minRedactableValueLength and report them through SkippedValueCount, so a genuinely short secret is surfaced rather than silently left unmasked. The Redactor already computed this length for its replacer but never used it. Two existing tests used sub-threshold fixtures ("hunter2", "sec"); they now use realistic values that still exercise the behavior they cover. Co-Authored-By: Claude Opus 4.8 (1M context) --- pkg/secrets/redact.go | 43 +++++++++++++++++---- pkg/secrets/redact_test.go | 77 +++++++++++++++++++++++++++++++++++--- 2 files changed, 107 insertions(+), 13 deletions(-) diff --git a/pkg/secrets/redact.go b/pkg/secrets/redact.go index 1223030cf5..9c1e2a492a 100644 --- a/pkg/secrets/redact.go +++ b/pkg/secrets/redact.go @@ -10,6 +10,14 @@ import ( const redactMask = "***" +// minRedactableValueLength is the shortest value that can be masked safely. +// Masking replaces the value as a literal substring, so a very short value +// matches unrelated text everywhere it appears: a single-character secret +// would corrupt every version string, path, and port number in captured +// output rather than protect anything. Values below this length are left +// unmasked; SkippedValueCount reports them so callers can surface the gap. +const minRedactableValueLength = 8 + var ( credentialURLPattern = regexp.MustCompile(`(?i)(https?://)[^\s/@]+@`) authorizationPattern = regexp.MustCompile( @@ -18,23 +26,31 @@ var ( ) type Redactor struct { - replacer *strings.Replacer - values []string - maxLength int + replacer *strings.Replacer + values []string + // skipped counts sensitive values too short to mask without corrupting + // unrelated text. + skipped int } -// NewRedactor masks the values (not keys) of KEY=VALUE entries; empty values are ignored. +// NewRedactor masks the values (not keys) of KEY=VALUE entries; empty values +// are ignored, as are values shorter than minRedactableValueLength. func NewRedactor(secretsEnv []string) *Redactor { values := make([]string, 0, len(secretsEnv)) + skipped := 0 for _, entry := range secretsEnv { _, value, ok := strings.Cut(entry, "=") if !ok || value == "" { continue } + if len(value) < minRedactableValueLength { + skipped++ + continue + } values = append(values, value) } if len(values) == 0 { - return &Redactor{} + return &Redactor{skipped: skipped} } // Mask longer values first so an overlapping prefix (e.g. "sec" of "secret") @@ -46,9 +62,9 @@ func NewRedactor(secretsEnv []string) *Redactor { } return &Redactor{ - replacer: strings.NewReplacer(pairs...), - values: values, - maxLength: len(values[0]), + replacer: strings.NewReplacer(pairs...), + values: values, + skipped: skipped, } } @@ -81,6 +97,17 @@ func NewEnvironmentRedactor(env []string) *Redactor { return NewRedactor(sensitive) } +// SkippedValueCount reports how many sensitive values were too short to mask. +// A non-zero count means at least one credential-bearing value is present in +// the environment but is not being redacted, so callers that emit captured +// output may want to warn about it. +func (r *Redactor) SkippedValueCount() int { + if r == nil { + return 0 + } + return r.skipped +} + func isSensitiveEnvironmentKey(key string) bool { key = strings.ToUpper(key) for _, marker := range []string{ diff --git a/pkg/secrets/redact_test.go b/pkg/secrets/redact_test.go index f2d5b2d568..3ce2c0bc08 100644 --- a/pkg/secrets/redact_test.go +++ b/pkg/secrets/redact_test.go @@ -7,16 +7,83 @@ import ( "github.com/devsy-org/devsy/pkg/secrets" ) +const ( + redactTestSecret = "sk-live-abcdef123456" + redactTestKey = "DEVSY_API_KEY=" + redactTestSecret +) + func TestRedactor_MasksValues(t *testing.T) { - r := secrets.NewRedactor([]string{"DB_PASSWORD=hunter2", "TOKEN=abc123"}) + r := secrets.NewRedactor([]string{ + "DB_PASSWORD=hunter2-correct-horse", + "TOKEN=abc123def456", + }) - got := r.Redact("connecting with hunter2 and token abc123") + got := r.Redact("connecting with hunter2-correct-horse and token abc123def456") want := "connecting with *** and token ***" if got != want { t.Errorf("Redact = %q, want %q", got, want) } } +// A value too short to mask safely is left alone rather than applied as a +// literal substring, because masking it would corrupt unrelated text +// everywhere it appears. +func TestRedactor_SkipsTooShortValues(t *testing.T) { + r := secrets.NewRedactor([]string{"DEVSY_AUTH_BOOTSTRAP=1"}) + + got := r.Redact("version v1.2.3 in /var/run/001/mount") + if want := "version v1.2.3 in /var/run/001/mount"; got != want { + t.Errorf("Redact = %q, want short value left unmasked (%q)", got, want) + } + if got := r.SkippedValueCount(); got != 1 { + t.Errorf("SkippedValueCount = %d, want 1", got) + } +} + +// Maskable values alongside a too-short one are still masked, and only the +// short value is reported as skipped. +func TestRedactor_MasksLongValuesAlongsideShortOnes(t *testing.T) { + r := secrets.NewRedactor([]string{ + "DEVSY_AUTH_BOOTSTRAP=1", + redactTestKey, + }) + + got := r.Redact("key=sk-live-abcdef123456 bootstrap=1") + if want := "key=*** bootstrap=1"; got != want { + t.Errorf("Redact = %q, want %q", got, want) + } + if got := r.SkippedValueCount(); got != 1 { + t.Errorf("SkippedValueCount = %d, want 1", got) + } +} + +func TestRedactor_SkippedValueCountIsZeroWhenAllMaskable(t *testing.T) { + r := secrets.NewRedactor([]string{redactTestKey}) + if got := r.SkippedValueCount(); got != 0 { + t.Errorf("SkippedValueCount = %d, want 0", got) + } + + var nilRedactor *secrets.Redactor + if got := nilRedactor.SkippedValueCount(); got != 0 { + t.Errorf("nil SkippedValueCount = %d, want 0", got) + } +} + +// The environment redactor is the path that selects sensitive keys, so a short +// value under such a key must not corrupt machine-readable output. +func TestEnvironmentRedactor_ShortSensitiveValueDoesNotCorruptOutput(t *testing.T) { + r := secrets.NewEnvironmentRedactor([]string{ + "DEVSY_DEFERRED_AUTH_BOOTSTRAP=1", + redactTestKey, + }) + + got := r.Redact("agent version v1.2.3 at /volumes/ws-001/_data key sk-live-abcdef123456") + want := "agent version v1.2.3 at /volumes/ws-001/_data key ***" + if got != want { + t.Errorf("Redact = %q, want %q", got, want) + } +} + func TestRedactor_IgnoresEmptyValues(t *testing.T) { r := secrets.NewRedactor([]string{"EMPTY=", "NO_EQUALS"}) @@ -51,11 +118,11 @@ func TestRedactor_ValueWithEquals(t *testing.T) { // longer value, regardless of input order. func TestRedactor_PrefixOverlap(t *testing.T) { for _, order := range [][]string{ - {"A=sec", "B=secret"}, - {"B=secret", "A=sec"}, + {"A=sec-abcd", "B=sec-abcd-efgh"}, + {"B=sec-abcd-efgh", "A=sec-abcd"}, } { r := secrets.NewRedactor(order) - got := r.Redact("value is secret here") + got := r.Redact("value is sec-abcd-efgh here") if got != "value is *** here" { t.Errorf("order %v: Redact = %q, want longer value fully masked", order, got) } From 7bfd7d07ae939fc9e067334f7716ff6c7571cf63 Mon Sep 17 00:00:00 2001 From: Samuel K <69881238+skevetter@users.noreply.github.com> Date: Sun, 27 Sep 2026 21:50:26 -0600 Subject: [PATCH 2/4] fix(subprocess): keep machine-readable stdout out of the redaction path Result.Stdout is redacted, but several callers parse it as data rather than display it. A redactor that masked any character of a value then corrupted the value itself: the docker volume mountpoint became a path that does not exist, and the agent version and seed labels parsed wrong. Add Result.RawStdout plus Options.UnredactedStdout so those callers can use the output verbatim while Stdout, Stderr and DiagnosticOutput stay redacted for display. RawStdout is empty unless requested, so the default path is unchanged. This mirrors the UnredactedStdout option pkg/git already used for `git credential fill`. Route the volume mountpoint, managed volume listing, detected agent version and seed label parsing in pkg/agent/delivery through raw capture, along with the rendered environment template in pkg/devcontainer/crane, which reaches subprocess through the options-based entry point. Co-Authored-By: Claude Opus 4.8 (1M context) --- pkg/agent/delivery/capture.go | 6 ++- pkg/agent/delivery/local_docker.go | 6 +-- pkg/agent/delivery/workspace_seed.go | 2 +- pkg/devcontainer/crane/run.go | 6 ++- pkg/subprocess/capture.go | 72 +++++++++++++++++++++----- pkg/subprocess/capture_test.go | 75 ++++++++++++++++++++++++++++ 6 files changed, 149 insertions(+), 18 deletions(-) diff --git a/pkg/agent/delivery/capture.go b/pkg/agent/delivery/capture.go index 4c1e1229e9..ea118d1ce1 100644 --- a/pkg/agent/delivery/capture.go +++ b/pkg/agent/delivery/capture.go @@ -10,12 +10,16 @@ import ( "github.com/devsy-org/devsy/pkg/subprocess" ) +// runCaptured executes cmd with bounded, redacted diagnostics. The unredacted +// stdout is also preserved on the result for the callers that parse docker +// output, such as volume mountpoints, where a masked character would corrupt +// the value. Diagnostics built from the result stay redacted. func runCaptured(ctx context.Context, cmd *exec.Cmd) (subprocess.Result, error) { env := cmd.Env if env == nil { env = os.Environ() } - result, err := subprocess.RunCommand(cmd, secrets.NewEnvironmentRedactor(env)) + result, err := subprocess.RunCommandUnredactedStdout(cmd, secrets.NewEnvironmentRedactor(env)) result.OperationID = status.OperationID(ctx) return result, err } diff --git a/pkg/agent/delivery/local_docker.go b/pkg/agent/delivery/local_docker.go index 6b40165ffa..7f77904431 100644 --- a/pkg/agent/delivery/local_docker.go +++ b/pkg/agent/delivery/local_docker.go @@ -169,7 +169,7 @@ func (d *LocalDockerDelivery) detectVolumeVersion(ctx context.Context, volumeNam log.Debugf("failed to detect agent version in volume: %v", err) return "" } - return strings.TrimSpace(result.Stdout) + return strings.TrimSpace(result.RawStdout) } func (d *LocalDockerDelivery) populateVolume( @@ -292,7 +292,7 @@ func (d *LocalDockerDelivery) volumeMountpoint( if err != nil { return "", fmt.Errorf("%s: %w", capturedOutput(result), err) } - return strings.TrimSpace(result.Stdout), nil + return strings.TrimSpace(result.RawStdout), nil } // removeVolume force-removes a single named volume. It is safe to call for a @@ -321,7 +321,7 @@ func (d *LocalDockerDelivery) listManagedVolumes( return nil, fmt.Errorf("%s: %w", capturedOutput(result), err) } var names []string - for line := range strings.SplitSeq(strings.TrimSpace(result.Stdout), "\n") { + for line := range strings.SplitSeq(strings.TrimSpace(result.RawStdout), "\n") { if name := strings.TrimSpace(line); name != "" { names = append(names, name) } diff --git a/pkg/agent/delivery/workspace_seed.go b/pkg/agent/delivery/workspace_seed.go index dd53a167cf..ced2050e20 100644 --- a/pkg/agent/delivery/workspace_seed.go +++ b/pkg/agent/delivery/workspace_seed.go @@ -136,7 +136,7 @@ func (d *LocalDockerDelivery) volumeSeedState( err, ) } - managedStr, seededStr, _ := strings.Cut(strings.TrimSpace(result.Stdout), ",") + managedStr, seededStr, _ := strings.Cut(strings.TrimSpace(result.RawStdout), ",") return managedStr == pkgconfig.LabelValueTrue, seededStr == pkgconfig.LabelValueTrue, nil } diff --git a/pkg/devcontainer/crane/run.go b/pkg/devcontainer/crane/run.go index 252fbf1ab1..c439328ea0 100644 --- a/pkg/devcontainer/crane/run.go +++ b/pkg/devcontainer/crane/run.go @@ -65,6 +65,10 @@ func (c *command) RunContext(ctx context.Context) (string, error) { } result, err := subprocess.Run(ctx, c.cmd, c.args, subprocess.Options{ Redactor: redactor, + // The rendered environment template is written to disk and must be + // reproduced verbatim, so capture it unredacted. Diagnostics above + // still use the redacted output. + UnredactedStdout: true, }) if err != nil { details := result.DiagnosticOutput() @@ -74,7 +78,7 @@ func (c *command) RunContext(ctx context.Context) (string, error) { return "", fmt.Errorf("failed to execute command: %w", err) } - return result.Stdout, nil + return result.RawStdout, nil } // ShouldUse takes CLIOptions and returns true if crane should be used. diff --git a/pkg/subprocess/capture.go b/pkg/subprocess/capture.go index 8aad2940d6..c506f22ff9 100644 --- a/pkg/subprocess/capture.go +++ b/pkg/subprocess/capture.go @@ -33,6 +33,10 @@ type Options struct { // subprocess output but are not represented by a sensitive environment // variable. Values are masked before capture and command rendering. SensitiveValues []string + // UnredactedStdout preserves machine-readable stdout in Result.RawStdout. + // Redaction still applies to the captured Stdout and Stderr, so rendered + // commands and diagnostic messages remain safe to display. + UnredactedStdout bool // OperationID links diagnostics to the semantic operation that spawned // the command. When empty, Run derives it from ctx. OperationID string @@ -120,10 +124,16 @@ type Result struct { OperationID string Stdout string Stderr string - ExitCode int - Signal string - Duration time.Duration - Truncated bool + // RawStdout holds unredacted stdout, populated only when the caller asked + // for machine-readable output that must be used verbatim rather than only + // shown to a user, such as a Docker volume mountpoint. It is empty + // otherwise, so Stdout remains the sole populated stdout field by default + // and diagnostics stay redacted. + RawStdout string + ExitCode int + Signal string + Duration time.Duration + Truncated bool } // DiagnosticOutput returns the useful captured tail for a failed command. @@ -163,7 +173,7 @@ func Run(ctx context.Context, binary string, args []string, options Options) (Re } cmd.Stdin = options.Stdin redactor := commandRedactor(options) - result, err := RunCommand(cmd, redactor) + result, err := runCommand(cmd, redactor, options.UnredactedStdout) err = withContextError(ctx, err) result.OperationID = options.OperationID return result, err @@ -202,12 +212,22 @@ func withContextError(ctx context.Context, err error) error { // stdout and stderr capture. Existing command setup such as an explicit // environment, working directory, or stdin is preserved. func RunCommand(cmd *exec.Cmd, redactor *secrets.Redactor) (Result, error) { + return runCommand(cmd, redactor, false) +} + +// RunCommandUnredactedStdout behaves like RunCommand but additionally preserves +// the unredacted stdout in Result.RawStdout for callers that parse the output +// rather than display it. +func RunCommandUnredactedStdout(cmd *exec.Cmd, redactor *secrets.Redactor) (Result, error) { + return runCommand(cmd, redactor, true) +} + +func runCommand(cmd *exec.Cmd, redactor *secrets.Redactor, unredactedStdout bool) (Result, error) { if cmd == nil { return Result{}, errors.New("subprocess command is nil") } - out := NewBuffer(redactor) + out, rawOut := captureStdout(cmd, redactor, unredactedStdout) errOut := NewBuffer(redactor) - cmd.Stdout = out cmd.Stderr = errOut binary := cmd.Path args := []string(nil) @@ -229,17 +249,45 @@ func RunCommand(cmd *exec.Cmd, redactor *secrets.Redactor) (Result, error) { Truncated: out.Truncated() || errOut.Truncated(), ExitCode: 0, } + if rawOut != nil { + result.RawStdout = rawOut.String() + result.Truncated = result.Truncated || rawOut.Truncated() + } if err != nil { - result.ExitCode = -1 - if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { - result.ExitCode = exitErr.ExitCode() - result.Signal = processSignal(exitErr.ProcessState) - } + applyExitFailure(&result, err) return result, fmt.Errorf("run %s: %w", result.DisplayCommand, err) } return result, nil } +// applyExitFailure records the exit code and terminating signal of a failed +// command on the result. +func applyExitFailure(result *Result, err error) { + result.ExitCode = -1 + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { + result.ExitCode = exitErr.ExitCode() + result.Signal = processSignal(exitErr.ProcessState) + } +} + +// captureStdout wires the redacting display buffer to cmd, and returns it +// alongside a second unredacted buffer when the caller needs the stdout +// verbatim. A buffer built without a redactor still bounds the copy. +func captureStdout( + cmd *exec.Cmd, + redactor *secrets.Redactor, + unredacted bool, +) (display, raw *Buffer) { + display = NewBuffer(redactor) + if !unredacted { + cmd.Stdout = display + return display, nil + } + raw = NewBuffer(nil) + cmd.Stdout = io.MultiWriter(display, raw) + return display, raw +} + func processSignal(state *os.ProcessState) string { if state == nil { return "" diff --git a/pkg/subprocess/capture_test.go b/pkg/subprocess/capture_test.go index c8b3e8c878..6bc808805c 100644 --- a/pkg/subprocess/capture_test.go +++ b/pkg/subprocess/capture_test.go @@ -5,6 +5,7 @@ import ( "context" "errors" "io" + "os/exec" "runtime" "strings" "testing" @@ -229,6 +230,80 @@ func TestRunCapturesSignal(t *testing.T) { } } +// Machine-readable stdout must be reproducible verbatim: a docker volume +// mountpoint containing digit runs must not be mangled by redaction, while the +// display channels stay masked. +func TestRunUnredactedStdoutKeepsMachineReadableOutputVerbatim(t *testing.T) { + if runtime.GOOS == testWindows { + t.Skip("test command uses sh") + } + secret := "sk-live-abcdef123456" //nolint:gosec // test-only redaction fixture + mountpoint := "/var/lib/docker/volumes/devsy-ws-001/_data" + result, err := Run( + context.Background(), + "sh", + []string{"-c", `printf '%s %s' "$TOKEN" "$1"`, "sh", mountpoint}, + Options{ + Env: []string{"TOKEN=" + secret}, + UnredactedStdout: true, + }, + ) + if err != nil { + t.Fatalf("Run: %v", err) + } + if !strings.Contains(result.RawStdout, mountpoint) { + t.Fatalf("RawStdout = %q, want mountpoint %q preserved", result.RawStdout, mountpoint) + } + if strings.Contains(result.Stdout, secret) { + t.Fatalf("secret escaped display stdout: %q", result.Stdout) + } + if !strings.Contains(result.Stdout, "***") { + t.Fatalf("Stdout = %q, want secret masked for display", result.Stdout) + } +} + +// Diagnostics must not leak the raw output back into user-facing messages. +func TestRunUnredactedStdoutKeepsDiagnosticsRedacted(t *testing.T) { + if runtime.GOOS == testWindows { + t.Skip("test command uses sh") + } + secret := "sk-live-abcdef123456" //nolint:gosec // test-only redaction fixture + result, err := Run( + context.Background(), + "sh", + []string{"-c", `printf '%s' "$TOKEN" >&2; exit 3`, "sh"}, + Options{ + Env: []string{"TOKEN=" + secret}, + UnredactedStdout: true, + }, + ) + if err == nil { + t.Fatal("Run succeeded, want failure") + } + if strings.Contains(result.DiagnosticOutput(), secret) { + t.Fatalf("secret escaped diagnostic output: %q", result.DiagnosticOutput()) + } +} + +// Without the opt-in, no raw copy is retained, so a redactor can never +// accidentally influence machine-readable output. +func TestRunCommandLeavesRawStdoutEmptyByDefault(t *testing.T) { + if runtime.GOOS == testWindows { + t.Skip("test command uses sh") + } + cmd := exec.Command("sh", "-c", "printf 'v1.2.3'") // #nosec G204 -- test-only command + result, err := RunCommand(cmd, secrets.NewRedactor([]string{captureToken + captureTestSecret})) + if err != nil { + t.Fatalf("RunCommand: %v", err) + } + if result.RawStdout != "" { + t.Fatalf("RawStdout = %q, want empty when not requested", result.RawStdout) + } + if result.Stdout != "v1.2.3" { + t.Fatalf("Stdout = %q, want v1.2.3", result.Stdout) + } +} + func TestDiagnosticOutputAnnotatesTruncatedTail(t *testing.T) { result := Result{ Stdout: "last useful line", From cffaedb3f14a676ef5e958b3d0cf82b90f35bc23 Mon Sep 17 00:00:00 2001 From: Samuel K <69881238+skevetter@users.noreply.github.com> Date: Sun, 27 Sep 2026 22:05:59 -0600 Subject: [PATCH 3/4] test(delivery): pin the redaction regression deterministically The eight failures that surfaced this bug only reproduce when the ambient environment happens to hold a credential-bearing variable with a low-entropy value. On a clean CI runner they pass, so the regression would not have been caught by the new unit-test job. Inject the offending variable explicitly through the delivery Environment field and assert that both consumers of machine-readable docker output survive it: the volume mountpoint, which is used as a filesystem path, and the detected agent version, which is compared verbatim. Both fail against the unfixed code with the original symptom ("v1.2.3" reported as "v***.2.3", and a mountpoint path rewritten to "00***"), and pass once the capture path is separated from redaction. Co-Authored-By: Claude Opus 4.8 (1M context) --- pkg/agent/delivery/local_docker_test.go | 60 +++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/pkg/agent/delivery/local_docker_test.go b/pkg/agent/delivery/local_docker_test.go index c2d6f44076..4b504361a2 100644 --- a/pkg/agent/delivery/local_docker_test.go +++ b/pkg/agent/delivery/local_docker_test.go @@ -557,3 +557,63 @@ func TestLocalDockerDelivery_Seed_CopyAndCleanupFailureJoined(t *testing.T) { assert.Contains(t, err.Error(), "seed workspace volume") assert.Contains(t, err.Error(), "remove partial volume") } + +// lowEntropySecretEnv is a credential-bearing variable whose value is a single +// character. Masking it as a literal substring would rewrite every occurrence +// of that character in captured output, corrupting machine-readable values such +// as volume mountpoints. It is injected explicitly so this regression is +// deterministic rather than dependent on the ambient environment. +var lowEntropySecretEnv = []string{"DEVSY_DEFERRED_AUTH_BOOTSTRAP=1"} //nolint:gosec // test fixture + +// writeFakeVolumeInspectScript writes a stand-in for `docker volume inspect` +// that reports mountDir, and returns the script path. +func writeFakeVolumeInspectScript(t *testing.T, mountDir string) string { + t.Helper() + + scriptPath := filepath.Join(t.TempDir(), "fake-docker.sh") + script := "#!/bin/sh\n" + + "case \"$1\" in\n" + + " volume) echo \"" + mountDir + "\" ;;\n" + + " *) exit 1 ;;\n" + + "esac\n" + require.NoError(t, os.WriteFile(scriptPath, []byte(script), 0o600)) + // #nosec G302 -- test script must be executable + require.NoError(t, os.Chmod(scriptPath, 0o755)) + return scriptPath +} + +// The mountpoint is used as a filesystem path, so redaction must not reach it. +func TestVolumeMountpoint_UnaffectedByLowEntropySecretInEnv(t *testing.T) { + // The "001" segment is what a single-character mask would corrupt. + mountDir := filepath.Join(t.TempDir(), "001", "mount") + require.NoError(t, os.MkdirAll(mountDir, 0o750)) + + d := &LocalDockerDelivery{ + DockerCommand: writeFakeVolumeInspectScript(t, mountDir), + Environment: lowEntropySecretEnv, + } + + got, err := d.volumeMountpoint(context.Background(), "test-vol") + require.NoError(t, err) + assert.Equal(t, mountDir, got) +} + +// The detected version is compared verbatim, so it must survive capture intact. +func TestDetectVolumeVersion_UnaffectedByLowEntropySecretInEnv(t *testing.T) { + scriptPath := filepath.Join(t.TempDir(), "fake-docker.sh") + script := "#!/bin/sh\n" + + "case \"$1\" in\n" + + " run) echo \"v1.2.3\" ;;\n" + + " *) exit 1 ;;\n" + + "esac\n" + require.NoError(t, os.WriteFile(scriptPath, []byte(script), 0o600)) + // #nosec G302 -- test script must be executable + require.NoError(t, os.Chmod(scriptPath, 0o755)) + + d := &LocalDockerDelivery{ + DockerCommand: scriptPath, + Environment: lowEntropySecretEnv, + } + + assert.Equal(t, "v1.2.3", d.detectVolumeVersion(context.Background(), "test-vol")) +} From beba14a6315d5c5b20202576702b10f43116316e Mon Sep 17 00:00:00 2001 From: Samuel K <69881238+skevetter@users.noreply.github.com> Date: Sun, 27 Sep 2026 23:23:27 -0600 Subject: [PATCH 4/4] style: trim comments in the redaction change Comments restated what the code already says. Cut them back to the part that is not derivable: why a value must be long enough to mask, and why the docker output is captured unredacted. Co-Authored-By: Claude Opus 4.8 (1M context) --- pkg/agent/delivery/capture.go | 7 +++---- pkg/agent/delivery/local_docker_test.go | 13 ++++--------- pkg/devcontainer/crane/run.go | 4 +--- pkg/secrets/redact.go | 23 ++++++++--------------- pkg/secrets/redact_test.go | 6 +----- pkg/subprocess/capture.go | 23 ++++++++--------------- pkg/subprocess/capture_test.go | 6 ------ 7 files changed, 25 insertions(+), 57 deletions(-) diff --git a/pkg/agent/delivery/capture.go b/pkg/agent/delivery/capture.go index ea118d1ce1..45e231a826 100644 --- a/pkg/agent/delivery/capture.go +++ b/pkg/agent/delivery/capture.go @@ -10,10 +10,9 @@ import ( "github.com/devsy-org/devsy/pkg/subprocess" ) -// runCaptured executes cmd with bounded, redacted diagnostics. The unredacted -// stdout is also preserved on the result for the callers that parse docker -// output, such as volume mountpoints, where a masked character would corrupt -// the value. Diagnostics built from the result stay redacted. +// runCaptured captures with redacted diagnostics, keeping the unredacted +// stdout for the callers that parse docker output such as volume mountpoints, +// where a masked character would corrupt the path. func runCaptured(ctx context.Context, cmd *exec.Cmd) (subprocess.Result, error) { env := cmd.Env if env == nil { diff --git a/pkg/agent/delivery/local_docker_test.go b/pkg/agent/delivery/local_docker_test.go index 4b504361a2..4c29f4d5f6 100644 --- a/pkg/agent/delivery/local_docker_test.go +++ b/pkg/agent/delivery/local_docker_test.go @@ -558,15 +558,10 @@ func TestLocalDockerDelivery_Seed_CopyAndCleanupFailureJoined(t *testing.T) { assert.Contains(t, err.Error(), "remove partial volume") } -// lowEntropySecretEnv is a credential-bearing variable whose value is a single -// character. Masking it as a literal substring would rewrite every occurrence -// of that character in captured output, corrupting machine-readable values such -// as volume mountpoints. It is injected explicitly so this regression is -// deterministic rather than dependent on the ambient environment. +// lowEntropySecretEnv is a credential-bearing variable with a one-character +// value: masking it as a literal substring corrupts captured paths and versions. var lowEntropySecretEnv = []string{"DEVSY_DEFERRED_AUTH_BOOTSTRAP=1"} //nolint:gosec // test fixture -// writeFakeVolumeInspectScript writes a stand-in for `docker volume inspect` -// that reports mountDir, and returns the script path. func writeFakeVolumeInspectScript(t *testing.T, mountDir string) string { t.Helper() @@ -582,9 +577,9 @@ func writeFakeVolumeInspectScript(t *testing.T, mountDir string) string { return scriptPath } -// The mountpoint is used as a filesystem path, so redaction must not reach it. +// The mountpoint is used as a filesystem path, and "001" is what a +// single-character mask would corrupt. func TestVolumeMountpoint_UnaffectedByLowEntropySecretInEnv(t *testing.T) { - // The "001" segment is what a single-character mask would corrupt. mountDir := filepath.Join(t.TempDir(), "001", "mount") require.NoError(t, os.MkdirAll(mountDir, 0o750)) diff --git a/pkg/devcontainer/crane/run.go b/pkg/devcontainer/crane/run.go index c439328ea0..598d263faf 100644 --- a/pkg/devcontainer/crane/run.go +++ b/pkg/devcontainer/crane/run.go @@ -65,9 +65,7 @@ func (c *command) RunContext(ctx context.Context) (string, error) { } result, err := subprocess.Run(ctx, c.cmd, c.args, subprocess.Options{ Redactor: redactor, - // The rendered environment template is written to disk and must be - // reproduced verbatim, so capture it unredacted. Diagnostics above - // still use the redacted output. + // The template is written to disk and must be reproduced verbatim. UnredactedStdout: true, }) if err != nil { diff --git a/pkg/secrets/redact.go b/pkg/secrets/redact.go index 9c1e2a492a..35d3f5bdd3 100644 --- a/pkg/secrets/redact.go +++ b/pkg/secrets/redact.go @@ -10,12 +10,9 @@ import ( const redactMask = "***" -// minRedactableValueLength is the shortest value that can be masked safely. -// Masking replaces the value as a literal substring, so a very short value -// matches unrelated text everywhere it appears: a single-character secret -// would corrupt every version string, path, and port number in captured -// output rather than protect anything. Values below this length are left -// unmasked; SkippedValueCount reports them so callers can surface the gap. +// minRedactableValueLength is the shortest value worth masking. Masking is +// literal substring replacement, so a shorter value corrupts unrelated output +// (paths, versions) instead of protecting anything. const minRedactableValueLength = 8 var ( @@ -28,13 +25,11 @@ var ( type Redactor struct { replacer *strings.Replacer values []string - // skipped counts sensitive values too short to mask without corrupting - // unrelated text. - skipped int + skipped int } -// NewRedactor masks the values (not keys) of KEY=VALUE entries; empty values -// are ignored, as are values shorter than minRedactableValueLength. +// NewRedactor masks the values (not keys) of KEY=VALUE entries, skipping empty +// values and those too short to mask safely. func NewRedactor(secretsEnv []string) *Redactor { values := make([]string, 0, len(secretsEnv)) skipped := 0 @@ -97,10 +92,8 @@ func NewEnvironmentRedactor(env []string) *Redactor { return NewRedactor(sensitive) } -// SkippedValueCount reports how many sensitive values were too short to mask. -// A non-zero count means at least one credential-bearing value is present in -// the environment but is not being redacted, so callers that emit captured -// output may want to warn about it. +// SkippedValueCount reports sensitive values left unmasked for being too +// short, so callers emitting captured output can surface the gap. func (r *Redactor) SkippedValueCount() int { if r == nil { return 0 diff --git a/pkg/secrets/redact_test.go b/pkg/secrets/redact_test.go index 3ce2c0bc08..37edcb1749 100644 --- a/pkg/secrets/redact_test.go +++ b/pkg/secrets/redact_test.go @@ -25,9 +25,6 @@ func TestRedactor_MasksValues(t *testing.T) { } } -// A value too short to mask safely is left alone rather than applied as a -// literal substring, because masking it would corrupt unrelated text -// everywhere it appears. func TestRedactor_SkipsTooShortValues(t *testing.T) { r := secrets.NewRedactor([]string{"DEVSY_AUTH_BOOTSTRAP=1"}) @@ -69,8 +66,7 @@ func TestRedactor_SkippedValueCountIsZeroWhenAllMaskable(t *testing.T) { } } -// The environment redactor is the path that selects sensitive keys, so a short -// value under such a key must not corrupt machine-readable output. +// The environment redactor is the path that selects sensitive keys. func TestEnvironmentRedactor_ShortSensitiveValueDoesNotCorruptOutput(t *testing.T) { r := secrets.NewEnvironmentRedactor([]string{ "DEVSY_DEFERRED_AUTH_BOOTSTRAP=1", diff --git a/pkg/subprocess/capture.go b/pkg/subprocess/capture.go index c506f22ff9..1c425a9590 100644 --- a/pkg/subprocess/capture.go +++ b/pkg/subprocess/capture.go @@ -34,8 +34,7 @@ type Options struct { // variable. Values are masked before capture and command rendering. SensitiveValues []string // UnredactedStdout preserves machine-readable stdout in Result.RawStdout. - // Redaction still applies to the captured Stdout and Stderr, so rendered - // commands and diagnostic messages remain safe to display. + // Display channels stay redacted. UnredactedStdout bool // OperationID links diagnostics to the semantic operation that spawned // the command. When empty, Run derives it from ctx. @@ -124,11 +123,8 @@ type Result struct { OperationID string Stdout string Stderr string - // RawStdout holds unredacted stdout, populated only when the caller asked - // for machine-readable output that must be used verbatim rather than only - // shown to a user, such as a Docker volume mountpoint. It is empty - // otherwise, so Stdout remains the sole populated stdout field by default - // and diagnostics stay redacted. + // RawStdout holds unredacted stdout when the caller asked for + // machine-readable output, such as a volume mountpoint. Empty otherwise. RawStdout string ExitCode int Signal string @@ -215,9 +211,8 @@ func RunCommand(cmd *exec.Cmd, redactor *secrets.Redactor) (Result, error) { return runCommand(cmd, redactor, false) } -// RunCommandUnredactedStdout behaves like RunCommand but additionally preserves -// the unredacted stdout in Result.RawStdout for callers that parse the output -// rather than display it. +// RunCommandUnredactedStdout is RunCommand with the stdout also preserved +// unredacted in Result.RawStdout. func RunCommandUnredactedStdout(cmd *exec.Cmd, redactor *secrets.Redactor) (Result, error) { return runCommand(cmd, redactor, true) } @@ -260,8 +255,6 @@ func runCommand(cmd *exec.Cmd, redactor *secrets.Redactor, unredactedStdout bool return result, nil } -// applyExitFailure records the exit code and terminating signal of a failed -// command on the result. func applyExitFailure(result *Result, err error) { result.ExitCode = -1 if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { @@ -270,9 +263,9 @@ func applyExitFailure(result *Result, err error) { } } -// captureStdout wires the redacting display buffer to cmd, and returns it -// alongside a second unredacted buffer when the caller needs the stdout -// verbatim. A buffer built without a redactor still bounds the copy. +// captureStdout wires the redacting buffer to cmd and, when the caller needs +// the stdout verbatim, a second buffer alongside it. A buffer with no redactor +// still bounds the copy. func captureStdout( cmd *exec.Cmd, redactor *secrets.Redactor, diff --git a/pkg/subprocess/capture_test.go b/pkg/subprocess/capture_test.go index 6bc808805c..0d0cb8f370 100644 --- a/pkg/subprocess/capture_test.go +++ b/pkg/subprocess/capture_test.go @@ -230,9 +230,6 @@ func TestRunCapturesSignal(t *testing.T) { } } -// Machine-readable stdout must be reproducible verbatim: a docker volume -// mountpoint containing digit runs must not be mangled by redaction, while the -// display channels stay masked. func TestRunUnredactedStdoutKeepsMachineReadableOutputVerbatim(t *testing.T) { if runtime.GOOS == testWindows { t.Skip("test command uses sh") @@ -262,7 +259,6 @@ func TestRunUnredactedStdoutKeepsMachineReadableOutputVerbatim(t *testing.T) { } } -// Diagnostics must not leak the raw output back into user-facing messages. func TestRunUnredactedStdoutKeepsDiagnosticsRedacted(t *testing.T) { if runtime.GOOS == testWindows { t.Skip("test command uses sh") @@ -285,8 +281,6 @@ func TestRunUnredactedStdoutKeepsDiagnosticsRedacted(t *testing.T) { } } -// Without the opt-in, no raw copy is retained, so a redactor can never -// accidentally influence machine-readable output. func TestRunCommandLeavesRawStdoutEmptyByDefault(t *testing.T) { if runtime.GOOS == testWindows { t.Skip("test command uses sh")