fix: Redact password arguments in logs #594 (#601)

This commit is contained in:
James Read 2025-06-06 22:00:56 +01:00 committed by GitHub
parent 18c5599704
commit 1ffdd93ddf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 1 deletions

View File

@ -67,6 +67,7 @@ func parseActionArguments(values map[string]string, action *config.Action, entit
parsedShellCommand, err := parseCommandForReplacements(action.Shell, values)
parsedShellCommand = sv.ReplaceEntityVars(entityPrefix, parsedShellCommand)
redactedShellCommand := redactShellCommand(parsedShellCommand, action.Arguments, values)
if err != nil {
return "", err
@ -74,12 +75,33 @@ func parseActionArguments(values map[string]string, action *config.Action, entit
log.WithFields(log.Fields{
"actionTitle": action.Title,
"cmd": parsedShellCommand,
"cmd": redactedShellCommand,
}).Infof("Action parse args - After")
return parsedShellCommand, nil
}
func redactShellCommand(shellCommand string, arguments []config.ActionArgument, argumentValues map[string]string) string {
for _, arg := range arguments {
if arg.Type == "password" {
argValue, exists := argumentValues[arg.Name]
if !exists {
log.Warnf("Redact shell command: Argument %s not found in values", arg.Name)
continue
}
if argValue == "" {
continue
}
shellCommand = strings.ReplaceAll(shellCommand, argValue, "<redacted>")
}
}
return shellCommand
}
func typecheckActionArgument(name string, value string, action *config.Action) error {
arg := action.FindArg(name)

View File

@ -132,3 +132,37 @@ func TestTypeSafetyCheckRegex(t *testing.T) {
})
}
}
func TestRedactShellCommand(t *testing.T) {
cmd := "echo 'The password for Fred is toomanysecrets'"
args := []config.ActionArgument{
{
Name: "personName",
Type: "ascii",
},
{
Name: "password",
Type: "password",
},
}
values := map[string]string{
"personName": "Fred",
"password": "toomanysecrets",
}
res := redactShellCommand(cmd, args, values)
assert.Equal(t, "echo 'The password for Fred is <redacted>'", res, "Redacted shell command should mask the password argument")
// Test with empty password
values["password"] = ""
res = redactShellCommand(cmd, args, values)
assert.Equal(t, cmd, res, "Empty password should not change the command")
// Test with missing password argument
delete(values, "password")
res = redactShellCommand(cmd, args, values)
assert.Equal(t, cmd, res, "Missing password argument should not change the command")
}