Skip to content

fix(service-bridge): resurrect soft-deleted shadow users + adjacent hardening (#50, #272) - #28

Merged
PMQ9 merged 2 commits into
wiki-v2from
fix/issue-50-272-shadow-session
Sep 13, 2026
Merged

PMQ9 merged 2 commits into
wiki-v2from
fix/issue-50-272-shadow-session

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 13, 2026 •

Copy link
Copy Markdown

Paired fork half of cccvu/wiki-v2 issue 50 / issue 272. The platform (super-repo) PR repoints the submodule to this branch and carries the platform-side fixes; merge this fork PR first, then the super PR.

What this does

Adds the fork-side real-dependency coverage the #50 shadow-session work needs, and fixes the fork defect it exposed (docmost#272 P5) plus adjacent latent defects the review hunt surfaced — all in the CCC-owned apps/server/src/service-bridge/ tree (no upstream-owned files touched). Bug-exposing tests were committed red first (commit 1), then fixed at root cause (commit 2); every red is now green.

Fixes

  • P5 + F3 — provisionShadowUser ON CONFLICT upsert now self-heals a fork-owned shadow row to the "plain, live member" shape: clears deletedAt (resurrects a soft-deleted shadow user — previously mintSession refused "deleted" forever), forces role: 'member' (de-escalates a tampered row; the literal is never caller-supplied, so this strengthens no-escalation), and refreshes name/emailVerifiedAt. Never touches password/email.
  • F1 — disqualify uses the platform-wide isUserDisabled (deactivatedAt OR deletedAt), so a deactivated shadow member is refused (was a mint fail-open; every native auth entrypoint already enforces this).
  • F2 — shadowEmailFor lower-cases the derived address (the single chokepoint both the case-sensitive upsert and case-insensitive findByEmail use), so case-variant externalIds can't create duplicate / cross-mapped rows.
  • F7 — isShadowEmail requires exactly one @ and an exact domain match (rejects embedded-@ lookalikes a bare endsWith() accepted).
  • F4 — WorkspaceResolver.resolveDefaultWorkspaceId is deterministic (createdAt, then id tiebreak) and warns on >1 non-deleted workspace (single-tenant invariant breach) instead of a silent non-deterministic flip; no hard-fail.
  • F5 — resolveUserWorkspaceId excludes soft-deleted users and rejects a null workspace_id (no bogus PDP anchor).
  • F6 — ServiceAuthGuard rate-limits the unauthenticated (missing/wrong-secret) path via a single coarse anon bucket, and treats a whitespace-only configured secret as unconfigured (503). Deliberately not keyed on req.ip: main.ts sets trustProxy: true, so req.ip is X-Forwarded-For-derived (attacker-controlled) — a per-IP key would let a spoofed header bypass the limit and grow the window map without bound. All legitimate callers hold the secret (separate bucket), so all traffic on this path is illegitimate and capping it collectively is the intent.

Tests

New/strengthened specs under apps/server/src/service-bridge/:

  • service-bridge.service.spec.ts — T-030 (conflict key-set, strengthened to pin role === 'member'), T-031/T-032/T-033, T-036 (P5, now green), F1 (deactivated), deactivated added to the uniform-403 T-033 set.
  • service-bridge.pg.spec.ts (new, real-PG) — T-040…T-046 (T-046 P5 now green), T-047 (F3 de-escalation on real ON CONFLICT), T-048 (F2 case convergence).
  • shadow-user.spec.ts — T-034 lookalike table + embedded-@ (F7) + case-normalization (F2).
  • dto/dto-validation.spec.ts — T-035 (ProvisionUserDto/MintSessionDto charset guard).
  • workspace-resolver.spec.ts — F4 (deterministic + warn), F5 (soft-delete filter, null-workspace 404).
  • service-auth.guard.spec.ts — F6 (anon rate-limit 429, XFF-spoof cannot bypass, valid-credential not starved, whitespace-secret 503).

Verified locally: pnpm exec jest src/authz src/service-bridge src/editor-compat (pg specs self-skip); full src/service-bridge with a real Postgres (AUTHZ_REQUIRE_PG=1) → 27 suites / 258 tests green; nest build clean; super-repo check-upstream-boundary.sh clean (no upstream-file divergence, no brand marks).

Risk & rollback

Low. All changes are additive/behavioral within CCC-owned service-bridge code; no schema migration, no upstream-owned file touched. The two behavioral shifts a reviewer should weigh: (a) a re-provision now de-escalates role to member (intended self-heal; mint was already fail-closed on non-member), and (b) shadowEmailFor now lower-cases (no-op for existing lowercase-UUID ids; no data migration needed). Rollback = revert the two commits.

🤖 Generated with Claude Code

PMQ9 and others added 2 commits September 12, 2026 23:31
…coverage (#50)

Adds the issue #50 boundaries the mocked specs could not prove:
- service-bridge.pg.spec.ts (new): real Postgres ON CONFLICT upsert —
  idempotency, planted-real-user untouched, foreign-workspace not matched,
  concurrent convergence, no-workspace 503, soft-delete resurrection (T-046 red).
- service-bridge.service.spec.ts: conflict target (email, workspaceId),
  no role/password rewrite, no-workspace-before-insert, name fallback,
  uniform 403, soft-delete resurrection unit proof (T-036 red).
- shadow-user.spec.ts: lookalike-domain suffix table + derivation boundary.
- dto/provision-user.dto + mint-session.dto validation: externalId charset is
  the no-injection boundary (rejects @, whitespace, >128, non-ASCII).

Companion bug-tracker: cccvu/wiki-v2#272 (P5 red tests here; P1-P3 platform-side).
Test plan: docs/test-plans/issue-50-docmost-bridge.md
…ardening (#50, docmost#272)

Fixes the fork-side defect the #50 coverage exposed (companion tracker docmost#272), plus
the adjacent latent defects the review hunt surfaced. Flips the intentionally-red
tests green at root cause; no test weakened.

- P5/F3 (service-bridge.service.ts): the provisioning ON CONFLICT upsert now
  self-heals a fork-owned shadow row back to the "plain, live member" shape —
  clears `deletedAt` (a soft-deleted user is resurrected; previously mintSession
  refused "deleted" forever), forces `role: 'member'` (de-escalates a tampered
  row; the literal is never caller-supplied, so this STRENGTHENS no-escalation),
  and refreshes `name`/`emailVerifiedAt`. Never touches `password`/`email`.
  Flips T-036 (unit) + T-046 (real-PG); T-030's key-set assertion updated to pin
  the new columns and prove role is only ever `member`. Adds T-047 (real-PG
  de-escalation proof).
- F1 (service-bridge.service.ts disqualify): use the platform-wide
  `isUserDisabled` (deactivatedAt OR deletedAt) so a DEACTIVATED shadow member is
  refused — the mint path checking only `deletedAt` was a fail-open divergence
  from every native auth entrypoint.
- F2 (shadow-user.ts shadowEmailFor): lower-case the derived address at the single
  chokepoint both the case-sensitive upsert and case-insensitive findByEmail flow
  through, so case-variant externalIds can't duplicate/cross-map. Adds T-048
  (real-PG convergence).
- F7 (shadow-user.ts isShadowEmail): require exactly one `@` + an exact domain
  match, rejecting embedded-`@` lookalikes a bare endsWith() accepted.
- F4 (workspace-resolver.ts): deterministic pick (createdAt asc, id asc tiebreak)
  + warn when >1 non-deleted workspace (single-tenant invariant breach) instead of
  a silent non-deterministic flip; no hard-fail (avoids an outage on an accidental
  2nd workspace).
- F5 (workspace-resolver.ts resolveUserWorkspaceId): exclude soft-deleted users
  and reject a null workspace_id rather than returning null typed as string (no
  bogus PDP anchor).
- F6 (service-auth.guard.ts): rate-limit the unauthenticated (missing/wrong-secret)
  path per client IP (the DoS backstop previously covered only authenticated
  calls); treat a whitespace-only configured secret as unconfigured (503).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@PMQ9
PMQ9 force-pushed the fix/issue-50-272-shadow-session branch from 670c7f7 to c4a89b7 Compare September 13, 2026 04:28
@PMQ9
PMQ9 merged commit 283ad52 into wiki-v2 Sep 13, 2026
@PMQ9
PMQ9 deleted the fix/issue-50-272-shadow-session branch September 16, 2026 12:22
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