juanbzpy / next-csrf

CSRF mitigation for Next.js
https://npm.im/next-csrf
MIT License
140 stars 22 forks source link

Env variable cant load on browser #25

Open vedrane opened 3 years ago

vedrane commented 3 years ago

As mentioned in #23, environment vars can't load on the browser (obviously). Ok then, how do I SECURELY load the secret for the browser, without exposing it? Is there any way?

juanbzpy commented 3 years ago

@Get-DevVed-YT the secret shouldn't reach the browser.

We use the secret to generate a token on the server-side. The token then is exposed to the browser by saving it in an httpOnly cookie (can't reach it with JS).

So I think the question is "How do I get the cookie with the token on the browser?" There are two ways:

vedrane commented 3 years ago

So even if the secret is publicly visible, it's fine?

bkotch commented 2 years ago

After much experimentation and help from this excellent community, you cannot leak the secret to the browser. Using NEXT_PUBLIC exposes it in the bundle. @j0lv3r4 is going to take a look into clarifying the documentation but you need to pass the token either via an API call (so the module loads for the request and the cookie gets set)

Or set it up using getServerSideProps which executes only on the server side.