fix(server): redact sensitive env vars and headers from connection logs#1296
Open
SarthakB11 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix(server): redact sensitive env vars and headers from connection logs#1296SarthakB11 wants to merge 1 commit intomodelcontextprotocol:mainfrom
SarthakB11 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #847.
Summary
The proxy logs the full incoming connection request to stdout in two places in
server/src/index.ts:console.log("Query parameters:", JSON.stringify(query)).query.envis a JSON-encoded map of process env vars supplied by the user, frequently containing*_TOKEN,*_KEY,*_SECRET,AWS_*,password, etc.SSE transport: url=..., headers=....headersregularly carriesAuthorizationand similar.Those values end up in the proxy's stdout and from there into terminals, shell history, log files, and screen recordings (the original scenario from #375). This PR sanitizes both log lines.
Change
Helpers live in a new
server/src/redact.ts, imported byserver/src/index.tsat both log sites:SENSITIVE_KEY_PATTERNS: case-insensitive regexes fortoken,secret,password,passwd,credential,api[-_]?key,(^|_)key($|_),auth,session,private,^aws_. The(^|_)key($|_)boundary avoids matchingmonkeywhile still catchingAPI_KEY,KEY_VALUE, plainkey.redactSensitiveEntries(obj): shallow clone, keys preserved, sensitive values replaced with"***".redactQueryForLogging(query): clones the query and re-serializes the embeddedenvJSON with sensitive entries redacted; falls back to"env": "***"ifenvisn't valid JSON, so a malformed payload can't spill raw.Both call sites now log the redacted view; the live
envandheadersobjects are still passed unchanged to the spawned transport /SSEClientTransport. Keys are kept in the log so users can still see which vars / headers were forwarded.Scope notes
KEY_NAME,AUTHOR). This only affects the log line, not what the spawned process receives, and the trade-off favors over-redaction.command/argslog a few lines below is intentionally left alone; shell args are already user-visible and not expected to carry secrets.redactSensitiveEntriesis shallow.envis a string-to-string map and Express normalizes headers to a flat object, so this is sufficient today; deep redaction can be added if a future request shape nests secrets.Test plan
server/previously had no test runner. This PR adds a minimalnode:testsetup usingtsx(already inserver/devDependencies); zero new dependencies. New test script:server/package.json"test": "node --import tsx --test test/*.test.ts".server/test/redact.test.tscovers:GITHUB_TOKEN,AWS_ACCESS_KEY_ID); benignPATHpreservedKEY,API_KEY,api-keyredactedMONKEYandKEYBOARDare NOT redacted (no false positives on words containingkey)Authorizationheader redactedenvJSON parsed and redacted entry-by-entry; non-secret entries preservedenvfalls back to"***"instead of leaking the raw payloadenvpass through unchangedRun with:
cd server && npm test(7 passed, 0 failed).