Skip to content

fix(global-variables): require auth on the /api/global-variables routes - #1024

Open
mmcintosh wants to merge 3 commits into
mainfrom
fix/global-variables-auth
Open

fix(global-variables): require auth on the /api/global-variables routes#1024
mmcintosh wants to merge 3 commits into
mainfrom
fix/global-variables-auth

Conversation

@mmcintosh

@mmcintosh mmcintosh commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

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

  • Auth order (security fix): `requireAuth()` now runs before the plugin-active gate so unauthenticated probes receive `401` rather than `404` — the original order leaked plugin active/inactive state to anonymous callers.
  • Auth (group): `apiRoutes.use('*', requireAuth())` — every API route requires a signed-in user. Reads are only consumed by the authenticated admin UI; server-side rendering resolves variables via the DB directly (`variable-resolver.ts`), not this HTTP endpoint.
  • Admin-only writes + resolve: `requireRole(['admin'])` on `POST`/`PUT`/`DELETE` and `GET /resolve` — `/resolve` returns all variable values which may include sensitive site config (API keys, tokens, etc.); restricting it to admins matches the write surface.
  • Type cleanup (only the routes this touches): the `apiRoutes` sub-app is typed `new Hono<{ 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. The remaining file-wide `any` debt (`adminRoutes` handlers, `variable-resolver.ts`) is left for a follow-up typing-only PR so this security change stays reviewable.

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

  • Added/updated unit tests
  • All unit tests passing

E2E Tests

  • Added/updated E2E tests
  • All E2E tests passing

Screenshots/Videos

N/A — API-only change.

Checklist

  • Code follows project conventions
  • Tests added/updated and passing
  • Type checking passes
  • No console errors or warnings
  • Documentation updated (if needed)

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>
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.

Security: global-variables API allows unauthenticated read + write of site-wide config

2 participants