Replies: 6 comments
-
Great question! This is an aspect of Single Page Apps we could help explain better (and probably need to). When using Client Side Rendering to protect pages any text on the page will be theoretically accessible to anyone in this way unless the content is served from behind a server side route. e.g. where the page fetches content from an endpoint like The expectation with an SPA is that you are comfortable with the page templates being visible to people in this way, but the actual data that is sensitive is served via API (e.g. a list of users if it's a user dashboard, or bunch of data if it's a graph, or article text if it's a news site with a paywall, etc) and that it enforces that they must be authorized to view the data. If you are not comfortable with this, you can move even the template data exclusively behind server side rendered pages using |
Beta Was this translation helpful? Give feedback.
-
The current behaviour meets a specific scenario. |
Beta Was this translation helpful? Give feedback.
-
Something that might be worth calling out as a caveat in documentation, is that any JS in the default exported function of a NextJS page can (and will eventually) be accessed by an attacker. You are right that any templated data behind a getServerSideProps() call will be correctly protected. If however you have a route that does an SSR redirect but still shows sensitive information (not externally fetched data) on the page component itself, it's possible to follow the JS through webpack+Next to find those files. An example (apologies if these links break in the future): An unauthenticated attacker can go to https://next-auth-example.now.sh/_next/static Following that URL takes us to which contains the phrase: This is not to say that it's not worth doing an SSR redirect, but more that when using NextJS, you should assume that all component JS could be publically visible to anyone. If data is sensitive, it must be brought in either through getServerSideProps or on the client as a fetch. NextJS does not currently provide a way to require authentication for JS or CSS - and given the NextJS/Vercel/JAMstack goal of "serve static files as quick as possible", I wouldn't expect to see this anytime soon. Also to be clear, this is not a bug in next-auth! This is outside the remit of an SPA authentication library - it's just an important caveat worth knowing about if you care about the security+visibility of your JS. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your answers! It's definitely not a bug or an issue of next-auth, but probably we should clarify it somehow for users. And if this solution can be implemented out of the box, I think it's worth to consider it to be default one. |
Beta Was this translation helpful? Give feedback.
-
I've updated the example project at https://next-auth-example.now.sh
Client Side Protection with this approach is the fastest method and is secure, the Server Side Protection method is not more secure but will work for devices that do not support JavaScript (with some performance cost). In practice you'd probably want to do something like use JSON, Markdown or HTML for protected content (e.g. from a database or API) and handling loading states a bit better (e.g. using I agree with @mikecoon's comment that we probably should make it clearer what the dangers are. |
Beta Was this translation helpful? Give feedback.
-
@iaincollins Is there a way to trigger the sign in process from the server? That is, instead of displaying a message saying "access denied", trigger an open id connect sign in. I tried using |
Beta Was this translation helpful? Give feedback.
-
So, since this is authentication, why am I able to see private page without being logged in?
Anyone can just open a dev tools and find content of private page.
Isn't it supposed to be up to server to decide whether private information should be sent or not?
Because now server sends everything, but client doesn't show it. Which is obviously not enough in terms of security.
Maybe I don't understand something?
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions