Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 3 min read

Redacting Secrets Without Breaking the Code Your AI Agent Reads

TokenCap builds context packs from your repository for AI coding agents. Every file read, git diff and snippet passes through one redaction function before it is written anywhere. In v3.1 that function got stricter about

TokenCap builds context packs from your repository for AI coding agents. Every file read, git diff and snippet passes through one redaction function before it is written anywhere. In v3.1 that function got stricter about secrets and more careful about code.

Both directions matter. A missed secret is a leak. A false positive is quieter but still costly: if const secret = process.env.SECRET gets rewritten, the agent loses the one line that tells it where the value comes from.

More secrets caught

Here is a config/production.env file and what ends up in the pack:

# input
DB_PASSWORD=hunter2hunter2
GITHUB_TOKEN=ghp_EXAMPLE_NOT_A_REAL_TOKEN
export SECRET_KEY=changeme-changeme
DB_URL=postgres://app:[email protected]:5432/app

# output
DB_PASSWORD=[REDACTED:SECRET]
GITHUB_TOKEN=[REDACTED:SECRET]
export SECRET_KEY=[REDACTED:SECRET]
DB_URL=postgres://app:[REDACTED:SECRET]@db.internal:5432/app

Prefixed keys (DB_PASSWORD, GITHUB_TOKEN) and shell export KEY=value lines are matched, including hex and UUID values. In a connection string only the password goes. The user name and host stay, because "which database" is useful context and not a secret.

Quoted keys in JSON and YAML are covered too:

{ "user": "admin", "password": "[REDACTED:SECRET]" }

So are HTTP Basic auth and curl -u:

curl -u deploy:[REDACTED:SECRET] https://ci.example.com/api
-H "Authorization: Basic [REDACTED:SECRET]"

Two bypasses closed

  • UTF-16 files. PowerShell 5.1 writes UTF-16 by default. Those files used to slip past redaction. They no longer do.
  • *.env and *.env.* files. A name like production.env is now treated as an env file. JS/TS modules such as app.env.ts are still treated as code.

The high-entropy check also stopped skipping secrets on long lines that only look minified, and known key prefixes (ghp_, sk-, AKIA) are never exempt.

Code references stay intact

This is the part that changes what agents see. In a source file, a value that is a reference, a type or a call is left as it is:

// src/auth.ts, after redaction
const secret = process.env.SECRET;
const token = getToken(user);
function login(password: string) {}
const OPENAI_API_KEY = '[REDACTED:KEY]';
const conn = "postgres://app:[REDACTED:SECRET]@db.internal:5432/app";
# app/config.py, after redaction
password = self.PASSWORD
secret_key = "[REDACTED:SECRET]"

The rule is simple to state. A quoted literal or an opaque value is redacted. An identifier, member access, type or call is not, because it is not the secret. It is where the secret comes from. Outside source files, everything is redacted as before.

Redaction also keeps the original separator and quotes now. 3.0 wrote OPENAI_API_KEY=[REDACTED:KEY]. 3.1 writes OPENAI_API_KEY = '[REDACTED:KEY]', so redacted JSON and YAML stay valid. If you match the old form anywhere, update it.

Fail closed

The Chrome companion gets its context from a loopback bridge on 127.0.0.1. Before v3.1, if redaction threw, the bridge could still send the context, and a secret in the project primer could reach a browser chat.

Now the bridge stops:

500 {"error": "redaction_failed", "detail": "Secret redaction failed; context withheld."}

No context is better than unredacted context. That is the rule for every path that leaves the repository.

Custom rules apply everywhere

tokencap.security.yaml now applies to task packs, review packs and constitution artifacts as well. One invalid customRules entry no longer discards the whole file, and an inline # comment no longer switches a setting off.

To check your own generated output:

tokencap analyze security scan

It no longer reports [REDACTED:*] markers as leaks, and it never writes secret prefixes into its report.

The full list of redaction fixes is in the v3.1 changelog. Docs and install: tokencap.vansharora.app

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.