Skip to content

Commit 24e58e1

Browse files
review improve
1 parent a1be620 commit 24e58e1

File tree

2 files changed

+109
-8
lines changed

2 files changed

+109
-8
lines changed

cmd/root.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"strings"
78

89
"codacy/cli-v2/config"
910
"codacy/cli-v2/utils/logger"
@@ -42,14 +43,7 @@ var rootCmd = &cobra.Command{
4243
})
4344

4445
// Create a masked version of the full command for logging
45-
maskedArgs := make([]string, len(os.Args))
46-
copy(maskedArgs, os.Args)
47-
for i, arg := range maskedArgs {
48-
if i > 0 && (arg == "--api-token" || arg == "--repository-token" ||
49-
arg == "--project-token" || arg == "--codacy-api-token") && i < len(maskedArgs)-1 {
50-
maskedArgs[i+1] = "***"
51-
}
52-
}
46+
maskedArgs := maskSensitiveArgs(os.Args)
5347

5448
// Log the command being executed with its arguments and flags
5549
logger.Info("Executing CLI command", logrus.Fields{
@@ -156,3 +150,37 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e
156150
https://github.com/codacy/codacy-cli-v2
157151
`)
158152
}
153+
154+
// maskSensitiveArgs creates a copy of the arguments with sensitive values masked
155+
func maskSensitiveArgs(args []string) []string {
156+
maskedArgs := make([]string, len(args))
157+
copy(maskedArgs, args)
158+
159+
sensitiveFlags := map[string]bool{
160+
"--api-token": true,
161+
"--repository-token": true,
162+
"--project-token": true,
163+
"--codacy-api-token": true,
164+
}
165+
166+
for i, arg := range maskedArgs {
167+
// Skip the first argument (program name)
168+
if i == 0 {
169+
continue
170+
}
171+
172+
// Handle --flag=value format
173+
for flag := range sensitiveFlags {
174+
if strings.HasPrefix(arg, flag+"=") {
175+
maskedArgs[i] = flag + "=***"
176+
break
177+
}
178+
}
179+
180+
// Handle --flag value format
181+
if sensitiveFlags[arg] && i < len(maskedArgs)-1 {
182+
maskedArgs[i+1] = "***"
183+
}
184+
}
185+
return maskedArgs
186+
}

cmd/root_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestMaskSensitiveArgs(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
args []string
13+
expected []string
14+
}{
15+
{
16+
name: "no sensitive flags",
17+
args: []string{"codacy-cli", "analyze", "--tool", "eslint"},
18+
expected: []string{"codacy-cli", "analyze", "--tool", "eslint"},
19+
},
20+
{
21+
name: "api token with space",
22+
args: []string{"codacy-cli", "init", "--api-token", "secret123", "--tool", "eslint"},
23+
expected: []string{"codacy-cli", "init", "--api-token", "***", "--tool", "eslint"},
24+
},
25+
{
26+
name: "api token with equals",
27+
args: []string{"codacy-cli", "init", "--api-token=secret123", "--tool", "eslint"},
28+
expected: []string{"codacy-cli", "init", "--api-token=***", "--tool", "eslint"},
29+
},
30+
{
31+
name: "repository token at end with space",
32+
args: []string{"codacy-cli", "init", "--repository-token", "secret123"},
33+
expected: []string{"codacy-cli", "init", "--repository-token", "***"},
34+
},
35+
{
36+
name: "repository token at end with equals",
37+
args: []string{"codacy-cli", "init", "--repository-token=secret123"},
38+
expected: []string{"codacy-cli", "init", "--repository-token=***"},
39+
},
40+
{
41+
name: "project token at start with space",
42+
args: []string{"codacy-cli", "--project-token", "secret123", "analyze"},
43+
expected: []string{"codacy-cli", "--project-token", "***", "analyze"},
44+
},
45+
{
46+
name: "project token at start with equals",
47+
args: []string{"codacy-cli", "--project-token=secret123", "analyze"},
48+
expected: []string{"codacy-cli", "--project-token=***", "analyze"},
49+
},
50+
{
51+
name: "multiple tokens mixed format",
52+
args: []string{"codacy-cli", "--api-token=secret1", "--project-token", "secret2"},
53+
expected: []string{"codacy-cli", "--api-token=***", "--project-token", "***"},
54+
},
55+
{
56+
name: "token flag at end without value",
57+
args: []string{"codacy-cli", "analyze", "--api-token"},
58+
expected: []string{"codacy-cli", "analyze", "--api-token"},
59+
},
60+
{
61+
name: "empty value after equals",
62+
args: []string{"codacy-cli", "--api-token="},
63+
expected: []string{"codacy-cli", "--api-token=***"},
64+
},
65+
}
66+
67+
for _, tt := range tests {
68+
t.Run(tt.name, func(t *testing.T) {
69+
masked := maskSensitiveArgs(tt.args)
70+
assert.Equal(t, tt.expected, masked, "masked arguments should match expected")
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)