Overview
envtrap protects against five distinct exfiltration paths that malicious packages commonly exploit:Network (HTTPS/HTTP)
Outbound requests carrying secrets in headers, body, or URL
stdout / stderr
Console output and log streams leaking secrets
child_process
Spawned subprocesses inheriting secrets via
options.envDNS
Secrets encoded in DNS subdomain queries to bypass HTTP monitoring
Entropy Detection
Statistical screening of high-entropy strings in
process.env and .env1. Network Channel (network)
Risk: Any npm package can make outbound HTTPS requests and silently include secrets in headers or the request body. Standard firewalls only filter by IP/domain β they cannot inspect encrypted TLS payloads.
What envtrap does:
- Starts a local MITM TLS proxy on
127.0.0.1 - Routes all HTTP/HTTPS traffic from the child process through it via
HTTP_PROXY/HTTPS_PROXY - Decrypts TLS connections using an in-memory CA (the child trusts it via
NODE_EXTRA_CA_CERTS) - Scans the decrypted URL path, request headers, and request body for loaded secrets
blockmode: Responds with403 Forbidden(HTTP) or destroys the socket (HTTPS CONNECT)warnmode: Logs the leak and forwards the request to the real upstream server
exclusions.domains to bypass proxy scanning for known-safe traffic:
2. stdout / stderr Channels (stdout, stderr)
Risk: Logging frameworks, debug utilities, and error handlers frequently serialize full request objects or environment maps β leaking secrets into log streams that may be stored, shipped to log aggregators, or captured by AI coding tools.
What envtrap does:
- Pipes the child processβs
stdoutandstderrstreams to the parent envtrap process - Scans every chunk for active secret values
- Redacts matched secrets:
[REDACTED: SHA256:<8-char hash>] blockmode: SendsSIGTERMto the child process immediately after the first detectionwarnmode: Redacts the output and logs the leak, child continues running
exclusions.paths, the hook pre-redacts the content to [REDACTED: PATH_EXCLUDED] inside the child process before it reaches the parent scanner, suppressing the alert entirely.
Example:
3. child_process Channel (child_process)
Risk: child_process.exec and spawn can be called with an explicit options.env object. Malicious code can pass the full process environment (containing all secrets) to shell utilities like curl, wget, or python to exfiltrate data.
What envtrap does:
- Wraps
spawn,spawnSync,exec,execSync,execFile,execFileSync, andfork - Checks if
options.envcontains any active secret value by exact key match - Works for both ESM (
import) and CommonJS (require()) β via module.register() hooks and Module.prototype.require patching respectively blockmode: Throws a synchronousErrorbefore any OS fork happenswarnmode: Reports the leak to the parent via stderr, subprocess execution continues
exclusions.paths, the check is skipped entirely.
Example attack blocked:
4. DNS Channel (dns)
Risk: DNS lookups bypass most HTTP-layer firewalls. Attackers can encode secrets in subdomain labels (e.g., c3RyaXBlX3NlY3JldA==.attacker.com) and recover them server-side from DNS query logs β without making a single HTTP connection.
What envtrap does:
- Intercepts all
node:dnsAPI calls:lookup,resolve,resolve4,resolve6,resolveAny,resolveCname,resolveMx,resolveNaptr,resolveNs,resolvePtr,resolveSoa,resolveSrv,resolveTxt, and alldns.promises.*equivalents - Checks the target hostname for exact matches of any registered secret value
- Performs high-entropy subdomain analysis: splits the hostname on
., checks each label β if a label has length β₯entropy.minLengthAND Shannon entropy β₯entropy.threshold, a tunneling warning is emitted regardless of whether a secret was matched - Works for both ESM and CommonJS
blockmode: Throws a synchronousErrorbefore any network packet is sentwarnmode: Emits the warning and allows the lookup to proceed
5. Entropy Detection (Secret Candidate Screening)
Risk: Randomly generated API tokens, encryption keys, and internal credentials may not match any known regex pattern, but they are just as sensitive. What envtrap does:- At startup, screens every
process.envvalue and.envfile entry through a Shannon entropy gate - Values with entropy β₯
entropy.threshold(default:3.5) AND length β₯entropy.minLength(default:12) are registered as active secrets - Once registered, they are protected across all five channels exactly like pattern-matched secrets
