How to detect secrets in outbound HTTP traffic

By the CapyShield team · · 5 min read

In short

Detecting secrets in outbound traffic means scanning each request in a local proxy before it leaves, using four checks: known token formats, the exact values of your own sensitive environment variables in common encodings, entropy on short values, and host policy. Pattern lists miss unstructured secrets like database passwords, which environment fingerprints catch. Most false positives come from legitimate authentication, so a good rule considers both where in the request a secret appears and which host receives it.

Secret scanners usually look at code at rest: a commit, a repository, a container image. Outbound traffic is a different problem. The secret is not sitting in a file you wrote; it is in a request a dependency's install script or an AI agent is about to send, and you get one look at it before it leaves. This post explains the four techniques that work on live traffic, what each one misses, and where false positives come from.

How do you detect secrets in outbound HTTP traffic?

You put a proxy between the process and the network and scan every request before forwarding it. Four checks cover most real leaks, and they catch different things:

  • Known token formats. Many providers issue credentials with a recognizable prefix and shape, such as AKIA for AWS access key IDs, ghp_ for GitHub personal access tokens or sk-ant- for Anthropic keys. A regex per format finds them anywhere in the request.
  • Your own environment values. The process already has its secrets in environment variables. Looking for those exact values, in common encodings, catches credentials that have no recognizable format, such as a database password.
  • Entropy. Random-looking strings in places that rarely carry them, such as query parameters and custom headers, are a weaker but format-free signal.
  • Host policy. The cheapest check of all: a request to a host the build has never needed is suspicious regardless of its contents.

Payload checks need to see the payload. Over HTTPS that means terminating TLS locally; with SNI-only inspection you only get the destination host and a byte count.

How does known-format secret detection stay fast?

Running a few hundred regexes over every request body would be slow. The standard trick is a keyword prefilter: each rule declares a literal that must appear for it to match (ghp_, AKIA, sk-ant-), and a single Aho-Corasick automaton finds all those literals in one pass over the bytes. Only rules whose keyword actually occurred run their full regex. Most requests contain none of the keywords, so most requests cost one linear scan.

This is the same approach used by repository scanners such as Gitleaks, and GitHub publishes the list of token formats its own secret scanning recognizes. The format list is the part that needs constant upkeep: providers add prefixes, and a token type without a distinctive prefix will never be a reliable regex.

CapyShield's scanner ships 210+ credential formats behind an Aho-Corasick prefilter, including AWS, GCP, Azure, GitHub, GitLab, Stripe, OpenAI, Anthropic, Slack, database URIs and private keys. Findings carry the rule id, never the matched value.

Why match environment variable values instead of patterns?

Because the most damaging secrets are often the least structured. DB_PASSWORD=hunter2-prod has no prefix and no checksum; no pattern list will ever recognize it. But the guarding process can read its own environment at start-up, and it knows the exact bytes to look for.

The details that make this work in practice:

  • Pick variables by name. Fingerprint values whose names contain parts like KEY, SECRET, TOKEN or PASSWORD, and skip obviously public ones such as PATH and HOME.
  • Match encodings, not just the raw value. Exfiltration code rarely sends a value verbatim. Matching the plain, Base64, URL-encoded and hex forms closes the cheapest evasions.
  • Skip very short values. A three-character value would match half the internet. CapyShield ignores values under 8 characters.
  • Never log the value. The match should be reported as env:DB_PASSWORD, so the log that records a leak doesn't become a second leak.

In CapyShield these are config keys under egress.scan:

yaml
egress:
  tls_inspection: mitm
  scan:
    builtin_secret_patterns: true
    runtime_env_fingerprints: true
    env_include: ["DEPLOY_HOOK_URL"]             # extra names to fingerprint
    env_exclude: ["PATH", "HOME", "PWD", "LANG"]

A blocked request then shows up with the variable name only, for example secret_leak:env:AWS_SECRET_ACCESS_KEY, and guard exits with code 2.

Is entropy-based secret detection reliable?

On its own, no. Shannon entropy measures how evenly a string uses its characters, and random tokens score high. So do UUIDs, content hashes, session ids, cache-busting parameters and compressed data, none of which are secrets. An entropy check that runs on every byte of every request would block normal traffic all day.

It becomes useful when you narrow where it looks:

  • Only short, isolated values: individual query parameter and custom header values, not whole bodies.
  • Only long enough tokens: CapyShield requires 24+ characters and a configurable entropy_threshold (4.2 bits per character in the example config).
  • Only hosts outside the allowlist, where a random-looking parameter is genuinely odd.
  • Known high-entropy formats skipped, such as Subresource Integrity hashes like sha512-… that package managers send constantly.

Treat entropy as a tripwire for unknown destinations, not as your primary detector.

Where do false positives come from, and how do you handle them?

The biggest source is legitimate authentication. An OPENAI_API_KEY in an Authorization header going to api.openai.com is the whole point of having the key. The same key in a request body, a URL, or sent to any other host is a leak. A good rule therefore depends on both where in the request the secret appears and which host receives it.

CapyShield encodes exactly that distinction:

  • A credential in an authentication header (Authorization, X-Api-Key, …) sent to an allow-listed host is treated as normal authentication.
  • The same value in a body, URL or custom header, or sent anywhere else, is a leak.
  • secret_allow grants named exceptions when a secret legitimately travels in the body:
yaml
egress:
  scan:
    secret_allow:
      OPENAI_API_KEY: ["api.openai.com"]

The other common sources are test fixtures (documented fake keys like AWS's EXAMPLEKEY values that still match a format) and generic variable names that the name heuristic picks up but which hold public data. Use env_exclude for the latter. Start in audit mode with capyshield watch, read the report, then enforce.

Why does this matter for install scripts and AI agents?

npm worms such as Shai-Hulud 2.0 ran during install, read tokens from the environment and the filesystem, and sent them out. A repository scanner sees none of that, because the secret never touches your code. An outbound check sees the request at the moment it matters. The same holds for a coding agent that has been prompt-injected into posting your .env somewhere: the detection does not depend on the agent noticing anything.

terminal
capyshield guard --config capyshield.yaml --tls mitm -- npm ci

The proxy is cooperative, so code that deliberately opens raw sockets can bypass it; for a hard boundary, run the command where its only route out is the proxy. The docs and the source on GitHub cover the full scanner pipeline.

Frequently asked questions

How do you detect a secret that has no recognizable format?
Match the exact values of the process's own sensitive environment variables, such as those named *PASSWORD* or *TOKEN*, in plain, Base64, URL-encoded and hex form. CapyShield does this in memory and reports only the variable name.
Is entropy-based secret detection reliable?
Not on its own: UUIDs, hashes and session ids are high-entropy too. It works as a tripwire when limited to long query and custom header values sent to hosts outside the allowlist, skipping known formats like Subresource Integrity hashes.
Can you scan HTTPS request bodies for secrets?
Only by terminating TLS locally with a trusted local CA, which CapyShield calls mitm mode. In SNI-only mode a proxy sees the destination host and byte counts, not the payload.
Why isn't an API key in an Authorization header flagged as a leak?
Sending a key to its own API is normal authentication. CapyShield treats a credential in an authentication header to an allow-listed host as normal, and the same value in a body, URL or custom header, or sent to any other host, as a leak.

Sources