Skip to content

fix(mcp): prevent byte-slicing multi-byte UTF-8 in excerpt() - #4

Merged
hackwither merged 1 commit into
hackwither:mainfrom
hannanmax:fix/utf8-excerpt
Sep 6, 2026
Merged

hackwither merged 1 commit into
hackwither:mainfrom
hannanmax:fix/utf8-excerpt

Conversation

@hannanmax

@hannanmax hannanmax commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Problem

excerpt() uses len(s) (byte count) for the length check and s[:n] (byte slice) for truncation. For non-ASCII text — emoji, RTF markup, or non-Latin server descriptions — this splits mid-rune, producing an invalid UTF-8 string that JSON-encodes to garbage or a Unicode replacement character.

Minimal repro:

excerpt("héllo", 3)  // returns "h\xc3" — invalid UTF-8, not "hél"

Fix

Convert to []rune so both the length check and the slice operate on Unicode codepoints:

func excerpt(s string, n int) string {
    r := []rune(s)
    if len(r) <= n {
        return s
    }
    return string(r[:n]) + "…"
}

len(s) returns bytes, so s[:n] could split mid-rune for non-ASCII
text (emoji, RTF markup in server descriptions, etc.), producing an
invalid UTF-8 string that JSON-encodes to garbage or a replacement
character.

Switch to []rune so the length check and slice both operate on Unicode
codepoints.
@hannanmax

Copy link
Copy Markdown
Contributor Author

Hey! Loved the repo - really clean approach to black-box agent recon with zero deps. Found a few bugs while reading through the code and raised 5 PRs (#4, #5, #6, #7, #8). Happy to discuss any of them if you have questions!

@hannanmax hannanmax closed this Sep 6, 2026
@hannanmax hannanmax reopened this Sep 6, 2026
@hackwither

Copy link
Copy Markdown
Owner

hey, thanks for the contributions! :D

@hackwither
hackwither merged commit 3aa00c3 into hackwither:main Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants