Skip to content

fix(#502): refuse the native space hard delete in remote mode - #58

Merged
PMQ9 merged 2 commits into
wiki-v2from
fix/502-remote-space-hard-delete
Sep 25, 2026
Merged

PMQ9 merged 2 commits into
wiki-v2from
fix/502-remote-space-hard-delete

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 25, 2026

Copy link
Copy Markdown

TL;DR

This is the fork half of the fix for cccvu/wiki-v2 issue 502. In AUTHZ_MODE=remote, the engine's native space delete (POST /api/spaces/delete, an unconditional DELETE FROM spaces) now answers 404 for every caller and the refusal is audited, so archive is the only way to remove a space. Native (standalone) mode is unchanged.

Context

  • What the route does. It runs SpaceService.deleteSpace → SpaceRepo.deleteSpace, a DELETE FROM spaces.
    • The delete cascades through 12 foreign keys: pages, page history, comments, page grants, shares and more.
    • It also queues the S3 purge of every attachment in the space.
    • None of it can be undone.
  • Its only gate is CASL Manage Settings. In remote mode that comes from PDP space#administer, which on a live space includes a delegated manage_members binding holder. On an archived space the direct admin keeps it: the + admin branch sits outside - archived (pdp/schema.zed:138).
  • Why that matters. Either of those principals could permanently destroy a space from the browser. That bypasses the platform's governed model (cccvu/wiki-v2 issue 193): archive, with an acting human, an audit row and a settle, and then a restore that only a workspace admin can do. Once the row is gone there is nothing to restore.
  • Audit gap. The only trace was a space.deleted row written after the delete, and a refused attempt left nothing.

Related issues

  • Refs cccvu/wiki-v2 issue 502. The paired super PR closes it.
  • Follow-ups in cccvu/wiki-v2:
    • issue 588: a governed permanent purge, since remote mode now has none;
    • issue 589: direct admins can still purge the pages inside an archived space;
    • issue 590: post-release prod check.

What changed

No controller, service or repo is touched. The only upstream-owned edits are:

  • the composition registration in app.module.ts (seam fix(authz): harden PLATFORM_AUTHZ_TIMEOUT_MS parse (empty/typo → deny-all self-DoS) #4);

  • the space-settings row in space-details.tsx (seam feat: user should have option to define maximum file size in environment variables docmost/docmost#170), plus its new test.

  • authz/space-delete/space-hard-delete.interceptor.ts (new, CCC-owned): SpaceHardDeleteInterceptor.

    • It is keyed Controller.handler against SPACE_HARD_DELETE_ROUTES = {'SpaceController.deleteSpace'}, the same pattern as NarrowingSettleInterceptor.
    • In remote mode on that key it returns throwError(() => new NotFoundException()) before the handler, so CASL, ValidationPipe and the delete never run.
    • It forwards one space.hard_delete_refused domain row through PlatformAuditClient directly, so the API_AUDIT_ENABLED kill switch cannot drop it.
    • The row carries the actor (req.user.user.id), the workspace, the attempted space id only if it is a UUID (as resourceId, never the spaceId column), client evidence and user agent, plus metadata{outcome:'denied', reason:'archive_only', route}.
    • The body is read null-safely, because the interceptor runs before the pipe. A failing audit forward never changes the 404.
    • Outside remote mode, on any other route, or in a non-HTTP context, it passes through.
  • app.module.ts (seam fix(authz): harden PLATFORM_AUTHZ_TIMEOUT_MS parse (empty/typo → deny-all self-DoS) #4): registered as an APP_INTERCEPTOR right after PrincipalRateLimitInterceptor, which makes it innermost.

    • A flood of attempts is rate-limited, and each attempt lands in the Docmost vs. Outline docmost/docmost#467 api.post access row as a 404.
    • It is deliberately not registered in AuthzModeModule or AuthzModule. CollabAppModule loads those without PlatformAuditModule, so the collab process would fail to boot.
  • authz/route-guard/static-route-scan.ts: adds a callsSpaceHardDelete handler-body tell, next to mintsNativeSession.

  • space-details.tsx (seam feat: user should have option to define maximum file size in environment variables docmost/docmost#170): in remote mode the "Delete space" row shows "Spaces can't be permanently deleted here. A workspace admin can archive this space instead; archived spaces can be restored." instead of the Delete button.

    • An "Open the Admin Console" link appears only for a platform workspace admin (usePlatformAdminContext). That reuses the header's cached query, so no extra request is made.
    • Native mode renders the upstream markup unchanged.
  • Why an interceptor and not a guard:

    • AGENTS.md forbids controller edits.
    • A guard's denial skips both the Docmost vs. Outline docmost/docmost#467 access row and the per-principal rate limit.
    • An interceptor still sees the resolved req.user.

How this was tested

  • space-hard-delete.interceptor.spec.ts (17 tests):
    • remote: 404 and the exact audit row;
    • slug, object, oversized, missing, null, array and string bodies all 404, and none is echoed into the row;
    • with no req.user it still 404s and writes no row;
    • an audit forward that rejects or throws does not change the outcome;
    • another handler, a same-named handler on another controller, a non-HTTP context, and native mode all pass through with no row.
  • space-hard-delete-http.spec.ts: boots the real SpaceController on Fastify, with the interceptor registered as APP_INTERCEPTOR and main.ts's ValidationPipe.
    • Remote: a valid id, {} and null all return 404; CASL and deleteSpace are never called; exactly one row is forwarded.
    • Native: {} returns 400, and an admin's valid request reaches CASL and the delete.
  • space-hard-delete.fitness.spec.ts (static):
    • The refused set equals the routes whose handler reaches .deleteSpace(, checked in both directions.
    • DELETE FROM spaces (Kysely or raw SQL) appears only in space.repo.ts.
    • .deleteSpace( callers are only the controller and the service.
    • The attachment-purge producer is only the service.
    • The registration order in app.module.ts is pinned.
    • A meta-guard pins the patterns' shapes.
  • Anti-vacuity: with the route check removed, 16 of the new tests go red.
  • space-details.test.tsx:
    • remote, admin: no Delete button, note and console link shown;
    • remote, non-admin: no link;
    • native: Delete button present;
    • readOnly: no row.
    • This file fails against the original upstream file.
  • Suites:
    • pnpm exec jest src/authz src/service-bridge src/editor-compat: 98 suites, 1057 passed, 0 failed.
    • pnpm --filter client test: 50 files, 369 passed.
    • Server tsc --noEmit -p tsconfig.build.json: clean. ESLint on the changed server files: clean.
  • End to end: run on the local compose stack in the super repo. The super PR has the details: the full content smoke with the new §10f, a browser check of the archived space, and the audit rows.

AWS / infra apply

N/A. Code only. No migration, config, secret or Terraform change.

Checklist

Scope

  • Complete and scoped to one logical concern — no unrelated drive-by changes (or follow-ups noted below).

Tests

  • New/changed behavior has appropriate tests — unit / integration / e2e / security / regression as applicable (or N/A, with why).
  • Existing tests pass, and any new tests pass.

Docs & contracts

  • Docs added/updated where needed (README / docs/ / an ADR), or N/A.
  • Any API/contract, config, migration, permission, or deployment impact is called out in this PR, or N/A.
  • Any AWS/infra apply is described precisely, scoped (no mass-apply), and captured in a reviewable one-time script — see "AWS / infra apply" above, or N/A.

Safety (this is a security-first, deny-by-default platform)

  • Security, authorization, data integrity, failure paths, and backward compatibility considered, or N/A.
  • Upstream boundary respected — no undocumented edits to Docmost-owned files (see UPSTREAM_MODIFICATIONS.md); proprietary/authz logic stays out-of-process.

Housekeeping

  • Related issue(s) linked above.
  • Known limitations, follow-ups, or intentionally deferred work identified below.

Known limitations / follow-ups

  • No remote purge. Remote mode has no way to permanently delete a space. The owner used this route to purge scratch spaces on 2026-09-07. A governed purge is cccvu/wiki-v2 issue 588.
  • Pages in archived spaces. Direct admins can still permanently delete pages inside an archived space, either natively or through /v1 ?permanent=true, or trash them and let retention purge them. This is cccvu/wiki-v2 issue 589.
  • Limits of the fitness spec. It is a text inventory. A sink reached through an unrecognized indirection would be invisible to it, and its meta-guard pins the shapes it does recognize.

🤖 Generated with Claude Code

PMQ9 and others added 2 commits September 25, 2026 01:23
POST /api/spaces/delete ran an unconditional DELETE FROM spaces (every page,
version, comment, grant and attachment) behind only CASL Manage Settings,
i.e. PDP space#administer — which a delegated manage_members holder and the
direct admin of an ARCHIVED space both hold. It bypassed the governed model
(archive → workspace-admin restore, docmost#193).

- authz/space-delete: SpaceHardDeleteInterceptor 404s SpaceController.deleteSpace
  for every caller in AUTHZ_MODE=remote, before CASL, ValidationPipe and the
  handler, and forwards one `space.hard_delete_refused` audit row through
  PlatformAuditClient (not subject to the API_AUDIT_ENABLED kill switch).
  Native mode is inert.
- app.module.ts (seam #4): registered innermost, after the per-principal rate
  limiter, so refusals are rate-limited and land in the docmost#467 access row. Not in
  AuthzModeModule (CollabAppModule has no PlatformAuditModule).
- static-route-scan: callsSpaceHardDelete handler tell; fitness spec pins the
  refused set to the routes that reach .deleteSpace( (both directions) and the
  spaces delete sink / attachment-purge producers to their current callers.
- client (seams 170/171): in remote mode space settings show an archive-only
  note instead of the Delete button, with a console link for workspace admins.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…e (review round 1)

Security (L3): the fitness inventory matched FILES, so an upstream
SpaceService.bulkDelete looping spaceRepo.deleteSpace — plus a new route
calling it — stayed green while that route went unrefused.

- space-hard-delete.fitness.spec: a TS-AST scanner attributes every sink
  occurrence to its enclosing Class.method and the spec pins the exact
  list (so a new method, or a second call, reds). Comments and string
  contents never count, which also drops the regex comment stripper that
  misread `'image/*'`.
- Adds the workspace delete as a sink (spaces.workspace_id cascades), pinned
  to none — invariant I25 now holds literally.
- Meta-guard: the bulkDelete scenario, per-occurrence counting, comments,
  every Kysely/raw-SQL shape, the watcher unwatch.
- "innermost" → "registered after the rate limiter" (imported modules'
  interceptors run inside it) in app.module.ts and the interceptor doc;
  the docmost#467 wiring spec comment likewise.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@PMQ9
PMQ9 merged commit 6858af6 into wiki-v2 Sep 25, 2026
@PMQ9
PMQ9 deleted the fix/502-remote-space-hard-delete branch September 25, 2026 14:42
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