fix: set sane API key rate-limit defaults#616
Conversation
Better Auth apiKey plugin defaults to 10 requests per 24-hour window, which effectively bricks keys for automation tools like n8n. Override with 100 req/min at the plugin level and add a migration to fix existing keys created with the old defaults.
Summary of ChangesHello @asmello, 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 addresses the issue of overly restrictive default rate limits for API keys, which previously made them unsuitable for automation tools. By configuring the Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a much more reasonable rate limit for API keys, changing it from 10 requests/day to 100 requests/minute. This is a great improvement for usability, especially for automation tools. The changes in auth.js correctly configure the new rate limit, and the accompanying SQL migration handles existing keys and sets database defaults. My review includes a couple of suggestions: one to make the rate-limit configuration more flexible by using environment variables, and another to improve the robustness of the SQL migration script to handle NULL values for existing keys.
SparkyFitnessServer/db/migrations/20260208120000_fix_api_key_rate_limit_defaults.sql
Outdated
Show resolved
Hide resolved
…ate_limit_defaults.sql Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Allow overriding the default 100 req/min limit with SPARKY_FITNESS_API_KEY_RATELIMIT_WINDOW_MS and SPARKY_FITNESS_API_KEY_RATELIMIT_MAX_REQUESTS environment variables.
|
@shantnudon Could you review on this. |
|
@asmello Do we want to leave the default as-is and control via the env variable you added? because not sure how many will use N8N and we dont wnat to leave it 10 per 24 hr to 100 per min. Not sure the best practice here. @shantnudon what do you think? |
|
10 per 24h is ultra conservative. I don't think 100 per min will be harmful, especially because this limit only applies to API keys, not IPs. If someone has a valid token, hitting the API faster than 100/min is the least harmful thing they can do. EDIT: that said, open to having a slightly more conservative default than 100/min. |
Add SPARKY_FITNESS_API_KEY_RATELIMIT_WINDOW_MS and SPARKY_FITNESS_API_KEY_RATELIMIT_MAX_REQUESTS to .env.example files, environment variables reference, and rate limiting docs.
Summary
Better Auth's
apiKeyplugin ships with extremely restrictive defaults: 10 requests per 24-hour window. Once exceeded, the key is effectively bricked for the remainder of the day (~86,400sRetry-After). This makes API keys unusable for automation tools like n8n.This PR overrides the defaults to 100 requests per minute — a reasonable limit that supports automation while still providing abuse protection.
rateLimiton theapiKey()plugin constructor inauth.jsTest plan
Validated against a live deployment:
Retry-Afterheader returns 18s (within the 60s window, not the old 86,400s){"error":"Rate limit exceeded."}— clean error