ValidateWebhookURL validates that the webhook URL matches the allowed domains for the webhook type.
(webhookType storepb.WebhookType, webhookURL string)
| 57 | |
| 58 | // ValidateWebhookURL validates that the webhook URL matches the allowed domains for the webhook type. |
| 59 | func ValidateWebhookURL(webhookType storepb.WebhookType, webhookURL string) error { |
| 60 | // Parse URL |
| 61 | u, err := url.Parse(webhookURL) |
| 62 | if err != nil { |
| 63 | return errors.Wrapf(err, "invalid URL format") |
| 64 | } |
| 65 | |
| 66 | // Only allow http/https |
| 67 | if u.Scheme != "http" && u.Scheme != "https" { |
| 68 | return errors.Errorf("invalid URL scheme: %s (only http and https are allowed)", u.Scheme) |
| 69 | } |
| 70 | |
| 71 | // Get allowed domains for this webhook type |
| 72 | allowedDomainsForType, ok := allowedDomains[webhookType] |
| 73 | if !ok { |
| 74 | return errors.Errorf("unknown webhook type: %s", webhookType) |
| 75 | } |
| 76 | |
| 77 | // Merge with test-only allowed domains |
| 78 | allAllowedDomains := append([]string{}, allowedDomainsForType...) |
| 79 | if testDomains, exists := TestOnlyAllowedDomains[webhookType]; exists { |
| 80 | allAllowedDomains = append(allAllowedDomains, testDomains...) |
| 81 | } |
| 82 | |
| 83 | // Check if hostname matches any allowed domain |
| 84 | hostname := strings.ToLower(u.Hostname()) |
| 85 | for _, domain := range allAllowedDomains { |
| 86 | domain = strings.ToLower(domain) |
| 87 | |
| 88 | // Support wildcard subdomains (e.g., ".office.com" matches "outlook.office.com") |
| 89 | if strings.HasPrefix(domain, ".") { |
| 90 | if hostname == domain[1:] || strings.HasSuffix(hostname, domain) { |
| 91 | return nil |
| 92 | } |
| 93 | } else { |
| 94 | // Exact match |
| 95 | if hostname == domain { |
| 96 | if webhookType == storepb.WebhookType_GOOGLE_CHAT { |
| 97 | return validateGoogleChatURL(u) |
| 98 | } |
| 99 | return nil |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return errors.Errorf("webhook URL domain %q is not allowed for webhook type %s (allowed domains: %v)", |
| 105 | hostname, webhookType, allowedDomainsForType) |
| 106 | } |
| 107 | |
| 108 | func validateGoogleChatURL(u *url.URL) error { |
| 109 | if u.Scheme != "https" { |