Most leaked credentials aren't the result of a clever attack. Someone pastes a key into a config file to test something, it works, and the file gets committed along with everything else. A few months later the repo goes public, gets shared with a contractor, or gets cloned onto a laptop that later goes missing. This post covers a small, boring setup that makes that kind of mistake much harder: where secrets should live, how to catch them before they're committed, what's different for mobile apps, and what to do when one gets through anyway.
First, decide what counts as a secret
Before you add any tools, make a list. For a typical small studio it looks something like this:
- Server-side credentials: database passwords, payment provider secret keys, LLM API keys, SMTP passwords, cloud storage access keys.
- Signing material: App Store Connect API keys (
.p8files), Android keystores and their passwords, code-signing certificates. - Deployment access: SSH private keys, CI tokens, DNS provider API tokens, Ghost Admin API keys.
- Things that look harmless but aren't: webhook URLs with an embedded token, and analytics or crash-reporting keys that also allow writes or deletes.
Some values are only identifiers, like a public ad unit ID or a publishable payment key that's meant to sit in client code. Those can go in the repo. The test is simple: if someone who has this value can spend your money, read your users' data, or act as you, it's a secret.
Where secrets should live instead
Local development: env files that git ignores
The simplest pattern that holds up is a committed template and an ignored real file:
# .env.example (committed)
DATABASE_URL=
OPENAI_API_KEY=
GHOST_ADMIN_KEY=
# .gitignore
.env
.env.*
!.env.example
*.p8
*.keystore
*.jksThe template documents which variables exist and stays in version control. The real .env never gets added. Put the ignore rules in place before anyone creates the file. If the file was already tracked, adding it to .gitignore does nothing, and you also need git rm --cached .env.
Have your app fail loudly when a required variable is missing, so nobody falls back to hardcoding one "just for now":
const required = ["DATABASE_URL", "GHOST_ADMIN_KEY"];
for (const name of required) {
if (!process.env[name]) {
throw new Error(`Missing required env var: ${name}`);
}
}On your Mac: the keychain beats a dotfile
For scripts you run yourself, like release scripts or publishing helpers, the macOS keychain is a better home than a plaintext file in your home directory:
# store once (prompts for the value, so it stays out of shell history)
security add-generic-password -a "$USER" -s ghost-admin-key -w
# read in a script
GHOST_ADMIN_KEY="$(security find-generic-password -a "$USER" -s ghost-admin-key -w)"One caveat: scripts started by launchd may run without access to an unlocked login keychain, depending on how the job is configured. If an unattended job suddenly can't read its key, that's the first thing to check. Make sure the failure is visible, too. The post on scheduling jobs with launchd and cron without silent failures covers how.
Servers and CI: the platform's secret store
CI systems have encrypted secret settings, and so do most hosting platforms. On a plain VPS, a root-owned env file with tight permissions, loaded by your service manager, works well:
# /etc/myapp/env (chmod 600, owned by root)
DATABASE_URL=postgres://...
# systemd unit
[Service]
EnvironmentFile=/etc/myapp/envKeep one rule across all of these: production values exist only in production. Developers get separate keys with low limits, so a leaked dev key is an annoyance rather than an incident.
Catch secrets before they're committed
Ignore rules only cover files you thought of in advance. They do nothing about a key pasted into config.ts. A pre-commit scanner covers that gap. Gitleaks, TruffleHog, and git-secrets all work this way: they look for patterns like known key formats and high-entropy strings and block the commit when they find a match.
With the pre-commit framework, setting up gitleaks is a short config file:
# .pre-commit-config.yaml
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: vX.Y.Z # pin to a current release tag
hooks:
- id: gitleakspre-commit installLocal hooks are opt-in. Anyone can skip them with --no-verify or by never running pre-commit install. So run the same scan in CI on every push, and treat a finding there as a failed build. If you host on GitHub, check whether secret scanning and push protection are available for your repos and turn them on. They catch many common provider key formats on the server side, whatever is or isn't installed locally.
Handling false positives
Scanners will flag test fixtures, sample tokens in documentation, and random-looking hashes. Don't disable the scanner. Use its allowlist mechanism (gitleaks supports a config file and inline ignore comments) and keep the allowlist small enough to review. If the allowlist keeps growing, your fixtures probably look too much like real keys. Make them obviously fake, like sk_test_FAKE_FAKE_FAKE.
Scan the history you already have
A new hook only protects future commits. Run a full-history scan once on every existing repo. Most scanners have a mode that walks all commits. Older repos often contain things nobody remembers adding, like a staging database password from three years ago that still works.
The mobile app problem
App developers have a harder version of this problem. Anything shipped inside an app binary can be extracted. Obfuscation, splitting the string, or moving it into an .xcconfig or local.properties file keeps it out of git, but not out of the IPA or APK. Anyone with the binary and a few free tools can pull out string constants.
So for mobile, the question isn't "how do I hide this key in the app". It's "should this key be in the app at all":
- Keys meant for clients (publishable payment keys, ad SDK app IDs, crash-reporting DSNs that only allow ingestion) are fine in the app. Use build configuration files mainly so you can have separate dev and prod values.
- Keys that cost money or grant broad access, like LLM provider keys, belong behind a small server endpoint of your own. That endpoint can check requests, rate-limit them, and set a spending cap. The app calls your endpoint, and the endpoint calls the provider. Keeping LLM feature costs predictable covers the budgeting side of that proxy.
- Signing material never goes in the repo. Store keystores and
.p8files in CI secrets (base64-encoded if the platform only accepts text) and write them to disk only for the duration of the build.
When a secret leaks anyway
Work through these steps in order. The order matters.
- Revoke and rotate first. Create a new key, deploy it, then revoke the old one. Assume the old value is compromised as soon as it's been pushed anywhere, even to a private repo.
- Check usage. Look at the provider's dashboard or logs for activity on the old key that you don't recognize, and note the time range you looked at.
- Then clean history, if it's worth it. Tools like
git filter-repocan rewrite history to remove a file or replace a string. Every clone, fork, and cache still holds the old commits, though, which is why rotating comes first. Rewriting history is cleanup, not containment. - Find out how it got in. Was the file missing from the ignore rules? Was the hook not installed? Fix that so the same thing can't happen again.
# replace a leaked value everywhere in history
echo 'sk_live_LEAKEDVALUE==>REMOVED' > replacements.txt
git filter-repo --replace-text replacements.txtCoordinate before force-pushing rewritten history. Everyone else will need to re-clone or reset their copies.
A checklist for each repo
.gitignorecovers env files, key files, and keystores, and.env.exampleis committed.- The app fails at startup if a required secret is missing.
- A pre-commit secret scanner is installed, and the same scan runs in CI.
- The full history has been scanned at least once.
- Dev and prod use separate keys, and dev keys have low limits.
- No key that costs money ships in a mobile binary.
- You have a written, three-line rotation procedure for each critical key, including where it's stored and who can revoke it.
Wrapping up
You don't need a vault product or a security team for this. Most of the protection comes from three habits: secrets live outside the repo by default, a scanner checks every commit locally and in CI, and leaks are handled by rotating first and cleaning up second. Setting it up takes an afternoon per project, and that's much cheaper than finding out about a leaked key from your provider's bill.