Skip to content

fix(service-bridge): per-principal rate buckets with a global backstop (wiki-v2 issue 780) - #83

Merged
PMQ9 merged 3 commits into
wiki-v2from
fix/780-bridge-rate-principal
Nov 9, 2026
Merged

PMQ9 merged 3 commits into
wiki-v2from
fix/780-bridge-rate-principal

Conversation

@PMQ9

@PMQ9 PMQ9 commented Nov 8, 2026 •

Copy link
Copy Markdown

What

Per-principal rate buckets for the service bridge, with a global backstop per scope. This fixes https://github.com/cccvu/wiki-v2/issues/780. It is paired with the super PR https://github.com/cccvu/wiki-v2/pull/787.

  • service-rate-limit.ts: token buckets replace the fixed 60 s window. Refill is continuous. A debit across several keys is all-or-nothing, so a refused call drains nothing. Full buckets are pruned when a call comes in; there are no timers.
  • service-auth.guard.ts: after the secret and scope checks, the guard reads x-ccc-rate-principal (user:<uuid> or service:<uuid>). It is only a rate key, never identity. A malformed value gets a fixed 400 that never echoes the value.
    • An attributed call debits its own scope:principal bucket and that scope's global backstop.
    • An unattributed (background) call debits only its own bucket, so attributed load can never starve it.
    • Sizes per principal / global / unattributed, each overridable by env: pages:read and pages:authz:read 6000/60000/6000, pages:import:read 120/1200/120, every other scope 1200/6000/600. Bad-secret calls share one anon bucket of 600.
  • A refusal is 429 {code: "rate_limited"} + Retry-After. The anon bucket answers service_auth_throttled instead, so a wrong secret is never reported as the caller's own throttle. The log line is SERVICE_BRIDGE_RATE_LIMITED scope=… bucket=principal|global|unattributed|anon, and it never includes the principal.
  • service-bridge.openapi.json 1.15.0 adds the RatePrincipal header parameter and RateLimitedError, and puts Retry-After on every 429. The README is updated to match.

Why

The old limiter was one fixed window per scope, shared by every platform caller. A single principal could use up pages:read or pages:import:read and lock every other caller out of /v1 page routes and imports until the window reset.

Scope

Only apps/server/src/service-bridge/** and docs/integrations/authorization/** change, both CCC-owned. There is no upstream seam, and check-upstream-boundary.sh passes.

Tests

  • Fork jest, CI FORK_SPEC_ROOTS lane (authz, service-bridge, editor-compat, prosemirror, import): 216 suites, 3516 passed. The pg specs skipped themselves because no Postgres was running.
  • After the review fixes, src/service-bridge again: 44 suites, 508 passed.
  • tsc --noEmit and the super repo's docmost-server-typecheck.mjs (forward and reversed) are clean.
  • The guard specs cover both pages:read and pages:import:read:
    • an exhausted principal never blocks another;
    • a flood of the A1 shape;
    • a saturated global degrades to proportional 429s that recover as the buckets refill;
    • the unattributed bucket is isolated;
    • with a bad secret the header is ignored, and a malformed header gets a 400;
    • buckets are separate per scope;
    • a throttled principal never drains the global bucket.
  • Mutation proofs: each of M6 (no all-or-nothing debit), M12 (principal key without the scope), M13 (global key without the scope) and the anon-code mutant fails a spec.

Merge this first, with a merge commit. The super PR (https://github.com/cccvu/wiki-v2/pull/787) then re-pins its gitlink to the merge SHA.

🤖 Generated with Claude Code

PMQ9 and others added 3 commits November 8, 2026 14:57
docmost#780)

The bridge limiter was one fixed 60 s window per scope, shared by every
platform caller, so one principal could lock all /v1 page routes and
imports out until the window reset.

- Token buckets (continuous refill, all-or-nothing debit, pruned) replace
  the fixed window.
- The platform names the acting principal in x-ccc-rate-principal (a rate
  key only, parsed after the secret check). Attributed calls debit their
  own bucket plus a per-scope global backstop; unattributed background
  calls keep their own bucket and never touch the global one.
- A refusal is 429 {code: rate_limited} with Retry-After; the bad-secret
  bucket answers service_auth_throttled. SERVICE_BRIDGE_RATE_LIMITED logs
  scope and bucket kind, never the principal.
- service-bridge.openapi.json 1.15.0: RatePrincipal header, RateLimitedError,
  Retry-After on every 429.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vwm9j5xBxNaA7WUcUdQroX
@PMQ9
PMQ9 merged commit 06e7bfd into wiki-v2 Nov 9, 2026
@PMQ9
PMQ9 deleted the fix/780-bridge-rate-principal branch November 9, 2026 03:07
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