Files
harukaandClaude Opus 5.5 575dd9bc2b fix(backend): make MCP client registration stateless
Registration was capped at 1000 pending clients site-wide while the limit
per IP was 10 per minute, so one address could exhaust the cap and block
every new MCP client.

- Issue client IDs as HMAC-signed tokens carrying the client name,
  redirect URI digests and grant types. Registration writes nothing;
  rows are created only when a user approves a client, so there is no
  shared capacity to exhaust.
- Persist the signing key in security_secrets via the existing
  get-or-create helper, cached lock-free, so instances share it.
- Drop mcp_oauth_clients from migration 249 and keep the client name on
  each grant. Prune expired grants on authorization, before the owner
  row is locked.
- Fix an unchecked type assertion in managementmcp tests flagged by
  golangci-lint v2.14.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 14:46:08 -04:00

45 lines
2.0 KiB
SQL

-- OAuth clients use public-client authorization code + S256 PKCE. No client
-- secrets, authorization codes, access tokens, or refresh tokens are stored raw.
-- Client registration writes nothing: the client ID is signed and carries its
-- own metadata. Rows are created only when a user approves a client, and each
-- grant keeps the client name shown at consent.
CREATE TABLE IF NOT EXISTS mcp_oauth_grants (
id VARCHAR(43) PRIMARY KEY,
client_id TEXT NOT NULL,
client_name VARCHAR(100) NOT NULL,
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token_version BIGINT NOT NULL,
resource TEXT NOT NULL,
scope VARCHAR(16) NOT NULL CHECK (scope IN ('user', 'admin')),
read_only BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMPTZ NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
last_used_at TIMESTAMPTZ,
revoked_at TIMESTAMPTZ
);
CREATE INDEX IF NOT EXISTS idx_mcp_oauth_grants_user_created ON mcp_oauth_grants(user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_mcp_oauth_grants_expiry ON mcp_oauth_grants(expires_at);
CREATE TABLE IF NOT EXISTS mcp_oauth_codes (
code_hash VARCHAR(64) PRIMARY KEY,
grant_id VARCHAR(43) NOT NULL REFERENCES mcp_oauth_grants(id) ON DELETE CASCADE,
client_id TEXT NOT NULL,
redirect_uri TEXT NOT NULL,
resource TEXT NOT NULL,
code_challenge VARCHAR(43) NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
consumed_at TIMESTAMPTZ
);
CREATE INDEX IF NOT EXISTS idx_mcp_oauth_codes_grant ON mcp_oauth_codes(grant_id);
-- Consumed refresh digests remain until the grant is pruned, allowing a replay
-- to revoke the entire family, including access tokens issued after rotation.
CREATE TABLE IF NOT EXISTS mcp_oauth_credentials (
key_hash VARCHAR(64) PRIMARY KEY,
grant_id VARCHAR(43) NOT NULL REFERENCES mcp_oauth_grants(id) ON DELETE CASCADE,
kind VARCHAR(8) NOT NULL CHECK (kind IN ('access', 'refresh')),
expires_at TIMESTAMPTZ NOT NULL,
used_at TIMESTAMPTZ
);
CREATE INDEX IF NOT EXISTS idx_mcp_oauth_credentials_grant ON mcp_oauth_credentials(grant_id);