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 <fran@tailscale.com>
This commit is contained in:
Fran Bull authored and franbull committed 2026-11-23 13:32:45 -08:00
1 parent 7caf9edb40
commit 3ec7c0cd6c
3 files changed
+98 -2

No files matched your search

+14
View File
@@ -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
+78
View File
@@ -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)
}
})
}
}
+6 -2
View File
@@ -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))