fix(setup): accept leading BOM in JSON client configs

This commit is contained in:
Kaoru0822
2026-10-06 03:40:42 -07:00
parent af48fe8791
commit fe843ec0b1
3 changed files with 238 additions and 1 deletions
@@ -0,0 +1,105 @@
import { describe, expect, it } from "vitest";
import {
clientConfigurationServersKey,
clientRegistrationEntry,
parseClientConfiguration,
serializeClientConfiguration,
} from "./ClientConfigurationDocument.js";
describe.each([
"json",
"vscode",
"copilot_cli",
"opencode",
"commandcode",
] as const)("%s client configuration BOM handling", (format) => {
const serversKey = clientConfigurationServersKey(format);
it.each(["", "\uFEFF"])("parses a document with prefix %j", (bom) => {
const registration = clientRegistrationEntry(format, ["rea", "mcp"], {});
const document = { [serversKey]: { rea: registration } };
expect(
parseClientConfiguration(`${bom}${JSON.stringify(document)}`, format),
).toEqual({ document, servers: document[serversKey], serversKey });
});
it.each(["", "\uFEFF"])(
"reports malformed JSON at the original offset with prefix %j",
(bom) => {
const text = `${bom}{\r\n "${serversKey}": {"rea": @}\r\n}`;
expect(() => parseClientConfiguration(text, format)).toThrow(
new SyntaxError(
`Invalid JSON/JSONC at offset ${text.indexOf("@")}: InvalidSymbol`,
),
);
},
);
it.each([
["\uFEFF\uFEFF{}", 1],
[" \uFEFF{}", 1],
['{"setting":\uFEFFtrue}', 11],
['\uFEFF{"setting":\uFEFFtrue}', 12],
] as const)("rejects a nonleading BOM in %j", (text, offset) => {
expect(() => parseClientConfiguration(text, format)).toThrow(
new SyntaxError(`Invalid JSON/JSONC at offset ${offset}: InvalidSymbol`),
);
});
it.each(["", "\uFEFF"])(
"preserves prefix %j and unrelated bytes across add, update, and removal",
(bom) => {
const prefix = `${bom}{\r\n // Keep \uFEFF in comments.\r\n "label": "left\uFEFFright",\r\n "${serversKey}": {\r\n // Other server.\r\n "other": {\r\n "command": "other\uFEFFtool"\r\n },\r\n`;
const suffix = ' },\r\n "theme": "dark",\r\n}\r\n';
const original = `${prefix}${suffix}`;
let text = original;
for (const registration of [
clientRegistrationEntry(format, ["rea", "mcp"], {}),
clientRegistrationEntry(format, ["rea-next", "mcp"], {
LABEL: "left\uFEFFright",
}),
undefined,
]) {
const parsed = parseClientConfiguration(text, format);
const servers = { ...parsed.servers };
if (registration === undefined) delete servers.rea;
else servers.rea = registration;
const document = { ...parsed.document, [serversKey]: servers };
text = serializeClientConfiguration(document, format, text, "rea");
expect(parseClientConfiguration(text, format)).toEqual({
document,
servers,
serversKey,
});
expect(Buffer.from(text.slice(0, prefix.length))).toEqual(
Buffer.from(prefix),
);
expect(Buffer.from(text.slice(-suffix.length))).toEqual(
Buffer.from(suffix),
);
expect(text.replaceAll("\r\n", "")).not.toContain("\n");
}
expect(text).toBe(original);
},
);
});
describe("TOML client configuration BOM handling", () => {
it("retains the TOML parser's existing leading-BOM rejection", () => {
const text = '[mcp_servers.rea]\ncommand = "rea"\n';
expect(parseClientConfiguration(text, "toml").servers).toEqual({
rea: { command: "rea" },
});
expect(() => parseClientConfiguration(`\uFEFF${text}`, "toml")).toThrow(
"Invalid TOML document",
);
});
});
@@ -66,7 +66,9 @@ const parseDocument = (
): Record<string, unknown> => {
if (format === "toml") return objectSchema.parse(parseToml(text));
const errors: ParseError[] = [];
const document = parseJsonc(text, errors, { allowTrailingComma: true });
// Accept a UTF-8 BOM without shifting diagnostics or editing the original text.
const jsonText = text.startsWith("\uFEFF") ? ` ${text.slice(1)}` : text;
const document = parseJsonc(jsonText, errors, { allowTrailingComma: true });
const firstError = errors[0];
if (firstError !== undefined)
throw new SyntaxError(