rastrillo / aviso Public

Clone
git clone https://amadan.net/rastrillo/aviso

Plain git — no account needed to clone.

Download

Download this file

1package aviso
2
3import (
4 "errors"
5 "fmt"
6 "net"
7 "net/http"
8 "net/url"
9 "syscall"
10 "time"
11)
12
13// ErrBadEndpoint means a subscription endpoint was refused before any
14// request: wrong scheme, credentials, fragment, too long, or an
15// address no push service lives at.
16var ErrBadEndpoint = errors.New("aviso: endpoint refused")
17
18const maxEndpointLen = 2048
19
20// validateEndpoint is the request-time half of the SSRF guard: shape
21// and literal-IP checks. The dial-time half (guardedIP in the dialer's
22// Control) catches what a hostname resolves to, which this cannot.
23func validateEndpoint(raw string) error {
24 if raw == "" || len(raw) > maxEndpointLen {
25 return fmt.Errorf("%w: empty or over %d bytes", ErrBadEndpoint, maxEndpointLen)
26 }
27 u, err := url.Parse(raw)
28 if err != nil || u.Scheme != "https" || u.Host == "" || u.Hostname() == "" ||
29 u.User != nil || u.Fragment != "" || u.RawFragment != "" {
30 return fmt.Errorf("%w: must be https, no credentials, no fragment", ErrBadEndpoint)
31 }
32 if ip := net.ParseIP(u.Hostname()); ip != nil {
33 if err := guardedIP(ip); err != nil {
34 return fmt.Errorf("%w: %v", ErrBadEndpoint, err)
35 }
36 }
37 return nil
38}
39
40// reservedNets are the ranges net.IP's own predicates do not cover,
41// from IANA's special-purpose registries: "this network", IETF
42// protocol assignments, the documentation and benchmarking nets,
43// class E; and for IPv6 the discard prefix, benchmarking, both
44// documentation prefixes, and the NAT64 prefixes — the well-known one
45// and RFC 8215's local-use one, whose low 32 bits are an IPv4 address
46// the IPv4 rules would otherwise never see, and which a local
47// translator may point at private space.
48var reservedNets = func() []*net.IPNet {
49 var out []*net.IPNet
50 for _, c := range []string{
51 "0.0.0.0/8", "192.0.0.0/24", "192.0.2.0/24", "198.18.0.0/15",
52 "198.51.100.0/24", "203.0.113.0/24", "240.0.0.0/4",
53 "64:ff9b::/96", "64:ff9b:1::/48", "100::/64", "2001:2::/48",
54 "2001:db8::/32", "3fff::/20",
55 } {
56 _, n, err := net.ParseCIDR(c)
57 if err != nil {
58 panic(err)
59 }
60 out = append(out, n)
61 }
62 return out
63}()
64
65// guardedIP refuses every address a push service cannot legitimately
66// have: loopback, private, link-local, unspecified, multicast, CGNAT,
67// the reserved ranges above, and their IPv4-mapped forms. Applied at
68// connect time so DNS rebinding after validation still fails.
69func guardedIP(ip net.IP) error {
70 if ip == nil {
71 return errors.New("address is not an IP")
72 }
73 if ip4 := ip.To4(); ip4 != nil {
74 ip = ip4
75 }
76 refuse := func() error { return fmt.Errorf("address %s is not routable to a push service", ip) }
77 switch {
78 case ip.IsLoopback(), ip.IsPrivate(), ip.IsLinkLocalUnicast(), ip.IsLinkLocalMulticast(),
79 ip.IsUnspecified(), ip.IsMulticast(), ip.IsInterfaceLocalMulticast():
80 return refuse()
81 }
82 if len(ip) == net.IPv4len && ip[0] == 100 && ip[1]&0xc0 == 64 { // 100.64.0.0/10, CGNAT
83 return refuse()
84 }
85 for _, n := range reservedNets {
86 if n.Contains(ip) {
87 return refuse()
88 }
89 }
90 return nil
91}
92
93// newClient is the only HTTP client that ever talks to a push
94// service: no redirects (a push service never redirects, and following
95// one is how a validated URL turns into an internal one), no proxy
96// from the environment (same reason), and the IP guard inside the
97// dialer's Control so it runs on the address actually connected to.
98func newClient() *http.Client {
99 dialer := &net.Dialer{
100 Timeout: 10 * time.Second,
101 Control: func(network, address string, _ syscall.RawConn) error {
102 host, _, err := net.SplitHostPort(address)
103 if err != nil {
104 return fmt.Errorf("aviso: dial: %w", err)
105 }
106 ip := net.ParseIP(host)
107 if ip == nil {
108 return fmt.Errorf("aviso: dial: %q is not an IP", host)
109 }
110 if err := guardedIP(ip); err != nil {
111 return fmt.Errorf("aviso: dial refused: %w", err)
112 }
113 return nil
114 },
115 }
116 return &http.Client{
117 Timeout: 30 * time.Second,
118 Transport: &http.Transport{
119 Proxy: nil,
120 DialContext: dialer.DialContext,
121 TLSHandshakeTimeout: 10 * time.Second,
122 MaxIdleConns: 64,
123 IdleConnTimeout: 90 * time.Second,
124 },
125 CheckRedirect: func(*http.Request, []*http.Request) error {
126 return errors.New("aviso: push service redirected; refused")
127 },
128 }
129}
130