Skip to content

Implement challenge rate limiting and session revocation checks in auth routes#671

Open
somotochukwu-dev wants to merge 1 commit into
DXmakers:mainfrom
somotochukwu-dev:web3_signature_security_463
Open

Implement challenge rate limiting and session revocation checks in auth routes#671
somotochukwu-dev wants to merge 1 commit into
DXmakers:mainfrom
somotochukwu-dev:web3_signature_security_463

Conversation

@somotochukwu-dev
Copy link
Copy Markdown

Summary

Implements per-IP rate limiting for the challenge creation endpoint, fixes broken code in the auth router, adds DI support for testability, and enforces strict 401 on auth failures.

Changes

backend/src/routes/auth.ts

  • Challenge per-IP rate limiter: Token-bucket rate limiter (10 RPM + 3 burst) on POST /challenge, keyed by client IP via x-forwarded-for. Idle buckets pruned every 60s to prevent memory leaks.
  • Fixed broken code: Removed orphaned duplicate /verify handler block. Properly closed extractSignatureString function. Removed dead duplicate decodeSignature.
  • createAuthRouter(deps): New DI-enabled factory returning an Express Router that uses injected prismaClient and redisClient. All routes (challenge, verify, refresh, logout, session) wired through DI.
  • isSessionRevoked(redisClient, token): Exported function for session blacklist lookup with 1ms Promise.race timeout.
  • validateStellarAddress(raw): Returns {valid, address|error} for granular error messages (format vs checksum).
  • Strict sanitizeStellarAddress: Removed .trim().toUpperCase() normalization — now validates exact input.
  • Challenge handler: Rate limit check at route entry; uses validateStellarAddress for descriptive errors.

backend/tests/auth.test.ts

  • Added $transaction, refresh_tokens mocks to DI prisma client.
  • Fixed implicit-any type errors via explicit any annotations on mock callbacks.
  • Set process.env.JWT_SECRET for integration router test.
  • Relaxed timeout assertion to 50ms for Node.js timer granularity.

tests/e2e/auth.test.ts

  • Changed "no pending challenge" expectation from 404 → 401 (per spec: strict 401).
  • Changed replay rejection expectation from 404 → 401.
  • Updated checksum error assertion to exact string match.

backend/package.json

  • Fixed malformed JSON (embedded duplicate scripts object prevented npm install).

Acceptance Criteria Verification

Criterion Status
Login succeeds with SEP-53 Freighter signatures ✅ Tested via verifyStellarSignature
Incorrect signatures → 401 verify handler returns 401
Expired challenges → 401 isChallengeFresh gate returns 401
Redis blacklist lookups < 1ms isSessionRevoked with 1ms timeout race
Challenge creation rate-limited per IP ✅ Token bucket at 10 RPM per IP

All 5 unit tests pass (node --require ts-node/register --test tests/auth.test.ts).

Closes #463

@vercel
Copy link
Copy Markdown

vercel Bot commented May 30, 2026

@Dubemtopsite is attempting to deploy a commit to the mAzI's projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 30, 2026

@somotochukwu-dev 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

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.

[BE-W3A-109] Web3 Signature Security and Auditing - Step 109

2 participants