Skip to content

fix(auth): gate /auth/seed-admin to non-production environments - #1063

Open
mmcintosh wants to merge 5 commits into
mainfrom
fix/seed-admin-env-gate
Open

mmcintosh wants to merge 5 commits into
mainfrom
fix/seed-admin-env-gate

Conversation

@mmcintosh

@mmcintosh mmcintosh commented Aug 20, 2026 •

Copy link
Copy Markdown
Collaborator

Description

POST /auth/seed-admin is mounted on every deployment with no authentication and no environment guard: only a rate limit, and it is CSRF-exempt. On any live site, an anonymous request creates or resets admin@sonicjs.com / sonicjs! with the admin role and admin RBAC: if that account exists, its password is reset. It also creates or resets an editor, e@e.com. That is an unauthenticated admin takeover that needs no misconfiguration at all.

Merge after #1104 (register form) and #1019 in this batch. This endpoint is one way into a broken fresh install, and those two PRs fix the normal way in, so closing it last avoids leaving people with no way in.

No public issue (security fix).

Changes

  • routes/auth.ts: the route needs both of these, or it returns 403:
    • ENABLE_SEED_ADMIN = "true", an explicit opt-in;
    • ENVIRONMENT set to one of development, test, e2e, preview or local. Unset or production is refused.
  • Why the environment check alone isn't enough: the create-sonicjs starter sets ENVIRONMENT = "development" in its top-level [vars], and its npm run deploy is a plain wrangler deploy. So a default deploy ships as "development".
  • my-sonicjs-app/wrangler.toml: sets ENABLE_SEED_ADMIN = "true" in the test app's top-level [vars], used by the CI preview and local dev, so E2E seeding still works. Never deploy that app for real with it set.
    • Nothing in create-sonicjs calls the route; its seed script writes to the database directly.
    • Self-host doesn't set the flag; it seeds through seed-self-host.ts.
  • 403 message: in an allowed environment without the flag, it names ENABLE_SEED_ADMIN. Any other environment, including unset, gets a generic message.
  • Breaking (dev only): apps outside this repo, including create-sonicjs apps under wrangler dev, must add ENABLE_SEED_ADMIN = "true" to their local vars to keep using seed-admin.
  • Docs: docs/authentication.md, packages/core/ROUTE-MIGRATION.md, www/src/app/authentication/page.mdx and tests/postman/README.md now describe the gate. The authentication page's seed-admin rate limit is corrected from 2 to 10 per minute.
  • E2E spec renamed from 108 to 114, because 108 is taken on main.

Testing

  • seed-admin-gate.test.ts (13 tests):
    • refused: production, unset, unknown, mixed-case Production;
    • refused when the flag is missing, false or empty in development;
    • refused in production even with the flag;
    • allowed only with the flag plus an allowed environment;
    • the message names the flag in development without it, and not in production even with the flag.

Unit Tests

  • Added/updated unit tests
  • All unit tests passing (gate tests 13 / 13; all five batch PRs merged: 2000 / 0)

E2E Tests

  • Added/updated E2E tests: 114-seed-admin-env-gate.spec.ts (@smoke @auth). It guards that seeding and admin login still work in the preview. The refused cases are covered by the unit test, because the preview can't be switched to production.
  • All E2E tests passing (not run locally, per the repo's E2E policy)

Screenshots/Videos

N/A. No UI changes.

Checklist

  • Code follows project conventions
  • Tests added/updated and passing
  • Type checking passes
  • No console errors or warnings
  • Documentation updated (if needed)

The seed-admin endpoint had no auth and no environment guard (only a rate
limit), despite its 'development/testing only' comment. On any deployment an
anonymous request created admin@sonicjs.com / sonicjs! with role admin, and
for an existing admin it overwrote the credential password — an unauthenticated
admin takeover requiring zero misconfiguration.

Gate it fail-closed to explicit non-production ENVIRONMENT values
(development/test/e2e/preview/local), matching the test-cleanup routes. An unset
or 'production' ENVIRONMENT is denied, so a default `wrangler deploy` cannot
expose it. Production admin provisioning goes through the bootstrap seed path.

The E2E preview runs with ENVIRONMENT=development, so seeding still works there;
the denied path is covered by a unit test.
The ENVIRONMENT allowlist alone does not close the endpoint for create-sonicjs
apps: the starter's wrangler.toml sets ENVIRONMENT = "development" in its
top-level [vars], and its `npm run deploy` is a plain `wrangler deploy`, so a
default deploy ships with ENVIRONMENT=development and the anonymous
fixed-credential admin reset stays open.

The route now also requires ENABLE_SEED_ADMIN = "true", set only in this repo's
my-sonicjs-app/wrangler.toml (E2E preview and local dev). Nothing in the
starter calls the route; it seeds through its own script.

Also: correct the comment that pointed production admins at a "bootstrap seed
path" (there is none on Workers; the first registered account becomes admin),
and renumber the E2E spec 108 -> 114 (108 is taken on main).
Review follow-up on the seed-admin gate.

- In an allowed (non-production) ENVIRONMENT without the opt-in, the 403 now
  says to set ENABLE_SEED_ADMIN = "true", so a developer isn't left guessing.
  Production keeps the generic message and never mentions the flag.
- docs/authentication.md, ROUTE-MIGRATION.md, the authentication docs page and
  the postman README now say the endpoint needs the flag and a non-production
  ENVIRONMENT. The docs page's rate limit for it said 2 per minute; the code
  allows 10.
'Non-production' was looser than the code: only development, test, e2e, preview
and local are allowed, so e.g. staging is refused.

This branch was successfully deployed

1 active deployment
internal — a7ca2f1d Deployed Oct 13, 2026 by mmcintosh via authorize #1610
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant