You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
handleDelegatedRequest in internal/proxy/delegation.go and the enclave request handler in internal/proxy/enclave.go implement structurally identical request-parsing/routing/error-handling flows (path parse → query parse → route match → tool/args extraction → DIFC context bind → dispatch), differing mainly in the authorization mechanism (delegation store vs. capability verifier).
Maintainability: The path/query parsing, route matching, tool/args extraction, fullPath reconstruction, and final handleWithDIFC dispatch are duplicated verbatim across both handlers. A change to the shared request-shape rules (e.g., adding a new denial condition or changing how fullPath is built) must be applied twice.
Bug Risk: High for authorization-adjacent logic — since these are both security enforcement points (enclave/delegation admission), divergence between the two copies over time could allow one path to skip a check the other has (as already evidenced: enclave.go has an extra cross-repo/public-repo check that delegation.go does not, and it isn't obvious from the code whether that's an intentional difference or a latent gap).
Code Bloat: ~25-30 lines of duplicated control flow.
Refactoring Recommendations
Extract a shared parseAndRouteEnclaveRequest helper
Factor the common path/query parse + MatchEnclaveRoute + tool/args extraction + fullPath reconstruction into a single helper function returning (route, toolName, args, fullPath, error), used by both handleDelegatedRequest and the enclave handler, each supplying its own authorization callback.
Suggested location: internal/proxy/enclave_common.go (new file) or as a method on proxyHandler.
Estimated effort: 3-4 hours (needs careful handling of the differing error-denial branch order — enclave.go checks method/body after auth, delegation.go checks it before route match)
Benefits: Single code path for the security-critical routing logic, eliminating risk of the two handlers drifting apart on validation order or denial conditions.
Document (or eliminate) the intentional differences
If the differing check order and additional cross-repo check in enclave.go are intentional, add an explicit comment in the shared helper (once extracted) noting which caller enables which optional check, so future readers don't assume the two are meant to be identical.
Estimated effort: <1 hour
Benefits: Prevents future refactors from "fixing" an intentional difference in one file without updating the other by accident.
Implementation Checklist
Review duplication findings
Prioritize refactoring tasks
Create refactoring plan
Implement changes
Update tests
Verify no functionality broken
Parent Issue
See parent analysis report: #13679
Related to #13679
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
proxy.golang.org
To allow these domains, add them to the network.allowed list in your workflow frontmatter:
Part of duplicate code analysis: #13679
Summary
handleDelegatedRequestininternal/proxy/delegation.goand the enclave request handler ininternal/proxy/enclave.goimplement structurally identical request-parsing/routing/error-handling flows (path parse → query parse → route match → tool/args extraction → DIFC context bind → dispatch), differing mainly in the authorization mechanism (delegation store vs. capability verifier).Duplication Details
Pattern: Repeated enclave-request parse/route/dispatch skeleton
internal/proxy/delegation.go(lines 29-63,handleDelegatedRequest)internal/proxy/enclave.go(lines ~200-247, enclave GET handler)Impact Analysis
fullPathreconstruction, and finalhandleWithDIFCdispatch are duplicated verbatim across both handlers. A change to the shared request-shape rules (e.g., adding a new denial condition or changing howfullPathis built) must be applied twice.Refactoring Recommendations
Extract a shared
parseAndRouteEnclaveRequesthelperMatchEnclaveRoute+ tool/args extraction +fullPathreconstruction into a single helper function returning(route, toolName, args, fullPath, error), used by bothhandleDelegatedRequestand the enclave handler, each supplying its own authorization callback.internal/proxy/enclave_common.go(new file) or as a method onproxyHandler.Document (or eliminate) the intentional differences
enclave.goare intentional, add an explicit comment in the shared helper (once extracted) noting which caller enables which optional check, so future readers don't assume the two are meant to be identical.Implementation Checklist
Parent Issue
See parent analysis report: #13679
Related to #13679
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
proxy.golang.orgTo allow these domains, add them to the
network.allowedlist in your workflow frontmatter:See Network Configuration for more information.