security(phase-3): sanitize error responses to MCP clients (M3)#20
Merged
Conversation
Add src/mcp/errors.ts with `sanitizeForClient` and `stripAbsolutePaths`.
Route every `catch (err) { ...return { error: String(err) } }` site
through the helper so caught exceptions can't leak absolute filesystem
paths (which embed the user's home dir, repo path, and often customer
or project names) to MCP / HTTP clients.
The helper:
- Logs the full original error to stderr (operator-visible).
- Returns the message with absolute paths (POSIX, Windows drive,
UNC) replaced by `<path>`.
Wired into:
- src/mcp/http.ts:113 — /mcp 500-handler
- src/mcp/tools.ts — every tool's catch block (search_docs,
list_docs, reindex_docs, get, multi_get, set_context,
list_contexts, remove_context), plus the per-file error inside
multi_get's loop.
Note: indexer.resolveRef error sites were already addressed in
Phase 1's refactor (they now return relFile-based errors that
never embed workspaceRoot). This phase covers the catch-handler
escape hatch and the OpenAI passthrough flagged in M1's notes.
Tests
- mcp-errors.test.ts: 9 cases for stripAbsolutePaths (POSIX,
Windows drive, UNC, multiple paths, path-only message,
relative paths preserved) and sanitizeForClient (stderr log,
non-Error throwables, context-hint prefix).
- mcp-tools.test.ts: 3 regression tests that throw an Error
embedding an absolute path from search_docs, reindex_docs, and
get; assert the client response contains the error context but
not `/Users/`, `secret-client`, or `/var/log`.
349 tests pass (was 337; added 12).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Phase 3 of plans/security-fixes-2026-05.md.
Summary
Add a tiny error-sanitization layer (`src/mcp/errors.ts`) and route every catch-handler in the MCP transport / tool surface through it. The helper logs the full error to stderr for the operator and returns the message with absolute paths (POSIX, Windows drive, UNC) replaced by ``.
Phase 1 already removed `absPath` interpolation from `indexer.resolveRef` error returns; this phase closes the catch-handler escape hatch.
Test plan
🤖 Generated with Claude Code