Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/547-official-auth-db-foundation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svforge": minor
---

New Dashboard projects now use the official `sv add drizzle better-auth` integrations for the auth/DB foundation instead of SVForge-generated copies: the official add-ons run first in `svforge create`, and SVForge only extends their output (sign-up policy, `role`/`disabled` via Better Auth `additionalFields`, one composed disabled-identity guard, domain tables). `auth:schema` is now the official generator (configure `DATABASE_URL`, then run `auth:schema` and `db:push` before the first check), the legacy Better Auth pin/tooling and review-copy workflow are removed, and the security audit resolves the range declared by the installed `sv`. Base projects are unchanged; existing projects are not migrated.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ jobs:
# a scratch project and audit its full closure (#319, #460).
- name: Better Auth stack audit
run: |
PIN=$(node --input-type=module -e "import { currentPin } from './scripts/better-auth-upgrade.mjs'; console.log(currentPin().version)")
RANGE=$(node --input-type=module -e "import { officialBetterAuthRange } from './scripts/better-auth-version.mjs'; console.log(officialBetterAuthRange())")
AUDIT_DIR="$(mktemp -d)"
cd "$AUDIT_DIR"
echo '{ "name": "better-auth-stack-audit", "private": true }' > package.json
bun add "better-auth@$PIN" >/dev/null
bun add "better-auth@$RANGE" >/dev/null
cd "$GITHUB_WORKSPACE"
node scripts/better-auth-audit.mjs "$AUDIT_DIR/bun.lock"

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ jobs:
# scratch project and audit its full closure (#319, #460).
- name: Better Auth stack audit
run: |
PIN=$(node --input-type=module -e "import { currentPin } from './scripts/better-auth-upgrade.mjs'; console.log(currentPin().version)")
RANGE=$(node --input-type=module -e "import { officialBetterAuthRange } from './scripts/better-auth-version.mjs'; console.log(officialBetterAuthRange())")
AUDIT_DIR="$(mktemp -d)"
cd "$AUDIT_DIR"
echo '{ "name": "better-auth-stack-audit", "private": true }' > package.json
bun add "better-auth@$PIN" >/dev/null
bun add "better-auth@$RANGE" >/dev/null
cd "$GITHUB_WORKSPACE"
node scripts/better-auth-audit.mjs "$AUDIT_DIR/bun.lock"

Expand Down
84 changes: 50 additions & 34 deletions docs/better-auth-upgrades.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,67 @@
# Better Auth upgrade runbook
# Better Auth ownership, upgrade and security runbook

The dashboard template pins Better Auth; it never scaffolds `latest` or `*`. The authoritative pin is declared in `packages/svforge/src/modes/dashboard.ts` and mirrored in the dashboard template. `tests/better-auth-upgrade.test.ts` guards the carriers.
**The official `sv add better-auth` (with `sv add drizzle`) integrations own the dashboard's auth/DB foundation (#547).** SVForge composes them and owns only its product/security policies. There is no SVForge Better Auth pin any more: the version comes from the published `sv` add-on and its dependency range.

## Ownership and upstream `sv` compatibility
## Ownership contract

| Concern | Owner today | Boundary |
| ------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------- | ------------------------------------------------------------------------------------------------------------ |
| Drizzle client lifecycle, schema, migrations, Better Auth adapter mapping, and pinned dependencies | SVForge dashboard scaffold | `createDb()` exposes the serverless pool limit; auth schema changes are reviewed against the runtime schema. |
| Auth request/session hooks, closed-by-default signup, invitations, roles, disabled-user lifecycle, first-admin bootstrap, and dashboard UX | SVForge dashboard | These are product/security policies, not generic auth setup. |
| Generic Better Auth and Drizzle add-on generation | Upstream `sv` add-ons | Not composed with the dashboard until their generated foundation is compatible and safely extensible. |
| Concern | Owner | Boundary |
| --- | --- | --- |
| Better Auth instance, Drizzle adapter, session cookies, request handler, auth tables, `auth:schema`, Drizzle config/client, dependency versions | Official `sv add drizzle better-auth` | Generated files are upstream-owned; SVForge only extends them |
| `user.role` / `user.disabled` columns | SVForge (`user.additionalFields` config, `input: false`) | Declared through the supported config; the official `auth:schema` script generates them (`not null default`) |
| Closed/invite/self-service sign-up policy, invitations, first-admin bootstrap, admin guards, disabled-session rejection, dashboard UX | SVForge (`auth-policy.ts`, `auth-guard.ts`) | Product/security rules, not generic auth setup |
| Schema/runtime parity gate | CI scaffold matrix (`scripts/check-auth-schema.mjs`) | Compares the **generated** schema with the installed runtime `getSchema()` |

The compatibility experiments below were recorded with published `sv@1.1.0`; the repository currently pins `sv@1.1.1` (see `package.json` and the [dated compatibility report](../packages/svforge/docs/sveltekit-compatibility.md)). The findings are historical evidence, **not a verified failure claim for `sv@1.1.1`**. In that tested fixture, `sv create` generated SvelteKit `^3.0.0`; the official Better Auth add-on requested `^1.6.24`, resolved to `1.7.7` at the time, and declared an optional `@sveltejs/kit: ^2.0.0` peer. npm installation failed with `ERESOLVE`. Bun, pnpm, and Yarn were also tested, but their more permissive peer handling did not make the generated add-ons composable in that fixture.
## How SVForge extends the official output

Both application orders were tested with `sv@1.1.0`. Adding official Drizzle after a dashboard scaffold cancels with `Preexisting drizzle config file`. The official Better Auth add-on proceeds but appends duplicate `auth`/`db` imports, auth exports, and request handlers to the dashboard's existing files; the generated dashboard then fails `svelte-check` with duplicate identifiers. Applying the dashboard after the official add-ons instead overwrites their generated auth, hook, schema, Drizzle, and script files; the package manifest can retain conflicting runtime/dev dependency entries and an unused upstream CLI. The official `demo:none` option only omits demo UI: its generated `emailAndPassword.enabled` remains true with no `disableSignUp`, so it does not meet SVForge's closed-signup default.
The dashboard add-on runs **after** the official add-ons (see `svforge create`) and applies anchored, idempotent patches:

Do not instruct dashboard users to stack the official add-ons with SvelteForge as a workaround. Revisit composition when upstream supports the generated SvelteKit major and provides idempotent extension points for existing auth/Drizzle files and closed signup. Until then, the table above is the ownership contract: keep only the minimum generic integration necessary in SVForge, and keep the differentiated policies here.
1. `src/lib/server/auth.ts` — imports `svforgeAuthPolicy` and spreads it before `plugins` (policy keeps the official `sveltekitCookies` plugin last).
2. `src/hooks.server.ts` — composes `sequence(handleParaglide, handleBetterAuth, handleSvforgePolicy)`; the official handler is untouched.
3. `src/env.ts` + `.env`/`.env.example` — declare/append `SIGNUP_MODE`.
4. `src/lib/server/db/schema.ts` — SVForge domain tables next to the official `export * from './auth.schema'`.

## Manual upgrade policy and validation
A missing anchor fails the scaffold loudly instead of shipping a dashboard without the security policy.

Upgrades are deliberate maintainer changes in a normal pull request. There is no scheduled bot or automatic publication.
## Security behavior (must stay identical)

- sign-up is `closed` by default (`SIGNUP_MODE`), `invite-only` and `self-service` are explicit;
- no sign-up body can set `role` or `disabled` (`input: false`, verified in the POC);
- `role`/`disabled` are persisted, NOT NULL columns with defaults;
- a disabled identity is rejected at session creation and its live sessions are neutralized by the composed guard;
- the initial administrator is created only by the atomic bootstrap (`admin:create` / dev-only `/setup`).

## Historical compatibility record (why the migration waited)

The experiments that motivated #547 were recorded with published `sv@1.1.0`; the repository pins `sv@1.1.1` (see `package.json` and the [dated compatibility report](../packages/svforge/docs/sveltekit-compatibility.md)). They are **historical evidence, not a claim about current versions**:

- that fixture produced SvelteKit `^3.0.0` while the Better Auth add-on declared an optional `@sveltejs/kit: ^2.0.0` peer; npm failed with `ERESOLVE`;
- `sv add drizzle` **after** a dashboard scaffold cancelled with `Preexisting drizzle config file`;
- `sv add better-auth` after a scaffold produced duplicate imports/handlers and broke `svelte-check`;
- applying the old dashboard **after** the official add-ons overwrote their foundation.

- PR CI runs the repository checks and Better Auth stack audit.
- The dashboard schema/runtime scaffold gate runs in the release scaffold matrix before publication, **not** on every PR. It uses real PostgreSQL and checks the installed runtime schema and auth flow.
- The stack audit queries OSV.dev for Better Auth packages and their resolved dependency closure. Critical/high advisories block; documented exceptions use [`audit-baseline.json`](audit-baseline.json).
The #547 proof-of-concept re-tested the published CLI: npm and Bun installs succeed without peer forcing, hooks already compose, and `role`/`disabled` are supported through `additionalFields`. See [`docs/spikes/547-official-integrations-poc.md`](spikes/547-official-integrations-poc.md) and `scripts/spike-547.mjs`.

## Pin and schema constraints
## Schema generation

Use the helper to inspect or update all version carriers:
`auth:schema` is the official script (`auth generate --config src/lib/server/auth.ts --output src/lib/server/db/auth.schema.ts`). It needs a valid `DATABASE_URL` and rewrites its output in place. The generated file is upstream-owned: never hand-edit it. The scaffold gate (`scripts/check-auth-schema.mjs`) compares it with the installed runtime schema, including the SVForge `role`/`disabled` columns (allowlisted as app-level auth-user extensions).

## Upgrade and audit policy

Upgrades are deliberate maintainer changes in a normal pull request. There is no scheduled bot or automatic publication.

- PR CI runs the repository checks and the Better Auth stack audit.
- The audit resolves the range declared by the **installed official `sv` add-on** (`scripts/better-auth-version.mjs`), not a repository pin:

```bash
node scripts/better-auth-upgrade.mjs pin
node scripts/better-auth-upgrade.mjs apply --better-auth <version>
RANGE=$(node --input-type=module -e "import { officialBetterAuthRange } from './scripts/better-auth-version.mjs'; console.log(officialBetterAuthRange())")
AUDIT_DIR=$(mktemp -d)
trap 'rm -rf "$AUDIT_DIR"' EXIT
( cd "$AUDIT_DIR" && echo '{"name":"audit","private":true}' > package.json && bun add "better-auth@$RANGE" )
node scripts/better-auth-audit.mjs "$AUDIT_DIR/bun.lock"
```

`@better-auth/cli` is not a scaffold dependency. Its bundled core can lag behind the runtime and conflict with the scaffold's dependency tree. The schema/runtime gate compares `auth.schema.ts` with the schema derived from the installed Better Auth runtime (`getSchema()`); the CLI generation step is only a smoke test of the `auth:schema` path.

Keep the generated schema output as a review copy. The CLI `--output` must be a new, relative path; generating directly over `src/lib/server/db/auth.schema.ts` can erase runtime-only fields such as `user.role` and `user.disabled`. Review and merge schema changes deliberately.
- Critical/high advisories block; documented exceptions live in [`audit-baseline.json`](audit-baseline.json).
- The dashboard schema/runtime scaffold gate runs in the release scaffold matrix (real PostgreSQL), not on every PR.

## Local validation

Expand All @@ -47,20 +73,10 @@ bun run build:all
bun run test
```

The release-only dashboard gate can be run locally when PostgreSQL is available:
With PostgreSQL available, run the dashboard scaffold gate:

```bash
bash scripts/test-scaffold.sh dashboard
```

To run the audit against the current pin, resolve it in a temporary project as CI does:

```bash
PIN=$(node --input-type=module -e "import { currentPin } from './scripts/better-auth-upgrade.mjs'; console.log(currentPin().version)")
AUDIT_DIR=$(mktemp -d)
trap 'rm -rf "$AUDIT_DIR"' EXIT
( cd "$AUDIT_DIR" && echo '{"name":"audit","private":true}' > package.json && bun add "better-auth@$PIN" )
node scripts/better-auth-audit.mjs "$AUDIT_DIR/bun.lock"
```

After review, open a normal pull request. See [`docs/RELEASE.md`](RELEASE.md) for publication and versioning steps.
Loading
Loading