From a6004bb7b16517f05509f09bd6a1c2b9b439156f Mon Sep 17 00:00:00 2001 From: Nathan Rover Date: Tue, 28 Jul 2026 11:25:27 -0400 Subject: [PATCH] fix: pin the applied D1 migration baseline --- .github/workflows/deploy.yml | 24 ++++++++---------------- docs/PROJECT_PLAN.md | 10 ++++++---- worker/README.md | 11 +++++++---- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 37b9086..1a50251 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -29,6 +29,14 @@ jobs: - run: npm run check - run: npm test - run: npm run build + # Migration 0001 is already applied remotely. Keep deployment fail-closed + # until a separately least-privileged D1 token exists for future changes. + - name: Verify production D1 migration baseline + shell: bash + run: | + test "$(find worker/migrations -maxdepth 1 -type f -name '*.sql' | wc -l)" -eq 1 + echo "3f820cb7c1f8e76196e83f8bc8448dd9e72e840884fc39641c84e6b9d60ff27f worker/migrations/0001_pegma_storage.sql" | + sha256sum --check --strict - name: Deploy to Cloudflare Pages env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} @@ -37,22 +45,6 @@ jobs: npx wrangler pages deploy dist --project-name pegma-dev --branch main - # Same triggers as production Pages (push/schedule/dispatch on main). - # Token must include Workers Edit and D1 Edit in addition to Pages Edit — see - # docs/PROJECT_PLAN.md Phase 2. - - name: Apply pegma-dev-identity D1 migrations - if: > - github.ref == 'refs/heads/main' && - (github.event_name == 'push' || - github.event_name == 'schedule' || - github.event_name == 'workflow_dispatch') - env: - CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} - CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - run: > - npx wrangler d1 migrations apply IDENTITY_DB - --remote - -c worker/wrangler.jsonc - name: Deploy pegma-dev-api Worker if: > github.ref == 'refs/heads/main' && diff --git a/docs/PROJECT_PLAN.md b/docs/PROJECT_PLAN.md index c670b23..50f247c 100644 --- a/docs/PROJECT_PLAN.md +++ b/docs/PROJECT_PLAN.md @@ -167,12 +167,14 @@ Actions deploy (SHA-pinned, per ecosystem standard). Exit: push-to-main publishes; the domain serves the site with an A grade on the obvious security headers. **Done:** project `pegma-dev`, deploys via plain `npx wrangler` in the SHA-pinned workflow (the CI token is scoped to -Pages:Edit, Workers:Edit, and D1:Edit — deliberately no DNS scope; the custom +Pages:Edit and Workers:Edit — deliberately no D1 or DNS scope; the custom domain was attached in the dashboard); security headers served from `public/_headers`, CSP with same-origin scripts only; `www` 301s to the apex. -Pull requests run the build gate but never receive that production token; -branch previews remain disabled until a separately scoped preview credential -exists. +The initial D1 migration was operator-applied and production CI pins its exact +hash; a future schema change remains fail-closed until it has a separate +least-privileged D1 credential. Pull requests run the build gate but never +receive the production token; branch previews remain disabled until a +separately scoped preview credential exists. ### Phase 3 — the compiled roadmap ✓ (2026-07-27) diff --git a/worker/README.md b/worker/README.md index e66f056..ea0413d 100644 --- a/worker/README.md +++ b/worker/README.md @@ -33,7 +33,8 @@ npx wrangler secret put DATADOG_SITE -c worker/wrangler.jsonc ``` CI deploys this Worker with the same `CLOUDFLARE_API_TOKEN` as Pages; that -token needs **Workers:Edit** and **D1:Edit** in addition to **Pages:Edit**. +token needs **Workers:Edit** in addition to **Pages:Edit**. It intentionally +does not receive D1 authority. ## Identity composition @@ -82,9 +83,11 @@ and must remain stable while any email-code or Mail job is live. Verify the The D1 schema is deployment-managed by `migrations/0001_pegma_storage.sql`. The adapter runs with `createSchemaIfMissing: false`, so request traffic never performs DDL. -Production CI applies pending migrations before activating a new Worker -version. The versioned route is limited to `pegma.dev/api/*`; Cloudflare Pages -continues to serve every static path. +Migration 0001 was applied to production before Worker activation. Production +CI verifies that exact frozen baseline byte-for-byte; any future migration +fails that gate until a separately least-privileged D1 deployment credential +and migration step are reviewed. The versioned route is limited to +`pegma.dev/api/*`; Cloudflare Pages continues to serve every static path. Passkey removal stays disabled until an email-code recovery implementation is live. This deliberately avoids a last-factor lockout in the passkey-only