package app import ( "bufio" "context" "encoding/base64" "fmt" "io" "net" "net/http" "net/netip" "net/url" "strings" "sync" "sync/atomic" "time" ) func parseWebURL(raw string) (*url.URL, error) { u, err := url.Parse(raw) if err != nil || len(raw) > 2048 || u == nil || u.Hostname() == "" || u.User != nil || (u.Scheme != "http" && u.Scheme != "https") { return nil, fmt.Errorf("only absolute http(s) urls without embedded credentials are supported") } return u, nil } func publicIP(a netip.Addr) bool { a = a.Unmap() if !a.IsGlobalUnicast() || a.IsPrivate() || a.IsLoopback() || a.IsLinkLocalUnicast() { return false } for _, raw := range []string{"0.0.0.0/8", "100.64.0.0/10", "192.0.0.0/24", "192.0.2.0/24", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "240.0.0.0/4", "2001:db8::/32"} { if netip.MustParsePrefix(raw).Contains(a) { return false } } return true } type Network struct { AllowPrivate bool proxies []*url.URL proxyCounter atomic.Uint64 transport *http.Transport client *http.Client mu sync.Mutex byProxy map[string]*http.Client } func newNetwork(allowPrivate bool) *Network { return newNetworkWithPool(allowPrivate, nil) } // newNetworkWithPool builds the guarded client. A proxy pool routes egress // through upstream proxies in rotation; an empty pool dials directly. func newNetworkWithPool(allowPrivate bool, proxies []*url.URL) *Network { n := &Network{AllowPrivate: allowPrivate, proxies: proxies, byProxy: map[string]*http.Client{}} n.transport = n.newTransport(nil) n.client = n.newClient(n.transport) return n } // newTransport builds a transport whose connections are guarded, and which // routes through proxy when one is selected. func (n *Network) newTransport(proxy *url.URL) *http.Transport { t := &http.Transport{DialContext: n.dial, MaxIdleConns: 32, MaxIdleConnsPerHost: 4, IdleConnTimeout: 30 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ResponseHeaderTimeout: 20 * time.Second, ForceAttemptHTTP2: true} if proxy != nil { t.Proxy = http.ProxyURL(proxy) } return t } func (n *Network) newClient(t *http.Transport) *http.Client { return &http.Client{Transport: t, Timeout: 35 * time.Second, CheckRedirect: func(r *http.Request, via []*http.Request) error { if len(via) >= 10 { return fmt.Errorf("too many redirects") } _, err := parseWebURL(r.URL.String()) return err }} } // clientFor returns a client routed through proxy, caching one per proxy so // connections are reused. A nil proxy selects the direct, guarded client. // // The proxy is fixed for the whole fetch: redirects and retries must not move // to a different egress address mid-session. func (n *Network) clientFor(proxy *url.URL) *http.Client { if proxy == nil { return n.client } key := proxy.String() n.mu.Lock() defer n.mu.Unlock() if c, ok := n.byProxy[key]; ok { return c } c := n.newClient(n.newTransport(proxy)) n.byProxy[key] = c return c } // closeIdle releases pooled connections, including those held for proxies. func (n *Network) closeIdle() { n.transport.CloseIdleConnections() n.mu.Lock() defer n.mu.Unlock() for _, c := range n.byProxy { if t, ok := c.Transport.(*http.Transport); ok { t.CloseIdleConnections() } } } // pickProxy rotates through the configured pool, returning nil for a direct // connection. One call per fetch keeps an egress address stable for that fetch. func (n *Network) pickProxy() *url.URL { if len(n.proxies) == 0 { return nil } index := n.proxyCounter.Add(1) - 1 return n.proxies[index%uint64(len(n.proxies))] } // guard is the local HTTP proxy Chrome is pointed at. Every connection the // browser makes is dialed through it, so redirects and subresources cannot // escape the policy. It optionally tunnels through an upstream proxy. type guard struct { net *Network upstream *url.URL } // dialTarget opens a connection to address, through the render's upstream proxy // when one was selected and directly through the guarded dialer otherwise. // // The proxy is reached through the same guarded dialer, so a proxy address on a // private or reserved network is refused unless private targets are allowed. func (g *guard) dialTarget(ctx context.Context, address string) (net.Conn, error) { if g.upstream == nil { return g.net.dial(ctx, "tcp", address) } return g.net.dialViaProxy(ctx, g.upstream, address) } // dialViaProxy establishes a CONNECT tunnel to address through an HTTP proxy. // The proxy resolves the destination, so the destination address cannot be // range-checked here; only the proxy itself is. func (n *Network) dialViaProxy(ctx context.Context, proxy *url.URL, address string) (net.Conn, error) { port := proxy.Port() if port == "" { if proxy.Scheme == "https" { port = "443" } else { port = "80" } } conn, err := n.dial(ctx, "tcp", net.JoinHostPort(proxy.Hostname(), port)) if err != nil { return nil, fmt.Errorf("proxy connect failed: %w", err) } req := &http.Request{ Method: http.MethodConnect, URL: &url.URL{Opaque: address}, Host: address, Header: http.Header{}, } if user := proxy.User; user != nil { password, _ := user.Password() credentials := base64.StdEncoding.EncodeToString([]byte(user.Username() + ":" + password)) req.Header.Set("Proxy-Authorization", "Basic "+credentials) } if err := req.Write(conn); err != nil { conn.Close() return nil, fmt.Errorf("proxy request failed: %w", err) } reader := bufio.NewReader(conn) res, err := http.ReadResponse(reader, req) if err != nil { conn.Close() return nil, fmt.Errorf("proxy response failed: %w", err) } res.Body.Close() if res.StatusCode != http.StatusOK { conn.Close() return nil, fmt.Errorf("proxy refused the tunnel: %s", res.Status) } // The proxy may have pipelined tunnel bytes into the same read, so they must // be drained before the raw connection is used. if reader.Buffered() > 0 { return &bufferedConn{Conn: conn, reader: reader}, nil } return conn, nil } // bufferedConn returns bytes already buffered by a proxy handshake before // reading from the underlying connection. type bufferedConn struct { net.Conn reader *bufio.Reader } func (c *bufferedConn) Read(p []byte) (int, error) { return c.reader.Read(p) } func (n *Network) dial(ctx context.Context, network, address string) (net.Conn, error) { host, port, err := net.SplitHostPort(address) if err != nil { return nil, err } ips, err := net.DefaultResolver.LookupNetIP(ctx, "ip", host) if err != nil { return nil, fmt.Errorf("resolve target: %w", err) } if len(ips) == 0 { return nil, fmt.Errorf("target has no addresses") } for _, ip := range ips { if !n.AllowPrivate && !publicIP(ip) { return nil, fmt.Errorf("private or reserved network target is blocked") } } var last error for _, ip := range ips { c, err := (&net.Dialer{Timeout: 10 * time.Second, KeepAlive: 30 * time.Second}).DialContext(ctx, network, net.JoinHostPort(ip.String(), port)) if err == nil { return c, nil } last = err } return nil, last } // Chrome sends every connection to this handler, including redirects and // subresources. CONNECT pins the checked address, avoiding a separate // check-then-browser-DNS lookup that would permit DNS rebinding. func (g *guard) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodConnect { up, err := g.dialTarget(r.Context(), r.Host) if err != nil { http.Error(w, "blocked destination", http.StatusForbidden) return } down, buf, err := w.(http.Hijacker).Hijack() if err != nil { up.Close() return } _, err = buf.WriteString("HTTP/1.1 200 Connection Established\r\n\r\n") if err == nil { err = buf.Flush() } if err != nil { up.Close() down.Close() return } up.SetDeadline(time.Now().Add(95 * time.Second)) down.SetDeadline(time.Now().Add(95 * time.Second)) go func() { defer up.Close() defer down.Close() go func() { io.Copy(up, buf); up.Close() }() io.Copy(down, up) }() return } if _, err := parseWebURL(r.URL.String()); err != nil { http.Error(w, "invalid destination", 400) return } r = r.Clone(r.Context()) r.RequestURI = "" r.Header.Del("Proxy-Authorization") r.Header.Del("Proxy-Connection") transport, ok := g.net.clientFor(g.upstream).Transport.(*http.Transport) if !ok { http.Error(w, "destination unavailable", 502) return } res, err := transport.RoundTrip(r) if err != nil { http.Error(w, "destination unavailable", 502) return } defer res.Body.Close() for key, values := range res.Header { if strings.EqualFold(key, "Connection") { continue } for _, v := range values { w.Header().Add(key, v) } } w.WriteHeader(res.StatusCode) io.Copy(w, res.Body) } func readBounded(r io.Reader, limit int) ([]byte, error) { b, err := io.ReadAll(io.LimitReader(r, int64(limit)+1)) if err != nil { return nil, err } if len(b) > limit { return nil, fmt.Errorf("response exceeds %d bytes", limit) } return b, nil }