[PR #6259] [MERGED] fix(api-key): remove strict length pre-check in verifyApiKey #6551

Closed
opened 2026-03-13 13:03:01 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6259
Author: @GautamBytes
Created: 11/24/2025
Status: Merged
Merged: 1/8/2026
Merged by: @himself65

Base: canaryHead: fix/api-key-verify-length


📝 Commits (3)

  • 93e6525 fix(api-key): remove brittle length check in verification to allow config drift
  • f4b4f91 update tests
  • cfa6951 Merge branch 'canary' into fix/api-key-verify-length

📊 Changes

2 files changed (+1 additions, -15 deletions)

View changed files

📝 packages/better-auth/src/plugins/api-key/api-key.test.ts (+1 -1)
📝 packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts (+0 -14)

📄 Description

Fixes #6258

Description

This PR removes the early-exit length check (key.length < opts.defaultKeyLength) in the verifyApiKey endpoint.

By removing this optimization, we rely on the database as the single source of truth. If a key exists in the database, it is valid, regardless of the local server's default length settings. This makes the library significantly more resilient to environment mismatches.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Verified that keys shorter than the default 64 characters (e.g., custom 43-char keys) are now correctly passed to the database lookup instead of throwing an immediate error.

Summary by cubic

Removed the strict length pre-check in verifyApiKey so every key is checked against the database. Invalid keys now consistently return INVALID_API_KEY instead of KEY_NOT_FOUND, fixing false rejections from length mismatches or custom/prefixed keys.

Written for commit cfa69516e3. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/6259 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 11/24/2025 **Status:** ✅ Merged **Merged:** 1/8/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/api-key-verify-length` --- ### 📝 Commits (3) - [`93e6525`](https://github.com/better-auth/better-auth/commit/93e65255f6f3a2a7991fabb388a4dccc4d1b37cc) fix(api-key): remove brittle length check in verification to allow config drift - [`f4b4f91`](https://github.com/better-auth/better-auth/commit/f4b4f9176540e1a31568e4f52d30161a236ce32f) update tests - [`cfa6951`](https://github.com/better-auth/better-auth/commit/cfa69516e390f3a10de92c03432e0474b9d14150) Merge branch 'canary' into fix/api-key-verify-length ### 📊 Changes **2 files changed** (+1 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/api-key/api-key.test.ts` (+1 -1) 📝 `packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts` (+0 -14) </details> ### 📄 Description ## Fixes #6258 ### Description This PR removes the early-exit length check (`key.length < opts.defaultKeyLength`) in the `verifyApiKey` endpoint. By removing this optimization, we rely on the database as the single source of truth. If a key exists in the database, it is valid, regardless of the local server's default length settings. This makes the library significantly more resilient to environment mismatches. ### Type of change - [x] Bug fix (non-breaking change which fixes an issue) ### How Has This Been Tested? - Verified that keys shorter than the default `64` characters (e.g., custom 43-char keys) are now correctly passed to the database lookup instead of throwing an immediate error. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Removed the strict length pre-check in verifyApiKey so every key is checked against the database. Invalid keys now consistently return INVALID_API_KEY instead of KEY_NOT_FOUND, fixing false rejections from length mismatches or custom/prefixed keys. <sup>Written for commit cfa69516e390f3a10de92c03432e0474b9d14150. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 13:03:01 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#6551