| 61 | } |
| 62 | |
| 63 | func (s subdomain) match(o string) bool { |
| 64 | isValid, normalizedOrigin := normalizeOrigin(o) |
| 65 | if !isValid || normalizedOrigin != o { |
| 66 | return false |
| 67 | } |
| 68 | |
| 69 | // Not a subdomain if not long enough for a dot separator. |
| 70 | if len(o) < len(s.prefix)+len(s.suffix)+1 { |
| 71 | return false |
| 72 | } |
| 73 | |
| 74 | if !strings.HasPrefix(o, s.prefix) || !strings.HasSuffix(o, s.suffix) { |
| 75 | return false |
| 76 | } |
| 77 | |
| 78 | // Check for the dot separator and validate that there is at least one |
| 79 | // non-empty label between prefix and suffix. Empty labels like |
| 80 | // "https://.example.com" or "https://..example.com" should not match. |
| 81 | suffixStartIndex := len(o) - len(s.suffix) |
| 82 | if suffixStartIndex <= len(s.prefix) { |
| 83 | return false |
| 84 | } |
| 85 | if o[suffixStartIndex-1] != '.' { |
| 86 | return false |
| 87 | } |
| 88 | |
| 89 | // Extract the subdomain part (without the trailing dot) and ensure it |
| 90 | // doesn't contain empty labels. |
| 91 | sub := o[len(s.prefix) : suffixStartIndex-1] |
| 92 | if sub == "" || strings.HasPrefix(sub, ".") || strings.HasSuffix(sub, ".") || strings.Contains(sub, "..") { |
| 93 | return false |
| 94 | } |
| 95 | |
| 96 | return true |
| 97 | } |