Part of duplicate code analysis: #3735
Summary
Two files in the latest commit perform manual inline string truncation using raw byte-index slicing + "..." concatenation, bypassing the established strutil.Truncate and sanitize.TruncateSecret utilities that already exist in the codebase for exactly this purpose.
Duplication Details
Pattern: Inline truncation instead of strutil.Truncate / sanitize.TruncateSecret
- Severity: Medium
- Occurrences: 2 (bypassing 2 different established utilities)
- Locations:
internal/logger/rpc_helpers.go (line 133)
internal/cmd/root.go (line 608)
Instance 1 — rpc_helpers.go:133 (bypasses strutil.Truncate):
// Current — manual truncation:
if len(cleanedLine) > 200 {
cleanedLine = cleanedLine[:197] + "..."
}
// Should be:
cleanedLine = strutil.Truncate(cleanedLine, 197)
The package already imports strutil (used in truncateAndSanitize on line 48). The manual pattern repeats logic already in strutil.Truncate.
Instance 2 — root.go:608 (bypasses sanitize.TruncateSecret):
// Current — custom 10-char truncation for secret display:
displayValue := value
if len(value) > 0 {
displayValue = value[:min(10, len(value))] + "..."
}
log.Printf(" Loaded: %s=%s", key, displayValue)
// Should use the established secret truncation utility:
log.Printf(" Loaded: %s=%s", key, sanitize.TruncateSecret(value))
The codebase uses sanitize.TruncateSecret throughout for exactly this purpose (auth headers, env vars in launcher, session IDs). The custom code truncates to 10 chars whereas TruncateSecret truncates to 4 chars — an inconsistency in security logging depth.
Impact Analysis
- Maintainability: If
strutil.Truncate or sanitize.TruncateSecret semantics change, these inline versions won't track the change
- Bug Risk: Medium —
root.go:608 exposes 10 chars of environment variable values in logs; TruncateSecret consistently exposes only 4 chars. This inconsistency could leak more of a secret value than intended
- Code Bloat: Minor — but the inconsistency creates confusion about which truncation approach is authoritative
Refactoring Recommendations
-
rpc_helpers.go:133 — Replace 4-line manual truncation with strutil.Truncate:
cleanedLine = strutil.Truncate(cleanedLine, 197)
Note: strutil.Truncate(s, 197) produces s[:197] + "..." for strings longer than 197 bytes, which is equivalent to the current code.
-
root.go:608 — Replace custom secret truncation with sanitize.TruncateSecret:
log.Printf(" Loaded: %s=%s", key, sanitize.TruncateSecret(value))
Add import: "github.com/github/gh-aw-mcpg/internal/logger/sanitize" (or the existing import if present).
Implementation Checklist
Parent Issue
See parent analysis report: #3735
Related to #3735
Generated by Duplicate Code Detector · ● 2.4M · ◷
Part of duplicate code analysis: #3735
Summary
Two files in the latest commit perform manual inline string truncation using raw byte-index slicing +
"..."concatenation, bypassing the establishedstrutil.Truncateandsanitize.TruncateSecretutilities that already exist in the codebase for exactly this purpose.Duplication Details
Pattern: Inline truncation instead of
strutil.Truncate/sanitize.TruncateSecretinternal/logger/rpc_helpers.go(line 133)internal/cmd/root.go(line 608)Instance 1 —
rpc_helpers.go:133(bypassesstrutil.Truncate):The package already imports
strutil(used intruncateAndSanitizeon line 48). The manual pattern repeats logic already instrutil.Truncate.Instance 2 —
root.go:608(bypassessanitize.TruncateSecret):The codebase uses
sanitize.TruncateSecretthroughout for exactly this purpose (auth headers, env vars in launcher, session IDs). The custom code truncates to 10 chars whereasTruncateSecrettruncates to 4 chars — an inconsistency in security logging depth.Impact Analysis
strutil.Truncateorsanitize.TruncateSecretsemantics change, these inline versions won't track the changeroot.go:608exposes 10 chars of environment variable values in logs;TruncateSecretconsistently exposes only 4 chars. This inconsistency could leak more of a secret value than intendedRefactoring Recommendations
rpc_helpers.go:133— Replace 4-line manual truncation withstrutil.Truncate:Note:
strutil.Truncate(s, 197)producess[:197] + "..."for strings longer than 197 bytes, which is equivalent to the current code.root.go:608— Replace custom secret truncation withsanitize.TruncateSecret:Add import:
"github.com/github/gh-aw-mcpg/internal/logger/sanitize"(or the existing import if present).Implementation Checklist
rpc_helpers.go:133withstrutil.Truncateroot.go:608withsanitize.TruncateSecretmake test-unitto confirm no regressionsTruncateSecretshows 4 chars vs the current 10 — confirm with security postureParent Issue
See parent analysis report: #3735
Related to #3735