Skip to content

Let agents create and revoke their own API keys - #64

Merged
maaland merged 2 commits into
mainfrom
feat/keys-api-36
Oct 12, 2026
Merged

maaland merged 2 commits into
mainfrom
feat/keys-api-36

Conversation

@maaland

@maaland maaland commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

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":

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

🤖 Generated with Claude Code

maaland and others added 2 commits October 12, 2026 20:24
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>
POST /api/keys returns a new key once, with its daily limit and how to
use and revoke it; a fourth key from one IP in a UTC day gets 429 with
Retry-After. DELETE /api/keys/current revokes the key it's sent with.
Requests made with a key are logged against it, never with the key
itself. IPs are hashed with STACKPROBE_IP_HASH_SECRET, required in
production.

Closes #36

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@drippa-ai

drippa-ai Bot commented Oct 12, 2026

Copy link
Copy Markdown

Drippa Sign-Off: Self-serve API key creation and revocation for agents

✅ Delivers ticket #36 — 5 of 5 outcomes

Open in Drippa

What was promised

  • ✅ Self-serve API keys #36 An agent can create an API key with no sign-up and gets it back at once, with its daily limit of 10 new scans.
  • ✅ Self-serve API keys #36 A fourth key from the same address in one day is refused with a clear message and the time it resets.
  • ✅ Self-serve API keys #36 The holder can revoke a key by sending it, and a revoked key stops working.
  • ✅ Self-serve API keys #36 The key itself is never shown again after creation, and caller addresses are kept only as a keyed hash.
  • ✅ Self-serve API keys #36 Each request made with a key is recorded against the key for usage counts, without storing the key.

Not in this change: Spending the daily scan allowance from the scan API is left to #37.

Proof
Not tested in the browser: behaviour change with no page to click.

Steps based on ticket #36, the pull request description and the code change.

Details

Agents can now get an API key by calling the key creation address, with no sign-up. The key is returned once, with a 10 new scans a day limit. They can revoke it by sending the key back. One address can create at most 3 keys a day; a fourth gets a clear limit message with the reset time. Caller addresses are only kept as a keyed hash, and responses are never cached.

Test coverage: ✅ Well covered — Tests cover creating a key, the 3-a-day refusal with retry time, revoking, revoked and bad keys refused, usage recorded without the key, and reading the caller address.

🔍 Checked: 2 tickets (#36 delivered, #37 cited only), 6 of 6 changed files read, plus the shared key and allowance code and its migration elsewhere in the repository


Updated by Drippa • 2026-10-12 18:28 UTC

Comment @drippa-ai review for a fresh review.

@drippa-ai drippa-ai Bot mentioned this pull request Oct 12, 2026
Base automatically changed from feat/access-core-36 to main October 12, 2026 19:04
@vercel

vercel Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
stackprobe Ready Ready Preview Oct 12, 2026 7:09pm UTC

Request Review

@maaland
maaland merged commit 0bca04e into main Oct 12, 2026
4 checks passed
@maaland
maaland deleted the feat/keys-api-36 branch October 12, 2026 19:09
maaland added a commit that referenced this pull request Oct 12, 2026
Closes #41

Covers both "Done when" lines of #41: the limits and the bot check are
tested (the bot check is behind a `BotCheck` interface, faked in tests),
and reading reports is never limited.

## What changes
- **`requestScan` allowance hook (core):** `allowance.check()` runs just
before a new scan is created and can refuse it; `allowance.spend()` runs
once the scan exists. Returning a running or fresh scan touches neither.
- **`lib/start-scan.ts`:** the website's scan logic, with the server
action now a thin wrapper.
1. **Bot check** first. A bot gets "We couldn't confirm this is a
browser. Reload the page and try again." and nothing is created.
2. **Visitor allowance:** 3 new scans a day per visitor (`visitor:<hmac
of IP>`, using `STACKPROBE_IP_HASH_SECRET` from #64).
3. **At the limit:** "You've started 3 new scans today, the daily limit
without a key. Scans that already exist are still free to open. You can
start more at midnight UTC, in N hours."
- **"Scan again"** is now a small client component on the same action,
so the limit message shows under the buttons instead of an error page.
Form messages use `--ink-2`: a limit isn't a failure.
- **Vercel BotID** (`botid`, MIT):
- `withBotId` in `next.config.ts`; `initBotId` in
`instrumentation-client.ts` protects POSTs, which is how scan forms
submit.
- `checkBotId()` runs in the action only on **Vercel production**
(`VERCEL_ENV=production`). Previews skip it: they're behind Vercel's
login already, and Drippa's review drives a headless browser there that
BotID would refuse.
  - Self-hosted setups skip it too, since there's no BotID service.

## Checked
- **Unit tests:**
  - 3 new scans, then the message, with nothing started.
  - Existing scans are free at the limit.
- Scan again on a finished scan counts; while the scan is still running
it doesn't.
  - Visitors are counted separately.
  - Bots are refused before anything is created.
  - Bad input spends nothing.
  - The message wording.
- **Core:** the allowance is checked and spent only for new scans; a
refusal creates nothing.
- **Production build in a real browser (throwaway database):**
- Three new scans from home open their reports; the fourth stays on home
with the message.
  - Scan again at the limit shows the message under the buttons.
  - Opening an existing report still works.
  - Screenshots at 1280 px and 390 px.
- `pnpm lint`, `pnpm typecheck`, `pnpm test` (385 passing), `pnpm build`
pass.

## Notes
- BotID's basic check is included on Vercel; nothing to turn on. It
needs `NEXT_PUBLIC_VERCEL_ENV`, which Vercel exposes by default
("Automatically expose System Environment Variables").
- The bot check itself only runs on production, which is still behind
Vercel's login, so it gets exercised for real once we open the site.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

This branch was successfully deployed

1 active deployment
Preview — 4a112204 Deployed Oct 12, 2026 by vercel[bot]
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.

Self-serve API keys

1 participant