From 1665f78e5bd114fabcc254db3cc178b231ef4ffd Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 23 Sep 2026 00:55:57 +1000 Subject: [PATCH] fix(stateadmin): drop the default users collection and pin the sqlite driver PocketBase creates a "users" auth collection on first bootstrap whose createRule is open, so an unauthenticated caller could POST a record and write a row into the State Store's database file. This surface authenticates superusers only and has no notion of end users, so the collection is deleted rather than given a tighter rule: one that is absent cannot be loosened again by a later PocketBase default. Also re-applies the modernc pin. It was applied while developing the previous commit but a concurrent go.mod resolution reverted it before that commit was taken, so the tree has been running 1.59.0/1.77.1 against a PocketBase built and tested on 1.57.0/1.74.4, printing the mismatch warning on every start. Note for future dependency work: go get -u will undo this pin, and modernc.org/libc does not follow semver, which is why PocketBase checks. The claim in the previous commit message that the pin landed there is wrong. --- go.mod | 4 +-- go.sum | 7 +++++ .../infrastructure/stateadmin/stateadmin.go | 22 +++++++++++++++ .../stateadmin/stateadmin_test.go | 28 +++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index a5f4658a4..6b6443806 100644 --- a/go.mod +++ b/go.mod @@ -71,7 +71,7 @@ require ( golang.org/x/time v0.16.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20260921155816-b14227669459 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20260921155816-b14227669459 // indirect - modernc.org/libc v1.77.1 // indirect + modernc.org/libc v1.74.4 // indirect modernc.org/mathutil v1.7.1 // indirect modernc.org/memory v1.12.1 // indirect ) @@ -101,5 +101,5 @@ require ( google.golang.org/grpc v1.85.0-dev.0.20260825072537-93e31b48545e google.golang.org/protobuf v1.36.12 gopkg.in/yaml.v3 v3.0.1 - modernc.org/sqlite v1.59.0 + modernc.org/sqlite v1.57.0 ) diff --git a/go.sum b/go.sum index deabe5645..b4c2313cc 100644 --- a/go.sum +++ b/go.sum @@ -298,8 +298,10 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q= gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA= +modernc.org/cc/v4 v4.29.1 h1:MKgdCV3WykTSPqpVrnxdEDS0HEd2FHpKZDzxzU5LyeI= modernc.org/cc/v4 v4.29.7 h1:q+NXGJ0bK3b4TXFYQQVr9pYETGnmwFWkrUzJnMya/Tg= modernc.org/cc/v4 v4.29.7/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI= +modernc.org/ccgo/v4 v4.34.6 h1:sBgfIwyN0TQ9C5hwIeuqyeAKyMWnbvj2fvpF4L11uzU= modernc.org/ccgo/v4 v4.36.0 h1:T5h7ZMcrw2r94oDLtBcU3YI6e1W8DiUg2SJIs2fwD2s= modernc.org/ccgo/v4 v4.36.0/go.mod h1:rrtGc2QkS239nYb/mQNuBMyjq3/y3ZXWbBjPoV3wqzA= modernc.org/ccgo/v4 v4.36.1 h1:ZNIUZAryN0UgnJwtyxrdEzcFc3yD4Cu4AzjfPXsLsIE= @@ -307,10 +309,13 @@ modernc.org/fileutil v1.4.0 h1:j6ZzNTftVS054gi281TyLjHPp6CPHr2KCxEXjEbD6SM= modernc.org/fileutil v1.4.0/go.mod h1:EqdKFDxiByqxLk8ozOxObDSfcVOv/54xDs/DUHdvCUU= modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI= modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito= +modernc.org/gc/v3 v3.1.4 h1:2g65LGVSmFQrXeITAw97x7hCRvZFcyE1uDP+7Vng7JI= modernc.org/gc/v3 v3.1.5 h1:21ldfPfRYE31Tb7B3mwAK8gy1AxP4+dKjrOQPfqakoc= modernc.org/gc/v3 v3.1.5/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY= modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks= modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI= +modernc.org/libc v1.74.4 h1:fX1Omw4o2/1C2iRkkIsrQTasJQldLhRmuPreXLoWs9k= +modernc.org/libc v1.74.4/go.mod h1:eeQAS9W3sZeKYMFubydxJpII9ybHWshk+7or7bLG9co= modernc.org/libc v1.77.0 h1:wV+faa9ixRoUNlakU0fHLi/1kV2YLG8D8Ksk3c2mPtY= modernc.org/libc v1.77.0/go.mod h1:BmXDCJcMkRMRbX2EI8+Zhh4fKWlHNbCCZEy0tiZevP4= modernc.org/libc v1.77.1 h1:Ct8j47QtiZ1Enj2DtFXQtUqrPCAjdCmPjtCuvrYQ0Hs= @@ -323,6 +328,8 @@ modernc.org/opt v0.2.0 h1:tGyef5ApycA7FSEOMraay9SaTk5zmbx7Tu+cJs4QKZg= modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns= modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w= modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE= +modernc.org/sqlite v1.57.0 h1:qNQP6xnx5M0ISNtlnxoOX0+cD5bJ0/gr9aMmndFczzg= +modernc.org/sqlite v1.57.0/go.mod h1:yCJ2cmAaIkHQ25oXWrF8H4O1lIfPYPR26yCEDj2P3pQ= modernc.org/sqlite v1.59.0 h1:X1es1GpqBlS/5T+vbM4HLUdaa8OtQx468DF2vrx+38A= modernc.org/sqlite v1.59.0/go.mod h1:+paeT2A3iPRHkQDwG7oA6Tk0zQd5woMEI8q7orfry8k= modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0= diff --git a/internal/infrastructure/stateadmin/stateadmin.go b/internal/infrastructure/stateadmin/stateadmin.go index 8ae03a45a..05a378037 100644 --- a/internal/infrastructure/stateadmin/stateadmin.go +++ b/internal/infrastructure/stateadmin/stateadmin.go @@ -107,6 +107,9 @@ func New(cfg Config) (*Server, error) { if err := app.RunAllMigrations(); err != nil { return nil, fmt.Errorf("stateadmin: run migrations: %w", err) } + if err := removeDefaultAuthCollection(app); err != nil { + return nil, err + } if err := registerViews(app); err != nil { return nil, err } @@ -144,6 +147,25 @@ func registerViews(app core.App) error { return nil } +// removeDefaultAuthCollection deletes the "users" collection PocketBase +// creates on first bootstrap. +// +// Its createRule is open, so leaving it in place lets an unauthenticated +// caller POST a record and write a row into the State Store's database file. +// This surface authenticates superusers only and has no notion of end users, +// so the collection is deleted rather than given a tighter rule: a collection +// that is not there cannot be loosened again by a later PocketBase default. +func removeDefaultAuthCollection(app core.App) error { + collection, err := app.FindCollectionByNameOrId("users") + if err != nil { + return nil // already absent, including on every restart after the first + } + if err := app.Delete(collection); err != nil { + return fmt.Errorf("stateadmin: remove default users collection: %w", err) + } + return nil +} + // refuseWritePaths answers every refusedPrefixes route with 404. func refuseWritePaths(r *router.Router[*core.RequestEvent]) { r.BindFunc(func(e *core.RequestEvent) error { diff --git a/internal/infrastructure/stateadmin/stateadmin_test.go b/internal/infrastructure/stateadmin/stateadmin_test.go index 03a6b3fbf..b867bae7f 100644 --- a/internal/infrastructure/stateadmin/stateadmin_test.go +++ b/internal/infrastructure/stateadmin/stateadmin_test.go @@ -5,6 +5,7 @@ import ( "net/http" "net/http/httptest" "path/filepath" + "strings" "testing" "github.com/samcharles93/archie-core/internal/domain/workflow" @@ -166,3 +167,30 @@ func TestDashboardIsServed(t *testing.T) { t.Errorf("GET /_/ status = %d, want 200: the dashboard is the surface's reason to exist", rec.Code) } } + +// TestNoOpenRegistration pins that the surface has no self-service signup. +// PocketBase creates a "users" auth collection on first bootstrap whose +// createRule is open, so an unauthenticated caller could POST a record and +// write a row into the State Store's own database file. The surface is +// superuser-only by design, so the collection is removed rather than left +// with a tightened rule: a collection that does not exist cannot regress. +func TestNoOpenRegistration(t *testing.T) { + srv := newAdmin(t, openArchieDB(t)) + + if _, err := srv.App().FindCollectionByNameOrId("users"); err == nil { + t.Error("the default users collection still exists; it accepts unauthenticated record creation into archie's database file") + } + + mux, err := srv.Handler() + if err != nil { + t.Fatalf("Handler() error = %v", err) + } + body := strings.NewReader(`{"email":"nobody@example.com","password":"hunter2hunter2","passwordConfirm":"hunter2hunter2"}`) + req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/collections/users/records", body) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + mux.ServeHTTP(rec, req) + if rec.Code < 400 { + t.Errorf("POST /api/collections/users/records status = %d, want a refusal: unauthenticated writes must not reach the State Store's file", rec.Code) + } +}