[security-fix] Fix unhandled error in mcp_inspect.go cleanup (Alert #407)#7869
Merged
Conversation
Added error handling for os.RemoveAll(tmpDir) at line 504 to properly check and log cleanup failures. This satisfies gosec G104 rule. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Security Fix: Unhandled Error in Temporary Directory Cleanup
Alert Number: #407
Severity: Low (Warning)
Rule: G104 - Errors unhandled
Tool: gosec (Golang security checks)
Location:
pkg/cli/mcp_inspect.go:504Vulnerability Description
Gosec detected an unhandled error from
os.RemoveAll(tmpDir)at line 504. The G104 rule flags situations where errors from function calls are silently ignored, which can lead to:While this specific case is a cleanup operation in an error path (cleaning up a temporary directory after failing to write files), proper error handling demonstrates defensive programming and helps maintain code quality.
Fix Applied
Added error handling for the
os.RemoveAll(tmpDir)call at line 504:Before:
After:
Security Best Practices Applied
✅ Error Handling: Properly checks and handles error return values
✅ User Visibility: Logs warning when cleanup fails (in verbose mode)
✅ Non-Breaking: Cleanup failure doesn't affect the main error return path
✅ Defensive Programming: Follows Go best practices for error handling
✅ G104 Compliance: Satisfies gosec security scanner requirements
Testing
✅ Build succeeded:
go build ./pkg/cli/...passes without errors✅ No breaking changes: Normal operation flow unchanged
✅ Error visibility: Cleanup failures are logged in verbose mode
✅ Non-critical handling: Cleanup failure doesn't interrupt the error return path
Impact Assessment
Risk: Minimal
Breaking Changes: None
Backwards Compatibility: Full
Performance: No impact
The fix only adds error checking for a cleanup operation in an error path. The main function behavior remains unchanged - it still returns an error when writing safe-inputs files fails, whether or not the cleanup succeeds. The warning message is only shown in verbose mode.
Why This Fix Is Important
While this specific error is non-critical (cleanup in error path), properly handling errors:
Files Modified
pkg/cli/mcp_inspect.go:References
🤖 Generated by Security Fix Agent in workflow run 20534157143