Cozy Commons stores other services' passwords, cookies and session tokens (Beli, Resy, ESPN, Partiful) and acts as its users there. Treat every change that touches that path as security-critical.
Read this before changing credential storage or entry, account linking, auth or OAuth, agent tools, SQL functions, grants or migrations, dependencies or lockfiles, CI workflows, the frontend CSP, background jobs, or any outbound network call.
- A rogue dependency in the API process. It can read every environment
variable (the
*_CREDENTIALS_KEYs,SUPABASE_SERVICE_KEY) and patch any module. Today the only defense is keeping it out (hash locks, the seven-day cooldown); there is no outbound allowlist yet, so it could reach any host. - One Commons user acting as another on a third-party service, through account matching, a race, or a partial write.
- Secrets leaking into responses, error bodies, logs, agent transcripts or exception text.
- Agents as a leak path. Agent chats are stored and replayed; a secret typed there is a secret disclosed.
Accepted for now: the API holds both the database and the keys, one key per service covers every user, and rate limits live in one worker's memory. Revisit a separate credential broker before Commons serves users beyond friends, or before a provider holds payment methods.
- Users enter third-party credentials only on
/account, which loads as its own document under the strict CSP (frontend/csp.jsdocumentKind,docs/frontend-csp.md). Credential forms call only${api}/account/.... Agents send users tocozycommons.dev/account#<service>and never ask for a password, cookie, code or token. - New agent capabilities go on
/mcp(backend/cc_mcp), where the user is the OAuth session. Never add a tool that takes a token as a parameter. The/<project>/mcpservers takeapi_tokenper call and are legacy; a token that reaches an agent transcript counts as leaked and gets retired. - On
/mcp, a tool is callable only ifProjectAccessMiddlewareregisters it andcheck_accesspasses on every call. Hiding a tool from the list is not authorization; write tools needcommons:editand a linked account. - Encrypt every stored secret with the project's key through
backend/credential_keys.py. Agent OAuth state stays encrypted underCC_MCP_STORAGE_ENCRYPTION_KEY. Never log, return or embed a secret in an exception message, and never present the agent-suppliedclient_nameas verified (showredirect_hostbeside it). - Prove ownership with the third-party login itself. Identifiers are printable
ASCII and only ASCII is case-folded (see
login_key). Match phone numbers by digits. - Insert a new row unowned, link it through the project's
*_link_accountRPC, and refresh an existing row's secrets only after the link, filtered by.eq("owner_user_id", caller). When a link also rewrites credentials, tokens or settings, do all of it in one RPC, as ESPN'sespn_claim_account_provendoes. Beli, Resy and Partiful still refresh in a second write; fold that into their RPC when next touched. - A write to an existing row must be conditional on the state the request read. Never acquire an unowned row as a side effect of refreshing one.
- A weaker proof never displaces a stronger one; a proven claim can take over an unproven one. Takeover retires the old token and expires everything the previous holder left pending.
- One-time codes are bound to the Commons user and the normalized phone,
expire, and are consumed the moment the provider accepts them; the lock is
never held across the provider call (
reserve_partiful_challenge). - Rate-limit credential and token attempts per Commons user and per third-party identifier.
- Request validation errors never echo input:
backend/request_validation.pyis installed app-wide. Check secret lengths in the route rather than with pydantic constraints. - Never read a credential or token through
CachedSupabaseClient(app.state.supabase); a retired token must stop working on the next request. - Rotate a
*_CREDENTIALS_KEYonly by prepending the new key, deploying, runningpython -m scripts.rotate_credentials, then removing the old key (docs/deployment.md#credential-key-rotation).
- Every backend Supabase client uses the service role, so row-level security
does not protect anything from the API. Every query returning a user's data
filters by the caller's id in the API. A new table still gets
enable row level securityandrevoke all ... from public, anon, authenticated, so the anon key the frontend ships cannot read it. - Log identifiers (row ids, HTTP status, hostnames), never request bodies,
cookies,
Authorizationheaders or third-party error bodies; a provider's error text can contain the credential that was sent. - Background jobs (the Instagram watcher, ESPN lineup automation) load only the rows they are about to act on, never log decrypted values, and stop acting for a row once its owner changes or its token is retired.
- The admin SQL tool runs as
commons_analytics_readeroveranalytics.*views (backend/admin/query.py). Exposing a new view, function, type or operator there is a security change and must not reach a table holding*_enc,token_hashor OAuth state. CORS_EXTRA_ORIGINSis combined with credentialed CORS: add only Commons frontends, never a wildcard./revokeallowing any origin is the one deliberate exception (RFC 7009 public clients).
- Functions are
security invoker,set search_path = '', revoked frompublic, anon, authenticated, and executable byservice_roleonly. - Grant explicitly every privilege a function relies on, and check it in the
project's
backend/migrations/*_schema_contract.sql; do not rely on Supabase default privileges. - Lock every row a function will change in one id-ordered
for updatebefore branching. Validate parameters (no NULL user ids, no empty secrets). - Never edit a migration that has been applied anywhere, including the shared local stack; add the next number. Page credential and token tables by id, never by offset.
- Every Python install uses a hash-locked file with
--require-hashes --only-binary=:all:. Change dependencies in the.infile, regenerate withscripts/lock-backend-requirements.sh, and read the lockfile diff for unfamiliar package names. - Bump
uv-exclude-neweronly to a date at least seven days old. Take a younger release only as a reviewed security exception. - Pin actions by commit SHA.
- Dependabot, secret scanning with push protection, and CodeQL (extended suite) are enabled on the repository. Fix their findings; never dismiss one without a written reason.
For changes in the scope above, an adversarial review before merge is recommended, best effort, not a merge requirement. When you do one:
- Use a reviewer other than the author, a person or a separate agent session on any model, and ask them to try to break the change.
- Give the reviewer the diff, what earlier passes already found, and the attack surfaces to probe. Ask for blocking versus hardening findings.
- Verify a disputed finding by reproducing it before acting, and fix blocking findings as new commits.
- Note the review and its fixes in the PR description. A finding in merged code ships as its own fix PR.
Contributors may hold their own work to a stricter personal bar, such as multiple reviewers or a specific model; that bar is theirs, not the repository's.