feat(mcp): MCP-SAN + MCP-COV hardening#76
Merged
josealekhine merged 1 commit intoMay 10, 2026
Conversation
Implements sanitization layer (MCP-SAN ActiveMemory#49) and coverage expansion (MCP-COV ActiveMemory#50) for the MCP server. Sanitization: - Add internal/sanitize package: Content, Reflect, SessionID, StripControl, truncate helpers - Add internal/config/sanitize constants (NullByte, DotDot, etc.) - Add internal/config/regex/sanitize compiled regexes - Add MaxSourceLimit, MaxContentLen, MaxNameLen, MaxQueryLen, MaxCallerLen, MaxURILen constants to internal/config/mcp/cfg - Add InputTooLong error constructor in internal/err/mcp - Add mcp.err-input-too-long and mcp.err-unknown-entry-type YAML keys - Apply sanitize.Content in extract EntryArgs and prompt buildEntry - Apply sanitize.Reflect for query, caller, name, URI in all tool/prompt/resource dispatch paths - Cap source limit to MaxSourceLimit in tool handler Coverage: - internal/entity/mcp_session_test.go: 10 tests covering MCPSession lifecycle methods including PendingCount, RecordSessionStart, RecordContextLoaded, RecordDriftCheck, RecordContextWrite, IncrementCallsSinceWrite - internal/mcp/server/server_test.go: 15+ new tests covering notification handling, subscribe/unsubscribe error paths, ctx_remind with active and future-dated reminders, ctx_drift missing-file violations, ctx_complete empty and non-matching query - internal/mcp/proto/schema_test.go: JSON round-trip tests - internal/mcp/server/def/{tool,prompt}/*_test.go: def count tests - internal/mcp/server/{extract,io,out,parse,stat}/*_test.go: unit tests Signed-off-by: CoderMungan <codermungan@gmail.com>
josealekhine
approved these changes
May 10, 2026
Member
josealekhine
left a comment
There was a problem hiding this comment.
Minor fixes needed; but I'll take care of those.
Mostly around unicode parsing edge cases.
Thanks for the good work 🙏 .
This was referenced May 10, 2026
omergk28
pushed a commit
to omergk28/ctx
that referenced
this pull request
May 11, 2026
Follow-up to PR ActiveMemory#76 (MCP-SAN + MCP-COV hardening) addressing three issues surfaced during post-merge review: 1. truncate() now backs up to a UTF-8 rune boundary via utf8.RuneStart so byte-level cuts never produce invalid UTF-8. Reflect() and SessionID() delegate to the shared helper. 2. StripControl() now drops U+2028 (LINE SEPARATOR) and U+2029 (PARAGRAPH SEPARATOR) explicitly. unicode.IsControl does not match these (categories Zl/Zp), yet Markdown renderers may treat them as line breaks — leaving them in opens a newline injection path through reflected content. Constants live in internal/config/sanitize. 3. SanitizedOpts now enforces MaxOptsFieldLen (4 KB) on context, rationale, consequence, lesson, and application — secondary prose fields that previously had no length cap. Signature changes to (entity.EntryOpts, error); the two call sites in route/tool/tool.go propagate the error to the MCP client via InputTooLong, naming the offending field. Spec: specs/sanitize-hardening-followup.md
Member
Author
|
It’s been great working with you and being part of this project, Jose. Looking forward to the new features... @josealekhine |
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.
Closes #49, closes #50
Summary
Re-implementation of PR #52 adapted to the current codebase. Addresses
MCP-SAN (#49) sanitization requirements and MCP-COV (#50) coverage
requirements in full.
MCP-SAN (#49) — Sanitization layer
New packages
internal/sanitize:Content,Reflect,SessionID,StripControl,truncateinternal/config/sanitize: string/length constants (NullByte,DotDot,ForwardSlash,Backslash,HyphenReplace,MaxSessionIDLen)internal/config/regex/sanitize: compiled regexes for Markdown structure chars and session ID unsafe charsConfig constants
Added to
internal/config/mcp/cfg:MaxSourceLimit,MaxContentLen,MaxNameLen,MaxQueryLen,MaxCallerLen,MaxURILenError + text
internal/err/mcp:InputTooLong(field, maxLen)constructormcp.err-input-too-longandmcp.err-unknown-entry-typekeys in embed/text and YAMLApplied sanitization
extract.EntryArgs: MaxContentLen guard →InputTooLongerrorextract.SanitizedOpts: sanitizes all option fieldsroute/prompt/entry.buildEntry:sanitize.Contenton every field valueroute/tool/tool.go:sanitize.Contenton content,sanitize.Reflecton query/caller, cap limit toMaxSourceLimitroute/tool/dispatch.go,route/prompt/dispatch.go,resource/dispatch.go:sanitize.Reflectfor name/URI in unknown-branch pathsMCP-COV (#50) — Coverage expansion
New test files (12)
internal/entity/mcp_session_test.gointernal/mcp/proto/schema_test.gointernal/mcp/server/def/tool/tool_test.gointernal/mcp/server/def/prompt/prompt_test.gointernal/mcp/server/extract/extract_test.gointernal/mcp/server/io/write_test.gointernal/mcp/server/out/out_test.gointernal/mcp/server/parse/parse_test.gointernal/mcp/server/stat/stat_test.gointernal/sanitize/sanitize_test.goExtended:
internal/mcp/server/server_test.goAdded 15 new tests covering all COV gaps:
TestServeNotificationIgnored— notification silently consumedTestServeEmptyLines,TestServeParseErrorWriteFailure,TestServeDispatchWriteFailureTestResourcesSubscribeInvalidJSON/EmptyURI,TestResourcesUnsubscribeInvalidJSON/EmptyURITestToolRemindWithActive,TestToolRemindFutureDatedTestToolDriftMissingFileTestToolCompleteEmptyQuery,TestToolCompleteNoMatchTestPromptAddLearningCI
go build ./...✅go test ./...✅ (133 packages, 0 failures)go vet ./...✅golangci-lint run✅ (0 issues)