feat: add regex pattern to mask API keys in sensitive information - #2587
Conversation
WalkthroughA new regex pattern was added to detect API key patterns (e.g., Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
common/str.go (1)
19-20: Consider broadening the pattern and validating quote matching.The current pattern has some limitations:
- Mismatched quotes: The pattern doesn't enforce that opening and closing quotes match. For example,
'api_key:secret"would incorrectly match.- Narrow scope: Only matches the exact string
api_key:(case-sensitive). Common variations likeapiKey:,api-key:,API_KEY:,access_token:,secret_key:, etc., won't be caught.- Case sensitivity:
API_KEY:xxxorApi_Key:xxxwon't be masked.🔎 Proposed improvements
Option 1: Enforce quote matching (minimal fix)
- maskApiKeyPattern = regexp.MustCompile(`(['"]?)api_key:([^\s'"]+)(['"]?)`) + maskApiKeyPattern = regexp.MustCompile(`(['"]?)api_key:([^\s'"]+)\1`)Using
\1as a backreference ensures the closing quote matches the opening quote.Option 2: Broader pattern to catch more API key formats (recommended)
- maskApiKeyPattern = regexp.MustCompile(`(['"]?)api_key:([^\s'"]+)(['"]?)`) + maskApiKeyPattern = regexp.MustCompile(`(?i)(['"]?)(?:api[_-]?key|access[_-]?token|secret[_-]?key)\s*:\s*([^\s'"]+)\1`)This pattern:
- Uses
(?i)for case-insensitive matching- Matches common API key patterns:
api_key,apikey,api-key,access_token,secret_key, etc.- Allows optional whitespace around the colon
- Uses
\1to enforce matching quotesYou'll also need to adjust the replacement pattern:
- str = maskApiKeyPattern.ReplaceAllString(str, "${1}api_key:***${3}") + str = maskApiKeyPattern.ReplaceAllString(str, "${1}***:***${1}")
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
common/str.go
🧰 Additional context used
🪛 Gitleaks (8.30.0)
common/str.go
[high] 240-240: Uncovered a GCP API key, which could lead to unauthorized access to Google Cloud services and data breaches.
(gcp-api-key)
🔇 Additional comments (1)
common/str.go (1)
240-242: The replacement logic looks correct, and the static analysis warning is a false positive.The replacement pattern
${1}api_key:***${3}correctly preserves the opening and closing quotes while masking the API key value.Regarding the Gitleaks warning: Line 240 contains an example API key in a comment for documentation purposes, not an actual secret. This is a false positive and can be safely ignored.
Note: If you implement the broader pattern suggested in the previous comment, remember to update the replacement pattern accordingly to avoid hardcoding
api_key:.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.