Skip to content

feat(authz): per-request /api central audit + per-principal rate limit (#467) - #50

Merged
PMQ9 merged 3 commits into
wiki-v2from
feat/467-api-audit-per-principal-limit
Sep 22, 2026
Merged

PMQ9 merged 3 commits into
wiki-v2from
feat/467-api-audit-per-principal-limit

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 22, 2026

Copy link
Copy Markdown

TL;DR

CCC authorization integration (fork-owned, apps/server/src/authz/): two global APP_INTERCEPTORs for authenticated Docmost /api traffic — a uniform per-request central-audit row and a per-principal rate limit — plus the shared docmost#320 clientEvidence helper. Delivers wiki-v2 docmost#467 (invariant #3) in-process; the sink + edge changes live in the paired cccvu/wiki-v2 PR.

What changed

New fork-owned dir apps/server/src/authz/request-controls/:

  • api-access-audit.interceptor.ts + api-access-audit.service.ts — one eventCategory:'access' row per authenticated /api request, forwarded fire-and-forget through the existing PlatformAuditClient seam. Rows are Cannot upload Image in webui in localhost docmost/docmost#320-clean (shared authz/audit/request-evidence.ts), carry a priority hint (reads low, mutations normal), and never include the body / Authorization. No-op in native mode.
  • principal-rate-limit.interceptor.ts — per-principal limit keyed on the VERIFIED req.user.user.id in a SINGLE bucket (principal:<id>, not the stock per-(class,handler) key a cross-endpoint scraper would evade). Default 200/10s, block 10s; fails OPEN on Redis outage; enforce|count|off.

Why interceptors, not guards: the two global APP_GUARDs run before the controller-scoped JwtAuthGuard, so req.user is unset at global-guard time (a guard could only key on the unverified cookie — a documented bypass). Interceptors run after all guards, so req.user is the verified principal.

Seam #4 (app.module.ts) registers both as global APP_INTERCEPTORs in a load-bearing order — AuditActorInterceptor → ApiAccessAuditInterceptor → PrincipalRateLimitInterceptor — so the audit interceptor WRAPS the rate limiter and a 429 is still audited, and the limiter is innermost so it rejects before the handler. Also exports PlatformAuditClient from PlatformAuditModule and extracts the shared docmost#320 evidence builder. UPSTREAM_MODIFICATIONS.md seam #4 updated (7 changes; interceptor order pinned).

Testing

principal-rate-limit.interceptor.spec.ts (keying / single-bucket-across-endpoints / distinct principals / skip @public+no-principal / fail-open reject+hang / count mode / 429+Retry-After), api-access-audit.interceptor.spec.ts (one docmost#320-clean row / priority / skip / 429 audited / fire-and-forget / native no-op), request-controls.wiring.spec.ts (registration + order source pin + the audited-AND-limited composition proof). Existing platform-audit specs stay green (the evidence refactor is behavior-preserving). scripts/check-upstream-boundary.sh exit 0; lint + typecheck clean.

Notes

Tracks: wiki-v2 docmost#467.

🤖 Generated with Claude Code

docmost#467)

Deliver invariant #3 of docmost#467 (successor to #27 / ADR 0022) in-process, with no
reverse proxy and no new AGPL surface:

- ApiAccessAuditInterceptor + ApiAccessAuditService (authz/request-controls/):
  one uniform per-request `access` audit row for every authenticated Docmost /api
  request, forwarded fire-and-forget through the existing PlatformAuditClient seam.
  Rows are docmost#320-clean (clientEvidence via the shared request-evidence helper) and
  carry a `priority` hint ('low' for reads) so the sink sheds reads before
  security-relevant mutations under overload.
- PrincipalRateLimitInterceptor (authz/request-controls/): a per-principal rate
  limit for authenticated /api traffic, keyed on the VERIFIED req.user.user.id in a
  SINGLE bucket per principal (not the stock per-handler key a cross-endpoint scraper
  would evade). Interceptor, not guard, because the controller-scoped JwtAuthGuard
  runs after the global guards; fails OPEN on Redis outage; mode enforce|count|off.
- Both are global APP_INTERCEPTORs registered in seam #4 in a load-bearing order
  (AuditActor -> ApiAccessAudit -> PrincipalRateLimit): the audit interceptor wraps
  the rate limiter so a 429 is still audited; the limiter is innermost so it rejects
  before the handler. Pinned by request-controls.wiring.spec.ts.
- Export PlatformAuditClient from PlatformAuditModule; extract the shared docmost#320
  clientEvidence builder to request-evidence.ts.

Tests: rate-limit (keying/skip/fail-open/count/429), access-audit (shape/clientEvidence/
429-audited/native no-op), wiring+order, and the audited-AND-limited composition proof.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@PMQ9

PMQ9 commented Sep 22, 2026

Copy link
Copy Markdown
Author

Paired super-repo PR: cccvu/wiki-v2#481 (the audit-sink delivery contract, monitoring.tf alarms, the AC1 /api/health deny + SSOT + check-infra §21, and the gitlink bump). Merge this fork PR first, then re-pin the wiki-v2 gitlink to this PR's merge commit.

PMQ9 and others added 2 commits September 22, 2026 10:19
Keeps the docmost#455 fork changes (notification-socket force-close, service-bridge
session revoke) alongside the docmost#467 request-controls interceptors. No file
overlap; clean merge. Re-pins the super gitlink target.
…w round 1)

DevOps review (L3): forward() used undici's timeout-less global fetch, and docmost#467
newly routes a PER-REQUEST /api access stream through it — so a slow-not-down
platform sink under /api load could accumulate unbounded in-flight forwards on
the fork container. Add AbortSignal.timeout (env PLATFORM_AUDIT_FORWARD_TIMEOUT_MS,
default 2000ms); an abort surfaces as a transport error in the existing catch
(logged AUDIT_FORWARD_FAILED, row dropped loudly) exactly like any other failure,
so loss stays alarmed and the request path is untouched.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@PMQ9
PMQ9 merged commit 26c85b1 into wiki-v2 Sep 22, 2026
@PMQ9
PMQ9 deleted the feat/467-api-audit-per-principal-limit branch September 22, 2026 16:28
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