Skip to content
TypeScript

fix(desktop): resolve executable commands when checking CLI shadowing (#22046)

Victor Vogele692729e22a2
diff --git a/apps/desktop/src/app/DesktopCliCommand.test.ts b/apps/desktop/src/app/DesktopCliCommand.test.ts
index 3cdfdb6cfa3..7141ebde705 100644
--- a/apps/desktop/src/app/DesktopCliCommand.test.ts
+++ b/apps/desktop/src/app/DesktopCliCommand.test.ts
@@ -1,5 +1,6 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { afterEach, beforeEach, expect, it } from "@effect/vitest";
+import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
@@ -183,6 +184,39 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
}).pipe(Effect.scoped),
);
+ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
+ "skips a t3 directory or a file this user cannot execute",
+ () =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const home = yield* fs.makeTempDirectoryScoped();
+ const folder = path.join(home, "folder");
+ const plain = path.join(home, "plain");
+ const shadow = path.join(home, "shadow");
+ yield* fs.makeDirectory(path.join(folder, "t3"), { recursive: true });
+ yield* fs.makeDirectory(plain);
+ yield* fs.writeFileString(path.join(plain, "t3"), "notes\n");
+ // Root can execute mode 0o601; other users must honor the owner permissions.
+ yield* fs.chmod(path.join(plain, "t3"), process.getuid?.() === 0 ? 0o600 : 0o601);
+ yield* fs.makeDirectory(shadow);
+ yield* fs.writeFileString(path.join(shadow, "t3"), "#!/bin/sh\n", { mode: 0o755 });
+ process.env.PATH = [folder, plain, shadow, path.join(home, ".local", "bin")].join(":");
+
+ const command = yield* commandIn({ home });
+ expect((yield* command.state).shadowedBy).toBe(path.join(shadow, "t3"));
+
+ const error = yield* Effect.flip(command.install);
+ expect(error.message).toContain(path.join(shadow, "t3"));
+
+ yield* fs.remove(shadow, { recursive: true });
+ expect(yield* command.install).toMatchObject({
+ onPath: true,
+ installedPath: path.join(home, ".local", "bin", "t3"),
+ });
+ }).pipe(Effect.scoped),
+ );
+
it.effect("leaves the Windows PATH alone when it cannot be read", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
diff --git a/apps/desktop/src/app/DesktopCliCommand.ts b/apps/desktop/src/app/DesktopCliCommand.ts
index 2ae070d6382..8a89a366814 100644
--- a/apps/desktop/src/app/DesktopCliCommand.ts
+++ b/apps/desktop/src/app/DesktopCliCommand.ts
@@ -1,3 +1,6 @@
+// @effect-diagnostics-next-line nodeBuiltinImport:off -- FileSystem.access has no execute check.
+import * as NodeFSP from "node:fs/promises";
+
import type { DesktopCliCommandState } from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
@@ -118,10 +121,19 @@ export const make = Effect.gen(function* () {
Effect.mapError(() => fail("Could not update your PATH.")),
);
+ const isExecutableFile = Effect.fn("DesktopCliCommand.isExecutableFile")(
+ function* (target: string) {
+ if ((yield* fs.stat(target)).type !== "File") return false;
+ yield* Effect.tryPromise(() => NodeFSP.access(target, NodeFSP.constants.X_OK));
+ return true;
+ },
+ Effect.orElseSucceed(() => false),
+ );
+
const firstOnPath = Effect.gen(function* () {
for (const directory of pathEntries(process.env.PATH, ":")) {
const candidate = path.join(directory, "t3");
- if (yield* exists(candidate)) return Option.some(candidate);
+ if (yield* isExecutableFile(candidate)) return Option.some(candidate);
}
return Option.none<string>();
});