From 1fb2bb81e6842703e420e331323aeeb1787286bf Mon Sep 17 00:00:00 2001 From: Pawel Rutkowski Date: Mon, 26 Oct 2026 19:47:59 -0500 Subject: [PATCH] derp/derphttp: bound response body read on non-101 upgrade response (#21980) connect read a non-101 response body with an unbounded io.ReadAll and inlined it into the returned error. * Cap the read, format resp.Status instead of the always-nil err, and use %q. * Add a regression test for the bound Updates: tailscale/corp#50441 Change-Id: Ie35fada982ccda4328fd4e9adb795947dee3a283 Signed-off-by: Pawel Rutkowski (cherry picked from commit 73b4c49b881664773e3f1e60a0e9b5653f74e51a) --- derp/derphttp/derphttp_client.go | 7 ++- derp/derphttp/derphttp_client_test.go | 62 +++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 derp/derphttp/derphttp_client_test.go diff --git a/derp/derphttp/derphttp_client.go b/derp/derphttp/derphttp_client.go index dfa0d1b52..8638d8075 100644 --- a/derp/derphttp/derphttp_client.go +++ b/derp/derphttp/derphttp_client.go @@ -336,6 +336,9 @@ func useWebsockets() bool { return false } +// maxErrBodyBytes is how much of a non-101 response body to include in the connect error message. +const maxErrBodyBytes = 1 << 10 + func (c *Client) connect(ctx context.Context, caller string) (client *derp.Client, connGen int, err error) { c.mu.Lock() defer c.mu.Unlock() @@ -551,9 +554,9 @@ func (c *Client) connect(ctx context.Context, caller string) (client *derp.Clien return nil, 0, err } if resp.StatusCode != http.StatusSwitchingProtocols { - b, _ := io.ReadAll(resp.Body) + b, _ := io.ReadAll(io.LimitReader(resp.Body, maxErrBodyBytes)) resp.Body.Close() - return nil, 0, fmt.Errorf("GET failed: %v: %s", err, b) + return nil, 0, fmt.Errorf("GET failed: status %v: %q", resp.Status, b) } } derpClient, err = derp.NewClient(c.privateKey, httpConn, brw, c.logf, diff --git a/derp/derphttp/derphttp_client_test.go b/derp/derphttp/derphttp_client_test.go new file mode 100644 index 000000000..f80a1d0ff --- /dev/null +++ b/derp/derphttp/derphttp_client_test.go @@ -0,0 +1,62 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package derphttp + +import ( + "bytes" + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "tailscale.com/net/netmon" + "tailscale.com/types/key" +) + +func TestConnectNon101ResponseBodyBounded(t *testing.T) { + const ( + serverResponseBodySize = 1 << 20 + + // The error wraps the %q-quoted body in a fixed prefix: caller, target URL, + // and HTTP status. That's 102 bytes for an IPv4 loopback URL with a 5-digit + // ephemeral port; an [::1] fallback or a shorter port only shrink it, so 102 + // is an upper bound. + maxErrPrefixLen = 102 + maxWantLen = maxErrBodyBytes + maxErrPrefixLen + ) + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadRequest) + // Write far more than the client's cap, to test that it bounds the read. + w.Write(bytes.Repeat([]byte("A"), serverResponseBodySize)) + })) + defer srv.Close() + + c, err := NewClient(key.NewNode(), srv.URL+"/derp", t.Logf, netmon.NewStatic()) + if err != nil { + t.Fatal(err) + } + defer c.Close() + + err = c.Connect(context.Background()) + if err == nil { + t.Fatal("Connect succeeded against a server that never sent 101; want error") + } + msg := err.Error() + + msgLen := len(msg) + if msgLen > maxWantLen { + t.Errorf("error string is %d bytes (server sent a %d byte body); want <= %d. "+ + "Is the io.LimitReader around the response body still there?", + msgLen, serverResponseBodySize, maxWantLen) + } + // The server sends far more than the cap, so a short message means the read was truncated too aggressively. + if msgLen < maxErrBodyBytes { + t.Errorf("error string is too short: %d bytes; want >= %d", msgLen, maxErrBodyBytes) + } + if !strings.Contains(msg, "400 Bad Request") { + t.Errorf("error %q does not include the HTTP status", msg) + } +}