--- title: "Exposed env.js: How Public Config Files Leak Secrets" url: https://playciso.com/blog/env-js-exposed-secrets updated: 2026-11-20 section: Blog publisher: PlayCISO --- # Exposed env.js: How Public Config Files Leak Secrets > How env.js, .env and NEXT_PUBLIC_/VITE_ variables leak API keys to every visitor, how to check your own site, and the fix: rotate first, then allow-list. Source: https://playciso.com/blog/env-js-exposed-secrets · Published: 2026-11-20 · Publisher: PlayCISO (https://playciso.com) --- **A public config file such as /env.js can hand your API keys, database password and signing secrets to anyone who opens it.** Many single-page apps load a small runtime config script that a container writes at start-up, and when that script copies every environment variable instead of a short list of public ones, the secrets go out with it. Bug-bounty hunters regularly report finding exactly this. This guide explains how it happens, how attackers find it, how to check your own site, and the order to fix it in. ## How runtime env.js happens Frameworks such as React, Vue and Angular build a static bundle. To run the same Docker image in test and production, teams add a script that runs when the container starts, reads environment variables and writes them into the web root as JavaScript, typically `window._env_ = { API_URL: "…", … }`. The page loads `/env.js` before the app and reads its settings from there. The pattern is fine for public settings: the API base URL, a feature flag, a publishable payment key. It turns into a leak when the script is written as "dump every variable" rather than "write these named variables". The container's environment usually also holds the database URL, server-side API keys and session secrets, so all of them are published. Because `env.js` is a static file, nothing in the app's own access control protects it. ## The other ways secrets reach the browser - Build-time prefixes. Next.js documents that a NEXT_PUBLIC_ variable is inlined at build time "into any JavaScript sent to the browser"; other variables stay in the Node.js environment. Vite says variables prefixed with VITE_ are exposed in client-side source after bundling and "should not contain sensitive information such as API keys". Create React App says REACT_APP_ variables are embedded into the build and "anyone can view them by inspecting your app's files". Giving a secret one of these prefixes to "make it work" ships it to every visitor. - Angular environment files. Angular's documentation says files in src/environments/ are bundled into the client and visible to anyone who loads the page, and tells you never to store API keys there. - A .env file in the web root. A site deployed with its .env next to index.html, on a web server that serves dotfiles, publishes the file as plain text. - Source maps and .git. Public .map files rebuild your original source code, including any constants in it. An exposed .git folder can let someone download the repository and its history, including secrets that were committed and later removed. ## How attackers find it MITRE ATT&CK lists this as Credentials In Files (T1552.001): adversaries search files for insecurely stored credentials, including configuration files and source code. On the web, the files are public, so finding them is mostly collection and pattern matching. Automated crawlers gather every JavaScript and config file a site serves, request well-known config paths, and run the results through secret detectors that recognise key formats such as AWS access key IDs, Stripe secret keys and GitHub tokens. Bug-bounty hunters use the same approach, which is why exposed config files show up so often in their reports. Assume that anything your site serves has already been read by an automated tool. ## Check your own site - Run the [Exposed env.js Checker](/tools/exposed-env-js-checker) on a domain you own. It requests the homepage and a fixed list of config paths (/env.js, /env-config.js, /config.js, /config.json, /.env, /.env.production, /.git/config and similar), ignores the single-page-app fallback page, and shows masked findings. Only check sites you own or are authorised to test. - Open your app in a browser, view the page source and the network panel, and look at every script whose name suggests config: env, config, runtime, settings. Paste them into the checker's "Paste a file" mode, which runs in your browser. - Search your built bundle (the output folder, not the source) for key prefixes such as sk_live_, AKIA, ghp_, xox, -----BEGIN and postgres://. The [Secret Scanner](/tools/secret-scanner) recognises the common formats. - List every variable your entrypoint writes and every NEXT_PUBLIC_, VITE_ or REACT_APP_ variable you set, and ask of each one: would I put this on a billboard? ## Fix checklist - Rotate first, then remove. Deleting the file does not un-leak the key: anyone who fetched it keeps a copy. Issue the new credential, verify it with a real call, switch every consumer, revoke the old one, and check the provider's logs for use you did not make. The OWASP Secrets Management Cheat Sheet says keys that were exposed "should undergo immediate revocation" and that "restarting the application does not itself revoke stolen credentials". The [secret rotation order planner](/tools/secret-rotation-order-planner) sequences several keys without an outage, and the [leaked credential response](/tools/leaked-credential-response) covers the rest of the incident. - Write an allow-list. Change the entrypoint to write only named public variables. Make it refuse to start if a name containing SECRET, PASSWORD, PRIVATE or TOKEN would be written. - Keep secrets server-side behind an API. If the browser needs data from a service that requires a secret key, call that service from your own server (a route handler, a serverless function, a small proxy) and return only the result. Vite's documentation recommends a backend server or serverless/edge functions for this; Next.js documents that non-NEXT_PUBLIC_ variables are only available on the server. - Scan every push. GitHub secret scanning scans a repository's entire Git history for hardcoded credentials, and push protection is designed to stop them from being pushed at all. OWASP recommends detecting secrets before commit, for example in a pre-commit hook. Add a scan of the built output too, since build-time inlining happens after the source passes review. - Deny dotfiles at the web server. In nginx, location ~ /\.(env|git) { deny all; } refuses .env and .git paths. Better still, keep them out of the image with a .dockerignore. - Remove or restrict source maps. Do not publish production .map files on the public web server; upload them to your error tracker instead if you need them. - Restrict the public keys you keep. A browser key for a service such as Google Maps should be restricted to your domains and to the APIs it needs. Then check the rest of what you send to browsers with the [security headers checker](/tools/security-headers-checker). ## Which "keys" are public by design Not every key in front-end code is a leak. Treat these as normal, as long as the controls behind them are in place: - Stripe publishable keys (pk_live_, pk_test_). Stripe lists them as safe to expose. Secret keys (sk_) and restricted keys (rk_) are not. - Firebase web API keys. Firebase says keys restricted to Firebase services do not need to be treated as secrets; data is protected by Firebase Security Rules and App Check. Check those rules, not the key. Firebase makes an exception for the Gemini Developer API key, which must not be in code. - Supabase publishable (anon) keys. Supabase says they are safe to expose and only reach what Row Level Security allows, so enable RLS on every table. The secret (service_role) key bypasses RLS and must never reach a browser. - Publishable keys of other services, such as an authentication provider's frontend key, and error-tracking DSNs that only let clients send events. Anything named secret, private, password or service role, and any database URL with a password in it, is never public by design. ## What security leaders should do this week - Ask each web team whether their apps load a runtime config file, and how the file is generated: allow-list or full environment. - Run the [Exposed env.js Checker](/tools/exposed-env-js-checker) on your main public domains, with the owners' agreement. - Confirm secret scanning and push protection are on for every repository that builds a front end, and that the build output is scanned too. - Make "rotate first, then remove" the written rule in your incident playbook for leaked keys. ## FAQ ### What is env.js? A small script that many single-page apps load before the main bundle so one built image can run in several environments. A container entrypoint writes it at start-up, typically as window._env_ = { ... } with values taken from environment variables. It is an ordinary public file: everything in it can be read by anyone who loads the site. ### How do I check if my env.js or .env file is exposed? Request the well-known paths on your own site (/env.js, /env-config.js, /config.js, /config.json, /.env, /.env.production, /.git/config and similar) and look at what comes back, remembering that many single-page apps answer every unknown path with their index page and status 200. The PlayCISO Exposed env.js Checker does this for a domain you own and shows masked findings, or you can paste a file and check it in your browser. ### Does NEXT_PUBLIC_ or VITE_ keep a variable secret? No. Next.js documents that NEXT_PUBLIC_ values are inlined at build time into the JavaScript sent to the browser. Vite says VITE_ variables are exposed in client-side code and should not contain API keys. Create React App says REACT_APP_ variables are embedded into the build and anyone can view them. The prefix marks a value as public. ### Which API keys are safe to put in front-end code? Keys designed to be public. Stripe lists publishable keys (pk_) as safe to expose and secret and restricted keys as not safe. Firebase says API keys restricted to Firebase services do not need to be treated as secrets because Security Rules and App Check protect the data. Supabase says the publishable (anon) key is safe to expose and only reaches what Row Level Security allows, while secret (service_role) keys bypass Row Level Security and must never reach a browser. ### I found a secret in a public file. Should I delete the file first? Rotate first. Deleting the file does not un-leak the key: anyone who already fetched it, and any crawler or archive, keeps a copy. Issue a new credential, verify it works, switch every consumer to it, revoke the old one and check the provider logs for use you did not make. Then fix the entrypoint or build so the secret cannot reach the file again. ### How do I stop it happening again? Make the entrypoint write an allow-list of named public variables only, keep secret-using calls on the server behind your own API, run secret scanning on every push (GitHub secret scanning and push protection, or a scanner in CI), deny dotfiles such as .env and .git at the web server, and do not publish production source maps. ## Sources - [Next.js: How to use environment variables](https://nextjs.org/docs/app/guides/environment-variables) - [Vite: Env variables and modes](https://vite.dev/guide/env-and-mode) - [Create React App: Adding custom environment variables](https://create-react-app.dev/docs/adding-custom-environment-variables/) - [Angular: Configuring application environments](https://angular.dev/tools/cli/environments) - [OWASP Secrets Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html) - [GitHub Docs: About secret scanning](https://docs.github.com/en/code-security/secret-scanning/introduction/about-secret-scanning) - [GitHub Docs: About push protection](https://docs.github.com/en/code-security/secret-scanning/introduction/about-push-protection) - [MITRE ATT&CK T1552.001: Unsecured Credentials: Credentials In Files](https://attack.mitre.org/techniques/T1552/001/) - [Stripe Docs: API keys](https://docs.stripe.com/keys) - [Firebase: Learn about using and managing API keys](https://firebase.google.com/docs/projects/api-keys) - [Supabase Docs: Understanding API keys](https://supabase.com/docs/guides/api/api-keys) *Educational guidance. Check only systems you own or are authorised to test.*