From 8bbd20a262298cbc4ee15546994820e3f414bdda Mon Sep 17 00:00:00 2001 From: Rudy Mizrahi Celekli <47457359+rudycelekli@users.noreply.github.com> Date: Wed, 21 Oct 2026 23:36:16 -0400 Subject: [PATCH] fix(codescripts): name the real character in a script-name refusal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ValidateName walks the name byte by byte, and reported the offending character as string(name[i]). Converting a byte to a string yields the code point of the same value, so the first byte of "語" (0xE8) was rendered as "è": a caller asking for a Japanese script name got an HTTP 400 INVALID_SCRIPT_NAME telling it to remove a character that is not in the name it sent. A byte that begins no valid rune is not a character at all and is now reported as a byte. The byte-wise loop is the confinement boundary and is unchanged; only the wording of the refusal moves. truncateForMessage, which bounds the echoed name at MaxNameLen+16, also cut at a raw byte offset, so a long non-ASCII name left a partial rune that %q renders as \xNN escapes — the refusal ended in hex noise instead of the name it quotes. It now goes through stringutil.UTF8Prefix (#1647, #1664, #1665). ASCII refusals are byte-for-byte unchanged. --- internal/codescripts/codescripts.go | 28 ++++++- .../codescripts/invalid_name_message_test.go | 78 +++++++++++++++++++ 2 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 internal/codescripts/invalid_name_message_test.go diff --git a/internal/codescripts/codescripts.go b/internal/codescripts/codescripts.go index a2b89fa1b..cbb4386c6 100644 --- a/internal/codescripts/codescripts.go +++ b/internal/codescripts/codescripts.go @@ -27,6 +27,9 @@ import ( "path/filepath" "sort" "strings" + "unicode/utf8" + + "github.com/smart-mcp-proxy/mcpproxy-go/internal/stringutil" ) const ( @@ -330,7 +333,7 @@ func ValidateName(name string) error { switch { case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9', c == '-', c == '_': default: - return &InvalidNameError{Name: name, Reason: fmt.Sprintf("character %q is not allowed", string(name[i]))} + return &InvalidNameError{Name: name, Reason: disallowedCharReason(name[i:])} } } return nil @@ -768,11 +771,32 @@ func describeEntry(scriptsDir, name, ext string, d fs.DirEntry) Entry { return entry } +// disallowedCharReason names the offending character in a name refusal. +// +// The loop above walks bytes — that is the confinement boundary and stays as +// it is; only the wording changes. It used to report `string(name[i])`, and +// converting a byte to a string yields the code point of the same value, so +// the first byte of "語" (0xE8) was rendered as "è": the caller was told to +// remove a character that is not in the name it sent. A byte that begins no +// valid rune is not a character at all and is reported as a byte. +func disallowedCharReason(rest string) string { + r, size := utf8.DecodeRuneInString(rest) + if r == utf8.RuneError && size <= 1 { + return fmt.Sprintf("byte %#02x is not allowed", rest[0]) + } + return fmt.Sprintf("character %q is not allowed", string(r)) +} + // truncateForMessage bounds caller-supplied text echoed back in an error. +// +// The cut goes through stringutil.UTF8Prefix (#1647, #1664, #1665): a script +// name is caller-supplied and need not be ASCII, and a raw byte cut leaves a +// partial rune that %q then renders as \xNN escapes, so the refusal ends in +// hex noise instead of the name it is quoting. func truncateForMessage(s string) string { const limit = MaxNameLen + 16 if len(s) <= limit { return s } - return s[:limit] + "…" + return stringutil.UTF8Prefix(s, limit) + "…" } diff --git a/internal/codescripts/invalid_name_message_test.go b/internal/codescripts/invalid_name_message_test.go new file mode 100644 index 000000000..5400f5c9f --- /dev/null +++ b/internal/codescripts/invalid_name_message_test.go @@ -0,0 +1,78 @@ +package codescripts + +import ( + "strings" + "testing" +) + +// The refusal for a non-ASCII script name named a character that is not in +// the name. The validation loop walks bytes, and `string(name[i])` converts a +// byte to the code point of the same value, so the first byte of "語" (0xE8) +// was rendered as "è". The message is the body of an HTTP 400 +// INVALID_SCRIPT_NAME (internal/httpapi/code_exec.go), so the caller is told +// to remove a character it never sent. +func TestInvalidNameErrorNamesTheRealCharacter(t *testing.T) { + err := ValidateName("語") + if err == nil { + t.Fatal("a non-ASCII name must be refused") + } + msg := err.Error() + if !strings.Contains(msg, "語") { + t.Errorf("refusal does not name the offending character:\n%s", msg) + } + if strings.Contains(msg, "è") { + t.Errorf("refusal names a Latin-1 character that is not in the name:\n%s", msg) + } +} + +// A malformed byte is not a character and must not be reported as one. +func TestInvalidNameErrorReportsMalformedByte(t *testing.T) { + err := ValidateName("a\x80b") + if err == nil { + t.Fatal("a stray continuation byte must be refused") + } + if !strings.Contains(err.Error(), "byte 0x80") { + t.Errorf("a stray byte is not a character and must be reported as a byte:\n%s", err.Error()) + } +} + +// The echoed name is bounded at MaxNameLen+16 bytes with a raw byte slice, so +// a long non-ASCII name was cut inside a rune. %q then renders the dangling +// bytes as \xNN escapes, so the refusal shown to the caller ends in hex noise +// instead of the name it is quoting (#1647, #1664, #1665). +// +// The 1-byte padding covers all three offsets inside a 3-byte rune. +func TestInvalidNameErrorEchoIsNotCutInsideARune(t *testing.T) { + for pad := 0; pad < 3; pad++ { + name := strings.Repeat("a", pad) + strings.Repeat("\u8a9e", 60) + + err := ValidateName(name) + if err == nil { + t.Fatalf("pad=%d: an oversized name must be refused", pad) + } + if strings.Contains(err.Error(), `\x`) { + t.Errorf("pad=%d: the echoed name was cut inside a rune: %s", pad, err.Error()) + } + } +} + +// ASCII refusals must not move. +func TestValidateNameASCIIMessagesUnchanged(t *testing.T) { + cases := map[string]string{ + "": `invalid script name "": name is empty (names are 1-64 characters of A-Z, a-z, 0-9, '-' or '_' — a name, never a path)`, + "bad/name": `invalid script name "bad/name": character "/" is not allowed (names are 1-64 characters of A-Z, a-z, 0-9, '-' or '_' — a name, never a path)`, + strings.Repeat("a", 65): `invalid script name "` + strings.Repeat("a", 65) + `": name is 65 characters long (names are 1-64 characters of A-Z, a-z, 0-9, '-' or '_' — a name, never a path)`, + } + for name, want := range cases { + err := ValidateName(name) + if err == nil { + t.Fatalf("%q: expected a refusal", name) + } + if err.Error() != want { + t.Errorf("%q:\n got %s\nwant %s", name, err.Error(), want) + } + } + if err := ValidateName("good-name_1"); err != nil { + t.Errorf("a valid name was refused: %v", err) + } +}