Skip to content

Commit 5b5a683

Browse files
jongioJon GallantCopilot
authored
fix: address preflight lint findings (gosec, goconst, stale nolint) (#99)
Co-authored-by: Jon Gallant <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent a410150 commit 5b5a683

5 files changed

Lines changed: 14 additions & 12 deletions

File tree

env/loader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type DefaultCommandRunner struct{}
2121

2222
// Run executes a command and returns its output.
2323
func (r *DefaultCommandRunner) Run(ctx context.Context, name string, args ...string) ([]byte, error) {
24-
cmd := exec.CommandContext(ctx, name, args...)
24+
cmd := exec.CommandContext(ctx, name, args...) // #nosec G204 -- Command name is caller-controlled, not user input
2525
return cmd.Output()
2626
}
2727

fileutil/fileutil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func AtomicWriteFile(path string, data []byte, perm os.FileMode) error {
155155
// ReadJSON reads JSON from a file into the target interface.
156156
// Returns nil error if file doesn't exist (target unchanged).
157157
func ReadJSON(path string, target any) error {
158-
data, err := os.ReadFile(path)
158+
data, err := os.ReadFile(path) // #nosec G304 -- Path provided by internal callers, not direct user input
159159
if err != nil {
160160
if errors.Is(err, fs.ErrNotExist) {
161161
return nil // File doesn't exist, not an error
@@ -322,7 +322,7 @@ func (m CacheMetadata) IsCacheValid(opts CacheOptions) bool {
322322
// // Rebuild cache
323323
// }
324324
func LoadCacheJSON(path string, target any, opts CacheOptions) (valid bool, err error) {
325-
data, err := os.ReadFile(path)
325+
data, err := os.ReadFile(path) // #nosec G304 -- Cache path constructed internally, not from user input
326326
if err != nil {
327327
if errors.Is(err, fs.ErrNotExist) {
328328
return false, nil // Cache doesn't exist, not an error

healthcheck/checker.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ func (c *HealthChecker) tryCustomHealthCheck(ctx context.Context, config *Health
435435
// performHTTPCheck performs a direct HTTP health check to a specific URL.
436436
func (c *HealthChecker) performHTTPCheck(ctx context.Context, urlStr string) *httpHealthCheckResult {
437437
startTime := time.Now()
438-
req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil)
438+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlStr, nil)
439439
if err != nil {
440440
return &httpHealthCheckResult{
441441
Endpoint: urlStr,
@@ -497,7 +497,7 @@ func (c *HealthChecker) performCommandCheck(ctx context.Context, args []string,
497497
ResponseTime: 0,
498498
}
499499

500-
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
500+
cmd := exec.CommandContext(ctx, args[0], args[1:]...) // #nosec G204 -- Args from Docker HEALTHCHECK config, not user input
501501
err := cmd.Run()
502502
result.ResponseTime = time.Since(startTime)
503503

@@ -521,9 +521,9 @@ func (c *HealthChecker) performShellCheck(ctx context.Context, command string, s
521521

522522
var cmd *exec.Cmd
523523
if runtime.GOOS == "windows" {
524-
cmd = exec.CommandContext(ctx, "cmd", "/C", command)
524+
cmd = exec.CommandContext(ctx, "cmd", "/C", command) // #nosec G204 -- Command from Docker HEALTHCHECK CMD-SHELL config
525525
} else {
526-
cmd = exec.CommandContext(ctx, "sh", "-c", command)
526+
cmd = exec.CommandContext(ctx, "sh", "-c", command) // #nosec G204 -- Command from Docker HEALTHCHECK CMD-SHELL config
527527
}
528528

529529
err := cmd.Run()
@@ -607,7 +607,7 @@ func (c *HealthChecker) checkSingleEndpoint(ctx context.Context, port int, endpo
607607
url := fmt.Sprintf("http://localhost:%d%s", port, endpoint)
608608

609609
startTime := time.Now()
610-
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
610+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
611611
if err != nil {
612612
return nil
613613
}

httpclient/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ func (c *Client) Execute(ctx context.Context, opts RequestOptions) (*Response, e
201201
for attempt := 0; attempt <= maxRetries; attempt++ {
202202
if attempt > 0 {
203203
// Exponential backoff: 1s, 2s, 4s, etc.
204-
backoff := time.Duration(1<<uint(attempt-1)) * time.Second //nolint:gosec // G115: safe conversion, attempt count is small
204+
shift := min(attempt-1, 30) // cap to prevent overflow
205+
backoff := time.Duration(1<<uint(shift)) * time.Second
205206
select {
206207
case <-ctx.Done():
207208
return nil, fmt.Errorf("request canceled: %w", ctx.Err())

httpclient/formatter.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
FormatJSON OutputFormat = "json"
2020
FormatRaw OutputFormat = "raw"
2121
redactedValue = "***REDACTED***"
22+
bearerPrefix = "Bearer "
2223
)
2324

2425
// Formatter handles response formatting and output
@@ -131,12 +132,12 @@ func RedactSensitiveHeader(key, value string) string {
131132

132133
if keyLower == "authorization" {
133134
if strings.HasPrefix(strings.ToLower(value), "bearer ") {
134-
token := strings.TrimPrefix(value, "Bearer ")
135+
token := strings.TrimPrefix(value, bearerPrefix)
135136
token = strings.TrimPrefix(token, "bearer ")
136137
if len(token) > 12 {
137-
return "Bearer " + token[:6] + "..." + token[len(token)-6:]
138+
return bearerPrefix + token[:6] + "..." + token[len(token)-6:]
138139
}
139-
return "Bearer " + redactedValue
140+
return bearerPrefix + redactedValue
140141
}
141142
return redactedValue
142143
}

0 commit comments

Comments
 (0)