Skip to content

feat(auth): rate limit anonymous session initialization (#312) - #475

Open
ankit-0903 wants to merge 2 commits into
modelence:mainfrom
ankit-0903:fix/session-rate-limiting-312
Open

ankit-0903 wants to merge 2 commits into
modelence:mainfrom
ankit-0903:fix/session-rate-limiting-312

Conversation

@ankit-0903

@ankit-0903 ankit-0903 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #312

Summary

Adds IP-based rate limiting to anonymous session initialization (_system.session.init) and guest user creation, protecting against anonymous session table flooding and token enumeration attacks:

  • Configurable sessionInit rate limits:
    • Added default rules: 100 requests per 15 minutes and 1,000 requests per day per IP.
    • Added auth.rateLimits.sessionInit to AuthRateLimitsConfig for per-app configuration overrides.
  • Anonymous session protection:
    • In _system.session.init, requests consume the sessionInit rate limit when connectionInfo.ip is present and the session has no authenticated user (!contextSession || !contextSession.userId).
    • Authenticated sessions are never throttled during regular application browsing.
    • Removed obsolete TODO: add rate-limiting and captcha handling comment on createSession.
  • Guest user creation:
    • createGuestUser consumes signupAttempt and signup rate limits when IP is provided.
    • Added retry logic on unique handle collisions (11000) before throwing, addressing TODO: re-try on handle collision.

Verification

  • npm run test -- src/auth/session.test.ts src/auth/user.test.ts (66 tests passed)
  • npm run test -- src/auth/ (34 test files / 639 tests passed)
  • npm run lint:check (0 errors, 0 warnings)
  • npm run build (ESM & DTS builds passed)

Note

Medium Risk
Changes authentication session init and guest signup paths with IP-based throttling; misconfiguration or aggressive defaults could block anonymous traffic, but setup and authenticated flows are explicitly exempt.

Overview
Adds per-IP rate limiting for anonymous session initialization and tightens guest account creation.

A new configurable sessionInit bucket (defaults: 100 per 15 minutes and 1,000 per day per IP) is wired into _system.session.init via consumeRateLimit. Limits apply only when the caller has an IP, an existing anonymous session, and setup mode is not active (session null); logged-in users and requests without IP are unchanged.

createGuestUser optionally accepts connectionInfo and, when an IP is present, consumes existing signupAttempt and signup limits. Guest handle creation now retries up to five times on Mongo duplicate-key (11000) before failing. Stale rate-limit TODOs on session/guest paths are removed. Tests cover init throttling edge cases, default/override sessionInit rules, guest rate limits, and collision retries.

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

@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 fe81c8f. Configure here.

Comment thread packages/modelence/src/auth/session.ts Resolved

This branch has not been deployed

No deployments
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.

Security: Add rate limiting to session management and user update routes

1 participant