Skip to content

Scoped API keys: least-privilege permissions per key #96

Open

Description

@JuansesDev

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/sdk can 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_keys gains a scopes column (text[]). The API-key middleware loads the key's scopes into the request context.
  • requireCap evaluates 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 the 401 for 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/sdk needs no code change; a scope failure surfaces as a typed SendDockError with the server's message.

Acceptance

  • Migration adds scopes to api_keys and backfills existing keys with the current-equivalent set
  • API-key middleware resolves scopes; requireCap honors them for key-authenticated requests
  • Every currently cookie-only project endpoint in the table above accepts an API key holding the matching scope
  • 403 with an explicit lacks the <scope> scope message on scope mismatch; 401 unchanged 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, requireCap for keys, migration backfill, at least one newly opened endpoint per resource

Activity

  1. added this to the 0.9 milestone on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backenddxenhancementsecurity

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions