Files
Charon/backend/internal/services/security_notification_service.go
GitHub Actions e0f69cdfc8 feat(security): comprehensive SSRF protection implementation
BREAKING CHANGE: UpdateService.SetAPIURL() now returns error

Implements defense-in-depth SSRF protection across all user-controlled URLs:

Security Fixes:
- CRITICAL: Fixed security notification webhook SSRF vulnerability
- CRITICAL: Added GitHub domain allowlist for update service
- HIGH: Protected CrowdSec hub URLs with domain allowlist
- MEDIUM: Validated CrowdSec LAPI URLs (localhost-only)

Implementation:
- Created /backend/internal/security/url_validator.go (90.4% coverage)
- Blocks 13+ private IP ranges and cloud metadata endpoints
- DNS resolution with timeout and IP validation
- Comprehensive logging of SSRF attempts (HIGH severity)
- Defense-in-depth: URL format → DNS → IP → Request execution

Testing:
- 62 SSRF-specific tests covering all attack vectors
- 255 total tests passing (84.8% coverage)
- Zero security vulnerabilities (Trivy, go vuln check)
- OWASP A10 compliant

Documentation:
- Comprehensive security guide (docs/security/ssrf-protection.md)
- Manual test plan (30 test cases)
- Updated API docs, README, SECURITY.md, CHANGELOG

Security Impact:
- Pre-fix: CVSS 8.6 (HIGH) - Exploitable SSRF
- Post-fix: CVSS 0.0 (NONE) - Vulnerability eliminated

Refs: #450 (beta release)
See: docs/plans/ssrf_remediation_spec.md for full specification
2025-12-23 15:09:22 +00:00

158 lines
4.2 KiB
Go

package services
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/Wikid82/charon/backend/internal/logger"
"github.com/Wikid82/charon/backend/internal/models"
"github.com/Wikid82/charon/backend/internal/security"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
// SecurityNotificationService handles dispatching security event notifications.
type SecurityNotificationService struct {
db *gorm.DB
}
// NewSecurityNotificationService creates a new SecurityNotificationService instance.
func NewSecurityNotificationService(db *gorm.DB) *SecurityNotificationService {
return &SecurityNotificationService{db: db}
}
// GetSettings retrieves the notification configuration.
func (s *SecurityNotificationService) GetSettings() (*models.NotificationConfig, error) {
var config models.NotificationConfig
err := s.db.First(&config).Error
if err == gorm.ErrRecordNotFound {
// Return default config if none exists
return &models.NotificationConfig{
Enabled: false,
MinLogLevel: "error",
NotifyWAFBlocks: true,
NotifyACLDenies: true,
}, nil
}
return &config, err
}
// UpdateSettings updates the notification configuration.
func (s *SecurityNotificationService) UpdateSettings(config *models.NotificationConfig) error {
var existing models.NotificationConfig
err := s.db.First(&existing).Error
if err == gorm.ErrRecordNotFound {
// Create new config
return s.db.Create(config).Error
}
if err != nil {
return fmt.Errorf("fetch existing config: %w", err)
}
// Update existing config
config.ID = existing.ID
return s.db.Save(config).Error
}
// Send dispatches a security event to configured channels.
func (s *SecurityNotificationService) Send(ctx context.Context, event models.SecurityEvent) error {
config, err := s.GetSettings()
if err != nil {
return fmt.Errorf("get settings: %w", err)
}
if !config.Enabled {
return nil
}
// Check if event type should be notified
if event.EventType == "waf_block" && !config.NotifyWAFBlocks {
return nil
}
if event.EventType == "acl_deny" && !config.NotifyACLDenies {
return nil
}
// Check severity against minimum log level
if !shouldNotify(event.Severity, config.MinLogLevel) {
return nil
}
// Dispatch to webhook if configured
if config.WebhookURL != "" {
if err := s.sendWebhook(ctx, config.WebhookURL, event); err != nil {
logger.Log().WithError(err).Error("Failed to send webhook notification")
return fmt.Errorf("send webhook: %w", err)
}
}
return nil
}
// sendWebhook sends the event to a webhook URL.
func (s *SecurityNotificationService) sendWebhook(ctx context.Context, webhookURL string, event models.SecurityEvent) error {
// CRITICAL FIX: Validate webhook URL before making request (SSRF protection)
validatedURL, err := security.ValidateExternalURL(webhookURL,
security.WithAllowLocalhost(), // Allow localhost for testing
security.WithAllowHTTP(), // Some webhooks use HTTP
)
if err != nil {
// Log SSRF attempt with high severity
logger.Log().WithFields(logrus.Fields{
"url": webhookURL,
"error": err.Error(),
"event_type": "ssrf_blocked",
"severity": "HIGH",
}).Warn("Blocked SSRF attempt in security notification webhook")
return fmt.Errorf("invalid webhook URL: %w", err)
}
payload, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("marshal event: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "POST", validatedURL, bytes.NewBuffer(payload))
if err != nil {
return fmt.Errorf("create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "Charon-Cerberus/1.0")
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("execute request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("webhook returned status %d", resp.StatusCode)
}
return nil
}
// shouldNotify determines if an event should trigger a notification based on severity.
func shouldNotify(eventSeverity, minLevel string) bool {
levels := map[string]int{
"debug": 0,
"info": 1,
"warn": 2,
"error": 3,
}
eventLevel := levels[eventSeverity]
minLevelValue := levels[minLevel]
return eventLevel >= minLevelValue
}