refactor: extract shared DIFC enforcement pipeline to internal/guard/pipeline.go#6685
Merged
Conversation
6 tasks
Eliminates the duplicate 6-phase DIFC enforcement pipeline that existed independently in proxy/handler.go and server/unified.go. - Add internal/guard/pipeline.go with PipelineInput, PipelinePreResult, PipelineAccessDenied, RunPipelinePrePhases (phases 0–2), RunPipelinePhase4, and RunPipelinePhase6 shared helpers - Refactor internal/proxy/handler.go to use shared helpers for phases 0–2, 4, and 6; proxy-specific phase 3 (HTTP forward) and phase 5 (GraphQL/REST response processing) remain inline - Refactor internal/server/unified.go to use shared helpers for phases 0–2, 4, and 6; server-specific phase 3 (MCP backend call via circuit breaker) and phase 5 (MCP filtering/notices) remain inline - Add internal/guard/pipeline_test.go with 16 focused tests covering all shared pipeline helpers (phases 0–2, 4, 6) including denial, bypass-for- read, label accumulation in all three enforcement modes, and error paths Behavioral equivalence: Phase 6 now always falls back to resource labels when labeledData is nil in both callers (was previously only done in unified.go). All existing proxy and server DIFC tests continue to pass.
Address code review feedback on pipeline_test.go.
Copilot
AI
changed the title
[WIP] Refactor DIFC pipeline to eliminate duplicate code
refactor: extract shared DIFC enforcement pipeline to internal/guard/pipeline.go
May 29, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR centralizes the shared DIFC enforcement phases used by both proxy and unified MCP server paths, reducing duplicated security-sensitive pipeline logic.
Changes:
- Adds
internal/guard/pipeline.gowith shared helpers for phases 0–2, 4, and 6. - Refactors
internal/proxy/handler.goandinternal/server/unified.goto call the shared pipeline helpers while keeping transport-specific execution/filtering inline. - Adds unit tests for the new guard pipeline helpers.
Show a summary per file
| File | Description |
|---|---|
internal/guard/pipeline.go |
Introduces shared DIFC pipeline input/result types and phase helpers. |
internal/guard/pipeline_test.go |
Adds unit coverage for the extracted pipeline phases. |
internal/proxy/handler.go |
Replaces duplicated proxy DIFC phase logic with shared guard pipeline calls. |
internal/server/unified.go |
Replaces duplicated unified-server DIFC phase logic with shared guard pipeline calls. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 4/4 changed files
- Comments generated: 1
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.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.
The 6-phase DIFC enforcement pipeline was independently implemented in both
internal/proxy/handler.goandinternal/server/unified.go(~100 lines of near-identical security logic), creating drift risk: a security fix in one path could silently leave the other exposed.Changes
New:
internal/guard/pipeline.goShared pipeline types and phase helpers placed in
guard(sits abovedifc, belowproxy/server— no circular import):PipelineInput— unified inputs struct (agent ID, tool name, args, guard, evaluator, registry, capabilities, enforcement mode, backend caller)PipelinePreResult— outputs from phases 0–2 (agent labels, labeled resource, operation, coarse outcome, eval result)PipelineAccessDenied— typed error for Phase 2 denial; carriesEvalResult,Resource, andAgentLabelsso callers can formulate context-appropriate denial responses (HTTP 403 vs MCP error)RunPipelinePrePhases— phases 0–2: get/create agent labels, store tool args in context,LabelResource, coarse access checkRunPipelinePhase4— conditionalLabelResponse(gated byShouldCallLabelResponse)RunPipelinePhase6— label accumulation in propagate mode; falls back to resource labels whenlabeledDatais nil (aligns proxy behavior with server — proxy previously skipped this fallback)Refactored:
proxy/handler.goandserver/unified.goPhases 0–2, 4, and 6 replaced with shared helper calls. Context-specific logic remains inline:
New:
internal/guard/pipeline_test.go16 unit tests covering all shared phases: Phase 0 label retrieval and context injection, Phase 1 error propagation, Phase 2 allow/bypass-for-read/deny outcomes, Phase 4 conditional labeling, Phase 6 accumulation across all three enforcement modes.