signUp.email throws unhandled TypeError when password is undefined instead of returning validation error #2524

Closed
opened 2026-03-13 10:00:46 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @samuel032khoury on GitHub (Dec 13, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Enable email/password authentication
  2. Call the signup endpoint with a request body missing the password field:
{
  "name": "John Doe",
  "email": "john@example.com"
}

Current vs. Expected behavior

Current behavior

When calling the email signup endpoint without a password field, the handler throws an unhandled TypeError instead of returning a proper 400 validation error, resulting in a 500 response with an empty body, and server console prints:

TypeError: undefined is not an object (evaluating 'password.length')
    at <anonymous> (/node_modules/better-auth/dist/api-CkmycQ2x.mjs:2342:7)

Expected behavior
Return a 400 Bad Request with a validation error message like "Password is required".

What version of Better Auth are you using?

1.4.5

System info

{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 25.2.0: Tue Nov 18 21:09:34 PST 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T8112",
    "release": "25.2.0",
    "cpuCount": 8,
    "cpuModel": "Apple M2",
    "totalMemory": "16.00 GB",
    "freeMemory": "0.13 GB"
  },
  "node": {
    "version": "v24.7.0",
    "env": "development"
  },
  "packageManager": {
    "name": "bun",
    "version": "1.3.0"
  },
  "frameworks": [
    {
      "name": "hono",
      "version": "^4.7.11"
    }
  ],
  "databases": null,
  "betterAuth": {
    "version": "^1.3.18",
    "config": null
  }
}

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

Root Cause
In api-CkmycQ2x.mjs line 2342, password.length is accessed before validating that password exists:

const minPasswordLength = ctx.context.password.config.minPasswordLength;
if (password.length < minPasswordLength) {  // crashes here if password is undefined

Suggested Fix
Add a null check before accessing password.length:

if (!password) {
  throw new APIError("BAD_REQUEST", { message: "Password is required" });
}
if (password.length < minPasswordLength) {
  // ...
}
Originally created by @samuel032khoury on GitHub (Dec 13, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Enable email/password authentication 2. Call the signup endpoint with a request body missing the password field: ```json { "name": "John Doe", "email": "john@example.com" } ``` ### Current vs. Expected behavior **Current behavior** When calling the email signup endpoint without a `password` field, the handler throws an unhandled `TypeError` instead of returning a proper 400 validation error, resulting in a 500 response with an empty body, and server console prints: ``` TypeError: undefined is not an object (evaluating 'password.length') at <anonymous> (/node_modules/better-auth/dist/api-CkmycQ2x.mjs:2342:7) ``` **Expected behavior** Return a 400 Bad Request with a validation error message like "Password is required". ### What version of Better Auth are you using? 1.4.5 ### System info ```bash { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 25.2.0: Tue Nov 18 21:09:34 PST 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T8112", "release": "25.2.0", "cpuCount": 8, "cpuModel": "Apple M2", "totalMemory": "16.00 GB", "freeMemory": "0.13 GB" }, "node": { "version": "v24.7.0", "env": "development" }, "packageManager": { "name": "bun", "version": "1.3.0" }, "frameworks": [ { "name": "hono", "version": "^4.7.11" } ], "databases": null, "betterAuth": { "version": "^1.3.18", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context **Root Cause** In `api-CkmycQ2x.mjs` line 2342, password.length is accessed before validating that password exists: ```javascript const minPasswordLength = ctx.context.password.config.minPasswordLength; if (password.length < minPasswordLength) { // crashes here if password is undefined ``` **Suggested Fix** Add a null check before accessing password.length: ```javascript if (!password) { throw new APIError("BAD_REQUEST", { message: "Password is required" }); } if (password.length < minPasswordLength) { // ... } ```
GiteaMirror added the honobug labels 2026-03-13 10:00:47 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Dec 13, 2025):

Hi @samuel032khoury! I'm Dosu and I’m helping the better-auth team.

This is a confirmed bug: the signUp.email handler accesses password.length without checking if password is undefined, which causes the unhandled TypeError you described. The code currently validates the email format with zod, but does not validate the presence of the password field before using its length. This means missing passwords result in a 500 error instead of a 400 with a clear validation message, as expected. Adding a null/undefined check for password before accessing its length would resolve this issue, and your suggested fix is correct and aligns with best practices citation.

There are related discussions and open PRs about improving password validation and error handling (like moving to schema-based validation and enforcing patterns), but these are not yet merged or released citation citation. The maintainers are aware that error handling in signUp.email needs improvement citation.

For a robust fix, consider using a schema validator (like zod) to enforce required fields at the start of the handler, as is done in other parts of the codebase. This would prevent similar bugs in the future and ensure consistent error responses.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Dec 13, 2025): <!-- Greeting --> Hi @samuel032khoury! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a confirmed bug: the signUp.email handler accesses password.length without checking if password is undefined, which causes the unhandled TypeError you described. The code currently validates the email format with zod, but does not validate the presence of the password field before using its length. This means missing passwords result in a 500 error instead of a 400 with a clear validation message, as expected. Adding a null/undefined check for password before accessing its length would resolve this issue, and your suggested fix is correct and aligns with best practices [citation](https://github.com/better-auth/better-auth/blob/99defeaf80b200f71be29c813d48afa985b86157/packages/better-auth/src/api/routes/sign-up.ts#L16-L351). There are related discussions and open PRs about improving password validation and error handling (like moving to schema-based validation and enforcing patterns), but these are not yet merged or released [citation](https://github.com/better-auth/better-auth/pull/5571) [citation](https://github.com/better-auth/better-auth/pull/5385). The maintainers are aware that error handling in signUp.email needs improvement [citation](https://github.com/better-auth/better-auth/issues/73). For a robust fix, consider using a schema validator (like zod) to enforce required fields at the start of the handler, as is done in other parts of the codebase. This would prevent similar bugs in the future and ensure consistent error responses. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/fdf566c7-b500-4fd7-855d-e9cebb81ccea?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/fdf566c7-b500-4fd7-855d-e9cebb81ccea?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/fdf566c7-b500-4fd7-855d-e9cebb81ccea?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/fdf566c7-b500-4fd7-855d-e9cebb81ccea?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/fdf566c7-b500-4fd7-855d-e9cebb81ccea?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/fdf566c7-b500-4fd7-855d-e9cebb81ccea?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/fdf566c7-b500-4fd7-855d-e9cebb81ccea?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20is%20a%20confirmed%20bug%3A%20the%20signUp.email%20handler%20accesses%20password.length%20without%20checking%20if%20password%20is%20undefined%2C%20which%20causes%20the%20unhandled%20TypeError%20you%20described.%20The%20code%20currently%20validates%20the%20email%20format%20with%20zod%2C%20but%20does%20not%20validate%20the%20presence%20of%20the%20password%20field%20before%20using%20its%20length.%20This%20means%20missing%20passwords%20result%20in%20a%20500%20error%20instead%20of%20a%20400%20with%20a%20clear%20validation%20message%2C%20as%20expected.%20Adding%20a%20null/undefined%20check%20for%20password%20before%20accessing%20its%20length%20would%20resolve%20this%20issue%2C%20and%20your%20suggested%20fix%20is%20correct%20and%20aligns%20with%20best%20practices%C2%A0%5Bcitation%5D%28https%3A//github.com/better-auth/better-auth/blob/99defeaf80b200f71be29c813d48afa985b86157/packages/better-auth/src/api/routes/sign-up.ts%23L16-L351%29.%0A%0AThere%20are%20related%20discussions%20and%20open%20PRs%20about%20improving%20password%20validation%20and%20error%20handling%20%28like%20moving%20to%20schema-based%20validation%20and%20enforcing%20patterns%29%2C%20but%20these%20are%20not%20yet%20merged%20or%20released%C2%A0%5Bcitation%5D%28https%3A//github.com/better-auth/better-auth/pull/5571%29%C2%A0%5Bcitation%5D%28https%3A//github.com/better-auth/better-auth/pull/5385%29.%20The%20maintainers%20are%20aware%20that%20error%20handling%20in%20signUp.email%20needs%20improvement%C2%A0%5Bcitation%5D%28https%3A//github.com/better-auth/better-auth/issues/73%29.%0A%0AFor%20a%20robust%20fix%2C%20consider%20using%20a%20schema%20validator%20%28like%20zod%29%20to%20enforce%20required%20fields%20at%20the%20start%20of%20the%20handler%2C%20as%20is%20done%20in%20other%20parts%20of%20the%20codebase.%20This%20would%20prevent%20similar%20bugs%20in%20the%20future%20and%20ensure%20consistent%20error%20responses.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/6743)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2524