From 92bc0b6226c9e2b220f20e22fa015fb2fd79d80d Mon Sep 17 00:00:00 2001 From: Seena Fallah Date: Sat, 20 Jun 2026 17:24:36 +0200 Subject: [PATCH] api/v1: persist omitted key expiration as NULL, not zero time CreateAPIKey and CreatePreAuthKey declared a zero-valued time.Time and passed its address to the state layer, so a request that omitted the expiration persisted 0001-01-01 instead of NULL. Both key types were then rejected as already-expired on first use. Pass the request body's *time.Time straight through (nil when omitted). Signed-off-by: Seena Fallah --- CHANGELOG.md | 1 + hscontrol/api/v1/apikeys.go | 12 ++++-------- hscontrol/api/v1/preauthkeys.go | 11 ++++------- hscontrol/apiv1_apikeys_test.go | 20 ++++++++++++++++++++ hscontrol/apiv1_preauthkeys_test.go | 22 ++++++++++++++++++++++ 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4991c9733..ada61ffe0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -196,6 +196,7 @@ the ACME DNS-01 challenge through a built-in DNS provider: `acmedns`, - The v2 API adds `GET /device/{id}/attributes`, scoped `devices:posture_attributes:read`. It reports `ip:publicAddress`: the source IP of the node's latest control connection, stored on the node. Behind a reverse proxy, set `trusted_proxies` or it reports the proxy. [#3419](https://github.com/juanfont/headscale/pull/3419) - PostgreSQL connection settings are assembled into a `postgres://` URL with proper escaping, so a password (or host, user, database name) containing spaces, quotes or other reserved characters no longer truncates the DSN [#3477](https://github.com/juanfont/headscale/pull/3477) - Expiring or deleting a non-existent pre-auth key now returns an error instead of silently succeeding [#3324](https://github.com/juanfont/headscale/pull/3324) +- Fix API keys and pre-auth keys created without an expiration being stored as `0001-01-01` instead of NULL, which made them immediately rejected as expired [#3254](https://github.com/juanfont/headscale/pull/3254) - Improve systemd service file hardening [#3341](https://github.com/juanfont/headscale/pull/3341) - Fix `headscale users destroy`/`rename` reporting "multiple users match query" when no user matches; an ambiguous match now lists the matching users [#3476](https://github.com/juanfont/headscale/pull/3476) - Deleting a user that still owns nodes now lists the nodes (ID and hostname) that must be deleted first [#3475](https://github.com/juanfont/headscale/pull/3475) diff --git a/hscontrol/api/v1/apikeys.go b/hscontrol/api/v1/apikeys.go index 9fe503922..c483fb967 100644 --- a/hscontrol/api/v1/apikeys.go +++ b/hscontrol/api/v1/apikeys.go @@ -85,14 +85,10 @@ func registerApiKeys(api huma.API, b Backend) { Tags: []string{"ApiKeys"}, Security: bearerAuth, }, func(ctx context.Context, in *createApiKeyInput) (*createApiKeyOutput, error) { - // CreateAPIKey requires a non-nil pointer; default a missing expiration - // to the zero time as the gRPC handler does. - var expiration time.Time - if in.Body.Expiration != nil { - expiration = *in.Body.Expiration - } - - keyStr, _, err := b.State.CreateAPIKey(&expiration) + // A missing expiration must persist as NULL, not the zero time, or the + // key is rejected as already-expired on first use. Body.Expiration is + // already *time.Time (nil when omitted), so pass it straight through. + keyStr, _, err := b.State.CreateAPIKey(in.Body.Expiration) if err != nil { return nil, huma.Error500InternalServerError("creating api key", err) } diff --git a/hscontrol/api/v1/preauthkeys.go b/hscontrol/api/v1/preauthkeys.go index 489d09dd7..013e9935a 100644 --- a/hscontrol/api/v1/preauthkeys.go +++ b/hscontrol/api/v1/preauthkeys.go @@ -102,12 +102,6 @@ func registerPreAuthKeys(api huma.API, b Backend) { } } - // CreatePreAuthKey requires a non-nil pointer; zero-stamp when unset. - var expiration time.Time - if in.Body.Expiration != nil { - expiration = *in.Body.Expiration - } - var userID *types.UserID if user != 0 { @@ -119,11 +113,14 @@ func registerPreAuthKeys(api huma.API, b Backend) { userID = u.TypedID() } + // A missing expiration must persist as NULL, not the zero time, or the + // key is rejected as already-expired on first use. Body.Expiration is + // already *time.Time (nil when omitted), so pass it straight through. preAuthKey, err := b.State.CreatePreAuthKey( userID, in.Body.Reusable, in.Body.Ephemeral, - &expiration, + in.Body.Expiration, in.Body.ACLTags, ) if err != nil { diff --git a/hscontrol/apiv1_apikeys_test.go b/hscontrol/apiv1_apikeys_test.go index 74b4634ae..4e839681e 100644 --- a/hscontrol/apiv1_apikeys_test.go +++ b/hscontrol/apiv1_apikeys_test.go @@ -54,6 +54,26 @@ func TestAPIV1ApiKeyCreate(t *testing.T) { require.NoError(t, json.Unmarshal(hum.body, &humBody)) assert.NotEmpty(t, humBody.APIKey) }) + + t.Run("no expiration is not immediately expired", func(t *testing.T) { + h := newAPIV1Harness(t) + + res := h.callHuma(http.MethodPost, "/api/v1/apikey", []byte(`{}`)) + require.Equal(t, http.StatusOK, res.status) + + var got struct { + APIKey string `json:"apiKey"` + } + require.NoError(t, json.Unmarshal(res.body, &got)) + require.NotEmpty(t, got.APIKey) + + // A missing expiration must persist as NULL, not the zero time, or the + // key is rejected as already-expired on first use (regression for the + // fix that the gRPC handler received in #3254). + valid, err := h.app.state.ValidateAPIKey(got.APIKey) + require.NoError(t, err) + assert.True(t, valid, "api key created without expiration must not be expired") + }) } func TestAPIV1ApiKeyList(t *testing.T) { diff --git a/hscontrol/apiv1_preauthkeys_test.go b/hscontrol/apiv1_preauthkeys_test.go index 4d50cbabc..ad599c4d3 100644 --- a/hscontrol/apiv1_preauthkeys_test.go +++ b/hscontrol/apiv1_preauthkeys_test.go @@ -76,6 +76,28 @@ func TestAPIV1CreatePreAuthKey(t *testing.T) { assert.Equal(t, []any{"tag:test"}, got.PreAuthKey["aclTags"]) }) + t.Run("no expiration persists as NULL not zero-time", func(t *testing.T) { + h := newAPIV1Harness(t) + + // No expiration field in the body. The JSON response can't distinguish + // NULL from the zero time (both render as 0001-01-01), so inspect the + // stored key directly. + res := h.callHuma(http.MethodPost, "/api/v1/preauthkey", + []byte(`{"aclTags":["tag:test"]}`)) + require.Equal(t, http.StatusOK, res.status) + + keys, err := h.app.state.ListPreAuthKeys() + require.NoError(t, err) + require.Len(t, keys, 1) + + // A missing expiration must persist as NULL, not the zero time, or the + // key is rejected as already-expired on first use (regression for the + // fix that the gRPC handler received in #3254). + assert.Nil(t, keys[0].Expiration, "expiration must be NULL when omitted") + assert.NoError(t, keys[0].Validate(), + "key created without expiration must be valid") + }) + t.Run("invalid tag parity", func(t *testing.T) { h := newAPIV1Harness(t) h.app.state.CreateUserForTest("alice")