Urgent.News

What's breaking now, across hundreds of outlets.

Tech

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

Read the original at dev.to →

Report a problem with this story

More in Tech

The `template` Data Type in ThingsDB

ThingsDB has long supported template strings using backtick syntax ( `Hello {name}!` ). While convenient for inline string interpolation, template strings are evaluated immediately at runtime, which…

  • template data type supports template strings in ThingsDB
  • Templates validated, stored, and rendered efficiently
  • Rendered using .render() method with valid variable names

Handle API errors by code, not by message

The first version of most API integrations handles errors by reading the message: if (body.error.includes('character limit')) . It works until someone rewords the message.

  • API errors handled by stable codes, not changing messages
  • Error codes appear in five locations: top-level, validation, warnings, media results, issues
  • Three handling categories: fix-input, retry, and reread-then-retry

A first-year SaaS discount is a price increase with a delayed invoice

A 50% off first year looks generous at checkout. On a $240 annual plan sold at $120, the customer has agreed to a $120 jump at renewal, often billed automatically.

  • First-year SaaS discount appears as price increase with delayed invoice
  • 80 customers buying discounted annual plan generate $9,600 in year-one cash
  • Additional 80 customers needed to offset discount and marketing costs

Reading the DOBI snapshot: device identity, demo fleets, and x402 payments

DOBI is positioning itself as an agent-native IoT platform: device identity, fleet telemetry, configurable automation, and optional pay-per-use via x402.

  • DOBI focuses on device identity, fleet telemetry, and x402 payments
  • Device identity is root, payments are leaf in platform structure
  • ESP32 reference path for device onboarding with device ID and key

More from Monday, 30 November 2026 →