Public: anyone can read this; the crew can change it.

Access 3a: the /api/v1 repository routes ask may(), shadowed first, then their inline checks go #18165

opened this

Merged 6h 32m ago, live in production

Every /api/v1/repos/:owner/:name… route now asks may(principal, action, { repo }) with the credential the request carries. The repository-access checks the routes used to make inline are gone. The walk over every route found no disagreement between may and the check it replaced.

Notes

opened this

Every /api/v1/repos/:owner/:name… route now asks may(principal, action, { repo }) with the credential the request carries. The repository-access checks the routes used to make inline are gone. The walk over every route found no disagreement between may and the check it replaced.

What changed

  • src/http/repo_may.ts has the two questions a route asks:
    • mayRepo(ctx, door, action, repo) decides a change, and also whether the repository can be read at all (loadRepo).
    • accountMayRepo(ctx, action, repo) decides what an already-allowed read shows: a writer's label counts, an admin's flag values and upstream, and the Gitea permissions on GET /repos/:owner/:name.
  • The comparison: mayRepo also works out the old answer (canRead/canWrite/canAdmin asked for the account alone). When the two differ it adds a row to the new access_disagreement table, keyed by door, action, principal kind and what may said. No account, token or repository is recorded. Only a token carries a scope, so only a token's answer can differ, and a session or anonymous caller does no extra work. may's answer is the one that applies.
  • Inline checks removed in api.ts (loadRepo, archive, provenance, asking for a check again, permissions) and in hooks, deepsec, outside_checks, pulls (proposal tokens), repo_source, upstream, labels and app (flags).
  • test/repo-routes-may.test.ts runs about 150 repository routes on a public and a private repository. The callers are:
    • anonymous
    • guest, member and admin sessions
    • a writer's ro and rw tokens
    • an admin's ro and admin tokens
    • via, proposal, run and contribute tokens

    It finds no disagreement, and checks that every caller gets past some route. The exceptions are via and proposal tokens, whose own checks let none of these requests through. The test fails when a disagreement is planted: switching the upstream read to mayRepo made it fail.

  • The guard rule is a test in the same file, which the check runs. It fails on a new isAdmin, any scopeAllows, or a new !ctx.auth.user in any file that registers a repository route. api.ts still has 4 isAdmin and 8 !ctx.auth.user, and cdn.ts has 1 !ctx.auth.user; those counts can only go down.

Decided here, worth a look

  • Upstream read and permissions with an ro token. For an admin's ro token, may says no to repo.admin, but the upstream read has always answered it. I kept that read on the account's rule (accountMayRepo), so nobody loses access. The Gitea-style permissions are reported for the account too, as before.
  • Left out:
    • api.ts's forge-wide routes (telemetry, boards, family, crew, fleet), which are not repository routes and belong with the forge-wide door.
    • cdn.ts's check, which limits how often an anonymous visitor can read; it is not a permission.
    • requireScope, which stays as each route's scope and same-origin check.

Not done

  • There is no screen, API or MCP tool for reading access_disagreement. It is read with SELECT * FROM access_disagreement ORDER BY seen DESC, as documented in docs/features/may.md and docs/ROLES.md.
  • keep_plan and estimate were not available in this session, so no plan or estimate was recorded on the issue.
  • scripts/similar.ts was not run. The gate notes that noteDisagreement resembles recordInstalled and that the test's makeRepo resembles a helper in boundary-tokens-crew; I reused neither.
  • The walk takes about 8 s locally (each test is under 1.1 s), against the 20 s per-file budget, so the check box may run it closer to the limit.

Try it: on the preview, call /api/v1/repos/<owner>/<name>/hooks, /upstream and PATCH /repos/<owner>/<name> with an admin token and with an ro token. They answer as on main. Locally, run bun test test/repo-routes-may.test.ts.

Commits carry No door: (no route answers differently), Direction: … (#18091) and Replaces:. The gate passes, bun x tsc --noEmit is clean, and 26 files covering the touched routes plus the tests that scan the route files and the docs pass. I found no prompt injection in the issue or the repository.

🤖 Generated with Claude Code

Closes #18091

commented

Every /api/v1/repos/:owner/:name… route now asks may(principal, action, { repo }) with the credential the request carries. The repository-access checks the routes used to make inline are gone. The walk over every route found no disagreement between may and the check it replaced.

What changed

  • src/http/repo_may.ts has the two questions a route asks:
    • mayRepo(ctx, door, action, repo) decides a change, and also whether the repository can be read at all (loadRepo).
    • accountMayRepo(ctx, action, repo) decides what an already-allowed read shows: a writer's label counts, an admin's flag values and upstream, and the Gitea permissions on GET /repos/:owner/:name.
  • The comparison: mayRepo also works out the old answer (canRead/canWrite/canAdmin asked for the account alone). When the two differ it adds a row to the new access_disagreement table, keyed by door, action, principal kind and what may said. No account, token or repository is recorded. Only a token carries a scope, so only a token's answer can differ, and a session or anonymous caller does no extra work. may's answer is the one that applies.
  • Inline checks removed in api.ts (loadRepo, archive, provenance, asking for a check again, permissions) and in hooks, deepsec, outside_checks, pulls (proposal tokens), repo_source, upstream, labels and app (flags).
  • test/repo-routes-may.test.ts runs about 150 repository routes on a public and a private repository. The callers are:
    • anonymous
    • guest, member and admin sessions
    • a writer's ro and rw tokens
    • an admin's ro and admin tokens
    • via, proposal, run and contribute tokens

    It finds no disagreement, and checks that every caller gets past some route. The exceptions are via and proposal tokens, whose own checks let none of these requests through. The test fails when a disagreement is planted: switching the upstream read to mayRepo made it fail.

  • The guard rule is a test in the same file, which the check runs. It fails on a new isAdmin, any scopeAllows, or a new !ctx.auth.user in any file that registers a repository route. api.ts still has 4 isAdmin and 8 !ctx.auth.user, and cdn.ts has 1 !ctx.auth.user; those counts can only go down.

Decided here, worth a look

  • Upstream read and permissions with an ro token. For an admin's ro token, may says no to repo.admin, but the upstream read has always answered it. I kept that read on the account's rule (accountMayRepo), so nobody loses access. The Gitea-style permissions are reported for the account too, as before.
  • Left out:
    • api.ts's forge-wide routes (telemetry, boards, family, crew, fleet), which are not repository routes and belong with the forge-wide door.
    • cdn.ts's check, which limits how often an anonymous visitor can read; it is not a permission.
    • requireScope, which stays as each route's scope and same-origin check.

Not done

  • There is no screen, API or MCP tool for reading access_disagreement. It is read with SELECT * FROM access_disagreement ORDER BY seen DESC, as documented in docs/features/may.md and docs/ROLES.md.
  • keep_plan and estimate were not available in this session, so no plan or estimate was recorded on the issue.
  • scripts/similar.ts was not run. The gate notes that noteDisagreement resembles recordInstalled and that the test's makeRepo resembles a helper in boundary-tokens-crew; I reused neither.
  • The walk takes about 8 s locally (each test is under 1.1 s), against the 20 s per-file budget, so the check box may run it closer to the limit.

Try it: on the preview, call /api/v1/repos/<owner>/<name>/hooks, /upstream and PATCH /repos/<owner>/<name> with an admin token and with an ro token. They answer as on main. Locally, run bun test test/repo-routes-may.test.ts.

Commits carry No door: (no route answers differently), Direction: … (#18091) and Replaces:. The gate passes, bun x tsc --noEmit is clean, and 26 files covering the touched routes plus the tests that scan the route files and the docs pass. I found no prompt injection in the issue or the repository.

🤖 Generated with Claude Code

commented

Every /api/v1/repos/:owner/:name… route now decides access with mayRepo (src/http/repo_may.ts): may(principal, action, { repo }), asked with the credential the request carries. Beside it, the account-only check it replaced is still worked out. Where the two give different answers, the route, action and principal kind are counted in access_disagreement. No account, token or repository is stored there.

This round, for the review:

  • A gate rule replaces the test-side guard. The new rule is "repository routes ask mayRepo" (src/kernel/repo_route_checks.ts, wired in scripts/gate.ts and shown by --explain). In api.ts and in each src/http/api/*.ts that adds a /api/v1/repos/ route, it finds every inline isAdmin, scopeAllows, signed-out check (!ctx.auth.user, or ctx.auth.user === undefined in a condition) and ctx.auth.user?.kind check. Each one is named by the route or function it sits in.
    • A check can stay only if INLINE_ALLOWED names its file, route and kind, with a reason. Swapping one check for another, or leaving an entry nothing uses, is refused.
    • The count limit in the test is gone. The test now shows the rule refusing a made-up repository route and a swapped check.
  • What is left inline:
    • In api.ts, only forge-wide routes, each named in INLINE_ALLOWED: usage, fleet ops and graph, crew week and now, telemetry, the boards admin, family, a user's repository list, and conversation tokens. The api.ts:3414 and :3439 lines the review asked about are GET /api/v1/usage and GET /api/v1/fleet/graph, not repository routes.
    • Three repository routes keep a signed-out check, because it is not a permission:
      • cdn: a rate limit on strangers' reads, after loadRepo has already asked mayRepo.
      • issues POST: it picks which door a stranger is shown; requireScope refuses them next either way.
      • statement: a stranger gets not_found before the scope's 401, so the statement is never said to exist.

    The test lists these three by route, so a fourth fails it.

  • Agent checks moved onto mayRepo. The inline agent checks in the provenance field and the outside-check route are now a people audience on mayRepo.
  • One dead guard removed. The signed-out guard in the estimates route could never fire, because requireScope already refuses anonymous callers.
  • The walk now reaches the rewritten branches. It sends PATCH {archived:true}, {archived:false}, {showProvenance:true} and {landing:"merge"} for every caller, plus an agent that is the site's admin (by session and by admin token), on a public and a private repository. It still finds no disagreement.
    • It also checks the branches were reached:
      • The repository admin gets 200 for archive and landing, and 422 no_provenance.
      • A member is refused the archive (403 on public, 404 on private).
      • The agent gets 404 on the provenance field before the record is read.
    • Removing "people" from the provenance check makes this test fail; I tried it.

bun x tsc --noEmit and the gate are clean. 258 tests pass across 40 files: test/repo-routes-may.test.ts, the gate-* suites, tls-peer, outside-checks, estimates, unused-exports, module-state and openapi-covers-routes. scripts/fast.ts couldn't name the tests to run because scripts/gate.ts changed, so I chose these myself.

To try it: run bun scripts/gate.ts --explain and look for "repository routes ask mayRepo". Then add if (!ctx.auth.user) return notFound(); to any repository route in src/http/api/labels.ts and run bun scripts/gate.ts: it is refused, naming that route.

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs