From 3ec7c0cd6cc04e087e7123d03a81fb3064b35dc3 Mon Sep 17 00:00:00 2001 From: Fran Bull Date: Mon, 23 Nov 2026 09:13:10 -0800 Subject: [PATCH] feature/conn25: enforce token verification When the connector handles a ConnectorTransitIPRequest, it checks there is a token and that it is valid. If there is not a valid token it rejects the request. Updates tailscale/corp#46169 Signed-off-by: Fran Bull --- feature/conn25/conn25.go | 14 ++++++ feature/conn25/conn25_test.go | 78 ++++++++++++++++++++++++++++++++ feature/conn25/connector_test.go | 8 +++- 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/feature/conn25/conn25.go b/feature/conn25/conn25.go index 422aebdc2..7046cb5a4 100644 --- a/feature/conn25/conn25.go +++ b/feature/conn25/conn25.go @@ -577,6 +577,7 @@ const ( missingAppPermissionMessage = "You do not have permission to use this App" transitIPNotInPoolMessage = "The transit address is not in a configured transit IP pool" disallowedDestinationMessage = "Destination address is in a disallowed address range" + invalidTokenMessage = "The destination IP token is missing or did not verify" ) // handleConnectorTransitIPRequest creates a ConnectorTransitIPResponse in response @@ -614,6 +615,15 @@ func (c *Conn25) handleConnectorTransitIPRequest(n tailcfg.NodeView, peerCaps ta seen := map[netip.Addr]bool{} for _, each := range ctipr.TransitIPs { + token, err := base64.StdEncoding.DecodeString(each.Token) + if err != nil || !c.connector.verifyToken(token, each.App, each.DestinationIP) { + resp.TransitIPs = append(resp.TransitIPs, TransitIPResponse{ + Code: InvalidDestinationIPToken, + Message: invalidTokenMessage, + }) + continue + } + // Canonicalize IPv4-in-IPv6 addresses, so that duplicate detection and // the keys we store in the connector's map match the unmapped form the // datapath produces when it parses packets. @@ -725,6 +735,10 @@ const ( // and private addresses, along with addresses within the configured magic and // transit IP pools). DisallowedDestination = 8 + + // InvalidDestinationIPToken indicates that the request's token did not + // authenticate, or there was no token provided. + InvalidDestinationIPToken = 9 ) // TransitIPResponse is the response to a TransitIPRequest diff --git a/feature/conn25/conn25_test.go b/feature/conn25/conn25_test.go index f5cb5baa9..03e486f15 100644 --- a/feature/conn25/conn25_test.go +++ b/feature/conn25/conn25_test.go @@ -60,6 +60,15 @@ func mustIPSetFromPrefix(s string) *netipx.IPSet { return set } +func mustValidDstToken(t *testing.T, c *connector, app string, dst netip.Addr) string { + return base64.StdEncoding.EncodeToString(mustMint(t, c, app, dst)) +} + +func withValidDstToken(t *testing.T, c *connector, tipr TransitIPRequest) TransitIPRequest { + tipr.Token = mustValidDstToken(t, c, tipr.App, tipr.DestinationIP) + return tipr +} + // TestHandleConnectorTransitIPRequest tests that if sent a // request with a transit addr and a destination addr we store that mapping // and can retrieve it. @@ -614,6 +623,12 @@ func TestHandleConnectorTransitIPRequest(t *testing.T) { peerCap = nil } + // add a valid token to each TransitIPRequest + for i := range req.TransitIPs { + tipr := &req.TransitIPs[i] + tipr.Token = mustValidDstToken(t, c.connector, tipr.App, tipr.DestinationIP) + } + resp := c.handleConnectorTransitIPRequest(peer, peerCap, req) // Ensure that we have the expected number of responses @@ -4980,3 +4995,66 @@ func TestMintTokensForDNSResponse(t *testing.T) { }) } } + +func TestHandleConnectorTransitIPRequestVerifiesToken(t *testing.T) { + const appName = "app" + + peerV4 := netip.MustParseAddr("100.101.101.101") + peerNode := (&tailcfg.Node{ID: 1, Addresses: []netip.Prefix{netip.PrefixFrom(peerV4, 32)}}).View() + peerCap := tailcfg.PeerCapMap{ + peercap.Conn25Prefix.ToAttribute(appName): []tailcfg.RawMessage{`"*"`}, + } + + transitIP := netip.MustParseAddr("169.254.0.1") + + newConfigured := func() *Conn25 { + c := newConn25(logger.Discard) + c.reconfig(&config{ + isConfigured: true, + appsByName: map[string]appctype.AppConnectorAttr{ + appName: {Name: appName, Mode: appctype.MagicIP}, + }, + ipSets: ipSets{v4Transit: mustIPSetFromPrefix("169.254.0.0/24")}, + }) + return c + } + + tests := []struct { + name string + token string + }{ + { + name: "missing_token", + token: "", + }, + { + name: "bad_token", + token: "not a token", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := newConfigured() + resp := c.handleConnectorTransitIPRequest(peerNode, peerCap, ConnectorTransitIPRequest{ + TransitIPs: []TransitIPRequest{{ + TransitIP: transitIP, + DestinationIP: netip.MustParseAddr("1.2.3.4"), + App: appName, + Token: tt.token, + }}, + }) + if len(resp.TransitIPs) != 1 { + t.Fatalf("got %d responses, want 1", len(resp.TransitIPs)) + } + if got := resp.TransitIPs[0].Code; got != InvalidDestinationIPToken { + t.Fatalf("code = %d, want %d", got, InvalidDestinationIPToken) + } + + gotDst, ok := c.connector.realIPForTransitIPConnection(peerV4, transitIP) + if ok { + t.Errorf("rejected request left a mapping: %v", gotDst) + } + }) + } +} diff --git a/feature/conn25/connector_test.go b/feature/conn25/connector_test.go index 3ca6e09bc..320fd7991 100644 --- a/feature/conn25/connector_test.go +++ b/feature/conn25/connector_test.go @@ -49,7 +49,11 @@ func TestConnectorExpireTransitIPs(t *testing.T) { register := func(peer tailcfg.NodeView, tip, dst netip.Addr) { c.handleConnectorTransitIPRequest(peer, peerCap, ConnectorTransitIPRequest{ - TransitIPs: []TransitIPRequest{{TransitIP: tip, DestinationIP: dst, App: appName}}, + TransitIPs: []TransitIPRequest{withValidDstToken(t, c.connector, TransitIPRequest{ + TransitIP: tip, + DestinationIP: dst, + App: appName, + })}, }) } @@ -250,7 +254,7 @@ func TestConnectorDisallowedDestinations(t *testing.T) { } resp := c.handleConnectorTransitIPRequest(peerNode, peerCap, ConnectorTransitIPRequest{ - TransitIPs: []TransitIPRequest{{TransitIP: transitIP, DestinationIP: dst, App: appName}}, + TransitIPs: []TransitIPRequest{withValidDstToken(t, c.connector, TransitIPRequest{TransitIP: transitIP, DestinationIP: dst, App: appName})}, }) if len(resp.TransitIPs) != 1 { t.Fatalf("got %d responses, want 1", len(resp.TransitIPs))