[PR #8516] fix(api): preserve response metadata for early before hook returns #8029

Open
opened 2026-03-13 13:57:20 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8516
Author: @ssijak
Created: 3/9/2026
Status: 🔄 Open

Base: canaryHead: codex/fix-before-hook-response-headers


📝 Commits (1)

  • 0cbdf5f fix(better-auth): preserve response headers for early before hook returns

📊 Changes

2 files changed (+70 additions, -7 deletions)

View changed files

📝 packages/better-auth/src/api/to-auth-endpoints.test.ts (+51 -0)
📝 packages/better-auth/src/api/to-auth-endpoints.ts (+19 -7)

📄 Description

Summary

  • stop passing request headers into toResponse() for early before hook returns
  • preserve actual response headers and status when returnHeaders / returnStatus are requested
  • add regression coverage for request-header leakage on early before responses

Root Cause

Early before hook responses were being converted with context?.headers, which are request headers. That could leak request content-length and other request metadata into the response.

Changes

  • derive response headers and status from toResponse(before)
  • return those derived headers/status for the early before short-circuit path
  • keep existing behavior for non-response return values

Testing

  • pnpm --filter better-auth exec vitest run src/api/to-auth-endpoints.test.ts
  • pnpm exec biome check packages/better-auth/src/api/to-auth-endpoints.ts packages/better-auth/src/api/to-auth-endpoints.test.ts

Summary by cubic

Fix early before hook handling in better-auth to preserve response headers and status and prevent leaking request headers. asResponse, returnHeaders, and returnStatus now reflect the actual Response from the hook.

  • Bug Fixes
    • Use toResponse(before) to derive headers and status; no longer reuse request headers.
    • For early before returns: respect asResponse, and return real headers/status for returnHeaders/returnStatus.
    • Add regression tests to ensure request headers (e.g., content-length, x-request) don’t leak into responses.

Written for commit 0cbdf5fb2d. 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/8516 **Author:** [@ssijak](https://github.com/ssijak) **Created:** 3/9/2026 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `codex/fix-before-hook-response-headers` --- ### 📝 Commits (1) - [`0cbdf5f`](https://github.com/better-auth/better-auth/commit/0cbdf5fb2db245370b8a53b93065cc459677e43f) fix(better-auth): preserve response headers for early before hook returns ### 📊 Changes **2 files changed** (+70 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/to-auth-endpoints.test.ts` (+51 -0) 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+19 -7) </details> ### 📄 Description ## Summary - stop passing request headers into `toResponse()` for early `before` hook returns - preserve actual response headers and status when `returnHeaders` / `returnStatus` are requested - add regression coverage for request-header leakage on early `before` responses ## Root Cause Early `before` hook responses were being converted with `context?.headers`, which are request headers. That could leak request `content-length` and other request metadata into the response. ## Changes - derive response headers and status from `toResponse(before)` - return those derived headers/status for the early `before` short-circuit path - keep existing behavior for non-response return values ## Testing - `pnpm --filter better-auth exec vitest run src/api/to-auth-endpoints.test.ts` - `pnpm exec biome check packages/better-auth/src/api/to-auth-endpoints.ts packages/better-auth/src/api/to-auth-endpoints.test.ts` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fix early before hook handling in `better-auth` to preserve response headers and status and prevent leaking request headers. `asResponse`, `returnHeaders`, and `returnStatus` now reflect the actual `Response` from the hook. - **Bug Fixes** - Use `toResponse(before)` to derive headers and status; no longer reuse request headers. - For early `before` returns: respect `asResponse`, and return real headers/status for `returnHeaders`/`returnStatus`. - Add regression tests to ensure request headers (e.g., `content-length`, `x-request`) don’t leak into responses. <sup>Written for commit 0cbdf5fb2db245370b8a53b93065cc459677e43f. 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:57:20 -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#8029