Next.js Protected Routes: Why a Session Cookie Check in proxy.ts Isn't Enough
I asked an AI assistant for a simple thing: a team dashboard in a Next.js 16 app (Better Auth + Drizzle + Postgres), visible only to signed-in users. It looked done. Signed out, /dashboard sent me to the sign-in page. Signed in, I saw the team. If you only tested by clicking around, you would ship it. Then I set one fake cookie and the page showed every name and email in the database. All of this…
This report details a critical security flaw in a Next.js 16 application protected by an AI-generated proxy.ts file. The issue arises when a session cookie is faked, allowing unauthorized access to sensitive user data. The proxy.ts file alone is insufficient to prevent this vulnerability.
The proxy.ts file checks for the presence of a session cookie using getSessionCookie() from the Better Auth library. However, it does not verify whether the cookie belongs to an actual authenticated session. This check is intentionally fast and lightweight, as the proxy is meant to stay performant without invoking the database for each request.
The flaw becomes apparent when a fake cookie is set. With this modification, accessing the dashboard page reveals the names and emails of all users in the database. This occurs because the proxy.ts file allows access to the protected pages regardless of the cookie's validity. The session check is delegated to the dashboard page itself, which in turn calls a session validation helper function. This function simply validates the session against the database, but without any additional checks.
To address this issue, the report suggests implementing a session check closer to the actual data access layer. A new helper function, getSession, is introduced to validate the session on the server side. This function uses header information to authenticate the request. Another utility function, requireUser, is created to redirect users to the sign-in page if no valid session is detected.
By incorporating these checks before querying the database, the application ensures that only authenticated users can access the protected pages.
The report emphasizes that the proxy.ts file should only serve as an optimistic filter, flagging unauthorized access attempts. The real authorization logic should be implemented directly next to the data access code. This separation of concerns ensures that the proxy remains fast and efficient while providing a robust security layer.
To prevent similar issues in the future, the report advises developers to carefully review the documentation on authentication in Next.js. It is essential to understand where the session validation takes place and to ensure that it is implemented close to the data access code. Testing with a fake cookie is a simple yet effective method to verify the application's security measures.
In summary, the AI-generated proxy.ts file alone is insufficient to protect sensitive pages in a Next.js application. Developers must implement additional session validation checks closer to the data access layer to ensure the integrity of their applications. By following these guidelines, developers can mitigate the risk of session cookie manipulation and provide a secure user experience.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to