|
| 1 | +package shellwrap |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "go.uber.org/zap" |
| 9 | + "go.uber.org/zap/zapcore" |
| 10 | + "go.uber.org/zap/zaptest/observer" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + secretValueSlack = "xoxc-abc123456789" |
| 15 | + secretValueJira = "xoxd-zzz987654321" |
| 16 | +) |
| 17 | + |
| 18 | +// assertNoSecret fails if s contains any of the cleartext secret values that |
| 19 | +// must never reach a log. |
| 20 | +func assertNoSecret(t *testing.T, s string) { |
| 21 | + t.Helper() |
| 22 | + for _, sv := range []string{secretValueSlack, secretValueJira} { |
| 23 | + if strings.Contains(s, sv) { |
| 24 | + t.Errorf("cleartext secret %q leaked in %q", sv, s) |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestRedactDockerArgs_TwoTokenForm(t *testing.T) { |
| 30 | + in := []string{"run", "--rm", "-i", "-e", "SLACK_TOKEN=" + secretValueSlack, "myimage:latest"} |
| 31 | + out := RedactDockerArgs(in) |
| 32 | + |
| 33 | + joined := strings.Join(out, " ") |
| 34 | + assertNoSecret(t, joined) |
| 35 | + |
| 36 | + // Non-secret args untouched. |
| 37 | + for _, want := range []string{"run", "--rm", "-i", "myimage:latest"} { |
| 38 | + if !contains(out, want) { |
| 39 | + t.Errorf("expected non-secret arg %q to be preserved, got %v", want, out) |
| 40 | + } |
| 41 | + } |
| 42 | + // Key and flag preserved. |
| 43 | + if !strings.Contains(joined, "-e") || !strings.Contains(joined, "SLACK_TOKEN=") { |
| 44 | + t.Errorf("expected -e and key SLACK_TOKEN= preserved, got %q", joined) |
| 45 | + } |
| 46 | + // Input slice must not be mutated. |
| 47 | + if in[4] != "SLACK_TOKEN="+secretValueSlack { |
| 48 | + t.Errorf("RedactDockerArgs mutated its input slice: %q", in[4]) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestRedactDockerArgs_SingleGluedToken(t *testing.T) { |
| 53 | + in := []string{"run", "-eSLACK_TOKEN=" + secretValueSlack, "myimage"} |
| 54 | + out := RedactDockerArgs(in) |
| 55 | + joined := strings.Join(out, " ") |
| 56 | + assertNoSecret(t, joined) |
| 57 | + if !strings.Contains(joined, "SLACK_TOKEN=") { |
| 58 | + t.Errorf("expected key preserved, got %q", joined) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestRedactDockerArgs_CommandStringElement(t *testing.T) { |
| 63 | + in := []string{"-l", "-c", "docker run --rm -e SLACK_TOKEN=" + secretValueSlack + " -e JIRA_TOKEN=" + secretValueJira + " myimage:latest"} |
| 64 | + out := RedactDockerArgs(in) |
| 65 | + joined := strings.Join(out, " ") |
| 66 | + assertNoSecret(t, joined) |
| 67 | + for _, want := range []string{"-l", "docker run", "--rm", "myimage:latest", "SLACK_TOKEN=", "JIRA_TOKEN="} { |
| 68 | + if !strings.Contains(joined, want) { |
| 69 | + t.Errorf("expected %q preserved in %q", want, joined) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestRedactDockerCommandString(t *testing.T) { |
| 75 | + in := "docker run --rm -e SLACK_TOKEN=" + secretValueSlack + " --env JIRA_TOKEN=" + secretValueJira + " myimage" |
| 76 | + out := RedactDockerCommandString(in) |
| 77 | + assertNoSecret(t, out) |
| 78 | + for _, want := range []string{"docker run", "--rm", "myimage", "SLACK_TOKEN=", "JIRA_TOKEN="} { |
| 79 | + if !strings.Contains(out, want) { |
| 80 | + t.Errorf("expected %q preserved in %q", want, out) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestRedactDockerArgs_NoEnvFlagsUnchanged(t *testing.T) { |
| 86 | + in := []string{"run", "--rm", "-i", "ghcr.io/foo/bar:latest", "--config", "/etc/x.json"} |
| 87 | + out := RedactDockerArgs(in) |
| 88 | + if strings.Join(in, " ") != strings.Join(out, " ") { |
| 89 | + t.Errorf("non-env args were altered: in=%v out=%v", in, out) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// Guard test: the WrapWithUserShell debug log must not emit cleartext secrets |
| 94 | +// in its wrapped_command / original_args fields. |
| 95 | +func TestWrapWithUserShell_LogsAreRedacted(t *testing.T) { |
| 96 | + obsCore, recorded := observer.New(zapcore.DebugLevel) |
| 97 | + logger := zap.New(obsCore) |
| 98 | + |
| 99 | + WrapWithUserShell(logger, "docker", []string{"run", "--rm", "-e", "SLACK_TOKEN=" + secretValueSlack, "myimage"}) |
| 100 | + |
| 101 | + if recorded.Len() == 0 { |
| 102 | + t.Fatal("expected a debug log entry from WrapWithUserShell") |
| 103 | + } |
| 104 | + for _, entry := range recorded.All() { |
| 105 | + for k, v := range entry.ContextMap() { |
| 106 | + assertNoSecret(t, k) |
| 107 | + assertNoSecret(t, fmt.Sprintf("%v", v)) |
| 108 | + } |
| 109 | + assertNoSecret(t, entry.Message) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func contains(ss []string, want string) bool { |
| 114 | + for _, s := range ss { |
| 115 | + if s == want { |
| 116 | + return true |
| 117 | + } |
| 118 | + } |
| 119 | + return false |
| 120 | +} |
0 commit comments