Skip to content

fix(mcp): normalize env names before secret-pattern matching - #106

Merged
jkyberneees merged 1 commit into
mainfrom
fix/mcp-env-filter-normalization
Jul 26, 2026
Merged

fix(mcp): normalize env names before secret-pattern matching#106
jkyberneees merged 1 commit into
mainfrom
fix/mcp-env-filter-normalization

Conversation

@jkyberneees

Copy link
Copy Markdown
Contributor

Summary

Fixes the reported finding (verified against current code before fixing):

MCP env-filter misses non-underscore secret spellings — LOW/INFO. isSensitiveEnvVar blocks substrings API_KEY, TOKEN, SECRET, … in variables forwarded to MCP subprocesses, but environment variable names may legally contain hyphens or no separator: an override named API-KEY or APIKEY does not match API_KEY and is forwarded to a potentially untrusted MCP server. (Matching against the inherited environment is allowlist-based and safe; only the override path is affected.)

Change

internal/mcpclient/client.go::isSensitiveEnvVar now normalises each name before matching — uppercased, with - and _ stripped — and the patterns are stored in normalised form (APIKEY, TOKEN, SECRET, PASSWORD, CREDENTIAL, CREDS, PRIVATEKEY, ACCESSKEY). This is the report's first mitigation option (normalise before matching).

Verified before the fix that API-KEY, APIKEY, and PRIVATE-KEY returned sensitive=false; all spellings are now caught:

  • API-KEY, APIKEY, Api-Key, PRIVATE-KEY, PRIVATEKEY, ACCESS-KEY, SECRET-KEY, SECRETKEY, AUTH-TOKEN, AUTHTOKEN, DB-PASSWORD, AWS-CREDS, …
  • Benign names stay allowed: PATH, HOME, EDITOR, NODE_ENV, MCP_SERVER_NAME, KEYSTONE, …

Tests

  • Extended TestBuildEnv_OverridesCannotInjectSecrets with non-underscore override spellings (API-KEY, APIKEY, PRIVATE-KEY, SECRET-KEY) — all dropped from the child environment.
  • New TestIsSensitiveEnvVar_NormalizesSeparators table test covering blocked spellings and benign names.
  • go test ./... -count=1 ✅ (28 packages), go vet ✅, golangci-lint → 0 issues ✅

Docs updated: docs/SECURITY.md §19 and AGENTS.md (MCP subprocess environment sanitisation entries).

isSensitiveEnvVar substring-matched patterns like API_KEY against the raw
variable name, so MCP server env overrides named API-KEY, APIKEY, or
PRIVATE-KEY - all legal spellings in environment variable names - were
forwarded to potentially untrusted MCP subprocesses. Only the override
path was affected; the inherited-environment path is allowlist-based.

Names are now normalised (uppercased, "-" and "_" stripped) before
matching, and the patterns are stored in normalised form, so
API-KEY/APIKEY/Api-Key/PRIVATE-KEY/SECRET-KEY/AUTH-TOKEN and similar
spellings are all stripped.

Regression tests extend TestBuildEnv_OverridesCannotInjectSecrets with
non-underscore spellings and add TestIsSensitiveEnvVar_NormalizesSeparators
covering blocked spellings and benign names. Docs updated (SECURITY.md
section 19, AGENTS.md).
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 26, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
odek 3b085b8 Commit Preview URL

Branch Preview URL
Jul 26 2026, 09:18 AM

@jkyberneees
jkyberneees merged commit b262a78 into main Jul 26, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant