Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(mcp): report malformed JSON config instead of falling back to INI (#41909)

Devin Roussoce37b096bdd1
diff --git a/packages/playwright-core/src/tools/mcp/config.ts b/packages/playwright-core/src/tools/mcp/config.ts
index 47c8c40335..fd03dc661c 100644
--- a/packages/playwright-core/src/tools/mcp/config.ts
+++ b/packages/playwright-core/src/tools/mcp/config.ts
@@ -447,10 +447,16 @@ export async function loadConfig(configFile: string | undefined): Promise<Config
if (configFile.endsWith('.ini'))
return configFromIniFile(configFile);
+ const raw = await fs.promises.readFile(configFile, 'utf8');
+ const data = raw.charCodeAt(0) === 0xFEFF ? raw.slice(1) : raw;
try {
- const data = await fs.promises.readFile(configFile, 'utf8');
- return JSON.parse(data.charCodeAt(0) === 0xFEFF ? data.slice(1) : data);
- } catch {
+ return JSON.parse(data);
+ } catch (jsonError) {
+ // A JSON config is always an object, so JSON-looking input must surface its
+ // parse error rather than silently falling back to INI (and the default
+ // config). A leading `[` stays with INI — it is a `[section]` header there.
+ if (/^\s*\{/.test(data))
+ throw jsonError;
return configFromIniFile(configFile);
}
}
diff --git a/tests/mcp/config-resolve.spec.ts b/tests/mcp/config-resolve.spec.ts
index b072355372..25c0910252 100644
--- a/tests/mcp/config-resolve.spec.ts
+++ b/tests/mcp/config-resolve.spec.ts
@@ -83,6 +83,25 @@ test.describe('browserName and channel', () => {
expect(config.browser.launchOptions.channel).toBeUndefined();
});
+ test('malformed JSON config throws instead of falling back to INI', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/41893' },
+ }, async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ // Trailing comma makes this invalid JSON; it must not be silently parsed as INI.
+ await fs.promises.writeFile(configFile, '{ "browser": { "browserName": "firefox", } }');
+ await expect(resolveCLIConfigForMCP({ config: configFile }, emptyEnv)).rejects.toThrow();
+ });
+
+ test('INI config starting with a section header still parses', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/41893' },
+ }, async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.cfg');
+ // A leading `[section]` is INI, not JSON — it must not be treated as malformed JSON.
+ await fs.promises.writeFile(configFile, '[browser]\nbrowserName = firefox\n');
+ const config = await resolveCLIConfigForMCP({ config: configFile }, emptyEnv);
+ expect(config.browser.browserName).toBe('firefox');
+ });
+
test('config file browserName + channel are both preserved', async ({}, testInfo) => {
const configFile = testInfo.outputPath('config.json');
const fileConfig: Config = {