Repository navigation
Add API keys and a daily new-scan allowance to core - #63
Conversation
One allowance for every caller: a subject is an API key or an anonymous visitor (a keyed hash of their IP, never the IP). New scans count against a daily limit that resets at UTC midnight; reused scans and reads don't. Keys are sp_ plus 32 random bytes, stored only as a SHA-256 with their first characters kept to tell them apart, at most 3 new keys a day per IP. Usage older than 30 days is dropped when keys are made. AccessStore sits beside Store: in memory for tests, Postgres in store-postgres with migration 0003 (row level security on, no policies). The web endpoints come next. Refs #36 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drippa Sign-Off: Add API keys and a shared daily new-scan allowance to core and PostgresWhat was promised
Not in this change: The public addresses for creating and revoking a key, which return the "too many requests" error, plus actually logging each keyed request; the description says these come next. Proof DetailsThis adds the core logic for self-serve API keys. Keys are generated, stored only as a hash with a short prefix kept, capped at 3 new keys per IP per day, and can be revoked. It also adds one daily new-scan allowance for both API keys and anonymous visitors, which resets at UTC midnight and does not charge for reused scans, plus the two database tables with row level security on. The public addresses for creating and revoking keys come in a later pull request, and the scan API and website gating will be the first to use the allowance. Test coverage: ✅ Well covered — Tests cover key format and hashing, revocation, the 3-keys-per-IP cap and its reset, the 30-day cleanup, allowance counting and visitor hashing, on both in-memory and database storage. 🔍 Checked:
Updated by Drippa • 2026-10-12 18:25 UTC Comment |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Closes #36 Stacked on #63 (keys and allowance in core and Postgres); merge that first and this retargets to `main`. Covers #36's "Done when": - Creating, revoking and the key-creation limit are tested at the endpoint, and using and exceeding the daily scan allowance at the allowance level (#63). The first endpoint that spends it is #37. - Keys never appear in logs or responses after creation. - Migration with row level security is in #63. ## What changes - **`POST /api/keys`** → `201 { key, prefix, limits: { scansPerDay: 10 }, note }`. The note says to store the key now, send it as `Authorization: Bearer <key>`, and revoke it with `DELETE /api/keys/current`. A fourth key from one IP in a UTC day → `429 { error: "rate_limited", message, resetAt }` with `Retry-After`. - **`DELETE /api/keys/current`** with the key → `200 { revoked: "<prefix>" }`. Missing, malformed, unknown or revoked → `401 { error: "invalid_key", message }` with `WWW-Authenticate: Bearer`. - **Usage:** every request made with a key is logged against `key:<id>`, never with the key. - **Headers:** responses are `Cache-Control: no-store` and `Access-Control-Allow-Origin: *`. - **Caller IP:** the first `x-forwarded-for` entry (Vercel sets it), then `x-real-ip`. It's hashed with **`STACKPROBE_IP_HASH_SECRET`** and never stored as is. Production refuses to run without the secret; development uses a fixed stand-in. Added to `.env.example`. - `services.ts` shares one Postgres connection between the scan store and the new `getAccess()`. ## Checked - Unit tests for create, the 3-a-day limit with `Retry-After`, revoke, then the revoked key failing, bad `Authorization` headers, usage logged without the key, and `clientIp`. - Production build against a throwaway database with the 0003 migration: three `POST /api/keys` → 201, the fourth → 429 with reset at the next UTC midnight; `DELETE` → 200, again → 401. No key appears in the server log. - `pnpm lint`, `pnpm typecheck`, `pnpm test` pass. ## Before deploying - Set `STACKPROBE_IP_HASH_SECRET` on Vercel (production and preview): `openssl rand -base64 32`. - Run `pnpm db:migrate` against Supabase (migration 0003 from #63). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Refs #36
The first of two PRs for #36: the keys and the allowance, in core and Postgres. The next PR adds
POST /api/keysandDELETE /api/keys/currentand closes #36. #41 (website gating) and #37 (HTTP scan API) spend the allowance.What changes
packages/core/src/access.ts(no Vercel, no Node-only APIs: Web Crypto only):key:<id>for API keys andvisitor:<hmac of IP>for anonymous website visitors. The IP is hashed with a secret (an env var, wired in Website gating: anonymous allowance and bot check #41), never stored.createApiKey(ipHash): a key issp_plus 32 random bytes in base64url. Stored only as a SHA-256, with its first 10 characters kept to tell keys apart. At most 3 new keys a day per IP, otherwise aLimitErrorwithresetAt. Each new key also drops usage older than 30 days.findApiKey(key): returns the live key, ornullif it's unknown, malformed or revoked.allowance/assertAllowance(subject, limit): new scans used today, with the day resetting at UTC midnight. ThrowsLimitError: "Daily limit of N new scans reached. Recent scans are still free to read. Resets at …".KEY_SCANS_PER_DAY = 10,VISITOR_SCANS_PER_DAY = 3,KEYS_PER_IP_PER_DAY = 3,USAGE_KEPT_DAYS = 30.AccessStoreinterface andMemoryAccessStore, with a shared contract suite incore/test/access-contract.ts.packages/store-postgres:0003_access.sql:api_keys(hash unique, prefix, ip_hash, limits, created/revoked) andusage(subject, endpoint, new_scan, at), indexed for the counts. Row level security is on with no policies, like the existing tables.PostgresAccessStorepasses the same contract suite on PGlite.connectPostgres()now also returnsaccess.SQLite isn't touched: it only backs the local CLI and MCP, which are never gated.
Checked
pnpm lint,pnpm typecheck,pnpm test(369 passing).After merge
Run
pnpm db:migrateagainst Supabase to apply 0003.🤖 Generated with Claude Code