fix(server): quote copied native provider update commands (#9856)

This commit is contained in:
Julius Marminge authored and GitHub committed 2026-09-04 19:29:46 -07:00
1 parent d7fe47fd09
commit d28077e585
4 files changed
+164 -5

No files matched your search

@@ -0,0 +1,62 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { expect, it } from "@effect/vitest";
import { ProviderInstanceId } from "@t3tools/contracts";
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { HttpClient } from "effect/unstable/http";
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
import { ServerConfig } from "../../config.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
import { NoOpProviderEventLoggers, ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
import { CursorDriver } from "./CursorDriver.ts";
const testLayer = ServerConfig.layerTest(process.cwd(), {
prefix: "t3-cursor-driver-copy-command-",
}).pipe(
Layer.provideMerge(NodeServices.layer),
Layer.provideMerge(ServerSettingsService.layerTest()),
Layer.provideMerge(
Layer.mock(BackgroundPolicy.BackgroundPolicy)({
shouldRunScopeWork: () => Effect.succeed(false),
}),
),
Layer.provideMerge(Layer.succeed(ProviderEventLoggers, NoOpProviderEventLoggers)),
Layer.provideMerge(
Layer.succeed(
HttpClient.HttpClient,
HttpClient.make(() => Effect.die("Disabled Cursor must not make an HTTP request")),
),
),
);
it.layer(testLayer)("CursorDriver", (it) => {
it.effect("formats a copied update command for the injected Windows environment", () =>
Effect.gen(function* () {
const binaryPath = "C:\\Users\\Example User\\.local\\bin\\cursor-agent.exe";
const instance = yield* CursorDriver.create({
instanceId: ProviderInstanceId.make("cursor-copy-command"),
displayName: "Cursor test",
enabled: false,
environment: [],
config: { ...CursorDriver.defaultConfig(), binaryPath },
});
expect(instance.snapshot.maintenanceCapabilities.update).toMatchObject({
command: `& '${binaryPath}' update`,
executable: binaryPath,
args: ["update"],
});
expect((yield* instance.snapshot.refresh).status).toBe("disabled");
}).pipe(
Effect.provideService(HostProcessPlatform, "win32"),
Effect.provideService(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make(() => Effect.die("Disabled Cursor must not spawn a process")),
),
Effect.scoped,
),
);
});
@@ -62,6 +62,7 @@ const UPDATE: ProviderMaintenanceCapabilitiesResolver = {
updateExecutable: options?.binaryPath?.trim() || "cursor-agent",
updateArgs: ["update"],
updateLockKey: "cursor-agent",
...(options?.platform ? { platform: options.platform } : {}),
}),
};
@@ -447,6 +447,77 @@ it.layer(NodeServices.layer)("providerMaintenance", (it) => {
expect(nativePackageToolUpdate.resolve({ binaryPath }).update?.executable).toBe(binaryPath);
});
for (const directoryName of ["plain", "with spaces", "with 'quotes' and $dollars"]) {
it.effect.skipIf(windowsHost)(
`runs the copied native update command from a ${directoryName} path`,
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-copy-native-update-" });
const binDir = NodePath.join(tempDir, directoryName, ".scoped-package-tool", "bin");
yield* fs.makeDirectory(binDir, { recursive: true });
const binaryPath = NodePath.join(binDir, "scoped-package-tool");
yield* fs.writeFileString(binaryPath, '#!/bin/sh\nprintf "%s" "$1"\n');
yield* fs.chmod(binaryPath, 0o755);
const capabilities = scopedPackageToolUpdate.resolve({ binaryPath });
const advisory = createProviderVersionAdvisory({
driver: driver("scopedPackageTool"),
currentVersion: "1.0.0",
latestVersion: "2.0.0",
maintenanceCapabilities: capabilities,
});
expect(advisory.updateCommand).not.toBeNull();
if (!advisory.updateCommand) return;
const result = NodeChildProcess.spawnSync("/bin/sh", ["-c", advisory.updateCommand], {
env: { PATH: "" },
encoding: "utf8",
});
expect(result.error).toBeUndefined();
expect(result.status, result.stderr).toBe(0);
expect(result.stdout).toBe("upgrade");
}).pipe(Effect.scoped),
);
}
it.each([
{
binaryPath: "C:\\Users\\Example\\.local\\bin\\native-package-tool.exe",
command: "C:\\Users\\Example\\.local\\bin\\native-package-tool.exe update",
},
{
binaryPath: "C:\\Users\\Example User\\.local\\bin\\native-package-tool.exe",
command: "& 'C:\\Users\\Example User\\.local\\bin\\native-package-tool.exe' update",
},
{
binaryPath: "C:\\Users\\O'Connor\\.local\\bin\\native-package-tool.exe",
command: "& 'C:\\Users\\O''Connor\\.local\\bin\\native-package-tool.exe' update",
},
{
binaryPath: "C:\\Users\\O\u2019Connor\\.local\\bin\\native-package-tool.exe",
command: "& 'C:\\Users\\O\u2019\u2019Connor\\.local\\bin\\native-package-tool.exe' update",
},
])("formats a copied PowerShell native update for $binaryPath", ({ binaryPath, command }) => {
expect(nativePackageToolUpdate.resolve({ binaryPath, platform: "win32" }).update).toEqual({
command,
executable: binaryPath,
args: ["update"],
lockKey: "native-package-tool-native",
});
});
it.effect("uses the environment's platform for copied native update commands", () =>
Effect.gen(function* () {
const binaryPath = "C:\\Users\\Example User\\.local\\bin\\native-package-tool.exe";
const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
nativePackageToolUpdate,
{ binaryPath, env: { PATH: "" } },
);
expect(capabilities.update?.command).toBe(`& '${binaryPath}' update`);
expect(capabilities.update?.executable).toBe(binaryPath);
}).pipe(Effect.provideService(HostProcessPlatform, "win32")),
);
it("uses the resolved launcher when its symlink target identifies a native install", () => {
const launcher = "/custom tools/native-launcher";
const capabilities = nativePackageToolUpdate.resolve({
@@ -5,6 +5,7 @@ import {
} from "@t3tools/contracts";
import { compareSemverVersions } from "@t3tools/shared/semver";
import { resolveCommandPath } from "@t3tools/shared/shell";
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
import * as Config from "effect/Config";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
@@ -52,6 +53,7 @@ export interface ProviderMaintenanceCommandAction {
export interface ProviderMaintenanceCapabilityResolutionOptions {
readonly binaryPath?: string | null;
readonly platform?: NodeJS.Platform;
readonly env?: NodeJS.ProcessEnv;
readonly resolvedCommandPath?: string | null;
readonly realCommandPath?: string | null;
@@ -94,18 +96,34 @@ function nonEmptyString(value: unknown): string | null {
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
}
function quoteUpdateExecutable(executable: string, platform: NodeJS.Platform): string {
const safePath = platform === "win32" ? /^[\w./:\\-]+$/ : /^[\w./:-]+$/;
if (safePath.test(executable)) return executable;
// Windows terminals default to PowerShell, where a quoted executable needs &.
return platform === "win32"
? `& '${executable.replace(/['\u2018\u2019]/g, "$&$&")}'`
: `'${executable.replaceAll("'", "'\\''")}'`;
}
export function makeProviderMaintenanceCapabilities(input: {
readonly provider: ProviderDriverKind;
readonly packageName: string | null;
readonly updateExecutable: string | null;
readonly updateArgs: ReadonlyArray<string>;
readonly updateLockKey: string | null;
readonly platform?: NodeJS.Platform;
}): ProviderMaintenanceCapabilities {
const update =
input.updateExecutable === null || input.updateLockKey === null
? null
: {
command: [input.updateExecutable, ...input.updateArgs].join(" "),
command: [
quoteUpdateExecutable(
input.updateExecutable,
input.platform ?? HostProcessPlatform.defaultValue(),
),
...input.updateArgs,
].join(" "),
executable: input.updateExecutable,
args: input.updateArgs,
lockKey: input.updateLockKey,
@@ -210,6 +228,7 @@ function makeHomebrewProviderMaintenanceCapabilities(
function makeNativeProviderMaintenanceCapabilities(
definition: PackageManagedProviderMaintenanceDefinition,
commandPath: string,
platform: NodeJS.Platform,
): ProviderMaintenanceCapabilities | null {
if (!definition.nativeUpdate) {
return null;
@@ -221,6 +240,7 @@ function makeNativeProviderMaintenanceCapabilities(
updateExecutable: commandPath,
updateArgs: definition.nativeUpdate.args,
updateLockKey: definition.nativeUpdate.lockKey,
platform,
});
}
@@ -298,8 +318,11 @@ export function resolvePackageManagedProviderMaintenance(
commandPaths.some((commandPath) => nativeUpdate.isCommandPath(commandPath))
) {
return (
makeNativeProviderMaintenanceCapabilities(definition, resolvedCommandPath) ??
makeNpmGlobalProviderMaintenanceCapabilities(definition)
makeNativeProviderMaintenanceCapabilities(
definition,
resolvedCommandPath,
options?.platform ?? HostProcessPlatform.defaultValue(),
) ?? makeNpmGlobalProviderMaintenanceCapabilities(definition)
);
}
if (commandPaths.some(isVitePlusGlobalCommandPath)) {
@@ -361,8 +384,9 @@ export const resolveProviderMaintenanceCapabilitiesEffect = Effect.fn(
options?: Omit<ProviderMaintenanceCapabilityResolutionOptions, "realCommandPath">,
) {
const binaryPath = nonEmptyString(options?.binaryPath);
const platform = options?.platform ?? (yield* HostProcessPlatform);
if (!binaryPath) {
return resolver.resolve(options);
return resolver.resolve({ ...options, platform });
}
const env = options?.env ?? (yield* readCommandLookupEnv);
@@ -371,7 +395,7 @@ export const resolveProviderMaintenanceCapabilitiesEffect = Effect.fn(
Effect.catchTag("CommandResolutionError", () => Effect.succeed(null)),
)) ?? (hasPathSeparator(binaryPath) ? binaryPath : null);
if (!resolvedCommandPath) {
return resolver.resolve(options);
return resolver.resolve({ ...options, platform });
}
const fileSystem = yield* FileSystem.FileSystem;
@@ -380,6 +404,7 @@ export const resolveProviderMaintenanceCapabilitiesEffect = Effect.fn(
.pipe(Effect.orElseSucceed(() => resolvedCommandPath));
return resolver.resolve({
...options,
platform,
env,
resolvedCommandPath,
realCommandPath,