Skip to content

Phase B1: ADR 0119, partner hosts, __Host- cookie and OIDC subject #2015

Closed

Description

@neuromechanist

Goal

After this phase the provider's binding decisions are recorded in ADR 0119 before any client can register, a partner subdomain such as compute.nemar.org is no longer trusted as a NEMAR origin, the app session cookie has a __Host- name with a safe dual-read window, and every account has an opaque, never-reused oidc_subject.
Nothing OIDC is served yet.

Routing

Scope

In: ADR 0119; shared/partner-hosts.ts and its two exclusions; the cookie rename with dual read in both repositories; migration 0120; the reserved-prefix username rule.
Out: any /oauth route, client or key table (#2016); turning the cookie flag on in production (the owner, after the website release).

Design references

Design section 3; ADR 0056, 0079, 0043, 0048, 0106; .context/decisions/0000-template.md.

Changes

  • Create .context/decisions/0119-nemar-is-an-openid-connect-provider-and-orcid-stays-the-upstream-identity.md, status proposed: every binding rule of design section 3, plus token prefixes nmo_at_ and nmo_rt_ (API keys are nm_), sub as nmu_ plus 32 lowercase hex, sign-in rules active_account and person_proven_orcid ("proven" is an oauth_identities ORCID row matching users.orcid; an ADR 0106 attestation does not count), first-party refused for a client with a partner-host redirect, and "general provider" (compute facts come from the API, never the ID token). Credential-ending table: admin revocation and owner soft delete end all grants and tokens and send back-channel logout; account merge (manual, ADR 0043) does the same for the loser through an owner command; compute suspension ends grants with a compute. scope and notifies only those clients; ORCID unlink or relink ends grants of person_proven_orcid clients; client disable revokes everything and sends no logout; role demotion, key revocation and sign-out leave OIDC grants alone, each with its reason. A "not decided" list (public clients, private_key_jwt, connected-apps page). Add the README index line and an AGENTS.md "Load-bearing ones" line.
  • test/adr-index.unit.test.ts demands gapless numbers: if origin/dev lacks 0118, add TOLERATED_GAPS = new Set([118]) with a comment to delete it in the PR that adds 0118.
  • Create shared/partner-hosts.ts (import-free): PARTNER_HOSTS = ["compute.nemar.org"], isPartnerHost(hostname) (exact or subdomain, lowercase). Make isNemarWebOrigin (backend/src/services/cors-origins.ts) and isAllowedOrigin (backend/src/services/web-session.ts) return false for a partner host first; the api cors, routes/zarr-data.ts and routes/mcp.ts inherit the first.
  • backend/src/services/web-session.ts: SESSION_COOKIE_NAME = "__Host-nemar_session", LEGACY_SESSION_COOKIE_NAME, readSessionCookie(header) (new first) and sessionCookieHeaders(env, value, {maxAgeSeconds}) returning an array: HOST_PREFIXED_SESSION_COOKIE unset gives today's header byte for byte; "1" gives the new name with Secure; HttpOnly; SameSite=Lax; Path=/, no Domain, plus legacy clears with and without Domain. Readers: middleware/webSession.ts and resolveCookieUser in middleware/auth.ts. Writers (append): routes/auth-web.ts verify, logout and the /me slide; routes/auth-orcid.ts both issuances. Optional binding in types/bindings.ts; "1" only in [env.dev.vars] of backend/wrangler-sccn.toml.
  • Website PR (base staging): src/lib/auth.ts (new name, readSessionCookie), src/middleware.ts (presence check before the cross-host redirect, applySession, forwarded-cookie fallback), src/pages/api/auth/logout.ts (defensive clear of both), and tests naming the cookie (also comments in auth-proxy.ts and api/v1/[...path].ts). auth-dev.ts keeps the legacy name; proxies mirror Set-Cookie unchanged.
  • backend/src/db/migrations/0120_users_oidc_subject.sql (style of 0096_...): add the column with a format CHECK, backfill every row including tombstones with 'nmu_' || lower(hex(randomblob(16))), unique index idx_users_oidc_subject, an AFTER INSERT trigger filling NULL, a BEFORE UPDATE OF trigger aborting any change to a non-NULL value.
  • shared/oidc-subject.ts (OIDC_SUBJECT_PREFIX, isOidcSubject), backend/src/services/oidc-subject.ts (userIdForOidcSubject), validateUsernameFormat in services/username.ts refusing a leading nmu_ (code username_reserved; map every consumer of UsernameFormatError), and owner-only GET /admin/users/by-oidc-subject/:sub in routes/admin/users.ts pinned in test/admin-route-inventory.unit.test.ts.

Tests (write these first)

  1. Partner exclusion through real routes: Origin: https://compute.nemar.org gets no CORS allowance from the api, zarr and mcp forks, and a cookie-authenticated admin write is 403 origin_not_allowed. Mutation: drop the partner test from either check. Also x.compute.nemar.org excluded, app.nemar.org unchanged.
  2. Cookie matrix: flag off is byte-identical at all five writers; flag on sets the new name without Domain and both clears; a legacy-only request authenticates; with both, the new one wins and an invalid new one is not rescued by a valid legacy one. Mutation: swap the read order.
  3. Migration 0120 (template backend/test/upstream-repull-dispatches-migration.test.ts): backfill unique and well-formed, insert gets one, update or duplicate aborts, the tombstone mask keeps it.
  4. The subject is in no body of /users/me, /auth/me or admin user detail; validateUsernameFormat("nmu_abc") is refused and nemar_x is not.
  5. The ADR index test passes with and without the tolerated gap.

Acceptance criteria

  • ADR 0119 is indexed and carries design section 3's binding rules.
  • Both origin checks exclude partner hosts and change nothing else.
  • The website PR is merged to staging with tests green before the flag is set anywhere.
  • oidc_subject exists on every row, cannot change, and is not exposed.

Gates

bun run typecheck, bun run lint, bunx biome check backend/src shared test, the new tests, test/adr-index.unit.test.ts, test/admin-route-inventory.unit.test.ts, backend/test/cors-origins.test.ts, bun run migrations:d1-check, full bun test. Website: bun run lint, bun run typecheck, bun run test.

Rules that bite here

  • The cookie stays host-only on purpose (ADR 0056); __Host- forbids Domain, so never route the new name through WEB_SESSION_COOKIE_DOMAIN.
  • Two readers of the cookie exist; do not add a third copy.
  • The repository is public: no private host details.

Escalate to the owner if

  • A production username already starts with nmu_ (the owner checks before release).
  • SDSC's answer to design question 13 makes the subject format unusable.

When merged

Tell the owner the order: website release, API release, then HOST_PREFIXED_SESSION_COOKIE in production. Mark design section 3 "decided in ADR 0119".

Activity

  1. added
    feature
    on Nov 4, 2026
  2. neuromechanist commented on Nov 4, 2026

    @neuromechanist
    ContributorAuthor

    Merged into the epic branch by #2031 (squash f7233e8a), 2026-11-04, after a round-1 panel review, the Opus checkpoint and its delta; every accepted finding fixed. Gates: phase tests, typecheck, lint and the D1 migration check pass; the full CI run is the epic gate.

    Still open from this phase's acceptance criteria: the website cookie reader nemarOrg/website#499 is green and waiting for the owner's approval to merge into staging (an integration branch). The flag stays off everywhere in production until that release is live. Release order: website to staging and released, then the API release, then HOST_PREFIXED_SESSION_COOKIE in production. The ADR is proposed until the owner settles the points listed on #2031.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurehpc

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions