fix(security): support IPv6 in SSRF validation via whitelist mechanism

- Keep strict mode blocking all direct IPs (IPv4 and IPv6 uniformly)
- Unify all SSRF call sites to use ValidateURLForSSRF (whitelist-aware)
- Add Teredo (2001:0000::/32) and 6to4 (2002::/16) tunnel detection
- Make redirect handler and DNS pinning respect SSRF_WHITELIST
- Unexport isSSRFSafeURL to prevent future callers bypassing whitelist
- Add scheme validation for whitelisted redirect targets
- Document IPv6 whitelist syntax in .env.example
- Add comprehensive IPv6 test coverage
This commit is contained in:
nullkey
2026-03-31 18:52:25 +08:00
committed by lyingbug
parent a5ca9a2784
commit 0b64ef9bd1
9 changed files with 231 additions and 52 deletions
+7 -5
View File
@@ -262,9 +262,9 @@ func (t *WebFetchTool) validateAndResolve(p webFetchParams) (*validatedParams, e
return nil, fmt.Errorf("invalid URL format")
}
// SSRF protection: validate URL is safe (scheme, hostname, and that resolved IPs are not restricted)
if safe, reason := utils.IsSSRFSafeURL(p.URL); !safe {
return nil, fmt.Errorf("URL rejected for security reasons: %s", reason)
// SSRF protection: validate URL is safe (uses centralised entry-point with whitelist support)
if err := utils.ValidateURLForSSRF(p.URL); err != nil {
return nil, fmt.Errorf("URL rejected for security reasons: %v", err)
}
u, err := url.Parse(p.URL)
@@ -281,14 +281,16 @@ func (t *WebFetchTool) validateAndResolve(p webFetchParams) (*validatedParams, e
}
}
// Resolve and pin to the first public IP (same resolver as IsSSRFSafeURL; we pin so chromedp cannot re-resolve)
// Resolve and pin to the first safe IP (same resolver as isSSRFSafeURL; we pin so chromedp cannot re-resolve).
// Whitelisted hosts may resolve to private/restricted IPs, so we allow any IP for them.
ips, err := net.DefaultResolver.LookupIP(context.Background(), "ip", hostname)
if err != nil || len(ips) == 0 {
return nil, fmt.Errorf("DNS lookup failed for %s: %w", hostname, err)
}
isWhitelisted := utils.IsSSRFWhitelisted(hostname)
var pinnedIP net.IP
for _, ip := range ips {
if utils.IsPublicIP(ip) {
if isWhitelisted || utils.IsPublicIP(ip) {
pinnedIP = ip
break
}