Skip to content

fix(server): resolve trusted client IPs for live queries - #437

Merged
omegascorp merged 3 commits into
modelence:mainfrom
NilotpalK:fix/session-creation-rate-limit-312
Nov 10, 2026
Merged

omegascorp merged 3 commits into
modelence:mainfrom
NilotpalK:fix/session-creation-rate-limit-312

Conversation

@NilotpalK

@NilotpalK NilotpalK commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

I moved the shared IP code into clientIp.ts and switched Socket.IO to proxy-addr on the upgrade request. It uses the same trusted proxy settings as HTTP, and live queries get that address in connectionInfo.ip.

I also moved getCallContext inside the method route's try block and wrapped the OAuth linking routes. Those routes return the HTTP status for known Modelence errors and pass unexpected errors to Express.

Refs #312.

I ran the affected tests and all 149 passed. They cover trusted proxy chains, forged headers and subscription payloads, and auth errors before a method runs. Lint and build passed, and formatting passed with --end-of-line auto on Windows. The full suite had 1,460 passes and the same 10 Windows failures I saw before (authCache, deploySpec, dev, and source).


Note

Medium Risk
Changes trusted-proxy and client-IP behavior for WebSocket live queries and auth endpoints; mistakes could mis-attribute IPs for rate limits or alter error responses on OAuth linking.

Overview
Adds shared client IP resolution via a new clientIp module (using proxy-addr) so HTTP and Socket.IO apply the same trusted-proxy rules—config, MODELENCE_TRUSTED_PROXIES, and optional headers like cf-connecting-ip only when the socket peer is trusted.

Socket.IO live queries now take the client address from the WebSocket upgrade request (not spoofable subscription payloads or handshake.address), so connectionInfo.ip matches HTTP for rate limits and auditing behind reverse proxies.

Auth error handling is tightened: handleAuthRequest on OAuth link cookie/nonce routes returns ModelenceError statuses directly; unexpected errors go to Express next. The internal method route returns 401 when session lookup fails before the method runs, instead of treating it as a 500.

Adds proxy-addr and tests for proxy chains, forged headers, and auth failures.

Reviewed by Cursor Bugbot for commit 0eca775. Bugbot is set up for automated code reviews on this repo. Configure here.

@NilotpalK
NilotpalK requested a review from omegascorp as a code owner October 28, 2026 09:24

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 2a29284. Configure here.

Comment thread packages/modelence/src/auth/index.ts Resolved
@omegascorp

Copy link
Copy Markdown
Collaborator

Thanks for digging into this, and for the thorough tests. You're right that anonymous session creation can be abused. While reviewing, we traced it to the cause: obtainSession persists a new session for every missing or unknown token, but only _system.session.init (over HTTP or in-process during SSR) ever returns that token to the client. Method calls, custom routes, Socket.IO handshakes and live-query subscriptions all write sessions nobody can use. We'd rather stop writing those records than rate-limit them, so we'll fix that in core.

Because of that, we don't want to merge the per-IP limiter. As it stands it would also hurt legitimate traffic:

  • If _system.session.init returns 429, initSession() throws and AppProvider stays on the loading screen. 100 new sessions per 15 minutes per IP is easy to reach behind school, office or carrier NAT.
  • Every SSR page load without a cookie creates a session, so a crawler fetching more than about 7 pages a minute from one IP would get the static shell instead of rendered HTML.
  • Public custom routes would return 429 for requests carrying a stale token, where auth used to be optional.

Could you narrow this PR to the parts that stand on their own? We'd be glad to merge:

  • the clientIp.ts extraction,
  • trusted-proxy IP resolution for Socket.IO handshakes and live-query subscriptions (the connectionInfo.ip fix),
  • moving getCallContext inside the try in the method route, and the handleAuthRequest wrapper.

Please drop the sessionCreation bucket and its docs, the consumeRateLimit call in obtainSession, the ip parameter on authenticate / obtainSession, and the 429 handling in routes/handler.ts.

One optional suggestion: resolving the Socket.IO client IP without building a fake Express request (for example, calling proxy-addr with the compiled trust function) would tie it less to Express internals. Not a blocker.

Thanks again, this was a useful find.

@NilotpalK NilotpalK changed the title fix(auth): rate limit anonymous session creation per IP fix(server): resolve trusted client IPs for live queries Nov 10, 2026
@NilotpalK

NilotpalK commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks, Hayk. I narrowed the PR to the proxy IP and HTTP error-handling changes. Socket.IO now reads the IP directly from the upgrade request, and the tests check the address that live-query methods receive.

@omegascorp omegascorp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, looks good

@omegascorp
omegascorp merged commit ed29a34 into modelence:main Nov 10, 2026
4 checks passed
omegascorp added a commit that referenced this pull request Nov 12, 2026
The live-query client IP test from #437 asserted authenticate calls without
options. This branch passes persistNewSession: false on the Socket.IO
handshake and live-query subscriptions, so assert that.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants