Skip to content

fix(service-bridge): record the platform-resolved client IP on minted sessions (wiki-v2 #330) - #48

Merged
PMQ9 merged 2 commits into
wiki-v2from
fix/330-session-client-ip
Sep 22, 2026
Merged

PMQ9 merged 2 commits into
wiki-v2from
fix/330-session-client-ip

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 22, 2026

Copy link
Copy Markdown

TL;DR

Record the platform-resolved client IP on Docmost sessions minted through the east-west service bridge, instead of the loopback relay peer. Fixes the fork half of wiki-v2 issue 330.

What this does

SessionService.createSessionAndToken writes user_sessions.ip_address from the CLS audit context, whose ipAddress is Docmost's req.ip. Under trustProxy: true that is the forgeable leftmost X-Forwarded-For token — and on the loopback POST /api/service/session mint (the only session-mint path reachable in the remote deployment; native auth is 404'd by NativeAuthModeGuard) it is simply the relay peer 127.0.0.1. So every session recorded a confidently-wrong loopback address.

The real client IP is not present on this hop — only the platform (which resolves its own req.ip behind the edge) knows it. So the platform now passes it on the mint call and the fork writes it into the session:

  • dto/mint-session.dto.ts — optional clientIp, a length-bounded string. Deliberately not @IsIP: minting a session is a sign-in, and a malformed value must degrade to NULL, never 400 the login.
  • service-bridge.service.ts — before createSessionAndToken, always overwrite the CLS audit-context ipAddress: to a syntactically valid, non-loopback/unspecified address, else NULL. "Always" because the middleware already set it to 127.0.0.1; the syntactic net.isIP check is load-bearing because the column is Postgres inet (a non-address would 500 the INSERT). Mirrors PlatformAuditService.setActorId's get→mutate→set.
  • docs/integrations/authorization/service-bridge.openapi.json + contract spec — declare the optional field (additive; additionalProperties: false preserved) and exercise it in the example.

Why

Session IPs are the forensic record of where a session was created. A confidently-wrong 127.0.0.1 on every session is worse than a blank one. Same root cause as wiki-v2 issue 320 (audit rows), fixed for the session sink here.

Boundary

No upstream-owned file is touched — all changes are under service-bridge/** and docs/** (fork-owned). core/session/session.service.ts and common/middlewares/audit-context.middleware.ts stay byte-identical to v0.95.0. The CLS overwrite reuses the existing AuditContext/AUDIT_CONTEXT_KEY that PlatformAuditService already reads. check-upstream-boundary.sh and the import-boundary spec are green.

Testing

  • service-bridge.service.spec.ts — valid IPv4/IPv6 stored; junk / absent / loopback (127.0.0.0/8, ::1, ::ffff:127.*) / unspecified → NULL (never 127.0.0.1, never a non-address to inet); the stored value derives only from the passed clientIp, never Docmost's req.ip; set before createSessionAndToken runs.
  • dto-validation.spec.ts — clientIp optional, non-IP tolerated at the DTO, length-bounded at 45.
  • service-bridge.contract.spec.ts — the request schema declares clientIp and the example exercises it.
  • Full fork jest suite + check-upstream-boundary.sh + import-boundary spec green; production build (tsconfig.build.json) clean.

Merge order

Merge this before the wiki-v2 submodule repoint + platform PR (which sends clientIp). Same ECS task / same-digest promotion means no version skew; the fork-first order just avoids a window where the platform sends a field the fork silently strips.

🤖 Generated with Claude Code

PMQ9 and others added 2 commits September 22, 2026 00:01
… sessions (wiki-v2 docmost#330)

Docmost boots `trustProxy: true`, so its middleware-time `req.ip` is the forgeable
leftmost `X-Forwarded-For` token. On the loopback service-bridge mint — the only
session-mint path reachable in the remote/prod deployment (native auth is 404'd by
NativeAuthModeGuard) — that value is just the relay peer `127.0.0.1`, so
`user_sessions.ip_address` recorded a confidently-wrong loopback value for every
session (wiki-v2 issue 330; same root cause as the audit fix in wiki-v2 issue 320).

The client IP is not present on this east-west hop, so the fork cannot recover it.
The platform already resolves the true client address (its own trusted-proxy
predicate) and now passes it on the mint call. The fork validates it and writes it
into the session:

- dto/mint-session.dto.ts: optional `clientIp`, a bounded string — deliberately NOT
  `@IsIP` (a malformed value must degrade to NULL, never 400 a sign-in).
- service-bridge.service.ts: before createSessionAndToken, ALWAYS overwrite the CLS
  audit-context `ipAddress` — to a syntactically valid, non-loopback/unspecified
  address, else NULL. "Always" because the middleware already set it to 127.0.0.1;
  the syntactic check is load-bearing (the column is Postgres `inet`, a non-address
  500s the INSERT). Mirrors PlatformAuditService.setActorId's get->mutate->set; no
  upstream file is touched.
- service-bridge.openapi.json + contract spec: declare the optional field (additive,
  additionalProperties:false preserved) and exercise it in the example.

Merge before the wiki-v2 submodule repoint.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…eview round 1)

Addresses the 7-lens review of the client-IP session fix (wiki-v2 docmost#330):

- trustedClientIp now rejects a value containing `%` (a scoped/zoned IPv6 like
  `fe80::1%eth0` passes net.isIP → 6 but Postgres `inet` REJECTS the %zone suffix,
  so it would 500 the session INSERT — contradicting the stated "isIP prevents a
  non-address 500" invariant; a zoned/link-local address is never a real remote
  client anyway → null). [Security L2]
- service-bridge.controller.spec.ts (new): pins that the controller forwards
  dto.clientIp into the service — dropping it would silently record NULL and leave
  the rest of the suite green. [Testing L3]
- dto-validation.spec.ts: at-limit VALID case — the 45-char max IPv6 text passes,
  pinning @maxlength(45) so it can't be tightened into 400-ing long-IPv6 sign-ins.
  [Testing L2]
- service-bridge.service.spec.ts: junk→NULL sweep now covers the zoned-IPv6, the
  IPv4-mapped loopback range, and uppercase spellings. [Testing L1]

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@PMQ9
PMQ9 merged commit c27be85 into wiki-v2 Sep 22, 2026
@PMQ9
PMQ9 deleted the fix/330-session-client-ip branch September 22, 2026 05:52
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