[PR #1838] [MERGED] fix: access of undefined in runtime that does have great support of instanceof #29623

Closed
opened 2026-04-17 20:55:19 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/1838
Author: @jamesone
Created: 3/16/2025
Status: Merged
Merged: 3/17/2025
Merged by: @Bekacru

Base: mainHead: fix/access-of-undefined


📝 Commits (3)

  • 0315085 fix: access of undefined in open-nextjs hosted runtime
  • e22a2b4 feat: use in operator for better reliabiltiy across runtimes
  • 49ae2ae fix: linting issue

📊 Changes

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

View changed files

📝 packages/better-auth/src/cookies/index.ts (+1 -1)
📝 packages/better-auth/src/utils/get-request-ip.ts (+1 -1)

📄 Description

Issue this solves

Discord thread

This fixes the issue The \"payload\" argument must be of type object. Received null where when debugged closer we got an error like this:
image

It wasn't easy to track down, I had to get better-auth setup in my own codebase and add a bunch of logs at certain areas to determine where this obfuscated i.get was coming from.

Summary of change

Replaces instanceof Request check with a more robust 'headers' in req property check. This change improves reliability by:

  1. Avoiding cross-realm issues where instanceof can fail when objects are passed between different JavaScript contexts
  2. Supporting scenarios with multiple versions of the Request class in the application
  3. Following "duck typing" principles - focusing on an object's capabilities rather than its constructor

The property check is more resilient in complex environments like server/client boundaries, iframes, or when working with polyfills, ensuring proper header extraction regardless of how the Request object was constructed.

Further details

I think it could be worth considering using alternatives to instanceof such as the in operator if possible to reduce the risk of these hard to track down type issues


🔄 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/1838 **Author:** [@jamesone](https://github.com/jamesone) **Created:** 3/16/2025 **Status:** ✅ Merged **Merged:** 3/17/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `main` ← **Head:** `fix/access-of-undefined` --- ### 📝 Commits (3) - [`0315085`](https://github.com/better-auth/better-auth/commit/031508521776f43277785e928741c3113549afc5) fix: access of undefined in open-nextjs hosted runtime - [`e22a2b4`](https://github.com/better-auth/better-auth/commit/e22a2b41e6385eeb1e8ee730161ea627e1688c75) feat: use in operator for better reliabiltiy across runtimes - [`49ae2ae`](https://github.com/better-auth/better-auth/commit/49ae2aeef510c2d228d40b4048d087cf55f7e814) fix: linting issue ### 📊 Changes **2 files changed** (+2 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/cookies/index.ts` (+1 -1) 📝 `packages/better-auth/src/utils/get-request-ip.ts` (+1 -1) </details> ### 📄 Description ## Issue this solves [Discord thread](https://discord.com/channels/1288403910284935179/1349503864260530209) This fixes the issue `The \"payload\" argument must be of type object. Received null` where when debugged closer we got an error like this: ![image](https://github.com/user-attachments/assets/6a36b331-ba93-4b1a-a502-64592727c6dd) It wasn't easy to track down, I had to get better-auth setup in my own codebase and add a bunch of logs at certain areas to determine where this obfuscated `i.get` was coming from. ## Summary of change Replaces `instanceof Request` check with a more robust `'headers' in req` property check. This change improves reliability by: 1. Avoiding cross-realm issues where `instanceof` can fail when objects are passed between different JavaScript contexts 2. Supporting scenarios with multiple versions of the Request class in the application 3. Following "duck typing" principles - focusing on an object's capabilities rather than its constructor The property check is more resilient in complex environments like server/client boundaries, iframes, or when working with polyfills, ensuring proper header extraction regardless of how the Request object was constructed. ## Further details I think it could be worth considering using alternatives to `instanceof` such as the [`in` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) if possible to reduce the risk of these hard to track down type issues --- <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-04-17 20:55:19 -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#29623