Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
89 changes: 89 additions & 0 deletions apps/server/src/process/externalLauncher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Deferred from "effect/Deferred";
import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
Expand Down Expand Up @@ -1381,6 +1383,93 @@ it.effect("keeps scanning after the caller is interrupted and shares that scan",
);
});

const launchWindowsShimEditor = (cwd: string) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
yield* fileSystem.writeFileString(path.join(binDir, "code.CMD"), "@echo off\r\n");
let spawned: ChildProcess.StandardCommand | undefined;
const exit = yield* Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
return yield* launcher.launchEditor({ editor: "vscode", cwd }).pipe(Effect.exit);
}).pipe(
Effect.provide(
layerTest({
platform: "win32",
env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" },
resolveExecutable: (command) =>
command === "code" ? "C:\\Program Files\\Microsoft VS Code\\bin\\code.CMD" : command,
onSpawn: (command) => {
spawned = command;
},
}),
),
);
return { exit, spawned };
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer));

it.effect.each([
["CRLF", "C:\\workspace\\file.ts\r\ncalc.exe"],
["LF", "C:\\workspace\\file.ts\ncalc.exe"],
["double quote", 'C:\\workspace\\file.ts" & calc.exe & "'],
] as const)("does not start a Windows command shim for a path with a $0", ([, cwd]) =>
Effect.gen(function* () {
const { exit, spawned } = yield* launchWindowsShimEditor(cwd);
assert.equal(spawned, undefined);
assert.isTrue(Exit.isFailure(exit));
if (Exit.isFailure(exit)) {
assert.instanceOf(
Cause.squash(exit.cause),
ExternalLauncher.ExternalLauncherEditorSpawnError,
);
}
}),
);

it.effect("starts a Windows command shim for unicode paths with cmd metacharacters", () =>
Effect.gen(function* () {
const { exit, spawned } = yield* launchWindowsShimEditor(
"C:\\Users\\jö\\R&D (100%)\\naïve ^file!.ts:3:7",
);
assert.isTrue(Exit.isSuccess(exit));
assert.ok(spawned);
assert.equal(spawned.options.shell, true);
assert.deepEqual(spawned.args, [
'^"--goto^"',
'^"C:\\Users\\jö\\R^&D^ ^(100^%^)\\naïve^ ^^file^!.ts:3:7^"',
]);
}),
);

it.effect.skipIf(windowsHost)("passes line breaks through to editors started without a shell", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
const zed = path.join(binDir, "zed");
yield* fileSystem.writeFileString(zed, "#!/bin/sh\n");
yield* fileSystem.chmod(zed, 0o755);
let spawned: ChildProcess.StandardCommand | undefined;
yield* Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
yield* launcher.launchEditor({ editor: "zed", cwd: "/workspace/odd\nname.ts" });
}).pipe(
Effect.provide(
layerTest({
platform: "linux",
env: { PATH: binDir, HOME: binDir },
onSpawn: (command) => {
spawned = command;
},
}),
),
);
assert.ok(spawned);
assert.deepEqual(spawned.args, ["/workspace/odd\nname.ts"]);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);

it.effect("rejects unknown editors through the service API", () =>
Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
Expand Down
17 changes: 17 additions & 0 deletions apps/server/src/process/externalLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ interface TargetPathAndPosition {
readonly column: Option.Option<string>;
}

/**
* Windows command shims (`code.cmd`) forward arguments through `%*`, which
* cmd.exe parses a second time after the first escaping layer is gone: a
* line break ends the command there, and a double quote closes the quoting
* that keeps `&` or `|` literal. Neither can occur in a Windows path.
*/
// oxlint-disable-next-line no-control-regex
const WINDOWS_SHIM_UNSAFE_ARG_PATTERN = /[\u0000-\u001f\u007f"]/;
const TARGET_WITH_POSITION_PATTERN = /^(.*?):(\d+)(?::(\d+))?$/;
const POWERSHELL_ARGUMENTS_PREFIX = [
"-NoProfile",
Expand Down Expand Up @@ -726,6 +734,15 @@ const launchEditorProcess = Effect.fn("externalLauncher.launchEditorProcess")(fu
}

const spawnCommand = yield* resolveSpawnCommand(launch.command, launch.args, { env });
if (spawnCommand.shell && launch.args.some((arg) => WINDOWS_SHIM_UNSAFE_ARG_PATTERN.test(arg))) {
return yield* new ExternalLauncherEditorSpawnError({
editor: launch.editor,
target: launch.target,
command: launch.command,
args: launch.args,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
cause: new Error("Editor arguments contain characters a Windows command shim cannot pass"),
});
}
yield* launchAndUnref(
{
command: spawnCommand.command,
Expand Down
Loading