Skip to content

feat: temporary read-only CLI tokens authorized through MCP - #29

Merged
qiansc merged 1 commit into
mainfrom
feat/temporary-cli-tokens
Nov 29, 2026
Merged

qiansc merged 1 commit into
mainfrom
feat/temporary-cli-tokens

Conversation

@qiansc

@qiansc qiansc commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Background

Agents often have only MCP configured: the service address and a personal token live in the MCP client settings. A command line running next to the agent (for example a knowledge tool that verifies source citations through the raw-read HTTP routes) has neither the address nor a token, and the token must not pass through the model's context.

Changes

  • MCP authorize_cli (optional client): returns a one-time code (single use, 10 minutes, stored as a digest), host (the API base address this MCP request came in on, the same form as SOURCEGRAPH_URL) and exchange_path (/auth/cli/exchange). Listed on personal-token and anonymous connections; without a token it answers 401 LOGIN_REQUIRED. 30 authorizations a minute per user.
  • POST <host>/auth/cli/exchange {code} (no sign-in): takes the code atomically and creates a temporary read-only personal token with the authorizing token's current scope. 20 requests a minute per client address, 3000 in total; 400 INVALID_CODE, 429 TEMPORARY_TOKEN_LIMIT (20 unexpired temporary tokens per user), 503 SITE_FROZEN on a frozen site.
  • Temporary tokens (temporary, read_only, parent_id, max_expires_at on the API key):
    • read-only: retrieval routes, MCP retrieval tools and contents preparation; everything else, including all /auth session routes and site settings, answers 403 TOKEN_READ_ONLY;
    • never carry site administration; preparation limits count with the owner;
    • expire 3 hours after their last admitted use (extended at most every 5 minutes; refused requests do not extend), at most 24 hours after the exchange;
    • fixed scope (PATCH answers 400 TEMPORARY_TOKEN_SCOPE_FIXED);
    • deleted with their parent, when the parent is rescoped, or when a tag in their scope is renamed; the query authentication cache is cleared also after a partial failure.
  • host: derived from the configured origin (or the matched extra public site) plus the external route prefix. A request forwarded by another site's forward_hosts reports the caller's address only when a trusted proxy delivered it and the address is one of the sites in config/deployment.json (new optional aliases field read by the server).
  • Stores: SQL and MongoDB implement GetAPIKey, CountTemporaryAPIKeys, ExtendAPIKey (never shortens), PutCLICode/TakeCLICode (single use: one serializable transaction on SQL, FindOneAndDelete on MongoDB), cascade deletion and scope refusal. MongoDB gets a TTL index on max_expires_at (ordinary keys lack the field); SQL rows carry expires_at and are removed by the existing hourly prune, codes included. No SQL schema change: keys and codes are documents in sourcegraph_documents.
  • Website: temporary tokens are labelled and can be deleted; their visibility button is hidden. CLI: sourcegraph tokens shows a kind column; version 0.6.0. Skill and docs/interfaces.md describe the flow. GET /v1/capabilities lists cli-authorization.

Risks

  • A client reaching the service directly through a trusted local proxy can choose X-Forwarded-Host among the deployment's own configured site addresses for its own response; arbitrary hosts and untrusted peers are ignored.
  • Two concurrent exchanges may let one user exceed 20 temporary tokens slightly (count, then insert).
  • On SQL, child lookups scan the key namespace; fine for the expected number of keys.

Compatibility

Existing tokens have none of the new fields and behave as before; CLI sign-in and existing routes and tools are unchanged. MCP gains one tool. Upgrading needs no migration (verified on Postgres by upgrading a deployment of current main).

Testing

  • gofmt, go vet ./..., go test -p 1 ./..., go test -race on sso, httpserver, mcpserver, storagecontract; bun run check; scripts/audit-public.py.
  • Storage contract (internal/storagecontract) with the new temporary-key and code cases on SQLite, PostgreSQL 17 and MongoDB 8 (replica set): pass.
  • Docker end-to-end on PostgreSQL 17, fresh deployment and upgrade from current main: authorize_cli → exchange → read allowed (repos, capabilities, availability, search) / denied matrix (TOKEN_READ_ONLY) → temporary MCP tool list → code reuse INVALID_CODE → listing fields → fixed scope → delete parent → 401: all pass; existing remote-read e2e on the upgraded deployment passes.

🤖 Generated with Claude Code

An agent that only has MCP configured can let a command line next to it
call the HTTP API as the same user, read-only, without the token passing
through the model.

- MCP tool authorize_cli returns a one-time code (10 minutes, stored as a
  digest), host (the API base the MCP request used, SOURCEGRAPH_URL form)
  and exchange_path; POST <host>/auth/cli/exchange {code} creates a
  temporary read-only personal token with the caller token's scope.
- Temporary tokens: read-only (retrieval, MCP retrieval tools, contents
  preparation; everything else 403 TOKEN_READ_ONLY), no site
  administration, 3 hours after their last admitted use and at most 24
  hours, fixed scope, 20 per user, deleted with their parent or when the
  parent is rescoped or a tag in scope is renamed.
- Stores: SQL and MongoDB implement the temporary-key and code methods;
  MongoDB removes temporary keys with a TTL index on max_expires_at, SQL
  rows expire through the hourly prune.
- Website labels temporary tokens and hides their visibility button; CLI
  tokens list shows a kind column; CLI 0.6.0; skill and docs updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@qiansc
qiansc merged commit 386f416 into main Nov 29, 2026
3 checks passed
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.

1 participant