Skip to content

Commit e692729

Browse files
only21milclaudemaria-rcks
authored
fix(desktop): resolve executable commands when checking CLI shadowing (pingdotgg#22046)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent a03e630 commit e692729

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎apps/desktop/src/app/DesktopCliCommand.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { afterEach, beforeEach, expect, it } from "@effect/vitest";
3+
import * as HostProcess from "@t3tools/shared/HostProcess";
34
import * as Effect from "effect/Effect";
45
import * as FileSystem from "effect/FileSystem";
56
import * as Option from "effect/Option";
@@ -183,6 +184,39 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
183184
}).pipe(Effect.scoped),
184185
);
185186

187+
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
188+
"skips a t3 directory or a file this user cannot execute",
189+
() =>
190+
Effect.gen(function* () {
191+
const fs = yield* FileSystem.FileSystem;
192+
const path = yield* Path.Path;
193+
const home = yield* fs.makeTempDirectoryScoped();
194+
const folder = path.join(home, "folder");
195+
const plain = path.join(home, "plain");
196+
const shadow = path.join(home, "shadow");
197+
yield* fs.makeDirectory(path.join(folder, "t3"), { recursive: true });
198+
yield* fs.makeDirectory(plain);
199+
yield* fs.writeFileString(path.join(plain, "t3"), "notes\n");
200+
// Root can execute mode 0o601; other users must honor the owner permissions.
201+
yield* fs.chmod(path.join(plain, "t3"), process.getuid?.() === 0 ? 0o600 : 0o601);
202+
yield* fs.makeDirectory(shadow);
203+
yield* fs.writeFileString(path.join(shadow, "t3"), "#!/bin/sh\n", { mode: 0o755 });
204+
process.env.PATH = [folder, plain, shadow, path.join(home, ".local", "bin")].join(":");
205+
206+
const command = yield* commandIn({ home });
207+
expect((yield* command.state).shadowedBy).toBe(path.join(shadow, "t3"));
208+
209+
const error = yield* Effect.flip(command.install);
210+
expect(error.message).toContain(path.join(shadow, "t3"));
211+
212+
yield* fs.remove(shadow, { recursive: true });
213+
expect(yield* command.install).toMatchObject({
214+
onPath: true,
215+
installedPath: path.join(home, ".local", "bin", "t3"),
216+
});
217+
}).pipe(Effect.scoped),
218+
);
219+
186220
it.effect("leaves the Windows PATH alone when it cannot be read", () =>
187221
Effect.gen(function* () {
188222
const fs = yield* FileSystem.FileSystem;

‎apps/desktop/src/app/DesktopCliCommand.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// @effect-diagnostics-next-line nodeBuiltinImport:off -- FileSystem.access has no execute check.
2+
import * as NodeFSP from "node:fs/promises";
3+
14
import type { DesktopCliCommandState } from "@t3tools/contracts";
25
import * as Context from "effect/Context";
36
import * as Effect from "effect/Effect";
@@ -118,10 +121,19 @@ export const make = Effect.gen(function* () {
118121
Effect.mapError(() => fail("Could not update your PATH.")),
119122
);
120123

124+
const isExecutableFile = Effect.fn("DesktopCliCommand.isExecutableFile")(
125+
function* (target: string) {
126+
if ((yield* fs.stat(target)).type !== "File") return false;
127+
yield* Effect.tryPromise(() => NodeFSP.access(target, NodeFSP.constants.X_OK));
128+
return true;
129+
},
130+
Effect.orElseSucceed(() => false),
131+
);
132+
121133
const firstOnPath = Effect.gen(function* () {
122134
for (const directory of pathEntries(process.env.PATH, ":")) {
123135
const candidate = path.join(directory, "t3");
124-
if (yield* exists(candidate)) return Option.some(candidate);
136+
if (yield* isExecutableFile(candidate)) return Option.some(candidate);
125137
}
126138
return Option.none<string>();
127139
});

0 commit comments

Comments
 (0)