refactor: move outlier functions, eliminate thin wrappers, centralize security policy#10061
Conversation
There was a problem hiding this comment.
Pull request overview
Refactors helper ownership and centralizes security-related policy without changing intended behavior.
Changes:
- Relocates utilities, configuration formatting, and tool policy.
- Removes thin HTTP wrappers while preserving request bodies and responses.
- Centralizes safe-output classification and cryptographic integer generation.
Show a summary per file
| File | Description |
|---|---|
internal/util/util.go |
Absorbs string normalization. |
internal/util/util_test.go |
Moves normalization tests. |
internal/util/random.go |
Adds random big integers. |
internal/util/random_test.go |
Tests random big integers. |
internal/util/normalize.go |
Deletes standalone normalization helper. |
internal/util/normalize_test.go |
Deletes relocated tests. |
internal/util/netutil.go |
Adds client-address utility. |
internal/util/netutil_test.go |
Tests address normalization. |
internal/server/unified.go |
Uses server-local tool policy. |
internal/server/tool_policy.go |
Houses singular-tool classification. |
internal/server/tool_policy_test.go |
Relocates policy tests. |
internal/server/session_auto_init.go |
Removes body-peek wrapper use. |
internal/server/session_auto_init_test.go |
Updates body-reading calls. |
internal/server/sdk_logging.go |
Inlines POST body guard. |
internal/server/peek_request_body_test.go |
Retargets body helper tests. |
internal/server/http_helpers.go |
Removes thin body wrapper. |
internal/server/handlers.go |
Inlines reflect response creation. |
internal/server/guard_visibility.go |
Removes local safe-output classifier. |
internal/server/guard_init.go |
Uses centralized guard policy. |
internal/server/difc_log_test.go |
Removes relocated policy tests. |
internal/proxy/tls.go |
Uses centralized random generation. |
internal/proxy/handler.go |
Inlines health and reflect responses. |
internal/mcpresult/mcpresult.go |
Documents lenient normalization. |
internal/mcp/helpers.go |
Removes server-specific policy. |
internal/httputil/httputil.go |
Removes domain-specific wrappers. |
internal/httputil/httputil_test.go |
Removes wrapper tests. |
internal/guard/guard.go |
Centralizes safe-output classification. |
internal/config/validation_errors.go |
Retains error definitions only. |
internal/config/error_format.go |
Houses error presentation helpers. |
internal/cmd/proxy.go |
Uses shared address utility. |
internal/cmd/proxy_test.go |
Updates relocated utility reference. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 31/31 changed files
- Comments generated: 1
- Review effort level: Medium
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| assert.Equal(t, tc.expected, clientAddr(tc.input)) | ||
| assert.Equal(t, tc.expected, util.ClientAddr(tc.input)) |
🔒 mcpg Read-Only Stress — gVisorSurface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Overall: PASS
References: §30162188146
|
🔒 mcpg Read-Only Stress — docker-sbxSurface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Overall: PASS
References: §30162188160
|
🔒 mcpg Read-Only Stress — defaultSurface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Overall: PASS
Run: §30162188144
|
Addresses 9 of the 16 findings from the semantic function clustering analysis: outlier functions in wrong packages, unnecessary thin wrappers, and scattered helpers that violate the import/responsibility hierarchy.
Outlier functions relocated
WriteSimpleHealthResponse/WriteReflectResponseremoved fromhttputil— the generic layer had no business importinginternal/difc. Inlined at both call sites (proxy/handler.go,server/handlers.go).IsSingularReadToolmovedmcp/helpers.go→server/tool_policy.go. GitHub-specific tool enumeration doesn't belong in the MCP wire-protocol layer.clientAddrextracted fromcmd/proxy.go→util/netutil.go(exported asClientAddr). Pure string utility buried in Cobra command state.proxy/tls.randomSerial()now callsutil.RandomBigInt(128)instead of callingcrypto/rand.Intdirectly — centralizes allcrypto/randuse inutil/random.go.AppendConfigDocsFooter/FormatConfigErrorsplit out ofconfig/validation_errors.gointo newconfig/error_format.go. Error type definitions and CLI output formatting shouldn't share a file.Near-duplicate / thin wrapper elimination
peekRequestBodyremoved — it was a one-liner POST guard delegating toreadAndRestoreRequestBody. Guard inlined at all four call sites (http_helpers.go,sdk_logging.go,session_auto_init.go).mcpresult.NormalizeContentItemsvsmcp/tool_result.goinline) documented with a shared specification comment explaining the intentional lenient/strict split, reducing future silent divergence risk.Security policy centralized
isSafeOutputsServermoved fromserver/guard_visibility.go→ exportedguard.IsSafeOutputsServeringuard/guard.go. The"-safe-outputs"suffix is a DIFC security invariant and belongs with other guard policy.Misc
util/normalize.go(single-function file:NormalizeStringCI) merged intoutil/util.goand deleted.Not addressed (with rationale)
io.ReadAllinproxy/handler.gohttputil.ReadResponseBodyrejects 4xx/5xx — semantically wrong forforwardAndReadBodywhich must return the body regardless of statussavePayloadrelocationlogger/fileutil.gogenerateRandomSpanIDutil.RandomBytes— no action needed