mcpproxy-go icon indicating copy to clipboard operation
mcpproxy-go copied to clipboard

feat(security): add data flow security with agent hook integration (Spec 027)

Open Dumbris opened this issue 9 months ago • 8 comments

Summary

  • Add data flow security system that detects and prevents data exfiltration by tracking how data moves between internal tools (Read, databases) and external tools (WebFetch, Slack)
  • Support two operating modes: proxy-only (universal, works with any MCP agent) and full mode (with agent hook integration for intercepting agent-internal tool calls like Read, Write, Bash)
  • Implement complete hook CLI (mcpproxy hook install/uninstall/status/evaluate) for Claude Code integration

Key Components

  • Classifier (internal/security/flow/classifier.go): Categorizes tools/servers as internal, external, hybrid, or unknown using name heuristics and config overrides
  • Content Hasher (internal/security/flow/hasher.go): SHA256 per-field extraction from JSON for content flow matching without storing raw data
  • Flow Tracker (internal/security/flow/tracker.go): Session-scoped origin recording and flow edge detection with configurable policies
  • Policy Evaluator (internal/security/flow/policy.go): Configurable actions (allow/warn/ask/deny) with graceful degradation in proxy-only mode (ask→warn)
  • Session Correlator (internal/security/flow/correlator.go): Links agent hook sessions to MCP proxy sessions via argument hash matching
  • Hook CLI (cmd/mcpproxy/hook_cmd.go): Install/uninstall/status/evaluate commands for Claude Code agent hooks
  • REST API (internal/httpapi/hooks.go): POST /api/v1/hooks/evaluate endpoint for hook event processing
  • Activity Logging: New hook_evaluation and flow_summary activity types with full metadata
  • Web UI Nudge: Dashboard hint encouraging hook installation when running in proxy-only mode
  • Diagnostics: Security coverage recommendation in mcpproxy doctor output
  • OpenAPI: Updated swagger.yaml with new endpoint, activity filters, and schemas

Architecture

Agent (Claude Code)
  ├── Internal tools (Read, Write, Bash) ──→ Hook CLI ──→ POST /api/v1/hooks/evaluate
  └── MCP tools (call_tool_read/write) ──→ MCP Proxy ──→ Upstream servers
                                              ↕
                                    Flow Tracker (session-scoped)
                                    Content hash matching
                                    Policy evaluation

Test plan

  • [x] 68/70 TDD tasks complete (2 benchmarks deferred to pre-merge)
  • [x] Unit tests: all pass including internal/security/flow/... (25 tests), internal/httpapi/..., internal/runtime/...
  • [x] Race detection: go test -race ./internal/security/flow/... passes cleanly
  • [x] E2E test: proxy-only flow detection pipeline (TestE2E_FlowSecurity_ProxyOnlyDetection)
  • [x] E2E test: hook-enhanced flow detection via HTTP API (TestE2E_FlowSecurity_HookEnhancedDetection) — verifies deny decision with flow_type=internal_to_external, risk_level=high
  • [x] API E2E: test-api-e2e.sh — no regressions (61/71 pass, 10 failures pre-existing)
  • [x] OpenAPI: verify-oas-coverage.sh — new endpoint documented
  • [x] Frontend: vue-tsc --noEmit passes cleanly

🤖 Generated with Claude Code

Dumbris avatar Feb 04 '26 19:02 Dumbris

Deploying mcpproxy-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 5f39bb7
Status: ✅  Deploy successful!
Preview URL: https://eaecdcb6.mcpproxy-docs.pages.dev
Branch Preview URL: https://027-data-flow-security.mcpproxy-docs.pages.dev

View logs

📦 Build Artifacts

Workflow Run: View Run Branch: 027-data-flow-security

Available Artifacts

  • archive-darwin-amd64 (26 MB)
  • archive-darwin-arm64 (23 MB)
  • archive-linux-amd64 (15 MB)
  • archive-linux-arm64 (13 MB)
  • archive-windows-amd64 (26 MB)
  • archive-windows-arm64 (23 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (20 MB)
  • installer-dmg-darwin-arm64 (18 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 26145206820 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

github-actions[bot] avatar Feb 04 '26 19:02 github-actions[bot]

Code review

Found 3 issues:

  1. Data race in CheckFlow method — Writing to session fields (LastActivity, ToolsUsed, Flows) while holding only a read lock. Will fail go test -race which is required per CLAUDE.md. Lock type should be upgraded to Lock() before mutations at lines 113-120.

https://github.com/smart-mcp-proxy/mcpproxy-go/blob/ec787a497596d4cf66404b813b02cf34730fc0de/internal/security/flow/tracker.go#L80-L123

  1. Missing FlowService.Stop() in Close() — Runtime.Close() stops OAuth manager and supervisor but never stops the FlowService, leaking background goroutines (sessionExpiryLoop, cleanupLoop). Should follow same pattern established in PR #255 with refreshManager.Stop().

https://github.com/smart-mcp-proxy/mcpproxy-go/blob/ec787a497596d4cf66404b813b02cf34730fc0de/internal/runtime/runtime.go#L535-L570

  1. Missing SetExpiryCallback registration — FlowService.SetExpiryCallback is defined but never registered during runtime initialization. This breaks flow session summary emission to the activity log, causing EventTypeActivityFlowSummary to never fire. Should register callback after flowService creation (line 210).

https://github.com/smart-mcp-proxy/mcpproxy-go/blob/ec787a497596d4cf66404b813b02cf34730fc0de/internal/runtime/runtime.go#L182-L214

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

tjsingleton avatar Feb 07 '26 14:02 tjsingleton

Opened #299 as a stacked PR with fixes for the 3 issues identified in the code review:

  1. Race condition in CheckFlow() — RLock → Lock (writes to session fields require write lock)
  2. Goroutine leak — added FlowService.Stop() in Runtime.Close()
  3. Missing callback — registered SetExpiryCallback so flow summaries emit to activity log

All 34 flow tests pass with -race. Includes 3 regression tests.

🤖 Generated with Claude Code

tjsingleton avatar Feb 07 '26 16:02 tjsingleton

Critic (Codex) review — Dumbris's PR #293 Verdict: request_changes Strengths: The PR includes tests and API/OpenAPI updates for the data-flow security surface, and its main build/unit/e2e jobs shown in the PR checks are otherwise passing. Findings:

  • Required checks are not all green: check-size failed in the CLAUDE.md Size Check workflow, job https://github.com/smart-mcp-proxy/mcpproxy-go/actions/runs/26145206887/job/76899082389. Per the pre-merge gate rule, a red check is an automatic request_changes until fixed or explicitly made non-required by the human gate owner. Provenance check: ok

Dumbris avatar Jun 01 '26 12:06 Dumbris

Critic (Codex) review — Dumbris's PR #293 Verdict: request_changes Head: 5f39bb726974e38c0e5423e281e9747808094f0e

Strengths: The security direction is relevant to the product scope, but this PR cannot advance while required checks are red.

Findings:

  1. Required CI is red. gh pr checks 293 --watch=false reports check-size failing at https://github.com/smart-mcp-proxy/mcpproxy-go/actions/runs/26145206887/job/76899082389. Per the pre-merge doctrine, a red required check is an automatic request_changes until rerun/fixed.

Provenance check: ok

Dumbris avatar Jun 01 '26 12:06 Dumbris

Triage (release-eng PR sweep, MCP-1760) — held, not merged.

  • Failing check: check-size — the diff touches only CLAUDE.md and pushes it over the hard 40,000-char CI gate (main is already ~39.9k).
  • Branch is also CONFLICTING with main (needs rebase).
  • Owner / next step (@Dumbris): rebase onto main, then move the Spec 027 doc content out of CLAUDE.md into docs/ and/or a memory file so CLAUDE.md stays under 40k. Once check-size is green and conflicts resolved, ping and we'll route to Codex review → Gatekeeper merge.

Dumbris avatar Jun 08 '26 17:06 Dumbris