Skip to content

fix(api): exempt live-preview reads from select/populate gate - #428

Merged
Ardnived merged 1 commit into
mainfrom
fix/live-preview-select-populate-bypass
May 29, 2026
Merged

Ardnived merged 1 commit into
mainfrom
fix/live-preview-select-populate-bypass

Conversation

@Ardnived

Copy link
Copy Markdown
Contributor

Summary

The Meditations (and Pages / wemeditate-web) live preview broke with a 500 after #294 / #420 introduced the mandatory select + populate query-param gate for API client reads.

Root cause: The admin live-preview iframe loads the external We Meditate Web frontend with the shared SAHAJCLOUD_PREVIEW_SECRET. That frontend fetches the draft document back from this CMS as the wemeditate-web client, forwarding the secret in the x-sahajcloud-preview-secret header to unlock drafts. PR #294's validateClientQueryParamsHook (beforeOperation on every non-system collection) now rejects that read with 400 because a preview fetch wants the whole document and doesn't enumerate select/populate. The frontend then surfaces a 500. Payload's built-in preview machinery is fine — only the new gate is the problem.

Fix: Skip the select/populate validation when a request carries the valid preview secret — the same trust signal that already unlocks drafts in createAccessConfig. A full-document render can't meaningfully enumerate select, and the param requirement is a quota/perf guardrail, not a security boundary; preview traffic is low-volume.

  • New src/lib/previewSecret.ts — hasValidPreviewSecret(req) + PREVIEW_SECRET_HEADER, a single source of truth for the secret check.
  • validateClientQueryParamsHook early-returns for valid-preview-secret reads (rate limiting + usage tracking untouched).
  • accessConfigs.ts de-duped to use the shared helper (was an inline header compare).
  • Docs: hook JSDoc + .claude/rules/api-clients.md.

Fixes meditations, pages, and the wemeditate-web global preview in one place — no external-frontend change required.

Test plan

  • pnpm lint — 0 errors
  • pnpm exec vitest run tests/int/client-query-validation.int.spec.ts --config ./vitest.config.mts — 27 passed (3 new: preview-secret bypasses the gate on find/findByID@Depth=2; a wrong secret is still rejected)
  • Manual: with the live preview pointed at a CMS carrying this change, confirm the meditation preview renders instead of 500ing.

🤖 Generated with Claude Code

The mandatory select/populate gate added in #294 fires for every
clients-collection read, including the draft fetch the admin live preview
makes via the We Meditate Web frontend. A preview renders the whole
document and doesn't enumerate select, so the gate returned 400 and the
preview surfaced a 500.

Skip the gate when a request carries the valid SAHAJCLOUD_PREVIEW_SECRET
header — the same trust signal that already unlocks drafts. Extract the
check into a shared hasValidPreviewSecret() helper and reuse it in both
the usage hook and the access config. Rate limiting and usage tracking
are untouched. Fixes meditations, pages, and wemeditate-web previews.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@Ardnived
Ardnived merged commit 6b35c65 into main May 29, 2026
1 of 2 checks passed
@Ardnived
Ardnived deleted the fix/live-preview-select-populate-bypass branch May 29, 2026 08:45
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