Skip to content

chores: add rate_limiting - #109

Merged
codebestia merged 3 commits into
codebestia:mainfrom
KodeSage:feat/rate_limiting
Jun 2, 2026
Merged

chores: add rate_limiting#109
codebestia merged 3 commits into
codebestia:mainfrom
KodeSage:feat/rate_limiting

Conversation

@KodeSage

@KodeSage KodeSage commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Description

CLOSE #9

Adds per-IP rate limiting to the unauthenticated auth endpoints to prevent abuse — both endpoints were previously unprotected and could be spammed indefinitely, generating nonces and burning CPU on Stellar signature verification.

Uses express-rate-limit with per-route middleware (not router-level), so authenticated routes are unaffected:

  • POST /auth/challenge10 requests / minute / IP
  • POST /auth/verify5 requests / minute / IP (lower because it runs SHA-256 + Stellar Ed25519 verification + a DB upsert)

Exceeding either limit returns 429 Too Many Requests with a Retry-After header (via standardHeaders: 'draft-7').

Files changed

Acceptance criteria

  • Exceeding the limit returns 429.
  • Legitimate requests within the limit pass through.
  • Rate limiter does not affect authenticated routes.
  • Retry-After header is present on 429 responses.

Follow-ups (not in this PR)

  • Swap the in-process MemoryStore for a Redis-backed store (e.g. rate-limit-redis) using the existing client in apps/backend/src/lib/redis.ts when the backend is scaled horizontally.
  • If the service is placed behind a reverse proxy / load balancer, call app.set('trust proxy', ...) so req.ip is the real client IP.

Type of change

  • Bug fix
  • New feature
  • Documentation update
  • Other

Checklist

  • I have read the contributing guidelines
  • I have tested my changes locally
  • My code follows the project's coding standards

Test run:

cd apps/backend
pnpm exec vitest run src/__tests__/auth.integration.test.ts
# Test Files  1 passed (1)
# Tests       15 passed (15)

New test cases:

  • 10 /auth/challenge requests succeed; 11th returns 429 with Retry-After.
  • 5 /auth/verify requests succeed; 6th returns 429 with Retry-After.
  • Limiters are independent — exhausting /auth/verify does not block /auth/challenge.
  • /me (authenticated) is unaffected — 20 consecutive requests never return 429.
  • /health is unaffected — 20 consecutive requests never return 429.

Note: src/__tests__/conversations.cache.test.ts has 3 failing tests on main that are unrelated to this change.

@drips-wave

drips-wave Bot commented Jun 2, 2026

Copy link
Copy Markdown

@KodeSage Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@codebestia
codebestia merged commit da99b84 into codebestia:main Jun 2, 2026
1 of 2 checks passed
codebestia added a commit that referenced this pull request Jun 26, 2026
@KodeSage
KodeSage deleted the feat/rate_limiting branch July 29, 2026 23:47
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.

[BACKEND] Implement rate limiting on auth endpoints

2 participants