Back to Blog
OpsecForge EditorialSecuritySources reviewed 2026-10-28

How to Sanitize .env Files Before Sharing: A Developer's Guide

Prepare a minimal .env example for AI, support, or a bug report. Try a browser-local redaction walkthrough, review missed values, and learn what to do after a leak.

Primary source: authoritative reference

Short answer: share the smallest configuration example needed to reproduce the problem, preferably with invented values. If an existing snippet needs redaction, make a draft with the browser-local sanitizer, then review it line by line. A clean-looking result is not proof that all confidential data was removed.

This walkthrough is for preparing a support message, bug report, or AI prompt. For production credential storage, use the separate environment-variable security guide. For an existing exposure, follow the leak-response guide.

1. Start with a synthetic example

Do not copy an entire production .env file just to demonstrate a configuration problem. Retain only the variable names and relationships the recipient needs. The following values are invented and do not authenticate to any service:

API_KEY=example-not-a-real-key
DATABASE_URL=postgresql://demo:example-password@db.example.test/app
PUBLIC_KEY=public-demo-value
SUPPORT_REFERENCE=customer-demo-reference

Try this example, not a real credential

Paste the four synthetic lines into the sanitizer. Compare its draft with the expected output below, then check what remains visible.

Review a .env snippet locally →

2. Compare the redacted draft

The current OpsecForge sanitizer produces this output for that exact example:

API_KEY=[REDACTED]
DATABASE_URL=postgresql://demo:[REDACTED]@db.example.test/app
PUBLIC_KEY=public-demo-value
SUPPORT_REFERENCE=customer-demo-reference

The named API-key field and URL password are masked. The public-key example and ordinary reference field remain unchanged. The hostname, database path and username also remain visible: preserving useful structure does not mean those details are appropriate to disclose.

This example is covered by an automated regression test. It demonstrates specific behavior, not exhaustive secret detection.

3. Review what the tool cannot know

Before copying the result, ask:

  • Does an ordinary-looking field contain a real customer identifier, internal hostname, email address or custom credential? Replace it with an invented value if it is not needed.
  • Is a value incorrectly left visible because its name or format is unfamiliar? Mask it manually. Detection patterns cannot identify every organization's secrets.
  • Was useful non-secret text masked? Restore only the minimum verified non-confidential context; do not restore a real credential just to make the reproduction run.
  • Are comments, connection-string parameters, screenshots or nearby log lines carrying information outside the edited snippet? Review the material you will actually send, not just this output panel.
  • Does the example still communicate the issue without granting access? Use placeholder values and explain that it is a redacted reproduction.

The current tool makes a heuristic pass over sensitive named fields, selected provider-token patterns, credentials in URLs, request headers, cURL fragments and private-key blocks. It deliberately avoids some public-key and key-identifier near misses. It does not classify every 40-character string as an AWS secret, validate credentials, scan your repository, or understand the confidentiality of every field.

4. Share only the reviewed excerpt

Copy the draft only after reviewing it, remove unrelated lines, and use the support or collaboration channel approved by your organization. A placeholder .env.example is often a better starting point than a redacted production configuration.

The loaded sanitizer processes text in the browser without sending tool inputs to an OpsecForge processing backend. This is not a promise that the entire page is network-free: resources and aggregate analytics may still load. Browser extensions, a compromised device, the clipboard and the service receiving your message are outside the redaction tool's protection. See the site's privacy boundary.

Worked examples: what the sanitizer does with common shapes

The synthetic walkthrough above covers a plain assignment and a URL password. Real files contain more shapes. Each pair below is an input and the output of the current sanitizer, and each is asserted in the repository's test file (lib/safe-share-sanitizer.test.ts), so the article and the code are checked against each other. All values are invented.

An export prefix, quotes and a trailing comment. The name is recognized, the quoted value is replaced, and everything around it stays:

# input
export DB_PASSWORD="example-not-real pass" # prod
# output
export DB_PASSWORD="[REDACTED]" # prod

The space inside the quotes did not matter because the whole quoted span is replaced. Note what survives: the comment. If the comment had said which customer the database belongs to, it would still be there.

A Bearer header pasted from cURL. Header patterns apply anywhere in the text, including inside a quoted command line:

# input
curl -H "Authorization: Bearer EXAMPLE-not-real-token" https://api.example.test/v1
# output
curl -H "Authorization: Bearer [REDACTED]" https://api.example.test/v1

The scheme word stays, the token goes, and the URL is untouched. The same pass also covers X-Api-Key, Cookie, curl -u user:password and query parameters such as access_token=.

A multi-line private key block. A PEM block between its BEGIN and END lines is replaced as a unit, whatever the number of body lines:

# input
NOTE=before
-----BEGIN RSA PRIVATE KEY-----
EXAMPLEnotreal1
EXAMPLEnotreal2
-----END RSA PRIVATE KEY-----
NEXT=1
# output
NOTE=before
-----BEGIN PRIVATE KEY-----
[REDACTED]
-----END PRIVATE KEY-----
NEXT=1

Two details are visible here. The key type (RSA) is normalized away, so the output does not reveal it. And a BEGIN line with no matching END line, which happens when a paste is cut off, is masked from the BEGIN line to the end of the text rather than left alone.

Where it misses: three gaps

These are cases the current tests assert as unchanged output. Treat them as a checklist for your own review.

A custom name. Detection by field name looks for words such as password, secret, token, api key and a trailing key. A name outside that vocabulary is not masked unless the value itself looks like a known token format:

# input and output are identical
INTERNAL_LOOKUP=hunter-test-value
CUSTOMER_EMAIL=jane@example.test

If INTERNAL_LOOKUP holds a credential in your system, the tool cannot know that. The email address is personal data and is also left as is.

A commented-out assignment. The line pattern expects a name at the start of the line, so a line that begins with # is not read as an assignment:

# input and output are identical
# DB_PASSWORD=old-example-password
APP_NAME=demo

Old values commented out "just in case" are an easy way for a real credential to ride along into a bug report. Delete such lines from the excerpt.

A multi-line value under an ordinary name. The sanitizer follows quoted values across lines only when the name is sensitive. MULTI="line1 followed by line2" on the next line, with a neutral name, is returned unchanged, even if the content is confidential.

Format-based matching (the prefix patterns for several provider tokens) can still catch a token under a neutral name, but a custom format has no such pattern.

Generate a .env.example from the real file

Often the best excerpt is not a redacted copy but a skeleton: the variable names and comments, with no values. The script below keeps full-line comments and blank lines, keeps each name (and an export prefix), and empties the value. Save it outside the repository, for example as env-to-example.mjs:

// Usage: node env-to-example.mjs .env > .env.example
import { readFileSync } from 'node:fs';

const text = readFileSync(process.argv[2], 'utf8');
const out = text.split(/\r?\n/).map((line) => {
  const m = line.match(/^(\s*(?:export\s+)?[A-Za-z_][A-Za-z0-9_.-]*\s*=)/);
  return m ? m[1] : line;
});
process.stdout.write(out.join('\n'));

I ran it on this synthetic file:

# Database
DATABASE_URL=postgresql://demo:example-password@db.example.test/app
export API_KEY="example-not-a-real-key" # rotate monthly

PORT=3000
MULTI="first-line
second-line"

and got:

# Database
DATABASE_URL=
export API_KEY=

PORT=
MULTI=
second-line"

The first five lines are what you want. The last two show the limit: a quoted value that continues on the next line is not understood, so second-line" is left behind. Had that line been part of a key body, it would have leaked. The script also drops trailing comments on assignment lines (the # rotate monthly above is gone, which is usually what you want), but it keeps full-line comments, so read those too. Check the result by eye, and add placeholder hints by hand where they help, such as PORT=3000 for a non-sensitive default.

Confirm the file never reached Git

Sharing a redacted snippet is pointless if the real file is already in your repository. Three read-only Git commands answer the question:

  • git check-ignore -v .env prints the ignore pattern that matches, with its source file and line number. Exit status 0 means the path is ignored, 1 means it is not. Per the git-check-ignore manual, tracked files are not shown by default; add --no-index to check what the rules would do regardless of tracking.
  • git ls-files --error-unmatch .env returns 1 when the file is not in the index, as described in the git-ls-files manual. Success means the file is tracked, which is the bad outcome here.
  • git log --all --oneline -- .env lists commits on any ref that touched that path (see the git-log manual). Any output means a past commit contained the file, even if it is gone from the current tree.

An ignored file that is not tracked and has no history is the state you want. If the log shows commits, treat the credentials in them as exposed; rotation comes first, and the leak-response guide covers the next steps. This article does not cover rewriting history.

Parsers disagree about quotes, comments and newlines

A snippet can look the same and still be read differently by two tools, which matters when you ask someone to reproduce a bug. These points are written in the sources themselves:

  • # inside a value. The dotenv README says comments begin where a # exists, so a value containing # must be wrapped in quotes. The Docker Compose env file syntax says an inline comment on an unquoted value must be preceded by a space, so VAR=VAL# not a comment has the value VAL# not a comment. The same line can have two different values.
  • Newlines. dotenv expands \n inside double quotes and also supports real line breaks in quoted values (v15.0.0 and later, per its README). Compose documents that single-quoted values can span multiple lines.
  • Interpolation. Compose applies variable interpolation to unquoted and double-quoted values and treats single-quoted values literally, so '$OTHER' stays as written.

The sanitizer takes a conservative position: it treats # or // as a comment only when whitespace precedes it, so PASSWORD=abc#def is masked in full rather than cut at the #. A test asserts this. Masking more than necessary is safer than masking less, but a value you restore by hand may parse differently in your tool than in the recipient's.

When an AI coding agent is the recipient

Pasting a .env into a chat window puts it in the model's context. A different approach is to keep the agent from reading the file at all. OpsecForge publishes a local MCP server, opsecforge-mcp, whose redact_file tool takes only a file path. The file is read on your machine and the agent receives the redacted text plus a summary of finding types, counts and line numbers, not the original values. Per its README, it makes no network requests, and the same heuristic limits apply: it can miss custom formats, and its Claude Code hook is a best-effort guard, not a sandbox. Codex and Cursor have no equivalent hook, so there the tool only helps if the agent chooses to call it. Setup and limitations are on the agents page.

If you already shared a real secret

Redacting a new message does not invalidate a previously exposed credential. GitHub's leaked-secret remediation guidance prioritizes provider-side revocation, updating affected services and checking for unauthorized use; deleting the visible copy alone is insufficient. Coordinate remediation with the secret owner and provider, then address reachable copies.

OWASP's Secrets Management Cheat Sheet treats rotation and revocation as parts of a credential lifecycle. Choose policy appropriate to the credential and system, rather than treating a universal quarterly schedule as a substitute for leak response.

Next step: try the synthetic snippet in the sanitizer, or use the incident-response checklist if an actual credential escaped.

Share this: