Skip to content

feat(authz): standalone AUTHZ_MODE, service-bridge, native-auth disable (Phase A + 2b) - #11

Merged
PMQ9 merged 4 commits into
wiki-v2from
feat/standalone-authz-mode
Sep 3, 2026
Merged

PMQ9 merged 4 commits into
wiki-v2from
feat/standalone-authz-mode

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 3, 2026

Copy link
Copy Markdown

What & why

Makes the AGPL Docmost fork independently runnable and turns the CCC platform into an opt-in network integration, selected by a required, server-controlled AUTHZ_MODE = native | remote. This is the fork half of the standalone-boundary work; the CCC platform side is the paired super-repo PR (see below). Tracking issue: cccvu/wiki-v2#157 (full plan, rationale, progress).

  • native → Docmost's OWN authorization (stock CASL + space_members / page_access / page_permissions) and native login. The fork boots and runs fully standalone with no external service. A legitimate upstream control, never "allow all".
  • remote → authorization decisions come from an external service (the CCC platform, or any server implementing the documented contract), fail-closed, and native credential auth is disabled server-side.

Base branch: wiki-v2 (our fork integration branch, based on upstream v0.95.0). 44 files, +1533/-32.

Scope (two phases on one branch)

  • Phase A (items 1+2a) — 698e3514 — built & verified in prior sessions (see Add Source Label to Dockerfile docmost/docmost#157): the AUTHZ_MODE selector (authz/mode/), implementation-neutral ports (authz/port/ + a compile-time conformance test, so we never add implements to upstream repos), mode-selected DI for the two authz repos + search + audit, the boot validator, the client capability flag NATIVE_AUTH_ENABLED + native login UI (features/auth-native/), and the service-bridge/ module.
  • Phase 2b — this session — delivered as expand/contract, TWO commits:
    • 2b-i (expand) — d5125191 — re-key POST /api/service/{session,users} on the caller's opaque externalId. The fork derives the shadow email (shadowEmailFor) and resolves its OWN default workspace, so a caller cannot select an arbitrary Docmost identity and never handles a Docmost user/workspace id. provisionShadowUser returns { userId, workspaceId }; mintSession(externalId) looks up by derived shadow email and keeps the disqualify() member/shadow/not-deleted checks as defense-in-depth.
    • 2b-ii (contract) — 58103971 — NativeAuthModeGuard + @NativeCredentialRoute() (authz/mode/) wired class-level onto AuthController (seam feat(collab): Hocuspocus 4.7.1 and collab ingress bounds (wiki-v2 #790) #87). In AUTHZ_MODE=remote every marked credential route (login, setup, forgot-password, password-reset, verify-token, change-password) returns 404 for ALL callers, including a service caller presenting the service secret (no exception). Session-scoped routes (collab-token, logout) are unmarked and stay reachable; the guard is inert in native mode.

58103971 is deliberately a separable commit. It disables native login and must ship in its own release, only AFTER the platform BFF switch (2b-i) is confirmed live in prod. Never before. See the deploy sequencing in the super PR / docmost#157.

Upstream seams (the boundary-critical review surface)

All decision logic lives in fork-owned, boundary-excluded authz/ / service-bridge/. Upstream-owned files are modified minimally (wiring only) and every edit is documented in UPSTREAM_MODIFICATIONS.md and enforced by scripts/check-upstream-boundary.sh (both in the super PR):

Security model (please review closely)

  • Mode is a server-controlled security boundary, fixed per container start, never inferred from availability / request params / headers / cookies / URLs / client state. resolveAuthzMode throws on anything but native/remote; the boot validator refuses ambiguous config. In remote mode the transport is fail-closed and NEVER falls back to native (that would be a downgrade; an operator must not "fix" a platform outage by switching to native).
  • /api/service/* are highly-privileged M2M endpoints. Scoped ServiceAuthGuard (least privilege, per-route ServiceScope, not mere secret possession; the single shared secret is transitional and per-scope credentials slot in later with no redesign), fail-closed (503 unconfigured / 401 wrong / 403 wrong-scope), constant-time compare, per-credential rate limit (429).
  • No arbitrary identity selection. Keying /api/service/* on externalId (the fork derives the shadow email) means a caller can only ever name a @shadow.wiki-v2.internal subject; it cannot mint a session for a real/privileged/native account by id or email. This is a strong narrowing, not an absolute guarantee, so the disqualify() check (member role, shadow namespace, not deleted) is retained as defense-in-depth against a tampered/mis-provisioned row.

Verification

  • nest build clean.
  • Fork jest: baseline-delta proven by stashing the change set. My changes add 4 passing tests and introduce zero new failures. (The repo has a pre-existing environmental failure: jest cannot resolve the @docmost/transactional/emails/*.tsx templates via moduleNameMapper, failing 16 suites / 11 tests identically with and without this change. Not touched by this PR.)
  • Boundary check (scripts/check-upstream-boundary.sh, in the super PR) green with seam feat(collab): Hocuspocus 4.7.1 and collab ingress bounds (wiki-v2 #790) #87.
  • Live smoke (make dev-build PROFILES=platform, AUTHZ_MODE=remote): all six native /api/auth/* credential routes → 404 (including WITH the service secret); logout/collab-token → 401 (exist, not 404); /api/service/session → 401 (no/wrong secret), 403 (unknown externalId). Full passwordless login end-to-end minted a session via POST /api/service/session (docmost logs confirm no /api/auth/login) and a real page call returned 200; first-use provisioning for a fresh externalId resolved the fork's own workspace, created a member, and minted a working session.

Merge order (paired-PR discipline)

Merge THIS fork PR first. Then the super PR repoints its docmost submodule to the merged fork commit and merges. Keep UPSTREAM_MODIFICATIONS.md + the boundary check green on every bump.

🤖 Generated with Claude Code

PMQ9 and others added 4 commits September 3, 2026 14:52
…idge (Phase A items 1+2a)

Make the fork independently runnable via a server-controlled AUTHZ_MODE (native|remote):

- authz/port/: implementation-neutral SpaceAuthzPort/PageAuthzPort/RemoteAuthzClient
  + a compile-time conformance test (stock repos AND PDP subclasses satisfy them).
- authz/mode/: AUTHZ_MODE token, resolveAuthzMode (throws on invalid — no silent
  default, no URL heuristic), @global AuthzModeModule, and mode-selected useFactory
  providers for the two authz repos + search. native=stock Docmost authz (standalone),
  remote=PDP-backed. Selection pinned by test.
- Seams #1 (database.module.ts) + #5 (search.module.ts) converted from unconditional
  useClass to the mode-selected providers; authz/audit/audit.module.ts is mode-aware
  (native=NoopAuditService) so app.module.ts is untouched.
- environment.validation.ts (seam #85): AUTHZ_MODE required + remote cross-fields —
  the fork refuses to boot on ambiguous config.
- static.module.ts (seam #86): inject the derived NATIVE_AUTH_ENABLED capability (never
  the raw mode); features/auth-native/ renders native Docmost login at /login in native
  mode; App.tsx/auth-service mode-aware (seams #6/#27).
- service-bridge/ (2a): fork-hosted /api/service/{session,users} — session brokerage
  (mint a Docmost session for a fork-owned shadow member; NO generic impersonation) +
  provisioning (fork writes its own users table). Scoped ServiceAuthGuard (least
  privilege, fail-closed, constant-time, per-credential rate limit; ServiceScope
  supports distinct per-scope credentials later). Recognized by the Layer-C classifier.

Verified: server nest build clean; client tsc clean; full fork suite 23 suites/225
tests green; 9 client vitest green.

NOT YET DONE (item 2b, next session): NativeAuthModeGuard disabling native /api/auth/*
in remote mode + switching the platform BFF onto /api/service/*, verified by a live
make dev-build login smoke.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…Id (2b-i)

Both east-west endpoints now take only the caller's opaque externalId; the fork
derives the shadow email (shadowEmailFor) and resolves its OWN default workspace,
so a caller cannot select an arbitrary Docmost identity and never handles a
Docmost user/workspace id. provisionShadowUser returns { userId, workspaceId };
mintSession(externalId) looks up by derived shadow email and keeps the
disqualify() member/shadow/not-deleted checks as defense-in-depth.

Part of Phase A6/2b (issue docmost#157). This is the expand step — native /api/auth/login
stays enabled; the platform BFF switches to these endpoints in the same release.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…remote (2b-ii)

Adds a fork-owned NativeAuthModeGuard + @NativeCredentialRoute() marker (authz/mode/)
and wires them class-level onto the upstream AuthController (documented seam #87).
In remote mode every marked credential route (login/setup/forgot-password/
password-reset/verify-token/change-password) returns 404 for ALL callers — including
a service caller presenting the service secret (no exception); sessions in remote mode
are brokered only via POST /api/service/session. Session-scoped routes (collab-token/
logout) are unmarked and stay reachable; the guard is inert in native mode. The guard
name is deliberately absent from AUTH_GUARD_NAMES so route classification / the
route-inventory fitness test are unchanged.

Part of Phase A6/2b (issue docmost#157). This is the CONTRACT step — it must ship only AFTER
the platform BFF is switched to /api/service/session (2b-i) is confirmed live; this
commit is kept separable so it can be held for its own release.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ssion tests (review round 1)

Addresses the 7-lens review of the paired standalone-AUTHZ_MODE PR (cccvu/wiki-v2#163 /
#11). Fork-side fixes:

- Security (Medium/L3): `POST /api/workspace/invites/accept` is a @public() route that mints a
  native authToken session + assigns invitation.role (up to owner), but lives on WorkspaceController
  so the AuthController-scoped NativeAuthModeGuard did not cover it — a residual native
  session-mint in AUTHZ_MODE=remote. Gate it method-level (@UseGuards(NativeAuthModeGuard) +
  @NativeCredentialRoute()), so it 404s in remote for ALL callers (seam #88, documented super-side).
  Audit confirmed it is the ONLY native-session mint outside AuthController + the service-bridge.

- Testing (L3) — regression resistance for invariants that are correct today but were pinned only
  by manual smoke:
  * static-route-scan.ts: capture @NativeCredentialRoute() markers + a body tell that a handler
    mints a native session (setCookie('authToken') / setAuthCookie).
  * native-credential-routes.spec.ts (new): pins the EXACT marked set (six AuthController routes +
    acceptInvite), asserts each is covered by NativeAuthModeGuard, and — the load-bearing invariant —
    that EVERY native-session-minting route is marked (except the service-bridge broker). A dropped
    marker or a new unmarked session route now fails RED, not in prod. Uses a source scan (no import)
    because importing the real controllers trips the known emails/*.tsx moduleNameMapper gap.
  * authz-mode.validation.spec.ts (new): pins the fail-closed boot cross-field rules (remote requires
    URL + >=16-char secret; mode must be exactly native|remote) via validateSync.
  * service-auth.guard.spec.ts: exercise the wrong-scope 403 branch (least privilege) and the
    constant-time compare at EQUAL length (the existing wrong-secret test differs in length and
    short-circuits before timingSafeEqual).

nest build + server tsc clean; targeted jest 12 suites / 59 tests green.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
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