Skip to content

Commit d8e1572

Browse files
Censor suspicious environment variables (#268)
1 parent 35c5446 commit d8e1572

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

pkg/assessor/manifest/manifest.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ func assessHistory(index int, cmd types.History) []*AssessmentWithColumns {
169169
var assesses []*AssessmentWithColumns
170170
cmdSlices := splitByCommands(cmd.CreatedBy)
171171

172-
found, varName := sensitiveVars(cmd.CreatedBy)
172+
found, varName, varVal := sensitiveVars(cmd.CreatedBy)
173173
if found {
174174
assesses = append(assesses, &AssessmentWithColumns{
175175
Assessment: types.Assessment{
176176
Code: types.AvoidCredential,
177177
Filename: ConfigFileName,
178-
Desc: fmt.Sprintf("Suspicious ENV key found : %s on %s (You can suppress it with --accept-key)", varName, cmd.CreatedBy),
178+
Desc: fmt.Sprintf("Suspicious ENV key found : %s on %s (You can suppress it with --accept-key)", varName, strings.ReplaceAll(cmd.CreatedBy, varVal, "*******")),
179179
},
180180
HistoryIndex: index,
181181
})
@@ -277,9 +277,9 @@ func useADDstatement(cmdSlices map[int][]string) bool {
277277
return false
278278
}
279279

280-
func sensitiveVars(cmd string) (bool, string) {
280+
func sensitiveVars(cmd string) (bool, string, string) {
281281
if !strings.Contains(cmd, "=") {
282-
return false, ""
282+
return false, "", ""
283283
}
284284
toklexer := shlex.NewLexer(strings.NewReader(strings.ReplaceAll(cmd, "#", "")))
285285
for {
@@ -304,11 +304,11 @@ func sensitiveVars(cmd string) (bool, string) {
304304
}
305305

306306
if suspiciousCompiler.MatchString(varName) {
307-
return true, varName
307+
return true, varName, varVal
308308
}
309309
}
310310

311-
return false, ""
311+
return false, "", ""
312312
}
313313

314314
func checkAptCommand(target []string, command string) bool {

pkg/assessor/manifest/manifest_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ func TestSensitiveVars(t *testing.T) {
459459
//"skip echo string": {cmd: `/bin/sh -c echo 'secret=foo;' > test.conf`, expected: true},
460460
}
461461
for testname, v := range tests {
462-
actual, _ := sensitiveVars(v.cmd)
462+
actual, _, _ := sensitiveVars(v.cmd)
463463
if actual != v.expected {
464464
t.Errorf("%s want: %t, got %t", testname, v.expected, actual)
465465
}

0 commit comments

Comments
 (0)