diff --git a/internal/connect/connect.go b/internal/connect/connect.go index 53a47dd45..0d63e9aa0 100644 --- a/internal/connect/connect.go +++ b/internal/connect/connect.go @@ -7,7 +7,6 @@ import ( "net/url" "os" "path/filepath" - "regexp" "sort" "strings" "sync" @@ -1701,8 +1700,6 @@ func entryPointsToBridge(entry map[string]interface{}, baseURL string) bool { return hasBridgePkg && pointsToUs } -var trailingCommaPattern = regexp.MustCompile(`,\s*([}\]])`) - func unmarshalLenientJSON(raw []byte, out interface{}) error { if err := json.Unmarshal(raw, out); err == nil { normalizeNilConfigMap(out) @@ -1715,7 +1712,7 @@ func unmarshalLenientJSON(raw []byte, out interface{}) error { if cerr != nil { return cerr } - cleaned = trailingCommaPattern.ReplaceAll(cleaned, []byte(`$1`)) + cleaned = stripTrailingCommas(cleaned) if err := json.Unmarshal(cleaned, out); err != nil { return err } @@ -1791,6 +1788,44 @@ func stripJSONComments(raw []byte) ([]byte, error) { return out, nil } +// stripTrailingCommas drops a ',' that is followed (after whitespace) by '}' +// or ']'. It is string-aware like stripJSONComments: a ", }" inside a JSON +// string value (a shell snippet in args, say) is content and must survive, +// because connect writes the parsed document back to the client's file. +func stripTrailingCommas(raw []byte) []byte { + out := make([]byte, 0, len(raw)) + inString := false + escaped := false + for i := 0; i < len(raw); i++ { + c := raw[i] + if inString { + switch { + case escaped: + escaped = false + case c == '\\': + escaped = true + case c == '"': + inString = false + } + out = append(out, c) + continue + } + if c == '"' { + inString = true + } else if c == ',' { + j := i + 1 + for j < len(raw) && (raw[j] == ' ' || raw[j] == '\t' || raw[j] == '\n' || raw[j] == '\r') { + j++ + } + if j < len(raw) && (raw[j] == '}' || raw[j] == ']') { + continue + } + } + out = append(out, c) + } + return out +} + // jsonHasComments reports whether stripping comments would change the content — // i.e. the file actually uses JSONC comments (not just slashes inside strings). // An unterminated block comment counts as commented (the write guard refuses). diff --git a/internal/connect/trailing_comma_strings_test.go b/internal/connect/trailing_comma_strings_test.go new file mode 100644 index 000000000..f389d4e78 --- /dev/null +++ b/internal/connect/trailing_comma_strings_test.go @@ -0,0 +1,48 @@ +package connect + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" +) + +// The trailing-comma cleanup must not touch ", }" / ", ]" inside string +// values: connect writes the parsed document back, so a rewritten shell +// snippet in another server's args would silently change on disk. +func TestConnect_TrailingCommaCleanupPreservesStringContents(t *testing.T) { + const snippet = `echo {a, } [b, ]` + svc, home := testService(t) + cfgPath := ConfigPath("opencode", home) + if err := os.MkdirAll(filepath.Dir(cfgPath), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(cfgPath, []byte(`{ + "mcp": { + "other": {"type": "local", "command": ["sh", "-c", "echo {a, } [b, ]"]}, + }, + }`), 0o644); err != nil { + t.Fatal(err) + } + + if res, err := svc.Connect("opencode", "mcpproxy", false); err != nil || !res.Success { + t.Fatalf("connect: %+v, %v", res, err) + } + + raw, err := os.ReadFile(cfgPath) + if err != nil { + t.Fatal(err) + } + var data struct { + MCP map[string]struct { + Command []string `json:"command"` + } `json:"mcp"` + } + if err := json.Unmarshal(raw, &data); err != nil { + t.Fatal(err) + } + cmd := data.MCP["other"].Command + if len(cmd) != 3 || cmd[2] != snippet { + t.Fatalf("other server's command rewritten: got %q, want [sh -c %q]", cmd, snippet) + } +}