Repository navigation
Open
Labels
Milestone
Description
Problem
A project API key is all-or-nothing today. Any sk_ key can send transactional email, launch a broadcast to the whole list, import subscribers and read stats. That violates least privilege in two ways that matter:
- Blast radius. The key embedded in a marketing site's backend for a newsletter signup form only needs to add subscribers — but if that site is compromised, the attacker can broadcast to every subscriber and exfiltrate the list. Customers running several clients per install (the agency case) feel this most: one leaked key is one client's whole audience.
- The API surface is capped. Because keys carry no permissions, every endpoint gated by a role capability (templates, segments, webhooks, campaigns, subscriber CRUD, suppressions) is cookie-only. That is why
@senddock/sdkcan only cover five endpoints (authentication → endpoints that accept API keys). Scoped keys are the mechanism that lets those endpoints open to API keys without handing out full project access.
Proposal
Give API keys an explicit set of scopes, enforced through the capability system the role model already uses.
Scopes
Scopes reuse the existing capability names so one authorization path serves both cookie sessions and API keys:
| Scope | Unlocks |
|---|---|
send |
POST /send, POST /send/batch |
broadcast |
POST /broadcast |
subscribers:read |
list/get subscribers, tags, custom field definitions |
subscribers:write |
POST /subscribers/import, create/update/delete, bulk, tags |
stats:read |
GET /stats |
templates:read / templates:write |
templates CRUD |
segments:read / segments:write |
segments CRUD + preview |
webhooks:read / webhooks:write |
webhooks CRUD + deliveries |
campaigns:read / campaigns:write |
campaigns CRUD |
suppressions:read / suppressions:write |
suppressions list / add / remove |
The first five cover today's API-key surface; the rest open currently cookie-only endpoints to keys that explicitly hold the scope.
Enforcement
api_keysgains ascopescolumn (text[]). The API-key middleware loads the key's scopes into the request context.requireCapevaluates them exactly as it evaluates role capabilities for cookie sessions. No second authorization mechanism.- A key calling an endpoint outside its scopes gets
403 {"error": "this key lacks the <scope> scope"}— distinct from the401for an invalid key, so SDK users can tell misconfiguration from bad credentials.
Dashboard
- Create-key dialog shows scope checkboxes with presets: Sending only (
send), Newsletter signup (subscribers:write), Read-only (all*:read+stats:read), Full access. The suggested default is the minimum preset; widening is a deliberate click. - The key list shows each key's scopes. Scope changes are audited (
api_key.scopes_updated).
Compatibility
- Existing keys are migrated with the scopes equal to what they can do today (
send,broadcast,subscribers:write,stats:read) — nothing breaks on upgrade. - No wire change for clients.
@senddock/sdkneeds no code change; a scope failure surfaces as a typedSendDockErrorwith the server's message.
Acceptance
- Migration adds
scopestoapi_keysand backfills existing keys with the current-equivalent set - API-key middleware resolves scopes;
requireCaphonors them for key-authenticated requests - Every currently cookie-only project endpoint in the table above accepts an API key holding the matching scope
-
403with an explicitlacks the <scope> scopemessage on scope mismatch;401unchanged for invalid keys - Dashboard: scope selection on create with presets, scopes visible in the key list, audit entry on change
- Docs: authentication page documents scopes and presets; each endpoint page states its required scope; the SDK page lists the newly reachable methods
- Tests: middleware scope resolution,
requireCapfor keys, migration backfill, at least one newly opened endpoint per resource
Activity