[GH-ISSUE #2482] Rate limits not applied when api-key sessions are enabled #17848

Closed
opened 2026-04-15 16:11:41 -05:00 by GiteaMirror · 8 comments
Owner

Originally created by @ntgussoni on GitHub (Apr 30, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2482

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I have api-keys setup and sessions enabled. I was testing making post requests passing the api key and everything works correctly.

Then I enabled rateLimits, and realized that the values in the DB were not being updated/reflected. If I disabled the api key, the request would still go through.

I did some digging and found that there seems to be a mismatch, which I don't know if im misinterpreting, but the implementation here: https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/api-key/index.ts#L124 doesn't match the one of verify-api-key https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts which correctly handles rate limits.

Am I wrong to assume this should be the case? Should I manually call verify, then create the session myself?

Current vs. Expected behavior

I would expect all the rate limits checks to be a precondition to the session being created just like when verify is called.

What version of Better Auth are you using?

1.2.7

Provide environment information

OS: MacOS
Browser: N/A

Which area(s) are affected? (Select all that apply)

Backend, Package

Auth config (if applicable)

plugins: [
    admin(),
    apiKey(),
    magicLink({
      sendMagicLink,
    }),
    nextCookies(),
  ],
});

Additional context

No response

Originally created by @ntgussoni on GitHub (Apr 30, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2482 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce I have api-keys setup and sessions enabled. I was testing making post requests passing the api key and everything works correctly. Then I enabled rateLimits, and realized that the values in the DB were not being updated/reflected. If I disabled the api key, the request would still go through. I did some digging and found that there seems to be a mismatch, which I don't know if im misinterpreting, but the implementation here: https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/api-key/index.ts#L124 doesn't match the one of verify-api-key https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts which correctly handles rate limits. Am I wrong to assume this should be the case? Should I manually call verify, then create the session myself? ### Current vs. Expected behavior I would expect all the rate limits checks to be a precondition to the session being created just like when verify is called. ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash OS: MacOS Browser: N/A ``` ### Which area(s) are affected? (Select all that apply) Backend, Package ### Auth config (if applicable) ```typescript plugins: [ admin(), apiKey(), magicLink({ sendMagicLink, }), nextCookies(), ], }); ``` ### Additional context _No response_
GiteaMirror added the locked label 2026-04-15 16:11:41 -05:00
Author
Owner

@ntgussoni commented on GitHub (May 1, 2025):

@Bekacru needless to say, if you confirm this is a bug I'll make a PR myself

<!-- gh-comment-id:2844452084 --> @ntgussoni commented on GitHub (May 1, 2025): @Bekacru needless to say, if you confirm this is a bug I'll make a PR myself
Author
Owner

@ping-maxwell commented on GitHub (May 1, 2025):

Hey @ntgussoni I developed the API key plugin, good catch on this.
It would be great if you can open a PR 🙏

<!-- gh-comment-id:2845055474 --> @ping-maxwell commented on GitHub (May 1, 2025): Hey @ntgussoni I developed the API key plugin, good catch on this. It would be great if you can open a PR 🙏
Author
Owner

@ntgussoni commented on GitHub (May 1, 2025):

Will do @ping-maxwell . My daughter is hours away from being born, so I don't promise anything but I'll try to fix it as soon as I can

<!-- gh-comment-id:2845069843 --> @ntgussoni commented on GitHub (May 1, 2025): Will do @ping-maxwell . My daughter is hours away from being born, so I don't promise anything but I'll try to fix it as soon as I can
Author
Owner

@ntgussoni commented on GitHub (May 2, 2025):

@ping-maxwell I made a PR with this fix for this, let me know if you have comments

#2514

<!-- gh-comment-id:2847089624 --> @ntgussoni commented on GitHub (May 2, 2025): @ping-maxwell I made a PR with this fix for this, let me know if you have comments #2514
Author
Owner

@ping-maxwell commented on GitHub (May 2, 2025):

Will do @ping-maxwell . My daughter is hours away from being born, so I don't promise anything but I'll try to fix it as soon as I can

Congrats on the big news!

<!-- gh-comment-id:2847652411 --> @ping-maxwell commented on GitHub (May 2, 2025): > Will do [@ping-maxwell](https://github.com/ping-maxwell) . My daughter is hours away from being born, so I don't promise anything but I'll try to fix it as soon as I can Congrats on the big news!
Author
Owner

@ntgussoni commented on GitHub (May 5, 2025):

Will do @ping-maxwell . My daughter is hours away from being born, so I don't promise anything but I'll try to fix it as soon as I can

Congrats on the big news!

OK, baby is out. Let's get this out too :)

<!-- gh-comment-id:2850111644 --> @ntgussoni commented on GitHub (May 5, 2025): > > Will do [@ping-maxwell](https://github.com/ping-maxwell) . My daughter is hours away from being born, so I don't promise anything but I'll try to fix it as soon as I can > > Congrats on the big news! OK, baby is out. Let's get this out too :)
Author
Owner

@ntgussoni commented on GitHub (May 10, 2025):

#2514 @ping-maxwell @Bekacru I updated the PR since it had recent conflicts. Let me know if there's anything else I can do, or if you guys need help checking other PRs. I see this is exploding (in the good sense)

<!-- gh-comment-id:2868724534 --> @ntgussoni commented on GitHub (May 10, 2025): #2514 @ping-maxwell @Bekacru I updated the PR since it had recent conflicts. Let me know if there's anything else I can do, or if you guys need help checking other PRs. I see this is exploding (in the good sense)
Author
Owner

@ntgussoni commented on GitHub (May 23, 2025):

@ping-maxwell @Bekacru hey guys! I know you are awfully busy (let me know if I can help). Do you think we can get this PR merged if you see it's fine?

I'd love to get this out of the way, otherwise i'll just use my work for prod in the meantime

<!-- gh-comment-id:2903986839 --> @ntgussoni commented on GitHub (May 23, 2025): @ping-maxwell @Bekacru hey guys! I know you are awfully busy (let me know if I can help). Do you think we can get this PR merged if you see it's fine? I'd love to get this out of the way, otherwise i'll just use my work for prod in the meantime
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#17848