security: implement SSRF protection and privacy redaction#814
security: implement SSRF protection and privacy redaction#814notsointresting wants to merge 2 commits intosipeed:mainfrom
Conversation
- Add SecurityConfig struct with SSRF, Audit Logging, Rate Limiting, Credential Encryption, and Prompt Injection configs - Add environment variable support for all security settings - Add default security settings in defaults.go - Foundation for comprehensive security framework
SSRF Protection (pkg/ssrf/guard.go): - IP blocklist for private ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 127.0.0.0/8) - Cloud metadata endpoint blocking (169.254.169.254) - DNS rebinding protection with caching - Configurable allowed hosts whitelist - Integrated into pkg/tools/web.go Privacy Redaction (pkg/redaction/redaction.go): - Auto-redacts API keys (OpenAI, Anthropic, AWS, etc.) - Masks passwords, tokens, and secrets - Partially masks emails (showing first char and domain) - Redacts phone numbers - Optional IP address redaction - Custom pattern support - Integrated into pkg/logger/logger.go
yinwm
left a comment
There was a problem hiding this comment.
Code Review: SSRF Protection and Privacy Redaction
Thanks for this comprehensive PR! The SSRF protection and privacy redaction features are valuable additions. However, I found several critical issues that need to be addressed before merging.
🔴 Critical Issues
1. Code Overlap with PR #813 - Will Cause Merge Conflicts
This PR contains identical configuration changes as PR #813:
SecurityConfigstruct definition inpkg/config/config.go- Default security settings in
pkg/config/defaults.go DMScopedefault value change- Removal of
RequestTimeoutfields
Recommendation: Split out the configuration changes and depend on PR #813 being merged first.
2. DNS Rebinding Protection is Incomplete
// GetResolvedIPs returns the cached IPs for DNS rebinding protection.
// This should be used when making the actual request to ensure the IP hasn't changed.
func (g *Guard) GetResolvedIPs(host string) []net.IP {While this function exists to return cached IPs, the actual HTTP request doesn't verify that the IP matches the cached value. An attacker could change the DNS record between the initial resolution and the actual request.
Recommendation: Use a custom Dialer to force using the cached IP addresses when making requests.
3. maskEmail Function Has Panic Risk
func (r *Redactor) maskEmail(email string) string {
parts := strings.Split(email, "@")
if len(parts) != 2 {
return r.config.Replacement
}
local := parts[0]
// ...
if len(local) <= 2 {
return string(local[0]) + "***@" + domain // PANIC if local is empty!
}When local is an empty string, accessing local[0] will cause a panic.
Fix:
if len(local) == 0 {
return r.config.Replacement
}
if len(local) <= 2 {
return string(local[0]) + "***@" + domain
}4. Regex Performance Regression - Recompiling on Every Call
In pkg/tools/web.go:
func stripTags(content string) string {
re := regexp.MustCompile(`<[^>]+>`) // Compiled on every call!
return re.ReplaceAllString(content, "")
}
func (t *WebFetchTool) extractText(htmlContent string) string {
re := regexp.MustCompile(`<script[\s\S]*?</script>`) // Compiled on every call!
// ...
}The original code used globally pre-compiled regexes. Now they're compiled on every call, which will significantly degrade performance.
Recommendation: Restore the globally pre-compiled regex expressions.
Summary
| Category | Count |
|---|---|
| 🔴 Critical Issues | 4 |
Verdict: Please address these critical issues before we can proceed with the review. The most important ones are:
- Resolve the merge conflict with PR #813
- Fix the
maskEmailpanic risk - Fix the regex performance regression
Once these are addressed, we can continue reviewing the medium-priority issues.
|
|
|
@notsointresting Hi! This PR has had no activity for over 2 weeks, so I'm closing it for now to keep things organized. Feel free to reopen anytime if you'd like to continue. |
📝 Description
This PR implements SSRF (Server-Side Request Forgery) protection and privacy redaction for sensitive data.
SSRF Protection (
pkg/ssrf/guard.go):pkg/tools/web.goPrivacy Redaction (
pkg/redaction/redaction.go):pkg/logger/logger.go🗣️ Type of Change
🤖 AI Code Generation
🔗 Related Issue
Part of #782
📚 Technical Context (Skip for Docs)
🧪 Test Environment
☑️ Checklist