Repository navigation
feat(auth): per-credential quota on bearer routes (#572) - #704
Merged
Merged
Conversation
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]>
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.
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:
requests(1 to 1,000,000) perwindowseconds (1 to 2,592,000). These are core throttle'squota/windowunits.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=..andRateLimit: "credential";r=0;t=... The header format mirrors core throttle, with the policy namedcredential. The principal header is not written on a refusal.AuthService.consumeApiKeyQuotabacks the check, with a newapiKeyQuotastore operation inapi-key-store.ts. It follows the sign-in attempt counter:auth_attempts, keyed by a SHA-256 of the key id plus the budget, never the secret.cleanup. The 100,000-row ceiling applies.BEGIN IMMEDIATE, so the count survives a restart and is shared by processes on one host. It is not shared across hosts.AuthError503 (auth_store_unavailableorauth_capacity_reached) propagates, and the request is never let through uncounted.RouteAuthConfigintypes.tsandschemas/urlcode.schema.json). PR feat(auth): bearer/API-key authentication #635 set this precedent: without it theauth:short form would refusequota. 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.llms-full.txtandpackages/auth/urlcode.json(build:addons).Evidence
npm run typecheck: pass.npm run verify: pass. Root suite 798/798, plus each workspace suite.check:docsincludes reference, llms and agent-asset checks.packages/authnpm 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 (execFileof 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:validateProjectrefuses bad values at load time.npm run test:package: pass.Limitations (filed)
RateLimit/RateLimit-Policyfields on the credential 429 with its owndefaultpolicy.Retry-Aftersurvives. I found this by reading the code; it is not reproduced end to end.authshort form reportsroute /api/items, auth (const): must be true(pointer/routes/~1api~1items/auth), not the nested quota field. This is pre-existing core error selection over thetrue | objectunion and affects everyauthkey. The test asserts the route- andauth-named pointer.authorize()hook can only refuse.test/capability-query.test.ts, which is why thequotanode has no schema description.🤖 Generated with Claude Code