Skip to content

Commit 9def5d0

Browse files
lpcoxCopilot
andcommitted
fix: sanitize env values in debug logs and fix GetEnvDuration doc
Address review feedback: 1. Sanitize raw env values using sanitize.TruncateSecret() in all 3 debug log lines (GetEnvInt, GetEnvDuration, GetEnvBool). Env vars could contain secrets, and debug logs are also written to the file logger, so full values should never be persisted. 2. Update GetEnvDuration doc comment to mention that zero/negative durations also fall back to defaultValue (matching the d > 0 condition in the code and the existing GetEnvInt doc pattern). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 63760a7 commit 9def5d0

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

internal/envutil/envutil.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/github/gh-aw-mcpg/internal/logger"
10+
"github.com/github/gh-aw-mcpg/internal/logger/sanitize"
1011
)
1112

1213
var logEnvUtil = logger.New("envutil:envutil")
@@ -29,20 +30,21 @@ func GetEnvInt(envKey string, defaultValue int) int {
2930
if value, err := strconv.Atoi(envValue); err == nil && value > 0 {
3031
return value
3132
}
32-
logEnvUtil.Printf("GetEnvInt: %s=%q is not a valid positive integer, using default=%d", envKey, envValue, defaultValue)
33+
logEnvUtil.Printf("GetEnvInt: %s=%q is not a valid positive integer, using default=%d", envKey, sanitize.TruncateSecret(envValue), defaultValue)
3334
}
3435
return defaultValue
3536
}
3637

3738
// GetEnvDuration returns the time.Duration value of the environment variable specified by envKey.
38-
// If the environment variable is not set, is empty, or cannot be parsed by time.ParseDuration,
39-
// it returns the defaultValue. Accepts any string valid for time.ParseDuration (e.g. "2h", "30m", "90s").
39+
// If the environment variable is not set, is empty, cannot be parsed by time.ParseDuration,
40+
// or is not positive (> 0), it returns the defaultValue.
41+
// Accepts any string valid for time.ParseDuration (e.g. "2h", "30m", "90s").
4042
func GetEnvDuration(envKey string, defaultValue time.Duration) time.Duration {
4143
if envValue := os.Getenv(envKey); envValue != "" {
4244
if d, err := time.ParseDuration(envValue); err == nil && d > 0 {
4345
return d
4446
}
45-
logEnvUtil.Printf("GetEnvDuration: %s=%q is not a valid positive duration, using default=%v", envKey, envValue, defaultValue)
47+
logEnvUtil.Printf("GetEnvDuration: %s=%q is not a valid positive duration, using default=%v", envKey, sanitize.TruncateSecret(envValue), defaultValue)
4648
}
4749
return defaultValue
4850
}
@@ -60,7 +62,7 @@ func GetEnvBool(envKey string, defaultValue bool) bool {
6062
case "0", "false", "no", "off":
6163
return false
6264
}
63-
logEnvUtil.Printf("GetEnvBool: %s=%q is not a recognized boolean value, using default=%v", envKey, envValue, defaultValue)
65+
logEnvUtil.Printf("GetEnvBool: %s=%q is not a recognized boolean value, using default=%v", envKey, sanitize.TruncateSecret(envValue), defaultValue)
6466
}
6567
return defaultValue
6668
}

0 commit comments

Comments
 (0)