Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"reflect"
"strings"
"sync"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
Expand All @@ -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")
}
Expand All @@ -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) },
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down
43 changes: 43 additions & 0 deletions auth_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}