| 1 | // Package websource is the one URL contract for Research's web sources |
| 2 | // (spec §3.1). A URL is parsed and made canonical once, and that form is |
| 3 | // what a card shows, what the runner requests and what the logs record. |
| 4 | // The instance and the runner share it. Pure and stdlib only: no lookup, |
| 5 | // no network. |
| 6 | package websource |
| 7 | |
| 8 | import ( |
| 9 | "errors" |
| 10 | "net/netip" |
| 11 | "net/url" |
| 12 | "strings" |
| 13 | ) |
| 14 | |
| 15 | // Bounds. |
| 16 | const ( |
| 17 | // MaxURL bounds a URL, raw or canonical, in bytes. |
| 18 | MaxURL = 2048 |
| 19 | // MaxProposalPath bounds a coordinator proposal's escaped path. |
| 20 | MaxProposalPath = 100 |
| 21 | ) |
| 22 | |
| 23 | // Refusal is a stable reason code. It is the error's whole text, so it is |
| 24 | // safe to log and to send on the wire. |
| 25 | type Refusal string |
| 26 | |
| 27 | func (r Refusal) Error() string { return string(r) } |
| 28 | |
| 29 | // Refusals. |
| 30 | const ( |
| 31 | ErrBadURL Refusal = "bad_url" |
| 32 | ErrTooLong Refusal = "url_too_long" |
| 33 | ErrScheme Refusal = "scheme_refused" |
| 34 | ErrUserinfo Refusal = "userinfo_refused" |
| 35 | ErrPort Refusal = "port_refused" |
| 36 | ErrHost Refusal = "host_refused" |
| 37 | ErrIPLiteral Refusal = "ip_literal" |
| 38 | ErrNonASCII Refusal = "non_ascii_host" |
| 39 | ErrQuery Refusal = "query_refused" |
| 40 | ErrPathTooLong Refusal = "path_too_long" |
| 41 | ErrSecret Refusal = "secret_refused" |
| 42 | ) |
| 43 | |
| 44 | // Reason is err's reason code: "" for nil, bad_url for an error that is |
| 45 | // not a Refusal. |
| 46 | func Reason(err error) string { |
| 47 | if err == nil { |
| 48 | return "" |
| 49 | } |
| 50 | var r Refusal |
| 51 | if errors.As(err, &r) { |
| 52 | return string(r) |
| 53 | } |
| 54 | return string(ErrBadURL) |
| 55 | } |
| 56 | |
| 57 | // URL is a canonical web address: scheme https or http, a lower-case ASCII |
| 58 | // host with no trailing dot, the scheme's own port (443 or 80, nothing |
| 59 | // else), the escaped path ("/" when empty) and the raw query, kept as |
| 60 | // they arrived. No userinfo, no fragment. |
| 61 | type URL struct { |
| 62 | Scheme string `json:"scheme"` |
| 63 | Host string `json:"host"` |
| 64 | Port int `json:"port"` |
| 65 | EscapedPath string `json:"path"` |
| 66 | RawQuery string `json:"query,omitempty"` |
| 67 | } |
| 68 | |
| 69 | // String is the canonical form. Parse(u.String()) == u for every URL |
| 70 | // Parse returned. |
| 71 | func (u URL) String() string { |
| 72 | s := u.Scheme + "://" + u.Host + u.EscapedPath |
| 73 | if u.RawQuery != "" { |
| 74 | s += "?" + u.RawQuery |
| 75 | } |
| 76 | return s |
| 77 | } |
| 78 | |
| 79 | // IsZero reports the zero URL. |
| 80 | func (u URL) IsZero() bool { return u == URL{} } |
| 81 | |
| 82 | // Parse makes raw canonical, or refuses it with a Refusal. |
| 83 | func Parse(raw string) (URL, error) { |
| 84 | if raw == "" { |
| 85 | return URL{}, ErrBadURL |
| 86 | } |
| 87 | if len(raw) > MaxURL { |
| 88 | return URL{}, ErrTooLong |
| 89 | } |
| 90 | p, err := url.Parse(raw) |
| 91 | if err != nil { |
| 92 | return URL{}, ErrBadURL |
| 93 | } |
| 94 | return fromParsed(raw, p) |
| 95 | } |
| 96 | |
| 97 | func fromParsed(raw string, p *url.URL) (URL, error) { |
| 98 | if p.Scheme != "https" && p.Scheme != "http" { |
| 99 | return URL{}, ErrScheme |
| 100 | } |
| 101 | if p.Opaque != "" || p.Host == "" { |
| 102 | return URL{}, ErrBadURL |
| 103 | } |
| 104 | if p.User != nil { |
| 105 | return URL{}, ErrUserinfo |
| 106 | } |
| 107 | if strings.HasPrefix(p.Host, "[") { |
| 108 | return URL{}, ErrIPLiteral |
| 109 | } |
| 110 | host := p.Hostname() |
| 111 | for i := 0; i < len(host); i++ { |
| 112 | if host[i] >= 0x80 { |
| 113 | return URL{}, ErrNonASCII |
| 114 | } |
| 115 | } |
| 116 | // Spaces, control bytes, non-ASCII and backslashes are refused |
| 117 | // anywhere: a browser and Go read them differently. |
| 118 | for i := 0; i < len(raw); i++ { |
| 119 | if c := raw[i]; c <= 0x20 || c >= 0x7f || c == '\\' { |
| 120 | return URL{}, ErrBadURL |
| 121 | } |
| 122 | } |
| 123 | host = strings.ToLower(host) |
| 124 | host = strings.TrimSuffix(host, ".") |
| 125 | if err := checkHost(host); err != nil { |
| 126 | return URL{}, err |
| 127 | } |
| 128 | u := URL{Scheme: p.Scheme, Host: host, Port: 443} |
| 129 | if p.Scheme == "http" { |
| 130 | u.Port = 80 |
| 131 | } |
| 132 | if port := p.Port(); port != "" && port != map[string]string{"https": "443", "http": "80"}[p.Scheme] { |
| 133 | return URL{}, ErrPort |
| 134 | } |
| 135 | u.EscapedPath = p.EscapedPath() |
| 136 | if u.EscapedPath == "" { |
| 137 | u.EscapedPath = "/" |
| 138 | } |
| 139 | if !strings.HasPrefix(u.EscapedPath, "/") { |
| 140 | return URL{}, ErrBadURL |
| 141 | } |
| 142 | u.RawQuery = p.RawQuery |
| 143 | if len(u.String()) > MaxURL { |
| 144 | return URL{}, ErrTooLong |
| 145 | } |
| 146 | return u, nil |
| 147 | } |
| 148 | |
| 149 | // refusedSuffixes are names that never leave the machine or the site. |
| 150 | var refusedSuffixes = []string{".localhost", ".local", ".internal", ".lan", ".home.arpa", ".arpa", ".ts.net", ".onion", ".test", ".invalid", ".example"} |
| 151 | |
| 152 | // CheckHost applies §3.1's host rules to a lower-case host with its |
| 153 | // trailing dot gone: a registered name whose last label has a letter, no |
| 154 | // IP literal in any form, and none of the names that never leave the |
| 155 | // machine or the site. The guarded client applies it to every hop. |
| 156 | func CheckHost(host string) error { return checkHost(host) } |
| 157 | |
| 158 | // checkHost is CheckHost. |
| 159 | func checkHost(host string) error { |
| 160 | labels := strings.Split(host, ".") |
| 161 | numeric := true |
| 162 | for _, l := range labels { |
| 163 | if !numericLabel(l) { |
| 164 | numeric = false |
| 165 | break |
| 166 | } |
| 167 | } |
| 168 | // 127.1, 2130706433, 0177.0.0.1, 0x7f.0.0.1, 1.0x7f: inet_aton reads |
| 169 | // every one as an address. |
| 170 | if numeric { |
| 171 | return ErrIPLiteral |
| 172 | } |
| 173 | if host == "localhost" { |
| 174 | return ErrHost |
| 175 | } |
| 176 | for _, s := range refusedSuffixes { |
| 177 | if strings.HasSuffix(host, s) || host == s[1:] { |
| 178 | return ErrHost |
| 179 | } |
| 180 | } |
| 181 | last := labels[len(labels)-1] |
| 182 | if !Hostname(host) || numericLabel(last) || !strings.ContainsAny(last, "abcdefghijklmnopqrstuvwxyz") { |
| 183 | return ErrHost |
| 184 | } |
| 185 | return nil |
| 186 | } |
| 187 | |
| 188 | // numericLabel is a label inet_aton could read as a number: decimal, |
| 189 | // octal or 0x hex. |
| 190 | func numericLabel(l string) bool { |
| 191 | if l == "" { |
| 192 | return false |
| 193 | } |
| 194 | if strings.HasPrefix(l, "0x") { |
| 195 | for _, c := range l[2:] { |
| 196 | if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f') { |
| 197 | return false |
| 198 | } |
| 199 | } |
| 200 | return true |
| 201 | } |
| 202 | for _, c := range l { |
| 203 | if c < '0' || c > '9' { |
| 204 | return false |
| 205 | } |
| 206 | } |
| 207 | return true |
| 208 | } |
| 209 | |
| 210 | // Hostname is the egress relay's host rule: a lower-case DNS name of at |
| 211 | // least two labels, each 1 to 63 of a-z, 0-9 and an inner hyphen, at most |
| 212 | // 253 bytes, and not an IP address. |
| 213 | func Hostname(s string) bool { |
| 214 | if len(s) > 253 || !strings.Contains(s, ".") || s != strings.ToLower(s) { |
| 215 | return false |
| 216 | } |
| 217 | for _, label := range strings.Split(s, ".") { |
| 218 | if len(label) == 0 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' { |
| 219 | return false |
| 220 | } |
| 221 | for _, c := range label { |
| 222 | if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-') { |
| 223 | return false |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | _, err := netip.ParseAddr(s) |
| 228 | return err != nil |
| 229 | } |
| 230 | |
| 231 | // Resolve is ref, a link on the page at base, as a canonical URL. |
| 232 | func Resolve(base URL, ref string) (URL, error) { |
| 233 | if len(ref) > MaxURL { |
| 234 | return URL{}, ErrTooLong |
| 235 | } |
| 236 | r, err := url.Parse(ref) |
| 237 | if err != nil { |
| 238 | return URL{}, ErrBadURL |
| 239 | } |
| 240 | if r.Scheme != "" && r.Scheme != "https" && r.Scheme != "http" { |
| 241 | return URL{}, ErrScheme |
| 242 | } |
| 243 | b, err := url.Parse(base.String()) |
| 244 | if err != nil { |
| 245 | return URL{}, ErrBadURL |
| 246 | } |
| 247 | abs := b.ResolveReference(r) |
| 248 | abs.Fragment, abs.RawFragment = "", "" |
| 249 | return Parse(abs.String()) |
| 250 | } |
| 251 | |
| 252 | // Proposal is Parse plus the rules for a URL the coordinator proposes: |
| 253 | // no query, an escaped path of at most MaxProposalPath bytes, and nothing |
| 254 | // that looks like a secret. |
| 255 | func Proposal(raw string) (URL, error) { |
| 256 | u, err := Parse(raw) |
| 257 | if err != nil { |
| 258 | return URL{}, err |
| 259 | } |
| 260 | if u.RawQuery != "" || strings.Contains(raw, "?") { |
| 261 | return URL{}, ErrQuery |
| 262 | } |
| 263 | if len(u.EscapedPath) > MaxProposalPath { |
| 264 | return URL{}, ErrPathTooLong |
| 265 | } |
| 266 | if LooksSecret(u.String()) { |
| 267 | return URL{}, ErrSecret |
| 268 | } |
| 269 | return u, nil |
| 270 | } |
| 271 | |