Docsity

Web Development, Mathematics,React.JS,Hiding Variables, Study Guides, Projects, Research of Design

Partial preview of the text

Download Web Development, Mathematics,React.JS,Hiding Variables and more Study Guides, Projects, Research Design in PDF only on Docsity!

~/app $ cat .env # API_KEY=sk_live_????????????????

Keeping Secrets Secret

Hiding environment variables & API keys in HTML/JS apps Web Development · Frontend Security Fundamentals

0 1 · T H E P R O B L E M

Everything you ship to the browser is public

View Source

Ctrl+U shows your full HTML

and any inline scripts — no

tools needed.

DevTools → Sources

Every JS bundle is

downloadable and readable,

even minified code.

Network Tab

Every request is visible —

including URLs, headers, and

query-string keys.

If it reaches the client, it is not a secret. There is no obfuscation, minification, or trick that hides a key from someone with DevTools open.

0 3 · M Y T H - B U S T I N G

A .env file does NOT hide keys in a frontend

build

.env

VITE_API_KEY=abc123secret // main.js const k = import.meta.env.VITE_API_KEY;

build ⟶

// dist/bundle.js (public!) const k= "abc123secret" ; // value is baked into the // file every visitor downloads What .env is actually for in frontend tooling

  • (^) Bundlers (Vite, Webpack, CRA) inline env values as plain strings at build time — it's configuration, not

secrecy.

  • (^) Prefixes like VITE_ / REACT_APP_ / NEXT_PUBLIC_ are a warning label: "this value will be public."
  • (^) Use them for non-secret config: API base URLs, feature flags, public identifiers.

0 4 · T H E G O L D E N R U L E

Know which keys may be public — and

which never

Golden rule: secret keys live only on a server you control. The browser gets, at most, public keys. OK in the browser (public)

  • (^) Firebase web config (apiKey is an identifier)
  • (^) Stripe publishable key (pk_...)
  • (^) Google Maps browser key (domain-restricted)
  • (^) Analytics / measurement IDs NEVER in the browser (secret)
  • (^) Stripe secret key (sk_...)
  • (^) OpenAI / Anthropic API keys
  • (^) Database URLs & passwords
  • (^) OAuth client secrets, JWT signing keys

0 5 · S O L U T I O N 1 — C O D E

A minimal Express proxy

// server.js (runs on YOUR server) import express from "express"; import "dotenv/config"; // loads .env const app = express(); app.get("/api/data", async (req, res) => { const r = await fetch("https://api.example.com", { headers: { Authorization: Bearer ${process.env.API_KEY} } }); res.json(await r.json()); }); app.listen( 3000 ); // frontend app.js (public) const data = await fetch( "/api/data" ).then(r => r.json()); // no key anywhere ✓ The secret stays in .env on the server process.env is real here — Node reads it at runtime from the machine, not from a shipped bundle.

0 6 · S O L U T I O N 2

No server? Use serverless functions

Static-site hosts can run tiny backend functions for you — perfect for HTML/JS apps with one or two secret calls. // netlify/functions/data.js export default async (req) => { const r = await fetch(URL, { headers: { Authorization: Bearer ${process.env.API_KEY} } }); return Response.json(await r.json()); }; Where to set the key

  • (^) Netlify → Site settings → Environment variables
  • (^) Vercel → Project → Settings → Environment Variables
  • (^) Cloudflare → Workers → Variables & Secrets Same idea as the proxy — the function runs on the host's servers, so the key set in the dashboard never appears in your repo or bundle.

0 8 · R E C A P

Best-practice checklist

Never ship secrets to the browser No secret keys in HTML, JS, or frontend .env builds. Proxy through a backend Express route or serverless function attaches the key server-side. Ignore .env, commit .env.example Keep real values out of git; document variable names. Restrict public keys Lock browser keys to your domain / specific APIs in the provider dashboard. Rotate on any leak Assume a committed or shipped key is compromised — revoke and reissue. Use host env settings in prod Set variables in Netlify / Vercel / your server, never in the code. If the browser can read it, so can everyone else. Keep secrets on the server.