fix(global-variables): require auth on the /api/global-variables routes - #1024
Open
mmcintosh wants to merge 3 commits into
Open
fix(global-variables): require auth on the /api/global-variables routes#1024mmcintosh wants to merge 3 commits into
mmcintosh wants to merge 3 commits into
Conversation
The public /api/global-variables routes performed no authentication — only a plugin-active check. Any unauthenticated caller could read every global variable and, via POST/PUT/DELETE, create/update/delete site-wide config. (The admin UI at /admin/global-variables was already protected by the global /admin/* requireAuth + requireRbac gate; only this /api/* surface was exposed.) - Require a signed-in user on the whole apiRoutes group — reads are only consumed by the authenticated admin UI; server-side rendering resolves variables via the DB directly, not this HTTP endpoint. - Require an admin (requireRole(['admin'])) on the POST/PUT/DELETE mutations — global variables are site-wide config. Type cleanup on the routes this touches (Split scope — adminRoutes and variable-resolver.ts remain for a follow-up typing PR): the apiRoutes sub-app is typed with Bindings/Variables (removing every `c: any`), a GlobalVariableRow interface replaces the `row: any` / `first() as any` casts, and the shared formatVariable/getVariablesMap helpers are typed. apiRoutes region: 0 `any`, 0 tsc errors.
Move requireAuth() before the plugin-active middleware so unauthenticated callers receive 401 instead of leaking plugin active/inactive state via 404. Add requireRole(['admin']) to GET /resolve — it returns all variable values which may include sensitive site config; restrict to admins to match writes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Three distinct CI failures, all from stale module-level caches: 1. document-scalar-schema: _columnCache/_indexCache persisted across createTestD1() calls. Each fresh in-memory DB was missing q_* generated columns because ensureScalarSchema saw them as already existing. Fix: export resetScalarSchemaCaches(); call it in createTestD1() and in migrations-d45.test.ts beforeEach (which uses its own raw adapter, not createTestD1). 2. plugin-middleware: _pluginStatusCache cached 'my-plugin' as true from the first isPluginActive test, causing all subsequent tests for the same pluginId to return the cached value instead of hitting the mock DB. Fix: call invalidatePluginStatusCache() in every beforeEach in plugin-middleware.test.ts. 3. api-collection-cache: c.executionCtx throws in Vitest/Node.js (no Cloudflare ExecutionContext). scheduleKvWrite already no-ops when globalKv is unset, but the throw happened before the guard. Fix: accept null|undefined in scheduleKvWrite; guard c.executionCtx access with try/catch at all 9 call sites in api.ts. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The public `/api/global-variables` routes performed no authentication — only a plugin-active check. Any unauthenticated caller could read every global variable and, via `POST`/`PUT`/`DELETE`, create/update/delete site-wide config. The admin UI at `/admin/global-variables` was already protected by the global `/admin/` gate; only this `/api/` surface was exposed.
Fixes #1023
Changes
One file, +58/−31.
Testing
Type-checked against the repo toolchain (`tsc --noEmit` — 0 errors for the file, no new errors project-wide). No unit/E2E tests added yet — opened as a draft; happy to add a route test asserting `401` (unauthenticated) / `403` (non-admin write or resolve) / `200`/`201` (admin) if preferred.
Unit Tests
E2E Tests
Screenshots/Videos
N/A — API-only change.
Checklist