Skip to content

Commit f70e95a

Browse files
only21milclaude
andcommitted
fix(ssh): do not reuse an askpass runtime path that is now a symlink
The reuse check used stat, which follows symlinks. If a temp cleaner removed the cached runtime directory and someone created a symlink with the same name pointing at a directory the user owns, the check passed and the helper was written through the symlink. Whoever owns the symlink could then repoint it before ssh runs the helper. Cache the resolved path of the new temp directory, and reuse it only while it still resolves to itself and is a directory owned by the current user. Resolving the new directory keeps reuse working when TMPDIR is itself a symlink, as on macOS. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8f78741 commit f70e95a

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎packages/ssh/src/auth.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ describe("ssh auth", () => {
8787
),
8888
);
8989

90+
it.effect("does not reuse a symlink left at the default askpass directory path", () =>
91+
Effect.gen(function* () {
92+
const fs = yield* FileSystem.FileSystem;
93+
const path = yield* Path.Path;
94+
const askpassPath = () =>
95+
buildSshChildEnvironment({ interactiveAuth: true, authSecret: "pw" }).pipe(
96+
Effect.map((env) => env.SSH_ASKPASS ?? ""),
97+
);
98+
const runtimeDirectoryOf = (askpass: string) => path.dirname(path.dirname(askpass));
99+
100+
const first = yield* askpassPath();
101+
const ownedTarget = yield* fs.makeTempDirectory({ prefix: "t3code-ssh-symlink-target-" });
102+
yield* fs.remove(runtimeDirectoryOf(first), { recursive: true });
103+
yield* fs.symlink(ownedTarget, runtimeDirectoryOf(first));
104+
105+
const next = yield* askpassPath();
106+
assert.notEqual(next, first);
107+
assert.equal(yield* fs.exists(path.join(ownedTarget, "t3code-ssh-askpass")), false);
108+
109+
yield* fs.remove(runtimeDirectoryOf(first));
110+
yield* fs.remove(ownedTarget, { recursive: true });
111+
yield* fs.remove(runtimeDirectoryOf(next), { recursive: true });
112+
}).pipe(
113+
Effect.provide(Layer.merge(NodeServices.layer, Layer.succeed(HostProcess.Platform, "linux"))),
114+
),
115+
);
116+
90117
it.effect("writes the default askpass helper once when interactive commands overlap", () =>
91118
Effect.gen(function* () {
92119
const fs = yield* FileSystem.FileSystem;

‎packages/ssh/src/auth.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ const ensureDefaultSshAskpassHelpers = Effect.fn("ssh/auth.ensureDefaultSshAskpa
176176
const userId = yield* HostProcess.UserId;
177177
const cached = defaultSshAskpassRuntimeDirectory;
178178
if (cached !== undefined) {
179-
const reusable = yield* fs.stat(cached).pipe(
179+
const reusable = yield* Effect.all([fs.realPath(cached), fs.stat(cached)]).pipe(
180180
Effect.map(
181-
(info) =>
181+
([resolved, info]) =>
182+
resolved === cached &&
182183
info.type === "Directory" &&
183184
(userId === undefined || Option.getOrUndefined(info.uid) === userId),
184185
),
@@ -190,7 +191,9 @@ const ensureDefaultSshAskpassHelpers = Effect.fn("ssh/auth.ensureDefaultSshAskpa
190191
});
191192
}
192193
}
193-
const runtimeDirectory = yield* fs.makeTempDirectory({ prefix: "t3code-ssh-runtime-" });
194+
const runtimeDirectory = yield* fs.realPath(
195+
yield* fs.makeTempDirectory({ prefix: "t3code-ssh-runtime-" }),
196+
);
194197
const launcherPath = yield* ensureSshAskpassHelpers({
195198
directory: path.join(runtimeDirectory, SSH_ASKPASS_DIR_NAME),
196199
});

0 commit comments

Comments
 (0)