Skip to content
Leaked secrets Write-up Beginner

CORS and API Keys in NextJS

A developer exposed a third-party API key in client-side JavaScript because a NEXT_PUBLIC_ environment variable was inlined into the browser bundle. An attacker could extract the key from the bundle and use it to call the third-party API directly.

medium.com

Published on medium.com

As always, it starts with a recent project that I was working on; when trying to call a third-party API (let’s say ExternalXYZAPI) from the client side, the request was blocked due to CORS issues. Our team was actually expecting the ExternalXYZAPI team to allow CORS on the production domain, so we developed, tested…

Niraj Niroula, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the API key was stored in an environment variable prefixed with NEXT_PUBLIC_, which Next.js inlines into the client-side JavaScript bundle at build time, making the secret publicly readable.

What the attacker needed

Access to the public website

The fix

The key was removed from the NEXT_PUBLIC_ variable, the API key was rotated, and the changes were deployed.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

When a website needs to talk to another service, it sometimes uses a secret key to prove it is allowed. If that key is placed in code that runs in the visitor's browser, anyone can open the browser's developer tools and read it. Here, a setting meant to keep the key on the server was accidentally made public, so the secret ended up in the files sent to every visitor.

What is Leaked secrets?

Keys, tokens or passwords left in public code, apps, logs or responses, that grant real access.

Similar reports

View all

Same bug class and techniques, clearest first.

Leaked secrets and credentials

The lesson behind this report.

Keys, tokens or passwords left in public code, apps, logs or responses, that grant real access.