Skip to content

fix(access)!: a client key reads draft globals with ?draft=true, and #777 made them cacheable - #785

Merged
Ardnived merged 1 commit into
mainfrom
claude/fix-globals-draft-leak
Sep 15, 2026
Merged

Ardnived merged 1 commit into
mainfrom
claude/fix-globals-draft-leak

Conversation

@Ardnived

Copy link
Copy Markdown
Contributor

Summary

  • An ordinary client API key plus ?draft=true read unpublished copy out of all three translations globals, with no preview secret. Reproduced, not inferred.
  • perf(api): cache, usage and purge coverage for /api/globals reads #777 escalated it from a leak to a cache-poisoning one: that same uncredentialed draft read came back public, max-age=600, s-maxage=600, storable at the Cloudflare edge and replayed by Vary: Authorization to every request sharing the API key — for WeMeditateWeb, every visitor.
  • Two independent fixes, because the two layers answer different questions.

The defect

createAccessConfig constrains a client read of a draft-enabled entity to { _status: { equals: 'published' } } unless the request carries a valid preview secret. That clause asked collectionHasDrafts, which resolved the slug through req.payload.collections[...] — a map a global is not in. It answered false for every global, so the clause never fired on one.

All three translations globals declare versions.drafts, so the gate mattered and was absent.

Then #777 made /api/globals/<slug> edge-cacheable. matchCacheableRead keys on the pathname, and middleware.ts gates no-store on the preview-secret header — neither looks at draft. So the draft read was not merely served, it was storable.

Changes

  • src/plugins/access/accessConfigs.ts — collectionHasDrafts → entityHasDrafts, which also checks req.payload.config.globals. This is the access fix.
  • src/plugins/cache/policy.ts — new isDraftRead, and buildCacheHeaders returns private, no-store for a draft read as well as a preview one. A caller may be legitimately authorised for drafts and the response still must not enter a shared cache.
  • src/plugins/cache/middleware.ts, src/plugins/cache/cacheHeaders.ts — both cache surfaces pass it, so they stay byte-identical.
  • DEPLOYMENT.md, docs/rules/access.md — record both, and the recommended Cache Rule hardening.

draft=false stays cacheable, deliberately

WeMeditateWeb passes draft: isPreview unconditionally, so every ordinary read serialises draft=false. Treating the parameter's presence as a draft read would make the whole site uncacheable. '', false and 0 are non-draft; anything else — including an unrecognised value — fails closed.

Test results

  • Lean gate: pnpm lint, pnpm typecheck, pnpm typecheck:tests, pnpm test:unit (2017 → 2023 unit tests) — all green
  • Integration: globals-client-reads, role-based-access, client-query-validation (109 passed); translations-globals, pages-localize-status, atlas-collections (56 passed)

Both halves watched failing first, as docs/rules/testing-reqs.md requires:

× serves published copy to a client asking for draft: true
  → expected 'Unpublished draft countries title' to be 'Published countries title'
× serves published copy over REST with ?draft=true
  → expected '{"countries":{"title":"Unpublished dr…' not to contain …

× returns private, no-store for a draft read with no preview secret
× never caches a ?draft=true read, even with no preview secret

Notes for reviewer

The Cache Rule is "Respect origin", so the app-layer private, no-store is sufficient on its own — this is not waiting on a dashboard change. But DEPLOYMENT.md now recommends adding not http.request.uri.query contains "draft=true" to the rule, mirroring the preview-secret exclusion already there, so the bypass does not rest on a single origin header.

Marked ! because a client sending ?draft=true against a global previously got drafts and now gets published content. That is the fix, not a regression, but it is a behaviour change on a live API.

Found while planning the cross-repo live-preview unification — the work that supersedes #776. It is fixed here rather than there because it is live, independent, and shipping it should not wait.

🤖 Generated with Claude Code

…777 made them cacheable

`createAccessConfig` constrains a client read of a draft-enabled entity to
`{ _status: { equals: 'published' } }` unless the request carries a valid preview
secret. That clause asked `collectionHasDrafts`, which resolved the slug through
`req.payload.collections[...]` — a map a global is not in. So it answered `false`
for every global, the clause never fired on one, and an ordinary client API key
plus `?draft=true` read unpublished copy out of all three translations globals
with no preview secret at all.

#777 raised the stakes. `/api/globals/<slug>` is now edge-cacheable, and
`matchCacheableRead` keys on the pathname while the middleware gates `no-store`
on the preview-secret *header*. So the same uncredentialed draft read came back
`public, max-age=600, s-maxage=600` and could sit at the Cloudflare edge for ten
minutes, replayed by `Vary: Authorization` to every request sharing that API key
— which, for WeMeditateWeb, is every visitor.

Two independent fixes, because the two layers answer different questions:

- `entityHasDrafts` checks `req.payload.config.globals` as well as collections,
  so the published-only clause fires on a global. This is the access fix.
- `isDraftRead` marks any read carrying a truthy `?draft=` as unstorable, whether
  or not a preview secret rides with it. A caller may be legitimately authorised
  for drafts and the response still must not enter a shared cache. Applied on
  both cache surfaces — the REST middleware and the in-handler decorator.

`draft=false`, `draft=0` and `draft=` stay cacheable: WeMeditateWeb passes
`draft: isPreview` unconditionally, so treating the parameter's presence as a
draft read would make the whole site uncacheable. An unrecognised value fails
closed.

Both halves were watched failing first — the integration cases returned the
unpublished string, and the cache cases returned `public, max-age=600`.

DEPLOYMENT.md records the recommended Cache Rule hardening: the rule already
excludes the preview-secret header, and should exclude `draft=true` too, so the
bypass does not rest on an origin header alone.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@railway-app

railway-app Bot commented Sep 15, 2026

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-785 environment in sahajcloud

Service Status Web Updated
SahajCloud ✅ Success (View Logs) Web Sep 15, 2026 at 1:28 am UTC
1 service not affected by this PR
  • mailpit

@Ardnived
Ardnived merged commit e998a16 into main Sep 15, 2026
6 checks passed
@Ardnived
Ardnived deleted the claude/fix-globals-draft-leak branch September 15, 2026 15:10
@sydevs-bot sydevs-bot moved this from Revising to Done in SYDevelopers Technical Roadmap Sep 15, 2026
sydevs-bot pushed a commit that referenced this pull request Sep 15, 2026
One conflict, in docs/rules/access.md, additive on both sides. This branch
rewrote the API-client paragraph for the #723 grants: `user-submissions`
create for `wemeditate-web` and `sahaj-atlas`, and the removal of
`registrations: ['update']`. `main` (#785) appended "and globals" to the
published-only clause and added the warning paragraph explaining why that
clause is load-bearing.

Both are kept: this branch's paragraph carries `main`'s "and globals", and
`main`'s warning follows it. Neither side's rule is dropped.

`main` renamed `collectionHasDrafts` to `entityHasDrafts` in
src/plugins/access/accessConfigs.ts; that merged cleanly and this branch has
no other caller. `main` brought no migration, so the snapshot chain is
untouched.

Gate at this merge: lint, tsc over src/ and tests/, unit lane (2045 tests,
143 files). Targeted specs — globals-client-reads, pages,
user-submissions-access, user-submissions-create: 4 files / 67 tests, green.
Snapshot probe `db:migrations:create --skip-empty` exits 0 and writes no
files.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CRPiPGyw3GbouYVZwkKihG

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-785 — 5de9115e Deployed Sep 15, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

1 participant