Skip to content

Commit 93645f1

Browse files
authored
Retire the legacy auth; add the pgstencil CLI, billing security rules and email options (#100)
2 parents 41f1705 + 1284e9d commit 93645f1

146 files changed

Lines changed: 4178 additions & 7274 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.env.example‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# Copy to .env for optional local OAuth. Blank credentials keep providers disabled.
2-
# Register the exact callbacks documented in OAUTH.md. Never commit real secrets.
3-
PORT=3000
4-
PUBLIC_ORIGIN=http://127.0.0.1:3000
2+
# Register the exact callbacks listed in examples/better-auth/README.md, on the
3+
# origin `pnpm dev` prints for this PORT. Never commit real secrets.
4+
PORT=8082
55
GOOGLE_CLIENT_ID=
66
GOOGLE_CLIENT_SECRET=
7+
APPLE_CLIENT_ID=
8+
APPLE_CLIENT_SECRET=
9+
FACEBOOK_CLIENT_ID=
10+
FACEBOOK_CLIENT_SECRET=
711
GITHUB_CLIENT_ID=
812
GITHUB_CLIENT_SECRET=
13+
MICROSOFT_CLIENT_ID=
14+
MICROSOFT_CLIENT_SECRET=

‎.github/audit/security.md‎

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
# Domain: security
22

3-
**Scope — this file, and no other:**
3+
**Scope — these, and no others:**
44

5-
- `SECURITY.md`
5+
- `SECURITY.md`: every `FAIL IF`, and the code and tests each bullet names
6+
- `packages/stripe/src/`: the `Billing` class in `index.ts` and `db.ts`, and
7+
the StripeDev simulator in `testing.ts` that the billing suites run against
8+
- `packages/stripe/migrations/`: the `pgstencil_billing` schema and its
9+
`owner_access` view
10+
- the billing suites: `tests/integration/billing.test.ts`,
11+
`tests/integration/billing-security.test.ts`,
12+
`tests/integration/billing-workers.test.ts` and
13+
`tests/integration/billing-http.test.ts`
614

715
**Output file:** `audit-report.md`
816

917
This is a code-and-policy audit of what the packed packages guarantee to a
1018
consuming application. Everything `SECURITY.md` assigns to the application —
1119
TLS, the origin gate, page CSP, secret storage, database provisioning, provider
12-
registration — is out of scope; so are `@pgstencil/stripe` and the original
13-
code/link `Auth` exports, which carry no rules yet.
20+
registration, and for billing the owner's authentication, CSRF on its billing
21+
routes, raw-body webhook delivery, Stripe key storage and the Stripe account's
22+
configuration — is out of scope; so is the rest of `@pgstencil/stripe` that
23+
`SECURITY.md` lists under "What is not defended".
1424

1525
## Mechanical pass
1626

@@ -21,12 +31,18 @@ each bullet names.
2131
```sh
2232
pnpm db:verify # migrations apply and match the committed schema
2333
pnpm test:unit # diagnostics allowlist and the other unit rules
24-
pnpm test:better-auth # the session, CSRF, email-code, OAuth and linking suites
34+
pnpm exec vitest run tests/integration/better-auth.test.ts \
35+
tests/integration/better-auth-workers.test.ts \
36+
tests/integration/better-auth-oauth.test.ts # the session, CSRF, email-code, OAuth and linking suites
37+
pnpm exec vitest run tests/integration/billing.test.ts \
38+
tests/integration/billing-security.test.ts \
39+
tests/integration/billing-workers.test.ts \
40+
tests/integration/billing-http.test.ts # the webhook, checkout and entitlement suites
2541
pnpm packages:verify # packs, installs into a clean project, checks provenance
2642
```
2743

28-
`test:better-auth` and `packages:verify` need a working Docker daemon (see
29-
`compose.yaml`). If the daemon is unreachable, every check those suites pin is
44+
The auth suites, the billing suites and `packages:verify` need a working
45+
Docker daemon (see `compose.yaml`). If the daemon is unreachable, every check those suites pin is
3046
`UNVERIFIABLE` — say so and say why. **Never record a check as PASS on the
3147
strength of a test you did not run**, and never quietly skip one.
3248

@@ -51,11 +67,19 @@ The remaining bullets are read rather than run:
5167
- The `Packed provenance` bullets are read over `scripts/build-packages.ts`,
5268
`scripts/pack-packages.ts` and `scripts/verify-packages.ts`, with
5369
`pnpm packages:verify` as the evidence.
70+
- The `Stripe webhooks`, `Checkout` and `Entitlement` bullets are pinned by the
71+
billing suites, and each also names the `Billing` members to read in
72+
`packages/stripe/src/index.ts`. Read those members in full: several clauses
73+
(the tolerance, the injected clock, the order of the ledger, resync and
74+
`apply`, what `synchronize` checks) are statements about the code that a
75+
passing test only samples. Compare `SUBSCRIBED` with the status list in
76+
`007_billing_owner_access.sql` character for character.
5477

5578
## Qualitative pass
5679

5780
Be adversarial, and go past the `FAIL IF` list. Read
58-
`packages/auth/src/better-auth*.ts`, `packages/pgstencil/src/*.ts`, and the
81+
`packages/auth/src/better-auth*.ts`, `packages/pgstencil/src/*.ts`,
82+
`packages/stripe/src/index.ts`, `packages/stripe/migrations/*.sql`, and the
5983
suites under `tests/` — including what they do _not_ cover. Ask at least:
6084

6185
- **Can a caller influence where an OAuth callback lands, or reuse state across
@@ -93,6 +117,25 @@ suites under `tests/` — including what they do _not_ cover. Ask at least:
93117
`pgstencil.json` migrations path, and the bundled `compose.yaml` fallback all
94118
cross into a child process. Look for string-built commands, unquoted
95119
interpolation, and a path that escapes the project root.
120+
- **Can anything from a webhook body reach a billing table, or grant access?**
121+
Follow every read of `event` in `webhook` and `completedCheckout` in
122+
`packages/stripe/src/index.ts`. The body may choose whose state to resync and
123+
which row `context.subscription` reads back; anything more — a status, a
124+
period end, a Price, an owner taken from metadata — is the finding.
125+
- **Can one owner end up with two chargeable subscriptions, a second trial, or
126+
another owner's Checkout?** Trace `checkout` through `account`, `customer`,
127+
`reconcile`, the locked transaction and the Stripe call made after it
128+
commits, and `cancelCheckout` and `replace` racing a completion in Stripe.
129+
Check that every Stripe write is either keyed by a value committed first or
130+
safe to repeat.
131+
- **Does a billing error path leak?** `verifyWebhook` puts the Stripe SDK's
132+
message into the `BillingError` an adapter may return to the sender; follow
133+
every error `webhook` raises to `diagnostic`, and look for a path that
134+
serializes a body, a header, a secret or a message.
135+
- **Does a billing test pin what its bullet claims, or only what StripeDev
136+
allows?** `packages/stripe/src/testing.ts` is a simplified Stripe; a test
137+
whose assertion holds only because the simulator cannot produce the bad case
138+
leaves its clause unenforced.
96139
- **What do the integration tests not cover?** Name the gaps. A guarantee whose
97140
only evidence is that nobody has attacked it is worth saying out loud.
98141
- **Have `SECURITY.md` and the code drifted?** Say which side is wrong. A bullet

‎.github/renovate.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@
9191
"minimumReleaseAge": "14 days"
9292
},
9393
{
94-
"description": "@types/node must not expose APIs newer than the Node major in .nvmrc and CI",
94+
"description": "@types/node must not expose APIs newer than the Node major pinned in devEngines.runtime, which CI reads",
9595
"matchManagers": ["npm"],
9696
"matchPackageNames": ["@types/node"],
9797
"allowedVersions": ">=24 <25"
9898
},
9999
{
100-
"description": "Node is governed by engines, .nvmrc and the CI setup-node version, and is bumped by hand",
100+
"description": "Node is pinned in the root package.json devEngines.runtime, which pnpm and CI both read, and is bumped by hand",
101101
"matchPackageNames": ["node"],
102102
"enabled": false
103103
}

‎.github/workflows/check.yml‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@ jobs:
1313
runs-on: ubuntu-latest
1414
timeout-minutes: 20
1515
steps:
16-
- uses: actions/checkout@v7
16+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
1717
with:
1818
persist-credentials: false
19-
- uses: pnpm/action-setup@v6
20-
- uses: actions/setup-node@v7
19+
- uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1
20+
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
2121
with:
22-
node-version: 26
22+
# The exact pin in `devEngines.runtime`, which pnpm also enforces locally.
23+
node-version-file: package.json
2324
cache: pnpm
2425
- run: pnpm install --frozen-lockfile
2526
- run: pnpm format:check
2627
- run: pnpm db:verify
2728
- run: pnpm typecheck
2829
- run: pnpm test:scripts
2930
- run: pnpm test
30-
- run: pnpm --filter @pgstencil/example-workers build
31+
- run: pnpm --filter @pgstencil/example-better-auth build
3132
- run: pnpm packages:verify
3233
- name: Remove this job's containers and volumes
3334
if: always()

‎.github/workflows/release.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ jobs:
120120

121121
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
122122
with:
123-
node-version: 26
123+
node-version-file: package.json
124124
cache: pnpm
125125
registry-url: https://registry.npmjs.org
126126

‎.github/workflows/security-audit.yml‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ jobs:
3535
# on any other branch, or a dispatch from one, never receives the token.
3636
environment:
3737
name: security-audit
38-
# Generous: the audit runs the integration suite against real Postgres
39-
# containers as evidence, then reads the auth package adversarially.
40-
timeout-minutes: 40
38+
# Generous: the audit runs the integration suites against real Postgres
39+
# containers as evidence, then reads the auth and billing packages
40+
# adversarially. Runs took 13 to 23 minutes before billing joined the scope.
41+
# Keep this under the 55 minutes `release.yml` waits for the verdict.
42+
timeout-minutes: 50
4143
steps:
4244
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
4345
with:
@@ -48,7 +50,7 @@ jobs:
4850

4951
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
5052
with:
51-
node-version: 26
53+
node-version-file: package.json
5254
cache: pnpm
5355

5456
- run: pnpm install --frozen-lockfile

‎.nvmrc‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎.prettierignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ pnpm-lock.yaml
44
**/dist/**
55
**/snapshots/**
66
**/db.generated.ts
7-
examples/login/schema.sql
7+
examples/better-auth/schema.sql
88

99
.vscode/

‎AGENTS.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
This codebase is yours: what it's for and how it's built are your call, and you'll keep building on it.
2+
3+
# Working in pgstencil
4+
5+
pgstencil is three npm packages (`packages/pgstencil`, `packages/auth`, `packages/stripe`), one example application (`examples/better-auth`) and the tests that pin them. README.md explains the recipe, PACKAGES.md the public API and release process, BILLING.md billing, WORKERS.md the Workers adapter, SECURITY.md every guarantee and the test that pins it, and CHANGELOG.md what changed.
6+
7+
## Commands
8+
9+
Docker must be running; nothing else needs preparing.
10+
11+
- `pnpm check`: formatting, typecheck, script tests and the whole suite. Run it before calling work done.
12+
- `pnpm test <file>`: one file. The whole suite takes about 20 seconds.
13+
- `pnpm snapshot:update <file>`: the only way snapshots are rewritten. Read the resulting diff.
14+
- `pnpm db:verify`: the committed schema dump and generated types still match the migrations. Rerun `pnpm db:schema` and `pnpm db:types` after touching SQL.
15+
- `pnpm packages:verify`: packs the tarballs and exercises them from an unrelated project. It refuses a modified tree, so commit first.
16+
- `pnpm dev`: the example with a local inbox and Stripe simulator.
17+
- `pnpm db:migration:create <name> --in <dir>`: a new numbered SQL file. Name the package's directory with `--in`; the default is the last source in `pgstencil.json`.
18+
19+
## Rules that are easy to break
20+
21+
- Applied SQL is immutable. Never edit, rename or delete a migration file that has shipped; add a new numbered file. Deployed databases validate every applied file by checksum.
22+
- A change to anything SECURITY.md describes needs its `FAIL IF` rule and pinning test updated in the same change. Tests are cited there by name, so renaming a cited test breaks the audit.
23+
- Time and randomness are injected (`Time`, `RandomSource`), never read from globals, in code that takes them. Better Auth cannot take them, so its tests run a bundle built with `@pgstencil/auth/better-auth-testing`; that module must never reach a production bundle.
24+
- Tooling dependencies (`testcontainers`, `node-pg-migrate`, `cheerio`, `turndown`, `kysely-codegen`) are optional peers loaded dynamically. No runtime entry point may import them statically; `scripts/runtime-deps.test.mjs` enforces it.
25+
- Better Auth is pinned to an exact version because the integration reads its internals. Treat every bump as a change to review, and rerun the auth suites.
26+
- The packages are libraries for other applications. Describe consumers generically in code and docs, and record every breaking change, with how to adapt, under Unreleased in CHANGELOG.md.
27+
- Node is pinned in the root `package.json`'s `devEngines.runtime`; pnpm and CI both read it. Change it there and nowhere else.
28+
- A release is a pull request that raises `version` in all three packages; merging one stages a publish. Do not bump versions for any other reason.

0 commit comments

Comments
 (0)