Skip to content

Redact API keys from MCP responses#146

Open
beanscg wants to merge 1 commit into
cppalliance:mainfrom
beanscg:bean-labs-redact-mcp-responses
Open

Redact API keys from MCP responses#146
beanscg wants to merge 1 commit into
cppalliance:mainfrom
beanscg:bean-labs-redact-mcp-responses

Conversation

@beanscg

@beanscg beanscg commented Jun 8, 2026

Copy link
Copy Markdown

Fixes #136.

Summary

  • expand API-key redaction to cover pcsk_... Pinecone key prefixes
  • apply recursive redaction at the MCP response boundary for both success and error payloads
  • add coverage for tool-error responses, catch-all SDK errors, query degradation reasons, and guided query returned content

Tests

Using Node 24.14.0 from the local bundled runtime because the system Node 22.11.0 is below several package engine ranges.

  • npm test -- src/core/server/tool-response-redaction.test.ts src/core/server/tools/query-tool.test.ts src/alliance/tools/guided-query-tool.test.ts
  • npm run typecheck
  • npm run lint
  • npm run format:check
  • npm run build
  • npm test
  • git diff --check

Summary by CodeRabbit

  • Bug Fixes

    • Tool responses and error messages now properly redact API keys and sensitive tokens, including Pinecone-style credentials and Bearer tokens, across all query operations to prevent accidental exposure in logs and output.
  • Tests

    • Added comprehensive test coverage verifying that sensitive credentials are properly redacted in tool responses and error messages.

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a7229454-0420-498e-b4a7-7219950f4b62

📥 Commits

Reviewing files that changed from the base of the PR and between 687a9a6 and 5a30d44.

📒 Files selected for processing (5)
  • src/alliance/tools/guided-query-tool.test.ts
  • src/core/server/tool-response-redaction.test.ts
  • src/core/server/tool-response.ts
  • src/core/server/tools/query-tool.test.ts
  • src/logger.ts

📝 Walkthrough

Walkthrough

This PR implements API key redaction verification by extending logger redaction patterns to cover pcsk_* Pinecone key formats, exporting the redactValue function, integrating redaction into all tool response serialization paths, and adding comprehensive test coverage across error responses, degradation reasons, and decision traces.

Changes

API Key Redaction in Tool Responses

Layer / File(s) Summary
Logger redaction enhancements
src/logger.ts
redactApiKey adds regex replacement for pcsk_* API key patterns; redactValue is exported as a public function to enable external redaction pipelines.
Tool response redaction integration
src/core/server/tool-response.ts
jsonResponse and jsonErrorResponse apply redactValue to payloads before JSON serialization, ensuring all error messages, suggestions, and nested metadata are sanitized.
Redaction pipeline test suite
src/core/server/tool-response-redaction.test.ts
New test file verifies redaction behavior across direct strings, tool error fields, DEBUG catch-all messages, and nested success-payload structures; assertions confirm raw keys are absent and redacted fields match expected masked values.
Tool-specific redaction validation
src/core/server/tools/query-tool.test.ts, src/alliance/tools/guided-query-tool.test.ts
Tests for query and guided_query tools mock degraded responses containing API keys, invoke handlers, and verify redaction of degradation_reason and decision trace output in returned payloads.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested labels

enhancement

Suggested reviewers

  • wpak-ai
  • whisper67265
  • AuraMindNest

Poem

🐰 Through logs and strings, a rabbit hops with care,
Redacting secrets floating in the air,
With pcsk_ patterns caught in regex snare,
And *** masking secrets everywhere,
The tests confirm: no leaks shall be unfair! 🔐

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Redact API keys from MCP responses' clearly and concisely summarizes the primary change: applying API key redaction at the MCP response boundary.
Linked Issues check ✅ Passed The PR implements all acceptance criteria from #136: redacts tool error fields, degradation reasons, guided query output, and catch-all SDK errors; supports pcsk_* format; adds comprehensive test coverage; and exports redactValue for cross-module use.
Out of Scope Changes check ✅ Passed All changes are directly scoped to implementing credential redaction across output paths as specified in #136: test files, redaction logic, and exporting utilities are all necessary for the objective.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

Credential redaction verification across all output paths

1 participant