|
| 1 | +// Package validators provides validators for various types of data. |
| 2 | +// |
| 3 | +// Domain validator is a simple utility that ensures two domains are exact |
| 4 | +// matches while ensuring that techniques used to bypass such checks do |
| 5 | +// not impact the validation. |
| 6 | + |
| 7 | +package validators |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "net" |
| 12 | + "net/url" |
| 13 | + "slices" |
| 14 | + "strings" |
| 15 | + |
| 16 | + "golang.org/x/net/idna" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + ErrInvalidURL = fmt.Errorf("invalid url") |
| 21 | + ErrSchemeMismatch = fmt.Errorf("scheme mismatch") |
| 22 | + ErrPortMismatch = fmt.Errorf("port mismatch") |
| 23 | + ErrHostnameMismatch = fmt.Errorf("hostname mismatch") |
| 24 | +) |
| 25 | + |
| 26 | +// DomainValidatorOptions is a set of options for DomainValidator. |
| 27 | +type DomainValidatorOptions struct { |
| 28 | + // Ensure domains have the same scheme. |
| 29 | + WithScheme bool |
| 30 | + // Ensure domains have the same port. |
| 31 | + WithPort bool |
| 32 | + // Specify a list of allowed schemes IF WithScheme is set to true. |
| 33 | + // Leave empty to allow any scheme. |
| 34 | + AllowedSchemes []string |
| 35 | +} |
| 36 | + |
| 37 | +// DomainValidator is a simple utility that ensures two domains are exact |
| 38 | +// matches while ensuring that techniques used to bypass such checks do |
| 39 | +// not impact the validation. |
| 40 | +type DomainValidator struct { |
| 41 | + opts DomainValidatorOptions |
| 42 | +} |
| 43 | + |
| 44 | +// NewDomainValidator creates a new DomainValidator. |
| 45 | +func NewDomainValidator(opts DomainValidatorOptions) *DomainValidator { |
| 46 | + return &DomainValidator{ |
| 47 | + opts: opts, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (v *DomainValidator) getURL(i string) (*url.URL, error) { |
| 52 | + u, err := url.Parse(i) |
| 53 | + |
| 54 | + if !v.opts.WithScheme && (err != nil || u.Host == "") { |
| 55 | + u, err = url.Parse("tinyauth://" + i) |
| 56 | + } |
| 57 | + |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("failed to parse input url: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + if u.Host == "" { |
| 63 | + return nil, ErrInvalidURL |
| 64 | + } |
| 65 | + |
| 66 | + if v.opts.WithPort && !v.opts.WithScheme && u.Port() == "" { |
| 67 | + return nil, fmt.Errorf("port validation is enabled but port is missing in input url and schemes are not enabled") |
| 68 | + } |
| 69 | + |
| 70 | + if v.opts.WithScheme { |
| 71 | + // Empty scheme means that we parsed the url with the tinyauth:// placeholder |
| 72 | + if u.Scheme == "tinyauth" { |
| 73 | + return nil, fmt.Errorf("input url is missing scheme") |
| 74 | + } |
| 75 | + if len(v.opts.AllowedSchemes) > 0 && !slices.Contains(v.opts.AllowedSchemes, u.Scheme) { |
| 76 | + return nil, fmt.Errorf("scheme %s not allowed", u.Scheme) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return u, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (v *DomainValidator) getEffectivePort(u *url.URL) (string, bool) { |
| 84 | + if u.Port() != "" { |
| 85 | + return u.Port(), true |
| 86 | + } |
| 87 | + switch u.Scheme { |
| 88 | + case "http": |
| 89 | + return "80", true |
| 90 | + case "https": |
| 91 | + return "443", true |
| 92 | + default: |
| 93 | + return "", false |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func (v *DomainValidator) formatHostname(hostname string) (string, error) { |
| 98 | + hostname = strings.ToLower(hostname) |
| 99 | + hostname = strings.TrimSuffix(hostname, ".") |
| 100 | + if net.ParseIP(hostname) != nil { |
| 101 | + return "", fmt.Errorf("ip addresses are not supported") |
| 102 | + } |
| 103 | + hostname, err := idna.Lookup.ToASCII(hostname) |
| 104 | + if err != nil { |
| 105 | + return "", fmt.Errorf("failed to convert hostname to ascii: %w", err) |
| 106 | + } |
| 107 | + return hostname, nil |
| 108 | +} |
| 109 | + |
| 110 | +// Validate ensures that two domains are exact matches with the |
| 111 | +// options defined in the DomainValidatorOptions. It ensures that the |
| 112 | +// inputs are proper URLs and contain a host. It lowercases the hostnames |
| 113 | +// and removes the trailing dot. Finally, it checks that the hostnames are |
| 114 | +// equal unless WithScheme or WithPort is set to true where it also |
| 115 | +// validates the scheme and port respectively. |
| 116 | +func (v *DomainValidator) Validate(expected, actual string) error { |
| 117 | + eu, err := v.getURL(expected) |
| 118 | + |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + au, err := v.getURL(actual) |
| 124 | + |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + if v.opts.WithScheme { |
| 130 | + if eu.Scheme != au.Scheme { |
| 131 | + return ErrSchemeMismatch |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if v.opts.WithPort { |
| 136 | + eup, ok := v.getEffectivePort(eu) |
| 137 | + if !ok { |
| 138 | + return fmt.Errorf("failed to get effective port for url: %s", eu.String()) |
| 139 | + } |
| 140 | + aup, ok := v.getEffectivePort(au) |
| 141 | + if !ok { |
| 142 | + return fmt.Errorf("failed to get effective port for url: %s", au.String()) |
| 143 | + } |
| 144 | + if eup != aup { |
| 145 | + return ErrPortMismatch |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + euf, err := v.formatHostname(eu.Hostname()) |
| 150 | + |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + |
| 155 | + auf, err := v.formatHostname(au.Hostname()) |
| 156 | + |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + |
| 161 | + if euf != auf { |
| 162 | + return ErrHostnameMismatch |
| 163 | + } |
| 164 | + |
| 165 | + return nil |
| 166 | +} |
| 167 | + |
| 168 | +// SafeHostname uses the internal validation for domains that Validator uses |
| 169 | +// to parse a hostname. It ensures the input URL is a valid URL, that a host |
| 170 | +// is present and that the hostname is lowercased and without a trailing dot. |
| 171 | +func (v *DomainValidator) SafeHostname(input string) (string, error) { |
| 172 | + u, err := v.getURL(input) |
| 173 | + |
| 174 | + if err != nil { |
| 175 | + return "", err |
| 176 | + } |
| 177 | + |
| 178 | + return v.formatHostname(u.Hostname()) |
| 179 | +} |
0 commit comments