Public Env Guard

Reads a .env the way the bundler does and flags secrets in public variables, so a secret key isn't shipped in the client bundle.

StopsHelps catch a secret key shipped in the client bundle

Installation

pnpm dlx shadcn@latest add https://bollard-ui.vercel.app/r/public-env-guard.json

The files land in your project. They're yours to edit.

Usage

public-env-guard-usage.tsx
import { PublicEnvGuard } from "@/components/ui/public-env-guard"

return <PublicEnvGuard framework="next" defaultValue={env} />

What it catches

  • Public-prefixed variables that reach the browser, per framework
  • Secret key shapes (for example Stripe secret keys) in public variables
  • Supabase service_role JWTs
  • Passwords inside connection URLs
  • Secret-sounding names and high-entropy values, while letting publishable keys through

Props

PublicEnvGuard props
PropTypeDefault
value

Controlled .env text.

string—
defaultValue

Initial text when uncontrolled.

string""
onValueChange

Called with the new text on every edit or fix.

(value: string) => void—
framework

Which bundler reads this file. Decides which prefixes are public; "auto" treats every known prefix as public.

EnvFramework"auto"
publicKeys

Keys you've confirmed are meant to be public, so they're never flagged.

string[][]
onMarkPublic

Called when the user marks a flagged key as public.

(key: string) => void—
onMoveToServer

Called after a key is moved off the client, so you can open a rotation task.

(from: string, to: string | null) => void—
label

Visible label; shorthand for `messages.label`.

string—
messages

Every visible string. `{key}`, `{fix}`, `{prefix}`, `{shape}`, `{framework}`, `{count}` are filled in.

Partial<PublicEnvGuardMessages>—

* required. Also accepts the native props of <textarea> (className, aria-*, data-*…), passed through to it.

Accessibility

  • Visible label; textarea has aria-invalid when errors exist and is described by the status
  • Status uses role=status; icons are aria-hidden
  • Focus returns to a nearby control after a fix

Only what the source does. Test with your own screen reader and content before shipping.

Limitations

This is a UX guard rail. Enforce on the server: Scan built bundles in CI for secrets and keep secret keys only in server-side environment variables.

  • Reads the .env text you paste; secrets set elsewhere (CI, hosting dashboard) aren't seen
  • High-entropy detection can flag harmless IDs
  • Renaming doesn't un-leak a key already shipped; rotate it

What it stops

Helps catch a secret key shipped in the client bundle

Also known as

next_public · vite_ · react_app_ · .env · env vars · supabase service_role · api key · client bundle

Dependencies

Pairs well with