Skip to content

Commit 728f42f

Browse files
committed
fix(devices): detect Windows behind POSIX SSH shells
1 parent 3a2c1b3 commit 728f42f

2 files changed

Lines changed: 91 additions & 46 deletions

File tree

‎apps/server/src/device/SshDeviceHost.test.ts‎

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -393,78 +393,112 @@ it.effect.each([
393393
exitCode: 1,
394394
stderr: "sh : The term 'sh' is not recognized as the name of a cmdlet\r\n",
395395
shellEcho: "%OS%\r\nWindows_NT\r\n",
396+
nodePlatform: null,
396397
windows: true,
397398
},
398399
{
399400
name: "a Windows cmd login shell",
400401
exitCode: 1,
401402
stderr: "'sh' is not recognized as an internal or external command,\r\n",
402403
shellEcho: "Windows_NT $env:OS\r\n",
404+
nodePlatform: null,
403405
windows: true,
404406
},
405407
{
406408
name: "a Windows cmd login shell returning 9009",
407409
exitCode: 9009,
408410
stderr: "'sh' is not recognized as an internal or external command,\r\n",
409411
shellEcho: "Windows_NT $env:OS\r\n",
412+
nodePlatform: null,
410413
windows: true,
411414
},
415+
{
416+
name: "a Windows Git Bash login shell without a spawnable npm",
417+
exitCode: 1,
418+
stderr: "npm is missing from the non-interactive SSH PATH\n",
419+
shellEcho: "%OS% :OS\n",
420+
nodePlatform: "win32",
421+
windows: true,
422+
},
423+
{
424+
name: "a Linux host without npm",
425+
exitCode: 1,
426+
stderr: "npm is missing from the non-interactive SSH PATH\n",
427+
shellEcho: "%OS% :OS\n",
428+
nodePlatform: "linux",
429+
windows: false,
430+
},
412431
{
413432
name: "a POSIX host without Node",
414433
exitCode: 1,
415434
stderr: "Node is missing from the non-interactive SSH PATH\n",
416435
shellEcho: "%OS% :OS\n",
436+
nodePlatform: null,
417437
windows: false,
418438
},
419439
{
420440
name: "a connection failure",
421441
exitCode: 255,
422442
stderr: "Connection refused\n",
423443
shellEcho: "Windows_NT\n",
444+
nodePlatform: "win32",
424445
windows: false,
425446
},
426-
])("probe failure on $name keeps the SSH cause", ({ exitCode, stderr, shellEcho, windows }) =>
427-
Effect.gen(function* () {
428-
const fs = yield* FileSystem.FileSystem;
429-
const home = yield* fs.makeTempDirectoryScoped();
430-
const shellChecks: string[] = [];
431-
const spawner = ChildProcessSpawner.make((command) =>
432-
Effect.gen(function* () {
433-
if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
434-
const remote = command.args.slice(command.args.indexOf("test.example") + 1).join(" ");
435-
const shellCheck = remote === "echo %OS% $env:OS";
436-
if (shellCheck) shellChecks.push(remote);
437-
return ChildProcessSpawner.makeHandle({
438-
pid: ChildProcessSpawner.ProcessId(123),
439-
stdout: Stream.make(new TextEncoder().encode(shellCheck ? shellEcho : "")),
440-
stderr: Stream.make(new TextEncoder().encode(shellCheck ? "" : stderr)),
441-
all: Stream.empty,
442-
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(shellCheck ? 0 : exitCode)),
443-
isRunning: Effect.succeed(false),
444-
kill: () => Effect.void,
445-
stdin: Sink.drain,
446-
getInputFd: () => Sink.drain,
447-
getOutputFd: () => Stream.empty,
448-
unref: Effect.succeed(Effect.void),
449-
});
450-
}),
451-
);
452-
const failure = yield* SshDeviceHost.probe({
453-
kind: "ssh",
454-
id: "test",
455-
label: "Test",
456-
target: "test.example",
457-
}).pipe(
458-
Effect.provide(ServerConfig.layerTest(home, home)),
459-
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
460-
Effect.flip,
461-
);
462-
expect(failure).toMatchObject({ _tag: "DeviceHostError", step: "probe" });
463-
const sshFailure = { _tag: "SshCommandError", exitCode, stderr };
464-
expect(failure.cause).toMatchObject(
465-
windows ? { _tag: "SshWindowsHostError", cause: sshFailure } : sshFailure,
466-
);
467-
// A transport failure has no remote shell to ask.
468-
if (exitCode === 255) expect(shellChecks).toEqual([]);
469-
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
447+
])(
448+
"probe failure on $name keeps the SSH cause",
449+
({ exitCode, stderr, shellEcho, nodePlatform, windows }) =>
450+
Effect.gen(function* () {
451+
const fs = yield* FileSystem.FileSystem;
452+
const home = yield* fs.makeTempDirectoryScoped();
453+
const shellChecks: string[] = [];
454+
const spawner = ChildProcessSpawner.make((command) =>
455+
Effect.gen(function* () {
456+
if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
457+
const remote = command.args.slice(command.args.indexOf("test.example") + 1).join(" ");
458+
const shellCheck = remote === "echo %OS% $env:OS";
459+
const platformCheck = remote === "node -p process.platform";
460+
if (shellCheck || platformCheck) shellChecks.push(remote);
461+
return ChildProcessSpawner.makeHandle({
462+
pid: ChildProcessSpawner.ProcessId(123),
463+
stdout: Stream.make(
464+
new TextEncoder().encode(
465+
shellCheck ? shellEcho : platformCheck ? `${nodePlatform ?? ""}\n` : "",
466+
),
467+
),
468+
stderr: Stream.make(
469+
new TextEncoder().encode(shellCheck || platformCheck ? "" : stderr),
470+
),
471+
all: Stream.empty,
472+
exitCode: Effect.succeed(
473+
ChildProcessSpawner.ExitCode(
474+
shellCheck ? 0 : platformCheck ? (nodePlatform === null ? 1 : 0) : exitCode,
475+
),
476+
),
477+
isRunning: Effect.succeed(false),
478+
kill: () => Effect.void,
479+
stdin: Sink.drain,
480+
getInputFd: () => Sink.drain,
481+
getOutputFd: () => Stream.empty,
482+
unref: Effect.succeed(Effect.void),
483+
});
484+
}),
485+
);
486+
const failure = yield* SshDeviceHost.probe({
487+
kind: "ssh",
488+
id: "test",
489+
label: "Test",
490+
target: "test.example",
491+
}).pipe(
492+
Effect.provide(ServerConfig.layerTest(home, home)),
493+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
494+
Effect.flip,
495+
);
496+
expect(failure).toMatchObject({ _tag: "DeviceHostError", step: "probe" });
497+
const sshFailure = { _tag: "SshCommandError", exitCode, stderr };
498+
expect(failure.cause).toMatchObject(
499+
windows ? { _tag: "SshWindowsHostError", cause: sshFailure } : sshFailure,
500+
);
501+
// A transport failure has no remote shell to ask.
502+
if (exitCode === 255) expect(shellChecks).toEqual([]);
503+
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
470504
);

‎apps/server/src/device/SshDeviceHost.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { quoteRemoteArg, remoteDeviceEnvironment, remoteDeviceScript } from "./s
3030

3131
const UNHEALTHY_PROBE_LIMIT = 3;
3232

33-
/** The host bootstrap failed on a host whose login shell reports Windows. */
33+
/** The host bootstrap failed on a host identified as Windows. */
3434
export class SshWindowsHostError extends Schema.TaggedError<SshWindowsHostError>()(
3535
"SshWindowsHostError",
3636
{ cause: Schema.Defect() },
@@ -104,14 +104,25 @@ const explainWindowsHost = Effect.fn("SshDeviceHost.explainWindowsHost")(functio
104104
(cause.exitCode > 254 && cause.exitCode !== 9009)
105105
)
106106
return yield* error;
107-
const windows = yield* runSshCommand(targetFor(config), {
107+
let windows = yield* runSshCommand(targetFor(config), {
108108
preHostArgs: identityArgs(config),
109109
remoteCommandArgs: ["echo %OS% $env:OS"],
110110
timeoutMs: 15_000,
111111
}).pipe(
112112
Effect.map(({ stdout }) => /(?:^|\s)Windows_NT(?:\s|$)/u.test(stdout)),
113113
Effect.orElseSucceed(() => false),
114114
);
115+
if (!windows) {
116+
// Git Bash does not expand Windows shell variables, but its Node still reports win32.
117+
windows = yield* runSshCommand(targetFor(config), {
118+
preHostArgs: identityArgs(config),
119+
remoteCommandArgs: ["node", "-p", "process.platform"],
120+
timeoutMs: 15_000,
121+
}).pipe(
122+
Effect.map(({ stdout }) => stdout.trim() === "win32"),
123+
Effect.orElseSucceed(() => false),
124+
);
125+
}
115126
if (!windows) return yield* error;
116127
return yield* new DeviceHost.DeviceHostError({
117128
hostId: config.id,

0 commit comments

Comments
 (0)