Repository navigation
feat: private site session scope and NemarApiRpc entrypoint - #1577
Conversation
📦 Package PreviewYour package is ready to test! bun install -g nemar-cli@PR1577Version: 🤖 Updated on each push. |
5b8580e to
cbd8eab
Compare
|
Review record. Three independent review passes on 5b8580e (authentication and revocation; migration and deploy safety; contract, tests, docs and repository hygiene) found no critical issues. Every finding was addressed in the rewritten history now ending at cbd8eab, and a focused verification pass on that round found no critical or important issues; its moderate items and nits are addressed in 6450364 through d5d79cd. Main changes from review:
Kept by design: a sequential retry of an already-revoked key cannot re-run a failed docs cascade (the pre-read filters revoked keys); the exposure is bounded by the 15-minute CLI docs session and the failure is logged with the account id. |
Summary
This adds what the private site (
private.nemar.org), which hosts access-controlled features, needs from the API: a session on its own host, and a way to ask who a caller is without reading the API's tables.The private site's Worker reaches the API through a service binding to a new
NemarApiRpcentrypoint (ADR 0078) with three methods:resolvePrincipal,exchangePrivateGrantandrevokePrivateSession. The exchange, the per-request check and sign-out are methods, not routes; the public API gains one route,POST /auth/private/grant.The session is a third
web_sessionsscope,private, minted by the same one-time-grant handoff the docs gate uses, for any active account, and ended by sign-out, admin revoke and the owner soft delete (ADR 0079). Role demotion and API key revocation deliberately leave it alone, and the ADR says why.Unlike the docs handoff, this one binds the browser that starts a sign-in to the browser that finishes it. The private site generates a random
state, keeps it in a host-only cookie and passes it through the website's authorize page; the grant stores only its hash, and the exchange mints only when the private site presents the same value from its cookie. The private site is a write surface, so without this an attacker could complete their own sign-in in a victim's browser and receive the victim's uploads.A method for dataset facts was drafted and is left out on purpose: it waits for a caller, so that anonymous-deposit disclosure and archived or deleted rows are settled with that caller, and an additive-only contract should not ship an unsettled method.
It also closes gaps in existing code: an owner revoking a user's key and the key-regeneration link now end that account's docs sessions and grants, and
optionalAuthMiddlewareno longer accepts an expired API key.Changes
backend/src/worker.tsis the Worker'smain: it re-exports the existing default handler and addsNemarApiRpc(backend/src/rpc/), soindex.tsnever importscloudflare:workers.resolveApiKeyUseris extracted fromresolveBearerUseras the one API-key lookup. The bearer path's SQL and responses are unchanged, andoptionalAuthMiddlewarenow uses it too, which also toucheslast_used_atfor a live key on those routes.web_sessionsto allow theprivatescope, copying every row, column and index behind an exact guard (equal counts and an emptyEXCEPTin both directions), and addsprivate_grantswith a requiredstate_hash.services/private-auth.tsholds the grant, mint, consume and revoke statements.routes/auth-private.tsis the grant route: Origin check, 401 without a session, 403 for an inactive account, 400 without a validstate,no-store, and a limit of ten grants a minute per account instead of the shared per-IP bucket (The strict auth rate-limit bucket is shared by every website visitor #1354).shared/contract/private-site.tsis the contract: method and result types,Principal, the rules a caller must follow (fail closed on unknown errors, never cache a principal, never log a presented key, thestaterequirements), and plain-literal constants for the other repositories, pinned byprivate-site-contract.test.ts.read-onlyandfull, reads infull.revokeDocsCredentialsmoves toservices/docs-auth.ts, is called by every route that revokes keys without ending every session, logs the account id when it fails, and also runs when a by-id revoke loses a race./health, that an anonymousPOST /auth/private/grantanswers 401 on both environments.Deploy notes
Merging runs migration 0089 on
nemar-db-dev, rebuildingweb_sessions. Existing sessions are preserved as asserted byprivate-sessions-migration.test.ts, which seeds every row shape at 0088 and replays the migration on bun:sqlite and on Miniflare's local D1, and by a seeded replay of all 91 migrations on local D1 during review;bun run migrations:d1-checkonly proves the schema replays. The live dev schema ofweb_sessionswas compared against the migration-derived one before merge and matches.The remote apply is not documented to be atomic. The migration's header records what was checked, what the guard does and does not protect, and the recovery path: redeploy the previous Worker version, then restore with D1 Time Travel to the start of the workflow's migration-apply step, or finish the remaining statements by hand. A Time Travel bookmark is recorded right before merge.
After deploy, the new workflow step should report the private grant route answering 401. The website authorize page and the private site deploy after this.
Testing
bun run typecheck(root and backend),bun run lint, Biome on changed backend and shared filesbun testin backend and at root: no new failures against the dev baselinebun run migrations:d1-checkwrangler deploy --dry-runfor both environments; the bundle exportsNemarApiRpcand the default handlerwrangler deploy --dry-run, with a second Worker holding the service binding