Skip to content

feat(auth): per-credential quota on bearer routes (#572) - #704

Merged
jimhoyd merged 1 commit into
mainfrom
feat/auth-credential-quota
Sep 25, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
feat/auth-credential-quota

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

Fixes #572

This follows the owner's decision on #572: per-credential limits live in the auth add-on, and core throttle is unchanged.

What changed

Bearer routes accept a budget per API key:

/api/items:
  function: functions/items.mjs
  auth: {bearer: {scopes: [items.read], quota: {requests: 1000, window: 3600}}}
  policies:
    throttle: {quota: 60, window: 60}   # per client, before auth: unauthenticated floods
  • Units. requests (1 to 1,000,000) per window seconds (1 to 2,592,000). These are core throttle's quota/window units.
  • What is counted. The gate counts a request only after the key has authenticated and covers the route's scopes, so 401 and 403 keep their meaning. Order is 401, then 403, then 429.
  • The refusal. It happens in authorize(), before the handler. The response is a 429 with body {"error":"credential_quota_exceeded"}, Cache-Control: no-store, Retry-After, RateLimit-Policy: "credential";q=..;w=.. and RateLimit: "credential";r=0;t=... The header format mirrors core throttle, with the policy named credential. The principal header is not written on a refusal.
  • Counter. AuthService.consumeApiKeyQuota backs the check, with a new apiKeyQuota store operation in api-key-store.ts. It follows the sign-in attempt counter:
    • The table is auth_attempts, keyed by a SHA-256 of the key id plus the budget, never the secret.
    • Fixed window from the first counted request. Refused requests are not counted.
    • Expired rows are swept on write and by cleanup. The 100,000-row ceiling applies.
  • Shared budgets. Routes that restate the same budget share one counter per key. A route with a different budget gets its own, which is how throttle keys its counters too.
  • Durability. Every store operation runs in BEGIN IMMEDIATE, so the count survives a restart and is shared by processes on one host. It is not shared across hosts.
  • Store unavailable. It fails closed, the same way the attempt counter and the key lookup do. An AuthError 503 (auth_store_unavailable or auth_capacity_reached) propagates, and the request is never let through uncounted.
  • Core. The only core change is the route short-form mirror (RouteAuthConfig in types.ts and schemas/urlcode.schema.json). PR feat(auth): bearer/API-key authentication #635 set this precedent: without it the auth: short form would refuse quota. Core does not import any package, and runtime and throttle logic are untouched.

Docs

  • packages/auth: README ("Per-credential quota"), CHANGELOG (Unreleased), IMPLEMENTATION-STATUS, SECURITY and llms.txt.
  • docs/: EXTENSIONS (bearer routes), policies/throttle.md (how throttle and the quota pair up, replacing "per-token is a plugin"), and AI-AUTHORING.
  • Regenerated: YAML-REFERENCE, llms-full.txt and packages/auth/urlcode.json (build:addons).

Evidence

  • npm run typecheck: pass.
  • npm run verify: pass. Root suite 798/798, plus each workspace suite. check:docs includes reference, llms and agent-asset checks.
  • packages/auth npm run verify: pass, 223/223, including 3 new tests:
    • auth-core.test.ts: fixed window, refusals not counted, separate credentials and budgets, restart on the same SQLite file, and a second OS process (execFile of node against the same file) seeing and adding to the count. It also checks window reset, that counter rows are hashes containing neither the id nor the secret, and that out-of-bounds quotas are refused.
    • auth-handler.test.ts, gate: under quota passes, over quota gives the 429 with the exact headers, 403 requests are not counted, the second key is independent from the same client address, the route budget is separate, and the window resets.
    • auth-handler.test.ts, config: validateProject refuses bad values at load time.
  • npm run test:package: pass.

Limitations (filed)

🤖 Generated with Claude Code

Add `auth: {bearer: {scopes, quota: {requests, window}}}`. Each API key
gets `requests` per `window` seconds, in the units core throttle uses.
The auth extension's authorize() gate counts a request only after the
key authenticates and covers the route's scopes. The request over budget
gets a 429 before the handler runs, with Retry-After and the IETF
RateLimit-Policy/RateLimit fields under the policy name "credential".

- AuthService.consumeApiKeyQuota counts by a SHA-256 of the key id and
  the budget, never the secret. It uses the sign-in attempt counter's
  auth_attempts table and fixed window: refusals are not counted,
  expired rows are swept, and the 100,000-row ceiling applies. The count
  survives a restart and is shared by processes on one host, which
  serialize on the SQLite file. It is not shared across hosts. A store
  failure is a 503; the request is never let through uncounted.
- Core is unchanged apart from the route short-form mirror
  (RouteAuthConfig and schemas/urlcode.schema.json), as with #635.
  Core throttle still counts per client before auth and remains the
  guard against unauthenticated floods.
- Tests cover: under and over quota, Retry-After and RateLimit fields,
  separate credentials and budgets, 403 not counted, window reset,
  restart on the same file, a second OS process sharing the count, and
  load-time refusal of bad values.

Follow-ups filed: #701 (throttle replaces RateLimit fields on an
extension 429), #702 (auth short-form errors report "must be true"),
#703 (per-key budget override, RateLimit fields on allowed responses).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd merged commit 9afd1e2 into main Sep 25, 2026
23 checks passed
@jimhoyd
jimhoyd deleted the feat/auth-credential-quota branch September 25, 2026 00:26
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.

[Request]: throttle partition keyed by credential, with a shared or durable counter store

1 participant