Menu

Check your Next.js bundle for leaked secrets in 30 seconds
📰

Check your Next.js bundle for leaked secrets in 30 seconds

DEV Community: javascript·Humanist Studio·about 20 hours ago
#sOHW1QvE
#devtojavascript#devto#dev#build#next
Reading 0:00
15s threshold

If a secret in your Next.js project has a NEXT_PUBLIC_ prefix, it is in the JavaScript you send to every visitor. Nothing warns you, and the site works normally. You can check right now. From your project root:

npm run build grep -rEl 'sk[A-Za-z0-9._-]{40,}|AKIA[0-9A-Z]{16}|BEGIN (RSA |EC |OPENSSH )?PRIVATE KEY' .next/static

Enter fullscreen mode

Exit fullscreen mode

No output means none of those patterns turned up. A file name means you have something to look at. The patterns cover Sanity tokens, AWS keys and private keys, so for anything else, search for the first few characters of the real value.

We ran into this ourselves. A Next.js and Sanity project read its token from NEXT_PUBLIC_SANITY_API_READ_TOKEN , a name picked to match the project ID and dataset next to it. Those two are public. The token isn't, and it ended up in three files under .next/static .

Why this happens

Next.js replaces every process.env.NEXT_PUBLIC_* reference with its value at build time.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.