fix(electron): refuse owned switches spelled with / on Windows

Chromium reads `/` as a switch prefix on Windows, so `/user-data-dir=...`
got past ownedArgument there. On Windows the check now reads a leading
`/` as a dash; elsewhere `/...` stays a path. The callers pass only the
argument, so `.map` cannot hand the index in as the platform.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
michal-e2e
2026-11-28 23:55:10 +01:00
co-authored by Claude Opus 5.5
parent 23c0f24a69
commit 58235a3ad5
4 changed files with 13 additions and 7 deletions
+1 -1
View File
@@ -206,7 +206,7 @@ them.
`app.launchArguments` and `launch({ launchArguments })` must not set `--user-data-dir`,
`--inspect`, `--inspect-brk`, `--inspect-brk-node`, `--inspect-port`,
`--debug-port`, `--inspect-publish-uid`, `--remote-debugging-port`, or
`--remote-debugging-pipe`, with `-` or `--` and in any letter case: the engine
`--remote-debugging-pipe`, with `-` or `--` (or `/` on Windows) and in any letter case: the engine
owns them.
#### Environment
+1 -1
View File
@@ -100,7 +100,7 @@ function validateApp(app: EngineAppDeclaration, { targetName }: EngineAppCheckIn
}
if (app.bundleId !== undefined) throw new ConfigurationError('INVALID_CONFIG', `${where} declares app.bundleId, which an electron() target cannot launch; name the app with app.appPath`);
if (app.permissions !== undefined) throw new ConfigurationError('INVALID_CONFIG', `${where} declares app.permissions, which an electron() target cannot set`);
const owned = app.launchArguments?.map(ownedArgument).find((reason) => reason !== undefined);
const owned = app.launchArguments?.map((argument) => ownedArgument(argument)).find((reason) => reason !== undefined);
if (owned !== undefined) throw new ConfigurationError('INVALID_CONFIG', `${where} app.launchArguments ${owned}`);
if (app.appPath === undefined && executablePath === undefined) {
throw new ConfigurationError(
+5 -4
View File
@@ -64,8 +64,9 @@ export interface ResolvedElectronOptions {
* the DevTools transport the engine drives the app through (a fixed port
* would also collide between workers).
*/
export function ownedArgument(argument: string): string | undefined {
const trimmed = argument.trim();
export function ownedArgument(argument: string, platform: NodeJS.Platform = process.platform): string | undefined {
// Chromium also reads `/` as a switch prefix on Windows; elsewhere `/...` is a path.
const trimmed = platform === 'win32' ? argument.trim().replace(/^\//, '-') : argument.trim();
if (/^--?user-data-dir(?:=|$)/i.test(trimmed)) return 'must not set --user-data-dir: the engine gives every attempt an empty one of its own';
const port = /^--?(inspect(?:-(?:brk(?:-node)?|port|publish-uid))?|debug-port|remote-debugging-port|remote-debugging-pipe)(?:=|$)/i.exec(trimmed);
if (port !== null) return `must not set --${port[1]!.toLowerCase()}: the engine opens the inspector and the DevTools pipe it drives the app through itself`;
@@ -74,7 +75,7 @@ export function ownedArgument(argument: string): string | undefined {
/** Checks the target's `app.launchArguments`: the app's own switches and Chromium's, none of the engine's. */
export function checkLaunchArguments(launchArguments: readonly string[] | undefined): void {
const owned = launchArguments?.map(ownedArgument).find((reason) => reason !== undefined);
const owned = launchArguments?.map((argument) => ownedArgument(argument)).find((reason) => reason !== undefined);
if (owned !== undefined) throw new ConfigurationError('INVALID_CONFIG', `app.launchArguments ${owned}`);
}
@@ -159,7 +160,7 @@ export function checkLaunchOptions(options: unknown, projectRoot: string): { rea
rejectUnknownKeys(`${label} options`, options, LAUNCH_KEYS, 'INVALID_ARGUMENT');
const { launchArguments: args = [], env, cwd } = options as { readonly launchArguments?: unknown; readonly env?: unknown; readonly cwd?: unknown };
if (!Array.isArray(args) || args.some((argument) => typeof argument !== 'string')) throw refusal('INVALID_ARGUMENT', 'electron.launch({ launchArguments }) must be a list of strings');
const owned = (args as string[]).map(ownedArgument).find((reason) => reason !== undefined);
const owned = (args as string[]).map((argument) => ownedArgument(argument)).find((reason) => reason !== undefined);
if (owned !== undefined) throw refusal('INVALID_ARGUMENT', `electron.launch({ launchArguments }) ${owned}`);
checkEnv('electron.launch({ env })', env, 'INVALID_ARGUMENT');
checkCwdShape('electron.launch({ cwd })', cwd, 'INVALID_ARGUMENT');
+6 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { electron } from '../../src/engine.ts';
import { checkCwd, checkLaunchArguments, checkLaunchOptions, resolveOptions } from '../../src/options.ts';
import { checkCwd, checkLaunchArguments, checkLaunchOptions, ownedArgument, resolveOptions } from '../../src/options.ts';
describe('electron()', () => {
it('declares the capabilities of a Chromium window driven over the DevTools protocol', () => {
@@ -139,6 +139,11 @@ describe('checkLaunchOptions', () => {
expect(() => checkLaunchOptions({ launchArguments: [flag] }, root), flag).toThrow(expect.objectContaining({ code: 'INVALID_ARGUMENT' }));
}
expect(() => checkLaunchOptions({ launchArguments: ['--user-data-directory=/tmp/x', '--inspector-theme=dark', '/inspect'] }, root)).not.toThrow();
// Chromium reads `/` as a switch prefix on Windows only; elsewhere `/...` is a path.
for (const flag of ['/user-data-dir=C:\\elsewhere', '/remote-debugging-port=9222', '/INSPECT']) {
expect(ownedArgument(flag, 'win32'), flag).toMatch(/must not set --/);
expect(ownedArgument(flag, 'linux'), flag).toBeUndefined();
}
expect(() => checkLaunchOptions({ env: { '': '1' } }, root)).toThrow(expect.objectContaining({ code: 'INVALID_ARGUMENT', message: expect.stringMatching(/empty variable name/) }));
expect(() => checkLaunchOptions({ env: { 'NODE_OPTIONS=--inspect': '1' } }, root)).toThrow(expect.objectContaining({ code: 'INVALID_ARGUMENT', message: expect.stringMatching(/variable name containing "="/) }));
});