Skip to content

MFA rate limit and rate limit tests#882

Open
apedley wants to merge 2 commits intoCodeWithCJ:mainfrom
apedley:update-rate-limits
Open

MFA rate limit and rate limit tests#882
apedley wants to merge 2 commits intoCodeWithCJ:mainfrom
apedley:update-rate-limits

Conversation

@apedley
Copy link
Contributor

@apedley apedley commented Mar 10, 2026

Tip

Help us review and merge your PR faster!
Please ensure you have completed the Checklist below.
For Frontend changes, please run pnpm run validate to check for any errors.
PRs that include tests and clear screenshots are highly preferred!

Description

Added rate limit to mfa factors endpoint /api/auth/mfa-factors
Added integration test to verify new rate limits do what we want

Related Issue

PR type [ ] Issue [ ] New Feature [ ] Documentation
Linked Issue: #

Checklist

Please check all that apply:

  • [MANDATORY for new feature] Alignment: I have raised a GitHub issue and it was reviewed/approved by maintainers
  • Tests: I have included automated tests for my changes.
  • [MANDATORY for UI changes] Screenshots: I have attached "Before" vs "After" screenshots below.
  • [MANDATORY for Frontend changes] Quality: I have run pnpm run validate (especially for Frontend).
  • Translations: I have only updated the English (en) translation file (if applicable).
  • Architecture: My code follows the existing architecture standards.
  • Database Security: I have updated rls_policies.sql for any new user-specific tables.
  • [MANDATORY - ALL] Integrity & License: I certify this is my own work, free of malicious code(phishing, malware, etc.) and I agree to the License terms.

Screenshots (if applicable)

Before

[Insert screenshot/GIF here]

After

[Insert screenshot/GIF here]

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the security posture of the authentication system by introducing robust rate limiting directly within the better-auth framework. The changes focus on protecting sensitive endpoints, particularly the MFA factors endpoint, from brute-force attacks and account enumeration. The refactoring simplifies the rate limiting setup and is thoroughly validated with new integration tests, ensuring the system behaves as expected under various load conditions.

Highlights

  • MFA Endpoint Rate Limiting: Implemented a specific rate limit for the /api/auth/mfa-factors endpoint to prevent account enumeration, allowing a maximum of 5 requests per 30 seconds.
  • Refactored Rate Limiting Implementation: Migrated from the standalone express-rate-limit middleware to the built-in rate limiting capabilities of the better-auth library, centralizing authentication-related rate limit configurations.
  • Integration Tests for Rate Limiting: Added a comprehensive integration test suite to verify the correct functioning of the new better-auth rate limits across various authentication endpoints, including the custom MFA factor limit and default global limits.
  • Dependency Clean-up: Removed the express-rate-limit and ip-address packages, streamlining dependencies and reducing the project's footprint.
Changelog
  • SparkyFitnessServer/SparkyFitnessServer.js
    • Removed the import of express-rate-limit.
    • Removed the authLimiter middleware application from the main server file.
  • SparkyFitnessServer/auth.js
    • Added a rateLimit configuration object to the betterAuth setup.
    • Enabled global rate limiting with a default of 100 requests per 60 seconds.
    • Introduced a custom rate limit rule for /mfa-factors allowing 5 requests per 30 seconds.
  • SparkyFitnessServer/package-lock.json
    • Updated the project version from 0.16.4.5 to 0.16.5.1.
    • Removed express-rate-limit and its dependency ip-address from the lock file.
  • SparkyFitnessServer/package.json
    • Updated the project version from 0.16.4.5 to 0.16.5.1.
    • Modified test scripts to include NODE_OPTIONS='--experimental-vm-modules' for Jest execution.
    • Removed express-rate-limit from direct dependencies.
  • SparkyFitnessServer/pnpm-lock.yaml
    • Removed entries related to express-rate-limit and ip-address.
    • Updated the ipaddr.js dependency version.
  • SparkyFitnessServer/tests/authRateLimit.test.js
    • Added a new integration test file to validate better-auth rate limiting.
    • Included tests for default special rules on /sign-in/email and /sign-up/email.
    • Verified the custom rate limit for /mfa-factors.
    • Confirmed global rate limits for general auth endpoints.
    • Ensured rate limits are tracked independently per IP address.
Activity
  • The author apedley created this pull request to add MFA rate limits and corresponding tests.
  • The author has marked the 'Tests' and 'Architecture' checklist items as complete, indicating that automated tests are included and code follows existing standards.
  • The author has also confirmed that only English translations were updated (if applicable) and certified the integrity and license terms.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces the express-rate-limit middleware with better-auth for rate limiting, intending to apply a stricter limit to the /mfa-factors endpoint. However, a critical vulnerability exists: an interceptor in SparkyFitnessServer.js bypasses the Better Auth handler for the sensitive /api/auth/mfa-factors endpoint. This leaves the MFA factors endpoint unprotected against brute-force attacks, a flaw not caught by the current integration tests in authRateLimit.test.js because they bypass the Express middleware stack. While the tests are comprehensive, I've provided suggestions within authRateLimit.test.js to improve their long-term maintainability.

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.

1 participant