Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions electron/bridges/terminalBridge.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ptyProcessTree = require("./ptyProcessTree.cjs");

const sessionLogStreamManager = require("./sessionLogStreamManager.cjs");
const { detectShellKind } = require("./ai/ptyExec.cjs");
const { trackSessionIdlePrompt } = require("./ai/shellUtils.cjs");
const { stripAnsi, trackSessionIdlePrompt } = require("./ai/shellUtils.cjs");
const { createZmodemSentry } = require("./zmodemHelper.cjs");
const { discoverShells } = require("./shellDiscovery.cjs");
const moshHandshake = require("./moshHandshake.cjs");
Expand Down Expand Up @@ -904,6 +904,19 @@ function addBundledMoshRuntimeEnv(env, bareClient, opts = {}) {
return env;
}

function stripMoshPromptControls(text) {
// eslint-disable-next-line no-control-regex
return stripAnsi(text).replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]/g, "");
}

function isMoshPassphrasePrompt(tail) {
return /(^|[\r\n]).*passphrase.*:\s*$/i.test(stripMoshPromptControls(tail));
}

function isMoshPasswordPrompt(tail) {
return /(^|[\r\n]).*password:\s*$/i.test(stripMoshPromptControls(tail));
}

function createMoshSshPasswordResponder(sshPty, password, passphrase) {
if (
(typeof password !== "string" || password.length === 0) &&
Expand All @@ -922,14 +935,14 @@ function createMoshSshPasswordResponder(sshPty, password, passphrase) {
if (!text) return;

tail = (tail + text).slice(-512);
if (typeof passphrase === "string" && passphrase.length > 0 && !answeredPassphrase && /(^|[\r\n]).*passphrase.*:\s*$/i.test(tail)) {
if (typeof passphrase === "string" && passphrase.length > 0 && !answeredPassphrase && isMoshPassphrasePrompt(tail)) {
answeredPassphrase = true;
sshPty.write(`${passphrase}\r`);
return;
}

if (typeof password !== "string" || password.length === 0 || answeredPassword) return;
if (!/(^|[\r\n]).*password:\s*$/i.test(tail)) return;
if (!isMoshPasswordPrompt(tail)) return;

answeredPassword = true;
sshPty.write(`${password}\r`);
Expand Down
13 changes: 13 additions & 0 deletions electron/bridges/terminalBridge.moshHandshakeSession.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,19 @@ test("startMoshSession writes the saved password when ssh prompts for one", asyn
assert.deepEqual(h.spawns[0].writes, ["saved-secret\r"]);
});

test("startMoshSession writes the saved password when ConPTY appends cursor controls to the prompt", async (t) => {
const h = makeHarness(t);
await h.bridge.startMoshSession(
h.event,
{ ...h.options, password: "saved-secret" },
{ moshClientLookup: h.lookupOpts },
);

h.spawns[0].emitData("alice@example.com's password: \x1b[?25h");

assert.deepEqual(h.spawns[0].writes, ["saved-secret\r"]);
});

test("startMoshSession passes vault private keys to ssh via a temp identity file", async (t) => {
const h = makeHarness(t);
await h.bridge.startMoshSession(
Expand Down
Loading