From 1cfd8f3f36a9bb245cd03dd415e9392204c71cdd Mon Sep 17 00:00:00 2001 From: Lachlan Collins <1438799+Lockyc@users.noreply.github.com> Date: Fri, 21 Aug 2026 16:03:04 +1000 Subject: [PATCH] auth: fix tailscaled connection leak from Authenticate's value receiver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Authenticate had a value receiver, so it ran on a copy of Auth: the localclient cache filled by client() landed in that copy and was lost on return. localclient is unexported and there is no Provision, so the cache could never be warm — every gated request built a fresh local.Client, dialled a new connection to tailscaled's LocalAPI socket, and dropped it without CloseIdleConnections(). The transport's readLoop goroutine keeps the dropped connection alive, so each request permanently costs a socket, two goroutines, and buffers — in caddy and in tailscaled, which holds the far end. Measured live: ~56 KiB of anon memory per gated request; after 3,000 requests, caddy held 7,792 goroutines and 3,835 open tailscaled.sock connections. Give Authenticate a pointer receiver so the cache sticks; local.Client builds its http.Client under a sync.Once and is safe to share. The now-shared lazy fill needs a mutex, which makes Auth non-copyable, so CaddyModule and the two sites that copied an Auth value take pointers too. go vet copylocks is clean. The test asserts the method set — a value receiver is exactly what puts Authenticate back into Auth's method set — and fails on the unfixed code. Signed-off-by: Lachlan Collins <1438799+Lockyc@users.noreply.github.com> --- auth.go | 15 +++++++++++---- auth_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 auth_test.go diff --git a/auth.go b/auth.go index c84ac09..b8c0986 100644 --- a/auth.go +++ b/auth.go @@ -11,6 +11,7 @@ import ( "net/http" "reflect" "strings" + "sync" "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddyconfig" @@ -22,7 +23,7 @@ import ( ) func init() { - caddy.RegisterModule(Auth{}) + caddy.RegisterModule(new(Auth)) httpcaddyfile.RegisterHandlerDirective("tailscale_auth", parseAuthConfig) httpcaddyfile.RegisterDirectiveOrder("tailscale_auth", httpcaddyfile.After, "basicauth") } @@ -32,10 +33,13 @@ func init() { // that node will be used to identify the user information for inbound requests. // Otherwise, it will attempt to find and use the local tailscaled daemon running on the system. type Auth struct { + // mu guards localclient. Authenticate runs concurrently across requests, + // so the lazy fill in client() would otherwise be a data race. + mu sync.Mutex localclient *local.Client } -func (Auth) CaddyModule() caddy.ModuleInfo { +func (*Auth) CaddyModule() caddy.ModuleInfo { return caddy.ModuleInfo{ ID: "http.authentication.providers.tailscale", New: func() caddy.Module { return new(Auth) }, @@ -94,6 +98,9 @@ type wrappedListener interface { // If the LocalClient has not already been configured, the provided request will be used to // lookup the tailscale node that serviced the request, and get the associated LocalClient. func (ta *Auth) client(r *http.Request) (*local.Client, error) { + ta.mu.Lock() + defer ta.mu.Unlock() + if ta.localclient != nil { return ta.localclient, nil } @@ -132,7 +139,7 @@ type tsnetListener interface { // - tailscale_name: the user's display name // - tailscale_profile_picture: the user's profile picture URL // - tailscale_tailnet: the user's tailnet name (if the user is not connecting to a shared node) -func (ta Auth) Authenticate(w http.ResponseWriter, r *http.Request) (caddyauth.User, bool, error) { +func (ta *Auth) Authenticate(w http.ResponseWriter, r *http.Request) (caddyauth.User, bool, error) { user := caddyauth.User{} client, err := ta.client(r) @@ -168,7 +175,7 @@ func (ta Auth) Authenticate(w http.ResponseWriter, r *http.Request) (caddyauth.U } func parseAuthConfig(_ httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { - var ta Auth + ta := new(Auth) return caddyauth.Authentication{ ProvidersRaw: caddy.ModuleMap{ diff --git a/auth_test.go b/auth_test.go new file mode 100644 index 0000000..642a38f --- /dev/null +++ b/auth_test.go @@ -0,0 +1,43 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: Apache-2.0 + +package tscaddy + +import ( + "reflect" + "testing" + + "github.com/caddyserver/caddy/v2/modules/caddyhttp/caddyauth" +) + +// Authenticate must have a pointer receiver: a value receiver runs against a +// copy of the module, so the localclient cache never sticks and every gated +// request leaks a tailscaled connection (see the commit that added this test). +// +// A behavioural test would need a live tailscaled, so this asserts the method +// set instead — a value receiver is exactly what puts Authenticate into Auth's +// method set, making the value type satisfy caddyauth.Authenticator. +func TestAuthenticateHasPointerReceiver(t *testing.T) { + authenticator := reflect.TypeFor[caddyauth.Authenticator]() + + if reflect.TypeFor[Auth]().Implements(authenticator) { + t.Error("Auth implements caddyauth.Authenticator by value, so Authenticate has a value receiver: " + + "client()'s cache fills a per-request copy, leaking a tailscaled connection per request") + } + if !reflect.TypeFor[*Auth]().Implements(authenticator) { + t.Error("*Auth does not implement caddyauth.Authenticator") + } +} + +// Caddy must instantiate the module as a pointer, or the provider it holds +// would be a value whose method set lacks the pointer-receiver Authenticate. +func TestAuthModuleInstantiatesPointer(t *testing.T) { + instance := (*Auth)(nil).CaddyModule().New() + + if _, ok := instance.(*Auth); !ok { + t.Fatalf("CaddyModule().New() = %T, want *Auth", instance) + } + if _, ok := instance.(caddyauth.Authenticator); !ok { + t.Fatal("module instance does not implement caddyauth.Authenticator") + } +}