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.
This commit is contained in:
sam committed 2026-09-23 00:55:57 +10:00
1 parent 97f55793c4
commit 3ccc57d445
4 files changed
+59 -2

No files matched your search

@@ -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 {
@@ -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)
}
}