siyuan-note/siyuan · error

proxy returned unexpected tunnel data

Error message

proxy returned unexpected tunnel data

What it means

The SSRF-safe proxy RoundTrip establishes a CONNECT tunnel through the proxy, then requires that the buffered reader is fully drained before starting TLS. Leftover bytes mean the proxy sent unexpected data inside the tunnel (protocol violation, or a non-CONNECT response such as an error page), so the connection is closed and this error returned.

Solutions

  1. Fix or replace the proxy so it performs proper CONNECT tunneling with no extra payload
  2. Authenticate/bypass the captive portal before making requests
  3. Check the proxy address/port (HTTP_PROXY/HTTPS_PROXY) points at a real HTTP proxy
  4. Capture the proxy's response (curl --proxy, packet sniffer) to identify the unexpected bytes

Example fix

// before
proxy: "http://proxy.internal:3128"  // intercepting proxy
// after
proxy: null  // direct SSRF-dialed connection, or a compliant proxy
Defensive patterns

Strategy: retry

Validate before calling

// verify the proxy actually tunnels before use
// curl --proxy <proxyURL> -sSI https://example.com

Try / catch

if strings.Contains(err.Error(), "proxy returned unexpected tunnel data") {
    // disable or replace the proxy and retry direct
}

Prevention

When it happens

Trigger: An HTTPS request through the ssrfSafeClient proxy where the proxy responds to CONNECT with extra bytes after the header terminator — e.g. a captive portal, an intercepting corporate proxy returning an HTML error page, or a misbehaving proxy implementation.

Common situations: Corporate/firewall proxies injecting content; hotel/airport captive portals intercepting CONNECT; proxy misconfiguration pointing at an HTTP-only proxy or wrong port; transparent proxies not honoring CONNECT semantics.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/d49d49a64ddd04c5. Report an issue: GitHub.

Appendix: source

Thrown at kernel/util/httprequest.go:109

		return nil, err
	}
	if proxyURL == nil {
		return t.directTransport.RoundTrip(req)
	}

	targetAddr, err := t.resolvePublicTarget(req.Context(), req.URL)
	if err != nil {
		return nil, err
	}
	conn, reader, err := dialProxyTunnel(req.Context(), proxyURL, targetAddr)
	if err != nil {
		return nil, err
	}

	if req.URL.Scheme == "https" {
		if reader.Buffered() != 0 {
			conn.Close()
			return nil, errors.New("proxy returned unexpected tunnel data")
		}
		tlsConn := tls.Client(conn, &tls.Config{ServerName: req.URL.Hostname(), NextProtos: []string{"http/1.1"}})
		if err = tlsConn.HandshakeContext(req.Context()); err != nil {
			conn.Close()
			return nil, err
		}
		conn = tlsConn
		reader = bufio.NewReader(conn)
	}

	targetReq := req.Clone(req.Context())
	targetReq.URL = cloneURL(req.URL)
	targetReq.URL.Scheme = ""
	targetReq.URL.Host = ""
	targetReq.RequestURI = ""
	targetReq.Header.Del("Proxy-Authorization")
	if targetReq.Host == "" {
		targetReq.Host = req.URL.Host

View on GitHub (pinned to 9f775e8a12)