Repository navigation
fix(service-bridge): record the platform-resolved client IP on minted sessions (wiki-v2 #330) - #48
Merged
Merged
Conversation
… 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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.createSessionAndTokenwritesuser_sessions.ip_addressfrom the CLS audit context, whoseipAddressis Docmost'sreq.ip. UndertrustProxy: truethat is the forgeable leftmostX-Forwarded-Fortoken — and on the loopbackPOST /api/service/sessionmint (the only session-mint path reachable in the remote deployment; native auth is 404'd byNativeAuthModeGuard) it is simply the relay peer127.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.ipbehind 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— optionalclientIp, a length-bounded string. Deliberately not@IsIP: minting a session is a sign-in, and a malformed value must degrade toNULL, never400the login.service-bridge.service.ts— beforecreateSessionAndToken, always overwrite the CLS audit-contextipAddress: to a syntactically valid, non-loopback/unspecified address, elseNULL. "Always" because the middleware already set it to127.0.0.1; the syntacticnet.isIPcheck is load-bearing because the column is Postgresinet(a non-address would500the INSERT). MirrorsPlatformAuditService.setActorId's get→mutate→set.docs/integrations/authorization/service-bridge.openapi.json+ contract spec — declare the optional field (additive;additionalProperties: falsepreserved) 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.1on 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/**anddocs/**(fork-owned).core/session/session.service.tsandcommon/middlewares/audit-context.middleware.tsstay byte-identical to v0.95.0. The CLS overwrite reuses the existingAuditContext/AUDIT_CONTEXT_KEYthatPlatformAuditServicealready reads.check-upstream-boundary.shand 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(never127.0.0.1, never a non-address toinet); the stored value derives only from the passedclientIp, never Docmost'sreq.ip; set beforecreateSessionAndTokenruns.dto-validation.spec.ts—clientIpoptional, non-IP tolerated at the DTO, length-bounded at 45.service-bridge.contract.spec.ts— the request schema declaresclientIpand the example exercises it.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