Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/agent/delivery/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import (
"github.com/devsy-org/devsy/pkg/subprocess"
)

// 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 {
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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/agent/delivery/local_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
55 changes: 55 additions & 0 deletions pkg/agent/delivery/local_docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,58 @@ 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 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

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, and "001" is what a
// single-character mask would corrupt.
func TestVolumeMountpoint_UnaffectedByLowEntropySecretInEnv(t *testing.T) {
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"))
}
2 changes: 1 addition & 1 deletion pkg/agent/delivery/workspace_seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/devcontainer/crane/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func (c *command) RunContext(ctx context.Context) (string, error) {
}
result, err := subprocess.Run(ctx, c.cmd, c.args, subprocess.Options{
Redactor: redactor,
// The template is written to disk and must be reproduced verbatim.
UnredactedStdout: true,
})
if err != nil {
details := result.DiagnosticOutput()
Expand All @@ -74,7 +76,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.
Expand Down
36 changes: 28 additions & 8 deletions pkg/secrets/redact.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (

const redactMask = "***"

// 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 (
credentialURLPattern = regexp.MustCompile(`(?i)(https?://)[^\s/@]+@`)
authorizationPattern = regexp.MustCompile(
Expand All @@ -18,23 +23,29 @@ var (
)

type Redactor struct {
replacer *strings.Replacer
values []string
maxLength int
replacer *strings.Replacer
values []string
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, skipping empty
// values and those too short to mask safely.
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")
Expand All @@ -46,9 +57,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,
}
}

Expand Down Expand Up @@ -81,6 +92,15 @@ func NewEnvironmentRedactor(env []string) *Redactor {
return NewRedactor(sensitive)
}

// 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
}
return r.skipped
}

func isSensitiveEnvironmentKey(key string) bool {
key = strings.ToUpper(key)
for _, marker := range []string{
Expand Down
73 changes: 68 additions & 5 deletions pkg/secrets/redact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,79 @@ 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)
}
}

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.
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"})

Expand Down Expand Up @@ -51,11 +114,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)
}
Expand Down
Loading
Loading