DEV Community

Cover image for We Masked a Secrets File With a Regex. The Key Printed Anyway.
Nabil A
Nabil A

Posted on AI-assisted

We Masked a Secrets File With a Regex. The Key Printed Anyway.

The output looked fine for about three lines.

DATABASE_URL=****
APP_ID=****
PRIVATE_KEY=****
Enter fullscreen mode Exit fullscreen mode

Then it kept going, and the next lines were not masked at all. They were the body of a private key.

This is the story of how that happened on Oct 25, 2026, while I was building Since, and why it happened again on Oct 29, after we had written down the rule that was supposed to stop it.

1. A reasonable thing to ask for

We needed a list of the variables set in a production env file. Names only, no values. It is the kind of chore you hand to a coding agent without thinking twice, because the request is small and the answer looks obvious: read the file, hide everything after the =, print the rest.

So my agent did exactly that. One regex, applied to each line: keep what is before the =, replace what is after it with stars.

If every value in the file fit on one line, that would have been fine. Most did. One did not.

2. The value that was not on one line

A private key in PEM format is several lines long. In a .env file it usually sits in double quotes, like this:

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAuS7pQm2vYQ9ZJXo3nU1fakefakefakefakefakefakefake
QWERTYUIOPASDFGHJKLZXCVBNM1234567890QWERTYUIOPASDFGHJKLZXCV=
-----END RSA PRIVATE KEY-----"
Enter fullscreen mode Exit fullscreen mode

(That key is fake. I typed it for this post. The real one was not.)

A line-by-line mask sees four lines here. Only the first one has a KEY= at the start. The other three are just text, so the regex has nothing to match, and it prints them exactly as they are. The key's whole body went into the session's transcript in plain view.

Here is the same thing, run in an empty folder with the fake file above plus three ordinary variables:

sed -E 's/=.*/=****/' .env
Enter fullscreen mode Exit fullscreen mode
DATABASE_URL=****
APP_ID=****
PRIVATE_KEY=****
MIIEowIBAAKCAQEAuS7pQm2vYQ9ZJXo3nU1fakefakefakefakefakefakefake
QWERTYUIOPASDFGHJKLZXCVBNM1234567890QWERTYUIOPASDFGHJKLZXCV=****
-----END RSA PRIVATE KEY-----"
WEBHOOK_SECRET=****
Enter fullscreen mode Exit fullscreen mode

The mask did its job perfectly on every line it understood. The problem is that a .env file is not a list of lines. It is a small language with quoting rules, and a value can span lines. A regex that treats it as lines is parsing a different file from the one that is actually there.

Notice the second key line, too. It ends in =, so the mask "worked" on it: it replaced the padding with stars and printed the other 59 characters. That is about as useful as hiding the last digit of a card number.

3. The rule we wrote

We wrote it down the same day, in the file where we keep every lesson we pay for:

  • To inspect a secrets file, parse it, then print only the key names and "set" or "empty".
  • Or use a pattern that prints only the name at the start of a line: grep -oE '^[A-Z_][A-Z0-9_]*='.
  • Never print lines of a secrets file, even masked.

Three bullets. Clear enough, I thought.

4. Four days later

On Oct 29 a different agent, working on a different task, needed the variable names in the same production env file. It listed them with a line-based command. The private key's body printed into its output again.

Same file. Same key. Same mistake. A new agent, starting fresh, had no reason to know the lesson existed. The rule lived in a file, and nothing in this agent's task pointed it there.

The key was rotated after that. That part was not optional.

5. The second thing I got wrong

When I sat down to write this post, I tested the fallback from our own rule, the grep -oE one. It is a much better idea than masking: it never prints a value, only the part of a line that looks like a name followed by =.

grep -oE '^[A-Z_][A-Z0-9_]*=' .env
Enter fullscreen mode Exit fullscreen mode
DATABASE_URL=
APP_ID=
PRIVATE_KEY=
QWERTYUIOPASDFGHJKLZXCVBNM1234567890QWERTYUIOPASDFGHJKLZXCV=
WEBHOOK_SECRET=
Enter fullscreen mode Exit fullscreen mode

Look at line four. A line of a key that happens to be only capitals and digits, ending in base64 padding, looks exactly like a variable name to that pattern. I built that line on purpose to make the point, and a real 64-character key line almost always has a lowercase letter, a + or a / in it somewhere. Almost always. "Almost always" is not a property you want in the one command whose whole job is to not print a secret.

So the grep was a safer regex, not a safe one. It still guesses what the file means from what each line looks like.

6. What actually works

Ask the thing that knows the file's rules. A dotenv parser already handles quotes and multi-line values, because it has to in order to load them. Let it read the file, then print what it found, and never the values:

node -e 'const fs=require("fs");const e=require("dotenv").parse(fs.readFileSync(".env"));for(const [k,v] of Object.entries(e)) console.log(k, v?"set":"empty")'
Enter fullscreen mode Exit fullscreen mode
DATABASE_URL set
APP_ID set
PRIVATE_KEY set
WEBHOOK_SECRET set
Enter fullscreen mode Exit fullscreen mode

Four variables, four names, no key. The multi-line value is one entry, the way the app that loads it sees it.

Our rule now says only that: names through a parser, and never grep, cut, sed or cat on a secrets file. The grep line came out of the rule.

7. The fix that mattered more than the command

The command was the easy part. The real gap was that the lesson from Oct 25 did not reach the agent on Oct 29.

What changed is where the rule lives. It is not only in the lessons file any more. Every brief we write for an agent whose task touches an env file now carries the rule inside the brief itself. The agent does not have to know the lessons file exists, or think to look, or connect "list some variable names" to "a thing that once leaked a key". The rule arrives with the task.

That is less elegant than one file of wisdom that everything reads. It is also the version that works, because the agent that leaks the secret is never the one that remembers why it should not.

What I'd tell myself before

  • A .env file is a format, not a list of lines. Anything that reads it line by line is reading a different file.
  • A lesson only protects the next person, or the next agent, if it shows up where they already are. Written down is not the same as delivered.
  • When a secret prints somewhere it should not, rotate it. Then fix the command. Then fix how the rule gets to whoever runs the command next.

What's the most innocent-looking command that ever printed something it shouldn't have on your machine?

I'm Nabil. I build Since, which tracks what your code depends on and tells you before it breaks.

Top comments (0)