Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions internal/codescripts/codescripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
"path/filepath"
"sort"
"strings"
"unicode/utf8"

"github.com/smart-mcp-proxy/mcpproxy-go/internal/stringutil"
)

const (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) + "…"
}
78 changes: 78 additions & 0 deletions internal/codescripts/invalid_name_message_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading