Please update the official documentation to avoid security issues #1360

Closed
opened 2026-03-13 08:34:50 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @sovetski on GitHub (Jun 15, 2025).

For me, the code and information provided here is not complete: https://www.better-auth.com/docs/integrations/next#middleware

This code is not secure:

const sessionCookie = getSessionCookie(request);

if (!sessionCookie) {
	return NextResponse.redirect(new URL("/", request.url));
}

I know that we have this information:

it's recommended to only check for the existence of a session cookie to handle redirection

But the problem is that many developers will just ignore this kind of text and will think your provided code will protect their application. And if we want to acces to the restricted pages, we can just bypass the middleware by adding manually a cookie named better-auth.session_token with a random value. I think the documentation with the current version can be a source of many security related issues, especially for the websites developed by junior developers (or AI agent)

I recommend that you add a warning here, to say "you have to protect your application and the code above is not secure"

Image

Or you can just provide a secure code example?

Originally created by @sovetski on GitHub (Jun 15, 2025). For me, the code and information provided here is not complete: https://www.better-auth.com/docs/integrations/next#middleware This code is not secure: ```js const sessionCookie = getSessionCookie(request); if (!sessionCookie) { return NextResponse.redirect(new URL("/", request.url)); } ``` I know that we have this information: > it's recommended to only check for the existence of a session cookie to handle redirection But the problem is that many developers will just ignore this kind of text and will think your provided code will protect their application. And if we want to acces to the restricted pages, we can just bypass the middleware by adding manually a cookie named `better-auth.session_token` with a random value. I think the documentation with the current version can be a source of many security related issues, especially for the websites developed by junior developers (or AI agent) I recommend that you add a warning here, to say "you have to protect your application and the code above is not secure" ![Image](https://github.com/user-attachments/assets/0c97c4d4-5b22-4eb1-8c31-c4264f59ec84) Or you can just provide a secure code example?
GiteaMirror added the securitydocumentation labels 2026-03-13 08:34:50 -05:00
Author
Owner

@kylegillen commented on GitHub (Jun 15, 2025):

Could someone point me to a secure implementation example?

@kylegillen commented on GitHub (Jun 15, 2025): Could someone point me to a secure implementation example?
Author
Owner

@de-mawo commented on GitHub (Jun 21, 2025):

Also waiting for a secure implementation of this

@de-mawo commented on GitHub (Jun 21, 2025): Also waiting for a secure implementation of this
Author
Owner

@ping-maxwell commented on GitHub (Jun 22, 2025):

Hey guys, I'll open a PR addressing this issue.

In regards of an example implementation of checking for a secure session per page/route:

Image

cc. @kylegillen @de-mawo

@ping-maxwell commented on GitHub (Jun 22, 2025): Hey guys, I'll open a PR addressing this issue. In regards of an example implementation of checking for a secure session per page/route: <img width="874" alt="Image" src="https://github.com/user-attachments/assets/29cda3df-40bd-4f28-a5f1-560496a9fff5" /> cc. @kylegillen @de-mawo
Author
Owner

@ping-maxwell commented on GitHub (Jun 22, 2025):

https://github.com/better-auth/better-auth/pull/3135

@ping-maxwell commented on GitHub (Jun 22, 2025): https://github.com/better-auth/better-auth/pull/3135
Author
Owner

@TimurBas commented on GitHub (Jul 22, 2025):

@ping-maxwell can be closed as #3135 is merged to main branch.

@TimurBas commented on GitHub (Jul 22, 2025): @ping-maxwell can be closed as #3135 is merged to main branch.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1360