[PR #5411] [MERGED] chore: refactor origin check middleware #14238

Closed
opened 2026-04-13 09:22:20 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5411
Author: @Bekacru
Created: 10/19/2025
Status: Merged
Merged: 10/19/2025
Merged by: @Bekacru

Base: canaryHead: fix/origin-check


📝 Commits (10+)

  • 6215481 chore: enforce content type in origin check middleware
  • 653ca45 Update packages/better-auth/src/api/middlewares/origin-check.ts
  • a2b54ee chore: type fix
  • 4bf225a chore: use func for is dev && default to disabling csrf check on test
  • 6bb5659 chore: update doc
  • b9a3bac chore: update security docs
  • 205ea4e update docs
  • d02b54e chore: header
  • 654ee3e add disable origin check
  • e3e59a3 change to post

📊 Changes

13 files changed (+171 additions, -18 deletions)

View changed files

📝 docs/content/docs/reference/security.mdx (+37 -1)
📝 packages/better-auth/src/__snapshots__/init.test.ts.snap (+2 -0)
📝 packages/better-auth/src/api/middlewares/origin-check.test.ts (+36 -1)
📝 packages/better-auth/src/api/middlewares/origin-check.ts (+50 -5)
📝 packages/better-auth/src/api/routes/sign-up.ts (+1 -1)
📝 packages/better-auth/src/init.ts (+8 -1)
📝 packages/better-auth/src/oauth2/link-account.ts (+1 -1)
📝 packages/better-auth/src/test-utils/test-instance.ts (+0 -4)
📝 packages/better-auth/src/utils/get-request-ip.ts (+1 -1)
📝 packages/core/src/env/env-impl.ts (+2 -1)
📝 packages/core/src/types/context.ts (+21 -0)
📝 packages/core/src/types/init-options.ts (+9 -1)
📝 packages/stripe/src/stripe.test.ts (+3 -1)

📄 Description

Summary by cubic

Enforce application/json content type and stricter Origin checks in the origin-check middleware to block invalid or unsafe POST requests.

  • Bug Fixes

    • Reject POST requests with non-JSON content type.
    • When cookies are used, require a non-null Origin/Referer and validate it unless CSRF checks are disabled.
  • Refactors

    • Standardized header access, renamed usesCookies to useCookies, and pass request to options.trustedOrigins callback.

🔄 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/5411 **Author:** [@Bekacru](https://github.com/Bekacru) **Created:** 10/19/2025 **Status:** ✅ Merged **Merged:** 10/19/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/origin-check` --- ### 📝 Commits (10+) - [`6215481`](https://github.com/better-auth/better-auth/commit/62154819d60f5e3c57bb1cf7df947f8260bf7d10) chore: enforce content type in origin check middleware - [`653ca45`](https://github.com/better-auth/better-auth/commit/653ca456f507c0ffae2c034c8689f7f14914eddc) Update packages/better-auth/src/api/middlewares/origin-check.ts - [`a2b54ee`](https://github.com/better-auth/better-auth/commit/a2b54eece8ed3225bff98168cee5890f06e9cd70) chore: type fix - [`4bf225a`](https://github.com/better-auth/better-auth/commit/4bf225aab4ecaca569ebdff0a90987681e293c74) chore: use func for is dev && default to disabling csrf check on test - [`6bb5659`](https://github.com/better-auth/better-auth/commit/6bb5659515b6585d42dde6c3a0526d062c049ead) chore: update doc - [`b9a3bac`](https://github.com/better-auth/better-auth/commit/b9a3bac4e31f87a5682f0bc83f04131bf0ceb351) chore: update security docs - [`205ea4e`](https://github.com/better-auth/better-auth/commit/205ea4e5ec4802f07d30205b769b34c8a22dff14) update docs - [`d02b54e`](https://github.com/better-auth/better-auth/commit/d02b54e9441e995a136253199908666498aa0f0f) chore: header - [`654ee3e`](https://github.com/better-auth/better-auth/commit/654ee3e9d531a79d7515042f48f0894ddf273e16) add disable origin check - [`e3e59a3`](https://github.com/better-auth/better-auth/commit/e3e59a3fd3abe7b1bfd734bd2809dc8ca86fb395) change to post ### 📊 Changes **13 files changed** (+171 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/reference/security.mdx` (+37 -1) 📝 `packages/better-auth/src/__snapshots__/init.test.ts.snap` (+2 -0) 📝 `packages/better-auth/src/api/middlewares/origin-check.test.ts` (+36 -1) 📝 `packages/better-auth/src/api/middlewares/origin-check.ts` (+50 -5) 📝 `packages/better-auth/src/api/routes/sign-up.ts` (+1 -1) 📝 `packages/better-auth/src/init.ts` (+8 -1) 📝 `packages/better-auth/src/oauth2/link-account.ts` (+1 -1) 📝 `packages/better-auth/src/test-utils/test-instance.ts` (+0 -4) 📝 `packages/better-auth/src/utils/get-request-ip.ts` (+1 -1) 📝 `packages/core/src/env/env-impl.ts` (+2 -1) 📝 `packages/core/src/types/context.ts` (+21 -0) 📝 `packages/core/src/types/init-options.ts` (+9 -1) 📝 `packages/stripe/src/stripe.test.ts` (+3 -1) </details> ### 📄 Description <!-- This is an auto-generated description by cubic. --> ## Summary by cubic Enforce application/json content type and stricter Origin checks in the origin-check middleware to block invalid or unsafe POST requests. - **Bug Fixes** - Reject POST requests with non-JSON content type. - When cookies are used, require a non-null Origin/Referer and validate it unless CSRF checks are disabled. - **Refactors** - Standardized header access, renamed usesCookies to useCookies, and pass request to options.trustedOrigins callback. <!-- 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-04-13 09:22: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#14238