From eb298524eeb793a6313d7f676ff2299015ed03df Mon Sep 17 00:00:00 2001 From: Fran Bull Date: Thu, 29 Oct 2026 10:12:21 -0700 Subject: [PATCH] feature/conn25: create a secret for TransitIPRequest tokens Create a secret that can be used for token minting and verification. Updates tailscale/corp#46169 Signed-off-by: Fran Bull --- feature/conn25/conn25.go | 1 + feature/conn25/connector.go | 5 +++++ feature/conn25/token.go | 18 +++++++++++++++++ feature/conn25/token_test.go | 39 ++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 feature/conn25/token.go create mode 100644 feature/conn25/token_test.go diff --git a/feature/conn25/conn25.go b/feature/conn25/conn25.go index 0efce8c423..4c92af35df 100644 --- a/feature/conn25/conn25.go +++ b/feature/conn25/conn25.go @@ -547,6 +547,7 @@ func newConn25(logf logger.Logf) *Conn25 { getIPSets: getIPSets, clock: tstime.StdClock{}, expiryQueue: list.New(), + tokenSecret: newTokenSecret(), } return c } diff --git a/feature/conn25/connector.go b/feature/conn25/connector.go index cd3ef2b74a..5ba1df35fb 100644 --- a/feature/conn25/connector.go +++ b/feature/conn25/connector.go @@ -99,6 +99,9 @@ type connector struct { // Remember to add new fields to [connector.reset] if needed. mu sync.Mutex // protects the fields below + // tokenSecret is the random secret used to mint and verify tokens in TransitIPRequests. + // It is generated in memory at creation, rotated on [connector.reset] (ie on profile change), and not persisted. + tokenSecret [tokenSecretLen]byte // transitIPs is a map of connector client peer IP -> client transitIPs that we update as connector client peers instruct us to, and then use to route traffic to its destination on behalf of connector clients. // Note that each peer could potentially have two maps: one for its IPv4 address, and one for its IPv6 address. The transit IPs map for a given peer IP will contain transit IPs of the same family as the peer's IP. transitIPs map[netip.Addr]map[netip.Addr]appAddr @@ -234,4 +237,6 @@ func (c *connector) reset() { c.transitIPs = make(map[netip.Addr]map[netip.Addr]appAddr) c.expiryQueue = list.New() + // old tokens should no longer verify + c.tokenSecret = newTokenSecret() } diff --git a/feature/conn25/token.go b/feature/conn25/token.go new file mode 100644 index 0000000000..4c094b20c5 --- /dev/null +++ b/feature/conn25/token.go @@ -0,0 +1,18 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package conn25 + +import "crypto/rand" + +// tokenSecretLen is the length in bytes of the connector's token secret. It is +// the recommended key size for HMAC-SHA-256, which is used to mint and verify +// destination IP tokens. +const tokenSecretLen = 32 + +// newTokenSecret returns a fresh random token secret. +func newTokenSecret() [tokenSecretLen]byte { + var secret [tokenSecretLen]byte + rand.Read(secret[:]) + return secret +} diff --git a/feature/conn25/token_test.go b/feature/conn25/token_test.go new file mode 100644 index 0000000000..338689633e --- /dev/null +++ b/feature/conn25/token_test.go @@ -0,0 +1,39 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package conn25 + +import ( + "testing" + + "tailscale.com/types/logger" +) + +func TestConnectorTokenSecretInit(t *testing.T) { + c := newConn25(logger.Discard) + + if c.connector.tokenSecret == ([tokenSecretLen]byte{}) { + t.Fatal("token secret is all zero bytes, expected random bytes") + } +} + +func TestConnectorTokenSecretRandomAcrossInstances(t *testing.T) { + a := newConn25(logger.Discard) + b := newConn25(logger.Discard) + + if a.connector.tokenSecret == b.connector.tokenSecret { + t.Fatal("two connectors minted identical token secrets, expected distinct random secrets") + } +} + +func TestConnectorResetRotatesTokenSecret(t *testing.T) { + c := newConn25(logger.Discard) + + before := c.connector.tokenSecret + c.connector.reset() + after := c.connector.tokenSecret + + if before == after { + t.Fatal("token secret unchanged after reset, expected rotation") + } +}