From cfd065c680d758c986a3976ec6ee33e700a6ec85 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sat, 17 Oct 2026 11:28:43 +0000 Subject: [PATCH] hscontrol: refuse a key expiry that cannot be encoded handleRegister, the OIDC auth path and SetNodeExpiry refuse it. Expiries are stored in UTC, which SQLite can read back, and loaded in UTC, as PostgreSQL returns the server's zone. The v1 API answers 400. --- hscontrol/api/v1/errors.go | 2 + hscontrol/apiv1_nodes_test.go | 9 +++ hscontrol/auth.go | 8 +++ hscontrol/auth_test.go | 54 ++++++++++++++++ hscontrol/state/persist_test.go | 108 ++++++++++++++++++++++++++++++++ hscontrol/state/state.go | 25 ++++++++ hscontrol/types/node.go | 4 ++ 7 files changed, 210 insertions(+) diff --git a/hscontrol/api/v1/errors.go b/hscontrol/api/v1/errors.go index f2a389f35..0724aaf78 100644 --- a/hscontrol/api/v1/errors.go +++ b/hscontrol/api/v1/errors.go @@ -6,6 +6,7 @@ import ( "github.com/danielgtaylor/huma/v2" "github.com/juanfont/headscale/hscontrol/db" "github.com/juanfont/headscale/hscontrol/state" + "github.com/juanfont/headscale/hscontrol/types" "gorm.io/gorm" ) @@ -33,6 +34,7 @@ func mapError(msg string, err error) error { errors.Is(err, state.ErrNodeMarkedTaggedButHasNoTags), errors.Is(err, state.ErrNodeHasNeitherUserNorTags), errors.Is(err, state.ErrRequestedTagsInvalidOrNotPermitted), + errors.Is(err, types.ErrExpiryOutOfRange), errors.Is(err, db.ErrUserStillHasNodes), errors.Is(err, db.ErrCannotChangeOIDCUser), errors.Is(err, db.ErrPreAuthKeyNotTaggedOrOwned), diff --git a/hscontrol/apiv1_nodes_test.go b/hscontrol/apiv1_nodes_test.go index 3a58addcc..d6197dc01 100644 --- a/hscontrol/apiv1_nodes_test.go +++ b/hscontrol/apiv1_nodes_test.go @@ -201,6 +201,15 @@ func TestAPIV1NodeExpire(t *testing.T) { assert.NotNil(t, got.Node["expiry"]) }) + t.Run("huma refuses an unencodable expiry", func(t *testing.T) { + h := newAPIV1Harness(t) + seedNodes(newNodeSeed("alice", "node-a"))(t, h.app) + + res := h.callHuma(http.MethodPost, "/api/v1/node/1/expire", + []byte(`{"expiry":"9999-12-31T23:59:59-23:59"}`)) + assertStatus(t, res, http.StatusBadRequest) + }) + t.Run("not found parity", func(t *testing.T) { h := newAPIV1Harness(t) res := h.assertParity(t, http.MethodPost, "/api/v1/node/99999/expire", nil) diff --git a/hscontrol/auth.go b/hscontrol/auth.go index 434ad6133..521f0fd03 100644 --- a/hscontrol/auth.go +++ b/hscontrol/auth.go @@ -41,6 +41,14 @@ func (h *Headscale) handleRegister( req tailcfg.RegisterRequest, machineKey key.MachinePublic, ) (*tailcfg.RegisterResponse, error) { + // Every path below may store the client's expiry and send it to peers. + expiry, err := types.ValidateExpiry(req.Expiry) + if err != nil { + return nil, NewHTTPError(http.StatusBadRequest, err.Error(), err) + } + + req.Expiry = expiry + // Check for logout/expiry FIRST, before checking auth key. // Tailscale clients may send logout requests with BOTH a past expiry AND an auth key. // A past expiry takes precedence - it's a logout regardless of other fields. diff --git a/hscontrol/auth_test.go b/hscontrol/auth_test.go index 50b00bf96..57d2a852a 100644 --- a/hscontrol/auth_test.go +++ b/hscontrol/auth_test.go @@ -4545,3 +4545,57 @@ func TestFollowupWaitPrefersCompletedAuthOverExpiredContext(t *testing.T) { timeouts, iterations) assert.Equal(t, iterations, authorized, "every completed registration must be returned as authorized") } + +// TestRegisterRejectsUnencodableExpiry proves a registration cannot store a key +// expiry that is in range in its own zone but outside the years [0,9999] in +// UTC, which every map carrying the node would fail to encode. +func TestRegisterRejectsUnencodableExpiry(t *testing.T) { + app := createTestApp(t) + + user := app.state.CreateUserForTest("expiry-range-user") + pak, err := app.state.CreatePreAuthKey(user.TypedID(), true, false, nil, nil) + require.NoError(t, err) + + register := func(expiry time.Time) (key.NodePublic, error) { + nodeKey := key.NewNode().Public() + + _, err := app.handleRegister(context.Background(), tailcfg.RegisterRequest{ + Auth: &tailcfg.RegisterResponseAuth{AuthKey: pak.Key}, + NodeKey: nodeKey, + Hostinfo: &tailcfg.Hostinfo{Hostname: "expiry-range-node"}, + Expiry: expiry, + }, key.NewMachine().Public()) + + return nodeKey, err + } + + for _, raw := range []string{ + "9999-12-31T23:59:59-23:59", + "0000-01-01T00:00:00+23:59", + } { + expiry, err := time.Parse(time.RFC3339, raw) + require.NoError(t, err) + + nodeKey, err := register(expiry) + + httpErr, ok := errors.AsType[HTTPError](err) + require.True(t, ok, "expiry %s: want an HTTPError, got %v", raw, err) + require.Equal(t, http.StatusBadRequest, httpErr.Code) + + _, found := app.state.GetNodeByNodeKey(nodeKey) + require.False(t, found, "expiry %s must not create a node", raw) + } + + // The latest encodable expiry still registers, and SQLite reads it back + // although it is in the year 10000 east of UTC. + latest := time.Date(9999, time.December, 31, 23, 59, 59, 0, time.UTC) + nodeKey, err := register(latest.In(time.FixedZone("", 60*60))) + require.NoError(t, err, "the latest encodable expiry must still register") + + node, found := app.state.GetNodeByNodeKey(nodeKey) + require.True(t, found) + + stored, err := app.state.DB().GetNodeByID(node.ID()) + require.NoError(t, err) + assert.True(t, latest.Equal(*stored.Expiry)) +} diff --git a/hscontrol/state/persist_test.go b/hscontrol/state/persist_test.go index fa71dd9a3..77f389776 100644 --- a/hscontrol/state/persist_test.go +++ b/hscontrol/state/persist_test.go @@ -1844,3 +1844,111 @@ func TestBringNodeExpiryForwardDecidesInWriter(t *testing.T) { require.NotNil(t, stored.Expiry) assert.True(t, past.Equal(*stored.Expiry)) } + +// TestSetNodeExpiryRejectsUnencodableExpiry proves an admin cannot store a key +// expiry that every map carrying the node would fail to encode. +func TestSetNodeExpiryRejectsUnencodableExpiry(t *testing.T) { + _, s, nodeID := persistTestSetup(t) + t.Cleanup(func() { _ = s.Close() }) + + beyond, err := time.Parse(time.RFC3339, "9999-12-31T23:59:59-23:59") + require.NoError(t, err) + + before := s.nodeStore.data.Load() + + _, _, err = s.SetNodeExpiry(nodeID, &beyond) + require.ErrorIs(t, err, types.ErrExpiryOutOfRange) + require.Same(t, before, s.nodeStore.data.Load(), + "a refused expiry must not reach the NodeStore") + + latest := time.Date(9999, time.December, 31, 23, 59, 59, 0, time.UTC) + _, _, err = s.SetNodeExpiry(nodeID, &latest) + require.NoError(t, err) +} + +// TestAuthPathRejectsUnencodableExpiry proves an expiry handed to the auth +// path, such as an OIDC token's, cannot store what SetNodeExpiry refuses. +func TestAuthPathRejectsUnencodableExpiry(t *testing.T) { + _, s, _ := persistTestSetup(t) + t.Cleanup(func() { _ = s.Close() }) + + user := s.CreateUserForTest("auth-path-user") + nodeKey := key.NewNode().Public() + authID := types.MustAuthID() + s.SetAuthCacheEntry(authID, types.NewRegisterAuthRequest(&types.RegistrationData{ + MachineKey: key.NewMachine().Public(), + NodeKey: nodeKey, + Hostname: "auth-path-node", + })) + + // How go-oidc reads an exp claim in the year 10000. + beyond := time.Unix(253402300800, 0) + + _, _, err := s.HandleNodeFromAuthPath( + authID, types.UserID(user.ID), &beyond, util.RegisterMethodOIDC, + ) + require.ErrorIs(t, err, types.ErrExpiryOutOfRange) + + _, found := s.GetNodeByNodeKey(nodeKey) + require.False(t, found, "a refused expiry must not create a node") +} + +// TestAcceptedExpirySurvivesRestart proves an accepted expiry that is already in +// the year 10000 in its own zone is stored so SQLite can load it at the next +// start. go-oidc reads a token's exp in the server's zone, east of UTC here. +func TestAcceptedExpirySurvivesRestart(t *testing.T) { + latest := time.Date(9999, time.December, 31, 23, 59, 59, 0, time.UTC) + east := latest.In(time.FixedZone("", 60*60)) + + dbPath, s, adminID := persistTestSetup(t) + + _, _, err := s.SetNodeExpiry(adminID, &east) + require.NoError(t, err) + + user := s.CreateUserForTest("oidc-user") + authID := types.MustAuthID() + s.SetAuthCacheEntry(authID, types.NewRegisterAuthRequest(&types.RegistrationData{ + MachineKey: key.NewMachine().Public(), + NodeKey: key.NewNode().Public(), + Hostname: "oidc-node", + })) + + oidcNode, _, err := s.HandleNodeFromAuthPath( + authID, types.UserID(user.ID), &east, util.RegisterMethodOIDC, + ) + require.NoError(t, err) + require.NoError(t, s.Close()) + + s = persistTestReopen(t, dbPath) + + for _, id := range []types.NodeID{adminID, oidcNode.ID()} { + nv, ok := s.GetNodeByID(id) + require.True(t, ok) + assert.True(t, latest.Equal(nv.Expiry().Get()), "node %d", id) + } +} + +// TestLoadedExpiryIsUTC proves an expiry the database returns in another zone is +// served in UTC. PostgreSQL returns the server's zone, where the latest accepted +// expiry is in the year 10000 and cannot be encoded. +func TestLoadedExpiryIsUTC(t *testing.T) { + dbPath, s, nodeID := persistTestSetup(t) + require.NoError(t, s.Close()) + + expiry := time.Date(2030, time.January, 1, 0, 0, 0, 0, time.UTC) + + database, err := db.NewHeadscaleDatabase(persistTestConfig(dbPath)) + require.NoError(t, err) + require.NoError(t, database.DB.Model(&types.Node{}).Where("id = ?", nodeID). + Update("expiry", expiry.In(time.FixedZone("", 60*60))).Error) + require.NoError(t, database.Close()) + + s = persistTestReopen(t, dbPath) + + nv, ok := s.GetNodeByID(nodeID) + require.True(t, ok) + + got := nv.Expiry().Get() + assert.True(t, expiry.Equal(got)) + assert.Equal(t, time.UTC, got.Location()) +} diff --git a/hscontrol/state/state.go b/hscontrol/state/state.go index c9c8b3078..2531631d3 100644 --- a/hscontrol/state/state.go +++ b/hscontrol/state/state.go @@ -277,6 +277,12 @@ func NewState(cfg *types.Config) (*State, error) { // This ensures we don't have stale online status from previous runs for _, node := range nodes { node.IsOnline = new(false) + + // PostgreSQL returns the server's zone, where the latest accepted + // expiry is in the year 10000 and cannot be encoded. + if node.Expiry != nil { + node.Expiry = new(node.Expiry.UTC()) + } } users, err := db.ListUsers(nil) @@ -1008,6 +1014,15 @@ func (s *State) setNodeExpiry( expiry *time.Time, allow func(*types.Node) bool, ) (types.NodeView, change.Change, error) { + if expiry != nil { + utc, err := types.ValidateExpiry(*expiry) + if err != nil { + return types.NodeView{}, change.Change{}, err + } + + expiry = &utc + } + var onlineChanged, expired, refused bool genBefore := s.polMan.NodesGeneration() @@ -2679,6 +2694,16 @@ func (s *State) HandleNodeFromAuthPath( expiry *time.Time, registrationMethod string, ) (types.NodeView, change.Change, error) { + // An OIDC token's expiry arrives here without passing handleRegister. + if expiry != nil { + utc, err := types.ValidateExpiry(*expiry) + if err != nil { + return types.NodeView{}, change.Change{}, err + } + + expiry = &utc + } + // Read before any NodeStore write below; see updatePolicyManagerNodes. genBefore := s.polMan.NodesGeneration() diff --git a/hscontrol/types/node.go b/hscontrol/types/node.go index faa3595fb..c8fb1d757 100644 --- a/hscontrol/types/node.go +++ b/hscontrol/types/node.go @@ -599,6 +599,10 @@ func ValidateGivenName(givenName, baseDomain string) error { // or an error when it cannot be sent to clients. [time.Time.MarshalJSON] refuses // a year outside [0,9999], failing every map response that carries it. A time // that is in range in its own zone can still leave the range in UTC. +// +// Callers store the returned value: SQLite writes a time in its own zone and +// cannot read back a year outside [0,9999], which that zone can reach when UTC +// does not. func ValidateExpiry(expiry time.Time) (time.Time, error) { utc := expiry.UTC() if year := utc.Year(); year < 0 || year > 9999 {