From 5676e91629ef218212eff48e698f8fe917003bdc Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 17 Nov 2026 18:45:19 +0000 Subject: [PATCH] api/v1: return 409 when a unique index refuses a write Rename to a taken user name always returned 500, and so did every concurrent create that passed the pre-check and lost the insert. --- CHANGELOG.md | 1 + hscontrol/api/v1/errors.go | 5 +++- hscontrol/apiv1_users_test.go | 45 +++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bf76ac07..fce35f738 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -283,6 +283,7 @@ the `tailscale.com/cap/tailsql` app capability lets a user query it; see - The v1 API stores an API key or pre-auth key created without `expiration` as one that never expires, and reports its expiration as `null`. It was stored as already expired and rejected on first use. The CLI always sends an expiration and was not affected. Keys created this way before stay expired; create new ones [#3254](https://github.com/juanfont/headscale/pull/3254) - The v2 API accepts an auth key create that leaves out `tags`, as Tailscale does [#3254](https://github.com/juanfont/headscale/pull/3254) - The v2 API refuses an auth key `expirySeconds` below 0 or above 90 days, as Tailscale does; a negative value used to mean 90 days [#3254](https://github.com/juanfont/headscale/pull/3254) +- The v1 API returns `409 Conflict`, not `500`, when a user create or rename asks for a name another user already has; a rename to a taken name always returned `500` ## 0.29.7 (2026-10-30) diff --git a/hscontrol/api/v1/errors.go b/hscontrol/api/v1/errors.go index b83b04bdb..2142030e4 100644 --- a/hscontrol/api/v1/errors.go +++ b/hscontrol/api/v1/errors.go @@ -55,7 +55,10 @@ func mapError(msg string, err error) error { errors.Is(err, state.ErrInvalidPolicy): return huma.Error400BadRequest(msg, err) - case errors.Is(err, state.ErrNodeKeyInUse), + // A unique index refused the write, e.g. a racing create or a rename to a + // taken user name. + case errors.Is(err, gorm.ErrDuplicatedKey), + errors.Is(err, state.ErrNodeKeyInUse), errors.Is(err, state.ErrAmbiguousNodeOwnership), errors.Is(err, state.ErrAuthCacheEntryExists): return huma.Error409Conflict(msg, err) diff --git a/hscontrol/apiv1_users_test.go b/hscontrol/apiv1_users_test.go index dd8d171ea..6fa670ece 100644 --- a/hscontrol/apiv1_users_test.go +++ b/hscontrol/apiv1_users_test.go @@ -3,6 +3,7 @@ package hscontrol import ( "encoding/json" "net/http" + "sync" "testing" "github.com/juanfont/headscale/hscontrol/types" @@ -51,9 +52,53 @@ func TestAPIV1CreateUser(t *testing.T) { []byte(`{"name":"dup"}`)) assertStatus(t, res, http.StatusConflict) }) + + // Concurrent creates all pass the handler's pre-check; the losers must + // still see a conflict when the unique index refuses their insert. + t.Run("concurrent duplicate name conflicts", func(t *testing.T) { + h := newAPIV1Harness(t) + + const n = 16 + + var wg sync.WaitGroup + + start := make(chan struct{}) + statuses := make(chan int, n) + + for range n { + wg.Go(func() { + <-start + + statuses <- h.callHuma(http.MethodPost, "/api/v1/user", []byte(`{"name":"race"}`)).status + }) + } + + close(start) + wg.Wait() + close(statuses) + + counts := map[int]int{} + for s := range statuses { + counts[s]++ + } + + assert.Equal(t, map[int]int{http.StatusOK: 1, http.StatusConflict: n - 1}, counts) + }) } func TestAPIV1RenameUser(t *testing.T) { + t.Run("duplicate name conflicts", func(t *testing.T) { + h := newAPIV1Harness(t) + seedUsers("alice", "bob")(t, h.app) + + res := h.callHuma(http.MethodPost, "/api/v1/user/2/rename/alice", nil) + assertStatus(t, res, http.StatusConflict) + + got, err := h.app.state.GetUserByID(2) + require.NoError(t, err) + assert.Equal(t, "bob", got.Name) + }) + t.Run("parity", func(t *testing.T) { assertParityIsolated(t, seedUsers("alice"), http.MethodPost, "/api/v1/user/1/rename/bob", nil)