mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-03 07:02:33 -08:00
ipn/ipnlocal: test authenticated funnel gate; own the whole reserved prefix
Add end-to-end gate tests driving serveWebHandler with a Funnel context: unauthenticated bounce to control, valid-session admission with identity passthrough, allowlist 403, expired-session re-auth, reserved callback/ logout interception, redirect-loop guard, and an unauthenticated-funnel regression that asserts today's behavior is unchanged. Document the manual browser round-trip steps that need a live dev control plane. Writing the tests surfaced a gate-bypass: the reserved-path interceptor was keyed on the matched handler's Auth, so an app route registered directly under /.well-known/tailscale/funnel-auth/ (whose own handler had no Auth) fell through to the backend. Intercept the reserved prefix before mount matching whenever the funnel host has any Auth-configured handler, so the node owns the entire prefix; keep the gate itself per matched mount point (Auth is per-mount). Factor the host/config resolution out of getServeHandler into webServerConfigForRequest for reuse. Updates tailscale/corp#38806 Change-Id: I39f77a6f4d2ca20101f4c7b041c24065edf13c63 Signed-off-by: Kabir Sikand <[email protected]>
This commit is contained in:
1 parent
a8742ad9c6
commit
7a7fa2c230
2 files changed
+307
-15
No files matched your search
@@ -10,13 +10,19 @@ import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha256"
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"tailscale.com/ipn"
|
||||
"tailscale.com/types/key"
|
||||
)
|
||||
|
||||
@@ -315,6 +321,259 @@ func TestFunnelRequestURLPinsHost(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Manual end-to-end verification (requires a dev control plane seeded with the
|
||||
// "tailscale-funnel" OIDC client and the oidc-authorization-flow feature flag;
|
||||
// see the cross-repo contract). The automated tests above cover the node-side
|
||||
// gate, cookie, JWKS/JWT verification, and allowlist in isolation; a full
|
||||
// browser round trip needs the corp side live:
|
||||
//
|
||||
// 1. On a funnel-capable node: `tailscale funnel --auth 3000` (serve any local
|
||||
// app on :3000). Confirm status shows "auth: Login With Tailscale".
|
||||
// 2. From a browser with no Tailscale installed, open https://<node>.ts.net/.
|
||||
// Expect a 302 to control's /a/oauth_authorize, sign in, and land back on
|
||||
// the app; the URL briefly carries ts_funnel_authed=1.
|
||||
// 3. Confirm the backend app receives Tailscale-User-Login / -Email headers.
|
||||
// 4. Reload: no re-auth (the ts_funnel_session cookie is reused). Open a fresh
|
||||
// browser / clear cookies: re-auth is required (stateless session).
|
||||
// 5. `tailscale funnel --auth [email protected] 3000`: allowed user still
|
||||
// works; a different Tailscale account gets a clean 403.
|
||||
// 6. Visit https://<node>.ts.net/.well-known/tailscale/funnel-auth/logout: the
|
||||
// session cookie is cleared and the next request re-authenticates.
|
||||
// 7. Regression: `tailscale funnel 3000` (no --auth) serves with no redirect;
|
||||
// a raw-TCP funnel (`tailscale funnel --tcp=...`) is unaffected.
|
||||
|
||||
// funnelReq builds a Funnel request to the given path for host example.ts.net,
|
||||
// with an optional session cookie value.
|
||||
func funnelReq(t *testing.T, path, cookie string) *http.Request {
|
||||
t.Helper()
|
||||
u, err := url.Parse("https://example.ts.net" + path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := &http.Request{
|
||||
Method: "GET",
|
||||
URL: u,
|
||||
Host: "example.ts.net",
|
||||
Header: make(http.Header),
|
||||
TLS: &tls.ConnectionState{ServerName: "example.ts.net"},
|
||||
}
|
||||
if cookie != "" {
|
||||
req.AddCookie(&http.Cookie{Name: funnelSessionCookieName, Value: cookie})
|
||||
}
|
||||
req = req.WithContext(serveHTTPContextKey.WithValue(req.Context(), &serveHTTPContext{
|
||||
Funnel: &funnelFlow{Host: "example.ts.net"},
|
||||
SrcAddr: netip.MustParseAddrPort("1.2.3.4:1234"),
|
||||
DestPort: 443,
|
||||
}))
|
||||
return req
|
||||
}
|
||||
|
||||
// TestFunnelGateEndToEnd exercises the security-critical gate paths through
|
||||
// serveWebHandler: unauthenticated bounce, valid-session admission, reserved
|
||||
// path interception (vs. app routes), allowlist rejection, and the
|
||||
// redirect-loop guard.
|
||||
func TestFunnelGateEndToEnd(t *testing.T) {
|
||||
backendHit := false
|
||||
testServ := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
backendHit = true
|
||||
w.Header().Set("X-Backend", "1")
|
||||
w.Header().Set("Echo-User-Login", r.Header.Get("Tailscale-User-Login"))
|
||||
w.Header().Set("Echo-User-Email", r.Header.Get("Tailscale-User-Email"))
|
||||
}))
|
||||
defer testServ.Close()
|
||||
|
||||
b := newTestBackend(t)
|
||||
conf := &ipn.ServeConfig{
|
||||
Web: map[ipn.HostPort]*ipn.WebServerConfig{
|
||||
"example.ts.net:443": {Handlers: map[string]*ipn.HTTPHandler{
|
||||
"/": {
|
||||
Proxy: testServ.URL,
|
||||
Auth: &ipn.FunnelAuth{Provider: "tailscale", Allow: []string{"[email protected]"}},
|
||||
},
|
||||
// An app route that lives under the reserved prefix must never
|
||||
// be reachable; the interceptor owns the whole prefix.
|
||||
funnelAuthReservedPrefix + "sneaky": {Proxy: testServ.URL},
|
||||
}},
|
||||
},
|
||||
AllowFunnel: map[ipn.HostPort]bool{"example.ts.net:443": true},
|
||||
}
|
||||
if err := b.SetServeConfig(conf, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
keys, err := b.funnelAuthKeys()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mint := func(sess *funnelAuthSession) string {
|
||||
v, err := keys.sealFunnelSession(sess)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
now := time.Now()
|
||||
|
||||
t.Run("no session bounces to control authorize", func(t *testing.T) {
|
||||
backendHit = false
|
||||
w := httptest.NewRecorder()
|
||||
b.serveWebHandler(w, funnelReq(t, "/private", ""))
|
||||
res := w.Result()
|
||||
if res.StatusCode != http.StatusFound {
|
||||
t.Fatalf("status = %d; want 302", res.StatusCode)
|
||||
}
|
||||
loc := res.Header.Get("Location")
|
||||
if !strings.Contains(loc, "/a/oauth_authorize") ||
|
||||
!strings.Contains(loc, "client_id=tailscale-funnel") ||
|
||||
!strings.Contains(loc, "code_challenge_method=S256") {
|
||||
t.Errorf("unexpected authorize redirect: %s", loc)
|
||||
}
|
||||
if backendHit {
|
||||
t.Error("backend must not be hit for an unauthenticated request")
|
||||
}
|
||||
// A state cookie must be set to carry CSRF.
|
||||
if len(res.Cookies()) == 0 {
|
||||
t.Error("expected a state cookie to be set")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("valid allowed session is admitted and identity forwarded", func(t *testing.T) {
|
||||
backendHit = false
|
||||
tok := mint(&funnelAuthSession{
|
||||
Sub: "u1", Email: "[email protected]", EmailVerified: true,
|
||||
Name: "Alice", FQDN: "example.ts.net", Expiry: now.Add(time.Hour).Unix(),
|
||||
})
|
||||
w := httptest.NewRecorder()
|
||||
b.serveWebHandler(w, funnelReq(t, "/private", tok))
|
||||
res := w.Result()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d; want 200", res.StatusCode)
|
||||
}
|
||||
if !backendHit {
|
||||
t.Error("backend should have been reached for a valid session")
|
||||
}
|
||||
if got := res.Header.Get("Echo-User-Login"); got != "[email protected]" {
|
||||
t.Errorf("forwarded Tailscale-User-Login = %q; want [email protected]", got)
|
||||
}
|
||||
if got := res.Header.Get("Echo-User-Email"); got != "[email protected]" {
|
||||
t.Errorf("forwarded Tailscale-User-Email = %q; want [email protected]", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("valid session not on allowlist is 403", func(t *testing.T) {
|
||||
backendHit = false
|
||||
tok := mint(&funnelAuthSession{
|
||||
Sub: "u2", Email: "[email protected]", EmailVerified: true,
|
||||
FQDN: "example.ts.net", Expiry: now.Add(time.Hour).Unix(),
|
||||
})
|
||||
w := httptest.NewRecorder()
|
||||
b.serveWebHandler(w, funnelReq(t, "/private", tok))
|
||||
if w.Result().StatusCode != http.StatusForbidden {
|
||||
t.Fatalf("status = %d; want 403", w.Result().StatusCode)
|
||||
}
|
||||
if backendHit {
|
||||
t.Error("backend must not be reached for a disallowed user")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("expired session bounces", func(t *testing.T) {
|
||||
tok := mint(&funnelAuthSession{
|
||||
Sub: "u1", Email: "[email protected]", EmailVerified: true,
|
||||
FQDN: "example.ts.net", Expiry: now.Add(-time.Hour).Unix(),
|
||||
})
|
||||
w := httptest.NewRecorder()
|
||||
b.serveWebHandler(w, funnelReq(t, "/private", tok))
|
||||
if w.Result().StatusCode != http.StatusFound {
|
||||
t.Fatalf("status = %d; want 302 (re-auth)", w.Result().StatusCode)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("reserved logout path is intercepted, not proxied", func(t *testing.T) {
|
||||
backendHit = false
|
||||
w := httptest.NewRecorder()
|
||||
b.serveWebHandler(w, funnelReq(t, funnelAuthLogoutPath, ""))
|
||||
res := w.Result()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
t.Fatalf("logout status = %d; want 200", res.StatusCode)
|
||||
}
|
||||
if backendHit {
|
||||
t.Error("logout must be handled by the node, not proxied to the app")
|
||||
}
|
||||
// Logout must clear the session cookie.
|
||||
var cleared bool
|
||||
for _, c := range res.Cookies() {
|
||||
if c.Name == funnelSessionCookieName && c.MaxAge < 0 {
|
||||
cleared = true
|
||||
}
|
||||
}
|
||||
if !cleared {
|
||||
t.Error("logout did not clear the session cookie")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unknown reserved path is 404, never proxied", func(t *testing.T) {
|
||||
backendHit = false
|
||||
w := httptest.NewRecorder()
|
||||
b.serveWebHandler(w, funnelReq(t, funnelAuthReservedPrefix+"sneaky", ""))
|
||||
if w.Result().StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("status = %d; want 404 for reserved-prefix app route", w.Result().StatusCode)
|
||||
}
|
||||
if backendHit {
|
||||
t.Error("an app route under the reserved prefix must not be reachable")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("redirect loop guard returns error, not another bounce", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
// Came back from a login (loop param set) but still no cookie.
|
||||
b.serveWebHandler(w, funnelReq(t, "/private?"+funnelAuthLoopParam+"=1", ""))
|
||||
if w.Result().StatusCode != http.StatusForbidden {
|
||||
t.Fatalf("status = %d; want 403 (loop guard)", w.Result().StatusCode)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestFunnelUnauthenticatedUnchanged verifies that a Funnel handler without
|
||||
// Auth is served exactly as before (no gate, no redirect, backend reached).
|
||||
func TestFunnelUnauthenticatedUnchanged(t *testing.T) {
|
||||
backendHit := false
|
||||
testServ := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
backendHit = true
|
||||
// The funnel request header must still be set; the auth identity
|
||||
// headers must not be present for unauthenticated funnel.
|
||||
w.Header().Set("Echo-Funnel", r.Header.Get("Tailscale-Funnel-Request"))
|
||||
w.Header().Set("Echo-User-Login", r.Header.Get("Tailscale-User-Login"))
|
||||
}))
|
||||
defer testServ.Close()
|
||||
|
||||
b := newTestBackend(t)
|
||||
conf := &ipn.ServeConfig{
|
||||
Web: map[ipn.HostPort]*ipn.WebServerConfig{
|
||||
"example.ts.net:443": {Handlers: map[string]*ipn.HTTPHandler{
|
||||
"/": {Proxy: testServ.URL}, // no Auth
|
||||
}},
|
||||
},
|
||||
AllowFunnel: map[ipn.HostPort]bool{"example.ts.net:443": true},
|
||||
}
|
||||
if err := b.SetServeConfig(conf, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
b.serveWebHandler(w, funnelReq(t, "/anything", ""))
|
||||
res := w.Result()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d; want 200", res.StatusCode)
|
||||
}
|
||||
if !backendHit {
|
||||
t.Fatal("backend should be reached directly for unauthenticated funnel")
|
||||
}
|
||||
if got := res.Header.Get("Echo-Funnel"); got != "?1" {
|
||||
t.Errorf("Tailscale-Funnel-Request = %q; want ?1", got)
|
||||
}
|
||||
if got := res.Header.Get("Echo-User-Login"); got != "" {
|
||||
t.Errorf("unauthenticated funnel unexpectedly forwarded identity: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsRealEmailDomain(t *testing.T) {
|
||||
tests := []struct {
|
||||
dom string
|
||||
|
||||
+48
-15
@@ -808,9 +808,9 @@ func (b *LocalBackend) forwardTCPWithProxyProtocol(conn, backConn net.Conn, prox
|
||||
return <-errc
|
||||
}
|
||||
|
||||
func (b *LocalBackend) getServeHandler(r *http.Request) (_ ipn.HTTPHandlerView, at string, ok bool) {
|
||||
var z ipn.HTTPHandlerView // zero value
|
||||
|
||||
// webServerConfigForRequest resolves the WebServerConfigView serving r's host
|
||||
// and port, applying the same hostname normalization getServeHandler uses.
|
||||
func (b *LocalBackend) webServerConfigForRequest(r *http.Request) (wsc ipn.WebServerConfigView, ok bool) {
|
||||
hostname := r.Host
|
||||
if r.TLS == nil {
|
||||
tcd := "." + b.CurrentProfile().NetworkProfile().MagicDNSName
|
||||
@@ -823,13 +823,31 @@ func (b *LocalBackend) getServeHandler(r *http.Request) (_ ipn.HTTPHandlerView,
|
||||
} else {
|
||||
hostname = r.TLS.ServerName
|
||||
}
|
||||
|
||||
sctx, ok := serveHTTPContextKey.ValueOk(r.Context())
|
||||
if !ok {
|
||||
b.logf("[unexpected] localbackend: no serveHTTPContext in request")
|
||||
return z, "", false
|
||||
return wsc, false
|
||||
}
|
||||
wsc, ok := b.webServerConfig(hostname, sctx.ForVIPService, sctx.DestPort)
|
||||
return b.webServerConfig(hostname, sctx.ForVIPService, sctx.DestPort)
|
||||
}
|
||||
|
||||
// funnelHostHasAuth reports whether any handler on wsc has authenticated Funnel
|
||||
// configured. When true, the whole /.well-known/tailscale/funnel-auth/ prefix
|
||||
// is owned by the node for that host and must never fall through to an app
|
||||
// route, even one whose own handler has no Auth.
|
||||
func funnelHostHasAuth(wsc ipn.WebServerConfigView) (auth ipn.FunnelAuthView, ok bool) {
|
||||
for _, h := range wsc.Handlers().All() {
|
||||
if a := h.Auth(); a.Valid() {
|
||||
return a, true
|
||||
}
|
||||
}
|
||||
return auth, false
|
||||
}
|
||||
|
||||
func (b *LocalBackend) getServeHandler(r *http.Request) (_ ipn.HTTPHandlerView, at string, ok bool) {
|
||||
var z ipn.HTTPHandlerView // zero value
|
||||
|
||||
wsc, ok := b.webServerConfigForRequest(r)
|
||||
if !ok {
|
||||
return z, "", false
|
||||
}
|
||||
@@ -1192,26 +1210,41 @@ func parseRedirectWithCode(redirect string) (code int, url string) {
|
||||
// serveWebHandler is an http.HandlerFunc that maps incoming requests to the
|
||||
// correct *http.
|
||||
func (b *LocalBackend) serveWebHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Authenticated Funnel: reserved-path interception. If this is a Funnel
|
||||
// request to a host that has any Auth-configured handler, the whole
|
||||
// /.well-known/tailscale/funnel-auth/ prefix belongs to the node. Handling
|
||||
// it here — before mount-point matching — means the OIDC callback and
|
||||
// logout endpoints can never be shadowed by, and a reserved-prefix app
|
||||
// route can never leak to, the backend. Unauthenticated funnel and tailnet
|
||||
// traffic are wholly unaffected.
|
||||
funnelReq := false
|
||||
if sctx, ok := serveHTTPContextKey.ValueOk(r.Context()); ok && sctx.Funnel != nil {
|
||||
funnelReq = true
|
||||
if wsc, ok := b.webServerConfigForRequest(r); ok {
|
||||
if auth, ok := funnelHostHasAuth(wsc); ok {
|
||||
if b.handleFunnelAuthReserved(w, r, auth) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
h, mountPoint, ok := b.getServeHandler(r)
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
// Authenticated Funnel gate. Only for Funnel requests to a handler with
|
||||
// Auth configured; unauthenticated funnel and tailnet traffic are wholly
|
||||
// unaffected (byte-for-byte identical to before). The reserved
|
||||
// /.well-known/tailscale/funnel-auth/* endpoints are handled here, before
|
||||
// any user mount point, so they can never be shadowed by an app route.
|
||||
if sctx, ok := serveHTTPContextKey.ValueOk(r.Context()); ok && sctx.Funnel != nil {
|
||||
|
||||
// Authenticated Funnel gate, per matched mount point (Auth is per-mount,
|
||||
// spec §6.3). Runs only for Funnel requests to a handler with Auth set.
|
||||
if funnelReq {
|
||||
if auth := h.Auth(); auth.Valid() {
|
||||
if b.handleFunnelAuthReserved(w, r, auth) {
|
||||
return
|
||||
}
|
||||
if !b.funnelAuthGate(w, r, auth) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if s := h.Text(); s != "" {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
io.WriteString(w, s)
|
||||
|
||||
Reference in new issue
Block a user