fix: validate API key name length to prevent opaque 500#4806
Open
tanaymishra wants to merge 1 commit into
Open
fix: validate API key name length to prevent opaque 500#4806tanaymishra wants to merge 1 commit into
tanaymishra wants to merge 1 commit into
Conversation
API key names longer than 32 characters were rejected by better-auth with a 400 that surfaced as an opaque INTERNAL_SERVER_ERROR (the generic "Failed to generate API key" toast). Add a shared name schema (min 1, max 32, matching better-auth's default maximumNameLength) used by both the tRPC input and the client form, and surface the limit on the Name field so users see it before submitting. Fixes Dokploy#4798
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Creating an API key with a name longer than 32 characters returned an opaque 500 with a generic "Failed to generate API key" toast, instead of a clear validation error.
Fixes #4798
Root cause
The tRPC input for
user.createApiKeyvalidated the name with onlyz.string().min(1):The
@better-auth/api-keyplugin, however, enforces a defaultmaximumNameLengthof 32 (Dokploy does not override it in theapiKey()config). A name over 32 characters passes our validation but is rejected inside better-auth withINVALID_NAME_LENGTH("The name length is either too large or too small."). better-auth throws this as aBAD_REQUEST(400), but because the service callsauth.createApiKey()and does not map that error, tRPC re-wraps it as an unknownINTERNAL_SERVER_ERROR, hence the opaque 500 and generic toast.Changes
apiKeyNameSchema(andAPI_KEY_NAME_MAX_LENGTH = 32) inapps/dokploy/lib/api-keys.ts, matching better-auth's default so the two limits cannot drift.apps/dokploy/server/api/routers/user.ts) and the client form (add-api-key.tsx). Over-length names now fail as a clean, field-level validation error before any request is sent, no more 500.maxLength={32}on the Name input plus a "Maximum 32 characters" helper line, so users see the constraint up front (the issue asked for this too).The accepted range (1–32) is identical to what better-auth already accepts, so no previously valid input is affected; only the previously-500 case now returns a proper validation message.
Testing
apps/dokploy/__test__/api/api-key-name.test.tscovering the shared schema used by both layers: empty name rejected, 32 chars accepted, 33 chars rejected with the expected message. Passing:tsc --noEmit(apptypecheck) passes with the changes.Notes
Related but different (already resolved): #4024, #4026.