From 282e5845caf00327ccd9955e4160d854d62f2205 Mon Sep 17 00:00:00 2001 From: QQ Date: Wed, 11 Nov 2026 22:37:44 +0800 Subject: [PATCH] Import vendor/dsh-remote-ssh/tests/askpass-ascii.test.mjs --- .../tests/askpass-ascii.test.mjs | 264 ++++++++++++++++++ 1 file changed, 264 insertions(+) create mode 100644 vendor/dsh-remote-ssh/tests/askpass-ascii.test.mjs diff --git a/vendor/dsh-remote-ssh/tests/askpass-ascii.test.mjs b/vendor/dsh-remote-ssh/tests/askpass-ascii.test.mjs new file mode 100644 index 0000000..31e1dfb --- /dev/null +++ b/vendor/dsh-remote-ssh/tests/askpass-ascii.test.mjs @@ -0,0 +1,264 @@ +// issue #36 回归测试:Windows 用户名含非 ASCII 字符时,password 认证全链路失效。 +// +// 根因(真机复现,OpenSSH_for_Windows_9.5p2 / Win11): +// Windows OpenSSH 用**窄字符路径**启动 askpass 助手(CreateProcessW / posix_spawnp), +// SSH_ASKPASS 指向含中文的路径时直接失败: +// ssh_askpass: posix_spawnp: No such file or directory +// CreateProcessW failed error:2 +// 助手没跑起来 → ssh 把**空口令**发给服务端 → 服务端只回 Permission denied。 +// 插件此前把助手写在 ~/.dsh/remote-ssh/askpass.cmd,而中文用户名下 %USERPROFILE% 与 %TEMP% +// 都不是 ASCII,于是整条密码认证 100% 失败,错误提示还误导成「公钥认证失败 / 检查 keyPath」。 +// +// 本文件覆盖: +// A. ensureWindowsAskpass:ProgramData → Public → 用户目录 回退、纯 ASCII 校验、 +// DSH_REMOTE_SSH_ASKPASS 覆盖、助手内容(无 BOM / CRLF / 两行 / 不含口令)、按用户分文件名 +// B. sshArgv:Windows 密码认证指向 ASCII 落点;找不到 ASCII 落点时抛**明确**错误;POSIX/key 不受影响 +// C. sshErrorHint:按认证方式归因 —— askpass 证据优先、密码连接不再提 keyPath、key 连接行为不变 +// D. 真机 OpenSSH E2E(Windows + ssh-keygen 可用时):中文路径失败 / ASCII 路径成功 / 插件落点成功 +// E. 源码接线断言 +import { readFileSync, writeFileSync, existsSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join, dirname } from 'node:path' +import { createHash } from 'node:crypto' +import { execFileSync } from 'node:child_process' + +const root = new URL('..', import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1') +const src = readFileSync(root + '/lib/index.js', 'utf8') + +function grab(name) { + const start = src.indexOf(name) + if (start < 0) throw new Error(name + ' not found') + const declStart = src.lastIndexOf('\n', start) + let i = src.indexOf('{', start) + let depth = 0 + for (; i < src.length; i++) { + if (src[i] === '{') depth++ + else if (src[i] === '}') { depth--; if (depth === 0) break } + } + return src.slice(declStart + 1, i + 1) +} +function grabConst(pattern, label) { + const m = src.match(pattern) + if (!m) throw new Error(label + ' not found') + return m[0] +} + +const askpassScriptConst = grabConst(/const ASKPASS_SCRIPT = [\s\S]*?;\n/, 'ASKPASS_SCRIPT') +const askpassBody = + 'let cachedSshPath;\n' + askpassScriptConst + '\n' + + grab('function isAsciiAskpassPath') + '\n' + grab('function askpassFileName') + '\n' + + grab('function writeAskpassAt') + '\n' + grab('function ensureWindowsAskpass') + + '\nreturn { ensureWindowsAskpass };' +const argvBody = + 'let cachedSshPath;\n' + grab('function sshExecutablePath') + '\n' + grab('function expandSshPath') + '\n' + + askpassScriptConst + '\n' + + grab('function isAsciiAskpassPath') + '\n' + grab('function askpassFileName') + '\n' + + grab('function writeAskpassAt') + '\n' + + grab('function ensureWindowsAskpass') + '\n' + grab('function sshArgv') + + '\nreturn { sshExecutablePath, ensureWindowsAskpass, sshArgv };' +const hintBody = + grabConst(/const SSH_DIAG_LINE = .*;\n/, 'SSH_DIAG_LINE') + '\n' + + grab('function stripPqBanner') + '\n' + grab('function stripLoginBanner') + '\n' + + grab('function findMissingIdentityFile') + '\n' + grab('function offeredAuthMethods') + '\n' + + grab('function sshExcerpt') + '\n' + grab('function sshErrorHint') + + '\nreturn { sshErrorHint };' + +/** 造一个沙箱:homedir 与 env(ProgramData / PUBLIC / DSH_REMOTE_SSH_ASKPASS)都可注入。 */ +function makeAskpass(homeDir, env) { + return new Function('join', 'dirname', 'homedir', 'mkdirSync', 'writeFileSync', 'existsSync', 'process', 'createHash', askpassBody)( + join, dirname, () => homeDir, mkdirSync, writeFileSync, existsSync, + { platform: 'win32', env: env }, createHash, + ) +} +function makeSshArgv(homeDir, env) { + return new Function('join', 'dirname', 'homedir', 'mkdirSync', 'writeFileSync', 'existsSync', 'process', 'createHash', argvBody)( + join, dirname, () => homeDir, mkdirSync, writeFileSync, existsSync, + { platform: 'win32', env: env }, createHash, + ) +} +const apiHint = new Function('join', hintBody)(join) + +let pass = 0, fail = 0 +const check = (cond, label) => { if (cond) { pass++; console.log(' ✓ ' + label) } else { fail++; console.log(' ✗ FAIL: ' + label) } } +const ASCII = /^[\x20-\x7E]+$/ + +const sandbox = mkdtempSync(join(tmpdir(), 'rssh-ask36-')) +try { + +console.log('A · askpass 落盘位置(issue #36 核心)') +{ + const cnHome = join(sandbox, '中文用户') + mkdirSync(cnHome, { recursive: true }) + const pd = join(sandbox, 'ProgramData') + const pub = join(sandbox, 'Public') + const env = { ProgramData: pd, PUBLIC: pub } + + const got = makeAskpass(cnHome, env).ensureWindowsAskpass() + check(got.ascii === true && existsSync(got.file), 'A1 落点可用(ascii:true 且文件已生成)') + check(!got.file.startsWith(cnHome), 'A2 不落在含非 ASCII 字符的用户目录') + check(ASCII.test(got.file), 'A3 落点路径纯 ASCII') + check(got.file.startsWith(join(pd, 'dsh-remote-ssh')), 'A4 首选 ProgramData(多用户共享的 ASCII 目录)') + const body = readFileSync(got.file, 'utf8') + check(body === '@ String(a).includes('p@ss&^!')), 'B3 口令只经环境变量(不进 argv)') + + // 无可用 ASCII 落点 → 立即抛错(不再让 ssh 发空口令 + 误报「公钥失败」) + const apiBad = makeSshArgv(cnHome, { ProgramData: join(cnHome, '程序数据'), PUBLIC: join(cnHome, '公共') }) + let threw = null + try { apiBad.sshArgv(pw, 'echo hi', false) } catch (e) { threw = e } + check(!!threw, 'B4 无 ASCII 落点 → 抛错(不让 ssh 发空口令)') + check(threw && /非 ASCII/.test(threw.message) && /DSH_REMOTE_SSH_ASKPASS/.test(threw.message) && /密钥认证/.test(threw.message), + 'B5 错误文案给出两条可执行出路(环境变量 / 密钥认证)') + + // key 认证不受影响(不注入密码环境) + const kb = api.sshArgv({ host: 'h', port: 22, user: 'u', authMethod: 'key', keyPath: '/k' }, 'echo hi', false) + check(kb.env === undefined && kb.argv.includes('BatchMode=yes'), 'B6 key 认证仍走 BatchMode 公钥通道') + + // POSIX 分支不变(平台分流只看 process.platform) + const posix = new Function('join', 'dirname', 'homedir', 'mkdirSync', 'writeFileSync', 'existsSync', 'process', 'createHash', argvBody)( + join, dirname, () => cnHome, mkdirSync, writeFileSync, existsSync, { platform: 'linux', env: {} }, createHash, + ) + const pb = posix.sshArgv(pw, 'echo hi', false) + check(pb.argv[0] === 'sshpass' && pb.argv[2] === 'p@ss&^!' && pb.env === undefined, 'B7 POSIX 仍前置 sshpass(行为不变)') +} + +console.log('C · 错误归因(把用户带向正确方向)') +{ + const pwProfile = { host: 'h', user: 'u', authMethod: 'password' } + const keyProfile = { host: 'h', user: 'u', authMethod: 'key', keyPath: '/x/id_rsa' } + + // issue #36 现场日志(节选):askpass 启动失败 + 服务端拒绝 + const reproLog = [ + 'debug1: read_passphrase: requested to askpass', + 'ssh_askpass: posix_spawnp: No such file or directory', + 'ave@h: Permission denied, please try again.', + 'ave@h: Permission denied (publickey,password).', + ].join('\n') + const h1 = apiHint.sshErrorHint(reproLog, pwProfile) + check(/askpass/.test(h1), 'C1 askpass 启动失败被识别') + check(/非 ASCII/.test(h1) && /DSH_REMOTE_SSH_ASKPASS/.test(h1), 'C2 给出非 ASCII 根因 + 覆盖环境变量出路') + check(!/公钥认证失败/.test(h1) && !/keyPath/.test(h1), 'C3 不再误导成「公钥认证失败 / 检查 keyPath」') + + // 纯 Permission denied(密码连接):仍按密码归因 + const h2 = apiHint.sshErrorHint('Permission denied, please try again.\nave@h: Permission denied (publickey,password).', pwProfile) + check(/密码认证失败/.test(h2), 'C4 密码连接拒绝 → 明确说密码') + check(!/keyPath/.test(h2) && !/公钥认证失败/.test(h2), 'C5 密码连接不提 keyPath / 公钥收录') + check(/PasswordAuthentication/.test(h2) && /publickey,password/.test(h2), 'C6 提示口令与 sshd 侧开关,并回传协商到的认证方法') + + // key 连接:原有归因保持不变 + const h3 = apiHint.sshErrorHint('Permission denied (publickey).', keyProfile) + check(/公钥认证失败/.test(h3) && /keyPath/.test(h3) && /authorized_keys/.test(h3), 'C7 key 连接仍给 keyPath / authorized_keys 清单') + const h4 = apiHint.sshErrorHint('Warning: Identity file /x/id_rsa not accessible: No such file or directory.', keyProfile) + check(/私钥文件不存在或不可读/.test(h4) && h4.includes('/x/id_rsa'), 'C8 私钥缺失的一手证据仍排在最前(issue #19 回归)') + // 密码连接不应因 stderr 里出现 Identity file 字样就转去说私钥 + const h5 = apiHint.sshErrorHint('Warning: Identity file /x/id_rsa not accessible: No such file or directory.\nPermission denied (publickey,password).', pwProfile) + check(!/私钥文件不存在/.test(h5), 'C9 密码连接不查私钥(避免张冠李戴)') +} + +console.log('D · 真机 OpenSSH E2E(路径是否可被 CreateProcessW 接受)') +{ + const keygen = 'C:\\Windows\\System32\\OpenSSH\\ssh-keygen.exe' + if (process.platform !== 'win32' || !existsSync(keygen)) { + console.log(' (非 Windows 或无 ssh-keygen:跳过真机 E2E)') + } else { + const work = join(sandbox, 'e2e') + const cnDir = join(work, '中文目录') + const asciiDir = join(work, 'ascii') + mkdirSync(cnDir, { recursive: true }) + mkdirSync(asciiDir, { recursive: true }) + const script = '@ { + try { + execFileSync(keygen, ['-p', '-f', key, '-N', toPw, '-q'], { + env: Object.assign({}, process.env, { SSH_ASKPASS: askpass, SSH_ASKPASS_REQUIRE: 'force', DISPLAY: 'localhost:0', RSSH_ASKPASS_PW: fromPw }), + stdio: 'pipe', encoding: 'utf8', + }) + return { ok: true, err: '' } + } catch (e) { + return { ok: false, err: String((e && (e.stderr || e.message)) || '') } + } + } + + const bad = runChange(cnAsk, 'pass1', 'pass2') + if (bad.ok) { + console.log(' (本机 OpenSSH 处理非 ASCII askpass 路径正常,D1 复现不成立 → 跳过该断言)') + } else { + check(/ssh_askpass|posix_spawnp|CreateProcessW/i.test(bad.err), 'D1 中文路径 askpass → OpenSSH 启动失败(issue #36 现象)') + } + const good = runChange(asciiAsk, 'pass1', 'pass2') + check(good.ok, 'D2 同一份脚本放到 ASCII 路径 → 成功(唯一变量就是路径)') + + // 插件在「中文 home」环境里选出的落点,必须能被真实 OpenSSH 接受 + const apiAsk = makeAskpass(join(work, '中文用户'), { ProgramData: join(asciiDir, 'ProgramData'), PUBLIC: join(asciiDir, 'Public') }) + const chosen = apiAsk.ensureWindowsAskpass() + check(chosen.ascii === true && ASCII.test(chosen.file), 'D3a 插入环境选出的落点是纯 ASCII') + const viaPlugin = runChange(chosen.file, 'pass2', 'pass3') + check(viaPlugin.ok, 'D3b 插件落点被真实 ssh-keygen 接受(密码认证可用的直接证据)') + } +} + +console.log('E · 源码接线') +{ + const calls = src.match(/sshErrorHint\(/g) || [] + check(calls.length >= 4, 'E1 sshErrorHint 仍有定义 + 3 个调用点(实际 ' + calls.length + ' 处)') + const withProfile = (src.match(/sshErrorHint\([^)]*,\s*p\)/g) || []).length + check(withProfile === 3, 'E2 三个调用点都传了 profile(按认证方式归因的前提,实际 ' + withProfile + ' 处)') + check(/authMethod === "password" && \/permission denied\/i\.test\(t\)/.test(src), 'E3 密码分支存在') + check(/authMethod === "password" \? null : findMissingIdentityFile\(t\)/.test(src), 'E4 密码连接不查私钥') + check(src.includes('DSH_REMOTE_SSH_ASKPASS'), 'E5 文档化的环境变量覆盖点存在') + check(/isAsciiAskpassPath/.test(src) && /ProgramData/.test(src) && /PUBLIC/.test(src), 'E6 ASCII 校验 + ProgramData/Public 回退') +} +} finally { + try { rmSync(sandbox, { recursive: true, force: true }) } catch (e) {} +} + +console.log('') +console.log('结果: ' + pass + ' 通过, ' + fail + ' 失败') +if (fail > 0) process.exit(1)