Our config validation required a JWT secret of at least 32 characters. The placeholder in .env.example was 40 characters long. It passed, and so did everyone who copied the repo.

This is a short story about three doors we left open in a NestJS codebase we maintain, all with the same root cause: a value that is published in the repository was treated as if it were secret. If your project has an .env.example, there's a good chance at least one of these applies to you.

The bug in one paragraph

Here's the shape of the check we had (simplified, standalone example):

import { z } from 'zod'; const environmentSchema = z.object({ JWT_ACCESS_SECRET: z.string().min(32), }); 

And the line in .env.example:

JWT_ACCESS_SECRET="replace-with-a-long-random-access-secret" 

That placeholder is 40 characters, so the validator is happy. Anyone who has read the repo (or the template, or the blog post that copied it) now knows the key your API uses to sign access tokens. With it they can mint a valid token for any user ID they can find, and if your guards trust a role claim inside the token, they can mint an admin.

Why length checks aren't secret checks

A validator checks shape. min(32) answers "is this string long enough?" It can't answer "does anyone else know this string?" The placeholder was designed to look like a real secret so people would recognise where to put one. That's exactly what made it pass.

The same applies to anything else in the example file: the seed admin password, SMTP credentials, API keys for sandboxes. If it's in git, assume it's public.

Fix 1: reject the values you published

The cheapest fix is a denylist of every value you've ever shipped in an example or fixture file:

export const placeholderSecrets = new Set([ 'replace-with-a-long-random-access-secret', 'change-this-before-seeding', // and the values from .env.test.example (see "the fourth door" below) ]); const environmentSchema = z.object({ JWT_ACCESS_SECRET: z .string() .min(32) .refine((value) => !placeholderSecrets.has(value), 'Replace the example JWT secret'), }); 

Be clear about what this is: an exact-match seatbelt. replace-with-a-long-random-access-secret2 still passes. It catches the common case (copied and forgot), not someone determined to use a weak value.

Fix 2: ship the secret empty and generate it

The better fix is to not ship a value at all. In our .env.example the line is now:

# Generate unique secrets of at least 32 characters for every environment. JWT_ACCESS_SECRET= 

An empty string fails min(32), so the app won't boot until something fills it. The setup script fills it with real randomness the first time it creates .env (simplified excerpt):

umask 077 if [ ! -f .env ]; then cp .env.example .env secret="$(openssl rand -base64 48 | tr -d '\n')" sed -e "s|^JWT_ACCESS_SECRET=.*|JWT_ACCESS_SECRET=\"$secret\"|" .env > .env.tmp mv .env.tmp .env fi chmod 600 .env 

Two small details matter here. umask 077 means the file is created readable only by its owner. And base64 output never contains | or &, so it can't break the sed expression.

Fix 3: stricter rules in production

Some settings are fine on a laptop and dangerous on a server. We added a second layer that runs only when NODE_ENV=production (simplified):

if (env.NODE_ENV === 'production') { if (env.JWT_ACCESS_SECRET.length < 43) { throw new Error('JWT_ACCESS_SECRET must be at least 43 characters in production'); } if (env.DOCS_ENABLED) throw new Error('DOCS_ENABLED must be false in production'); if (!env.SECURE_COOKIES) throw new Error('SECURE_COOKIES must be true in production'); } 

Why 43? That's the length of 32 random bytes in base64 without padding, about 256 bits. But be honest with yourself: 'a'.repeat(43) also passes. Length is a proxy for entropy, not proof of it. The generator in fix 2 is what actually gives you entropy.

The second door: the seed script

This one surprised us. The app refused to boot with the placeholder, so we thought we were done. But the seed script read process.env directly:

// before (simplified) const adminPassword = process.env.ADMIN_PASSWORD!; 

The documented setup copied .env.example (which then contained ADMIN_PASSWORD="change-this-before-seeding") and ran the seed before the app ever started. So the database got an admin with the public password. The app then refused to boot, the developer edited .env, the app booted… and the database still held the hash of the placeholder. Nothing re-seeded it.

The lesson: your app's validator only protects your app. Every entry point that reads config needs the same checks: seed scripts, migrations, workers, one-off CLIs. Share the list instead of copying it:

// config/environment.ts export function assertSeedPassword(password: string): void { if (password.length < 12 || placeholderSecrets.has(password)) throw new Error('Set a real ADMIN_PASSWORD of at least 12 characters before seeding.'); } // prisma/seed.ts import { assertSeedPassword } from '../src/config/environment.js'; assertSeedPassword(adminPassword); 

We also stopped putting the admin password in .env at all. Setup generates it into a separate owner-only file and passes it only to the seed command, so it doesn't end up in the running API's environment.

The third door: an upsert that rewrites the password

Our seed used to look like this (simplified):

await prisma.user.upsert({ where: { email }, update: { passwordHash }, // runs on every seed create: { email, passwordHash, role: 'ADMIN' }, }); 

Run the seed again with an old .env, or point it at the wrong database, and the real admin password is silently replaced. Seeds should create, never overwrite:

const result = await prisma.user.createMany({ data: [{ email, passwordHash, role: 'ADMIN' }], skipDuplicates: true, // INSERT ... ON CONFLICT DO NOTHING }); const created = result.count === 1; 

Thinking like an attacker here: two seeds running at once can't create two admins, because the unique constraint on email plus ON CONFLICT DO NOTHING settles the race in the database. And if someone registered the admin email through the public sign-up form before you seeded, that account is not promoted. It stays a normal user, and the seed logs "already present, unchanged" with its role so you notice.

The fourth door: test fixtures

A later review found one more: .env.test.example had its own published admin password and a 58-character JWT secret. Those passed every check, including the 43-character production rule. Someone starting from the test file would ship a key every other copy of the repo shares. Fixture values go into the same denylist, and CI now generates random secrets instead of reusing them.

How to test it

We have unit tests for both entry points (Vitest):

it('rejects published placeholder secrets', () => { expect(() => validateEnvironment({ ...base, JWT_ACCESS_SECRET: 'replace-with-a-long-random-access-secret' }), ).toThrow('Replace the example JWT secret'); }); it('rejects placeholder or short admin passwords before seeding', () => { expect(() => assertSeedPassword('change-this-before-seeding')).toThrow(); expect(() => assertSeedPassword('short')).toThrow(); expect(() => assertSeedPassword('long-random-test-password')).not.toThrow(); }); 

Two manual checks take a minute, and are worth doing on your own project right now:

  1. cp .env.example .env and start the app. It should refuse to boot.
  2. Run the seed with the example password (for example ADMIN_PASSWORD=change-this-before-seeding). It should exit with an error before touching the database.

One idea we'd add if we were starting again: a test that parses .env.example itself with dotenv.parse and asserts that validation fails. Then a future edit can't quietly put a "working" secret back in the example.

A checklist to paste into a PR

  • [ ] .env.example ships secrets empty, not with placeholders.
  • [ ] Setup generates secrets (openssl rand -base64 48) and writes .env with mode 600.
  • [ ] Every published example or fixture value is in a shared denylist.
  • [ ] Seeds, migrations, workers and CLIs use the same validation as the app.
  • [ ] Seeds create accounts; they never update existing ones.
  • [ ] Production has stricter rules (secret length, docs off, secure cookies).

If you've already shipped a placeholder

Rotate the signing secret (this invalidates every access token signed with the old one, which is what you want), revoke refresh sessions too, reset the admin password, and check your audit or access logs for activity you don't recognise from the time the placeholder was live.

When not to bother

A throwaway demo or a workshop repo where the example values are the point. Even then, mark them loudly (DEMO_ONLY_NOT_A_SECRET) so nobody mistakes the repo for a starting point.

What's in your .env.example right now, and would your seed script accept it?