DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(ssh): 校验 Node 校验清单自身的摘要,避免镜像清单被信任 (#948)
iuuuuuuuu7ab1738eff3b
diff --git a/packages/dsh-tauri-ssh/src/host/service/bootstrap.test.ts b/packages/dsh-tauri-ssh/src/host/service/bootstrap.test.tsindex fb69f7d4..ee45c2ec 100644--- a/packages/dsh-tauri-ssh/src/host/service/bootstrap.test.ts+++ b/packages/dsh-tauri-ssh/src/host/service/bootstrap.test.ts@@ -391,15 +391,32 @@ describe('buildInstallScript', () => {const npmScript = buildInstallScript(await planRemoteInstall('Linux 5.15 aarch64', {}, healthyFetchers()))expect(npmScript).toContain('fetch_verified "$TMP/dsh.tgz"')})+ it('authenticates the node checksum manifest against the pinned digest before trusting it', async () => {+ const plan = await planRemoteInstall('Linux 6.8 x86_64', {}, healthyFetchers())+ const script = buildInstallScript(plan)+ // 清单自身也必须过摘要:镜像提供的清单只有在与官方逐字节相同时才被接受,+ // 否则「镜像改过的运行时 + 镜像改过的清单」会被当成一次合法安装。+ expect(script).toContain(+ `fetch_verified "$TMP/SHASUMS256.txt" "sha256:${plan.node.shasumSha256}" "SHASUMS256.txt"`,+ )+ expect(script).not.toContain('fetch "$TMP/SHASUMS256.txt"')+ })+ it('fails closed when the remote cannot compute a digest instead of skipping verification', async () => {+ const script = buildInstallScript(await planRemoteInstall('Linux 6.8 x86_64', {}, healthyFetchers()))+ expect(script).toContain('REMOTE_INSTALL_NO_DIGEST_TOOL')+ expect(script).toContain('exit 14')+ // 摘要工具缺失或失效时必须报错退出:走「跳过校验」会让镜像清单与镜像归档被一起替换后仍然装成功+ expect(script).not.toContain('远端缺少摘要工具,跳过校验')+ })})describe('install script execution (real POSIX sh)', () => {/*** Run one generated script under the real `sh` inside a sandboxed HOME,* with a fake `curl` first on PATH that "downloads" tampered bytes and a- * SHASUMS256.txt pinning a digest those bytes cannot match.+ * SHASUMS256.txt whose own bytes cannot match the pinned manifest digest.*/- function runScript(script: string, sandbox: string): Promise<{ code: number, stdout: string, stderr: string }> {+ function runScript(script: string, sandbox: string, options: { brokenDigestTool?: boolean } = {}): Promise<{ code: number, stdout: string, stderr: string }> {const binDir = join(sandbox, 'fake-bin')mkdirSync(binDir, { recursive: true })const fakeCurl = join(binDir, 'curl')@@ -424,6 +441,13 @@ describe('install script execution (real POSIX sh)', () => {'exit 0',].join('\n'))chmodSync(fakeCurl, 0o755)+ if (options.brokenDigestTool === true) {+ for (const tool of ['sha256sum', 'shasum']) {+ const broken = join(binDir, tool)+ writeFileSync(broken, '#!/bin/sh\nexit 1\n')+ chmodSync(broken, 0o755)+ }+ }const scriptPath = join(sandbox, 'install.sh')writeFileSync(scriptPath, script)const run = promisify(execFile)@@ -452,6 +476,21 @@ describe('install script execution (real POSIX sh)', () => {expect(existsSync(join(sandbox, REMOTE_ROOT, 'dependencies', 'dsh'))).toBe(false)})+ /**+ * The sandbox sha256sum/shasum exist but cannot produce a digest, i.e. the+ * remote reaches every URL yet cannot verify anything. Installing anyway+ * would hand the mirror both the runtime and the manifest that vouches for it.+ */+ it('refuses to install when the remote cannot compute a digest (real POSIX sh)', async () => {+ const plan = await planRemoteInstall('Linux 6.8 x86_64', {}, healthyFetchers())+ const sandbox = tempDir()+ const outcome = await runScript(buildInstallScript(plan), sandbox, { brokenDigestTool: true })+ expect(outcome.code).toBe(14)+ expect(outcome.stdout).toContain('REMOTE_INSTALL_NO_DIGEST_TOOL')+ expect(outcome.stdout).not.toContain('跳过校验')+ expect(existsSync(join(sandbox, REMOTE_ROOT, 'runtime'))).toBe(false)+ })+it('skips every section when the three components are already installed', async () => {const plan = await planRemoteInstall('Linux 6.8 x86_64', {}, healthyFetchers())const sandbox = tempDir()@@ -573,7 +612,13 @@ describe('install script execution (real POSIX sh)', () => {}))// The pinned pnpm digest is the real release's; point it at the crafted// tarball so the verify step passes against what the fake curl serves.- const plan: RemoteInstallPlan = { ...basePlan, pnpm: { ...basePlan.pnpm, sha256: digest(pnpmTgz, 'sha256', 'hex') } }+ // Same for the node manifest: its own digest is pinned in the plan, so the+ // crafted SHASUMS256.txt must be declared as the trusted one.+ const plan: RemoteInstallPlan = {+ ...basePlan,+ node: { ...basePlan.node, shasumSha256: digest(join(served, 'SHASUMS256.txt'), 'sha256', 'hex') },+ pnpm: { ...basePlan.pnpm, sha256: digest(pnpmTgz, 'sha256', 'hex') },+ }const sandbox = tempDir()const outcome = await runScriptServing(buildInstallScript(plan), sandbox, served)@@ -647,8 +692,14 @@ describe('install script execution (real POSIX sh)', () => {`${createHash('sha256').update(readFileSync(nodeTgz)).digest('hex')} node-v22.22.0-linux-x64.tar.gz\n`,)- const plan = await planRemoteInstall('Linux 6.8 x86_64', {}, healthyFetchers())- expect(plan.node.urls[0]).toContain('nodejs.org')+ const basePlan = await planRemoteInstall('Linux 6.8 x86_64', {}, healthyFetchers())+ expect(basePlan.node.urls[0]).toContain('nodejs.org')+ // The crafted manifest stands in for the official one: pin its own digest so+ // the run exercises the ARCHIVE fallback, not the manifest check.+ const plan: RemoteInstallPlan = {+ ...basePlan,+ node: { ...basePlan.node, shasumSha256: createHash('sha256').update(readFileSync(join(served, 'SHASUMS256.txt'))).digest('hex') },+ }const sandbox = tempDir()// pnpm and dsh are pre-installed so the run exercises the node section,@@ -669,6 +720,42 @@ describe('install script execution (real POSIX sh)', () => {expect(outcome.stdout).toContain('远端初始化完成')expect(existsSync(join(root, 'runtime', 'bin', 'node'))).toBe(true)})++ it('fails closed when no served manifest matches the pinned digest (real POSIX sh)', async () => {+ const work = tempDir()+ const served = join(work, 'served')+ mkdirSync(served, { recursive: true })++ const nodeDir = join(work, 'node-v22.22.0-linux-x64')+ mkdirSync(join(nodeDir, 'bin'), { recursive: true })+ mkdirSync(join(nodeDir, 'include'), { recursive: true })+ writeFileSync(join(nodeDir, 'bin', 'node'), '#!/bin/sh\nexec sh "$@"\n')+ writeFileSync(join(nodeDir, 'include', 'node'), 'headers\n')+ const nodeTgz = join(served, 'node-v22.22.0-linux-x64.tar.gz')+ await promisify(execFile)('tar', ['-czf', nodeTgz, '-C', work, 'node-v22.22.0-linux-x64'])+ // A manifest that matches the served archive but NOT the pinned digest: the+ // whole point is that the archive fallback must not rescue a rewritten manifest.+ writeFileSync(+ join(served, 'SHASUMS256.txt'),+ `${createHash('sha256').update(readFileSync(nodeTgz)).digest('hex')} node-v22.22.0-linux-x64.tar.gz\n`,+ )++ const plan = await planRemoteInstall('Linux 6.8 x86_64', {}, healthyFetchers())++ const sandbox = tempDir()+ const root = join(sandbox, REMOTE_ROOT)+ mkdirSync(join(root, 'dependencies', 'pnpm', 'bin'), { recursive: true })+ writeFileSync(join(root, 'dependencies', 'pnpm', 'bin', 'pnpm.cjs'), 'placeholder')+ mkdirSync(join(root, 'dependencies', 'dsh', 'node_modules', '@deepseek-ai', 'dsh', 'lib'), { recursive: true })+ writeFileSync(join(root, 'dependencies', 'dsh', 'node_modules', '@deepseek-ai', 'dsh', 'lib', 'bin.js'), 'placeholder')++ const outcome = await runScriptWithBrokenFirstSource(buildInstallScript(plan), sandbox, served)+ expect(outcome.stderr).toBe('')+ expect(outcome.code).toBe(11)+ expect(outcome.stdout).toContain('checksum mismatch: SHASUMS256.txt')+ expect(outcome.stdout).not.toContain('node 安装完成')+ expect(existsSync(join(root, 'runtime'))).toBe(false)+ })})describe('remote pnpm shim (real POSIX sh)', () => {diff --git a/packages/dsh-tauri-ssh/src/host/service/bootstrap.types.ts b/packages/dsh-tauri-ssh/src/host/service/bootstrap.types.tsindex 58cde486..03bc1dc5 100644--- a/packages/dsh-tauri-ssh/src/host/service/bootstrap.types.ts+++ b/packages/dsh-tauri-ssh/src/host/service/bootstrap.types.ts@@ -29,7 +29,7 @@ export interface RemoteInstallPlan {repo: stringdshEntry: stringdshVersion: string- node: { urls: string[], shasumUrls: string[], filename: string, version: string }+ node: { urls: string[], shasumUrls: string[], shasumSha256: string, filename: string, version: string }dsh: DshNpmPlan | DshZipPlanpnpm: { urls: string[], sha256: string, version: string }notes: string[]diff --git a/packages/dsh-tauri-ssh/src/host/service/bootstrap.utils.ts b/packages/dsh-tauri-ssh/src/host/service/bootstrap.utils.tsindex b697bf65..2c310c03 100644--- a/packages/dsh-tauri-ssh/src/host/service/bootstrap.utils.ts+++ b/packages/dsh-tauri-ssh/src/host/service/bootstrap.utils.ts@@ -5,7 +5,7 @@ import { Buffer } from 'node:buffer'import { readFileSync } from 'node:fs'import { messageOf } from '../../shared/error'import { DEFAULT_REMOTE_PROFILE, REMOTE_ROOT } from '../config/constants'-import { assetMatrixFor, dshNpmTarballUrls, dshZipDownloadUrls, NODE_VERSION, nodeDownloadUrls, nodeShasumUrls, parsePlatform, PNPM_SHA256, PNPM_VERSION, pnpmDownloadUrls } from '../utils/assets'+import { assetMatrixFor, dshNpmTarballUrls, dshZipDownloadUrls, NODE_SHASUMS256_SHA256, NODE_VERSION, nodeDownloadUrls, nodeShasumUrls, parsePlatform, PNPM_SHA256, PNPM_VERSION, pnpmDownloadUrls } from '../utils/assets'import { clientUrlsFromBootHtml, looksLikePluginBundle } from '../utils/boot-html'import { shQuote } from '../utils/shell'import { listGithubAssets, listGithubReleases, npmDistMetadata, parseGitHubRepo, pickReleaseTag, pkgRepoOf } from '../utils/version'@@ -110,6 +110,7 @@ export async function planRemoteInstall(const node = {urls: nodeDownloadUrls(os, arch),shasumUrls: nodeShasumUrls(),+ shasumSha256: NODE_SHASUMS256_SHA256,filename: matrix.nodeFilename,version: NODE_VERSION,}@@ -287,8 +288,9 @@ export function buildInstallScript(plan: RemoteInstallPlan): string {' fi',' fi',' if [ -z "$_got" ]; then',- ' log verify "警告: 远端缺少摘要工具,跳过校验 $_name"',- ' return 0',+ // 跳过校验等于让镜像同时决定运行时和它的校验依据,这里必须失败而不是放行。+ ' log failed "REMOTE_INSTALL_NO_DIGEST_TOOL: 远端缺少可用的摘要工具(sha256sum/shasum),无法校验 $_name"',+ ' exit 14',' fi',' if [ "$_got" != "$_hex" ]; then',' log verify "校验不通过: $_name (want sha$_algo:$_hex, got $_got)"',@@ -328,7 +330,7 @@ export function buildInstallScript(plan: RemoteInstallPlan): string {' log install "node 已就绪: $("$ROOT/runtime/bin/node" --version 2>/dev/null || echo installed)"','else',` log download "node ${plan.node.version}"`,- ` fetch "$TMP/SHASUMS256.txt" ${quoteUrls(plan.node.shasumUrls)}`,+ ` fetch_verified "$TMP/SHASUMS256.txt" "sha256:${plan.node.shasumSha256}" "SHASUMS256.txt" ${quoteUrls(plan.node.shasumUrls)}`,` _want="$(grep " ${plan.node.filename}\$" "$TMP/SHASUMS256.txt" | head -n 1 | tr -d '\\r' | sed 's/^ *//' | cut -d' ' -f1)"`,` fetch_verified "$TMP/node.tar.gz" "\$_want" "${plan.node.filename}" ${quoteUrls(plan.node.urls)}`,' rm -rf "$ROOT/runtime.new"',diff --git a/packages/dsh-tauri-ssh/src/host/utils/assets.ts b/packages/dsh-tauri-ssh/src/host/utils/assets.tsindex 9c2162e6..8e1796fc 100644--- a/packages/dsh-tauri-ssh/src/host/utils/assets.ts+++ b/packages/dsh-tauri-ssh/src/host/utils/assets.ts@@ -97,6 +97,15 @@ export function nodeShasumUrls(): string[] {]}+/**+ * v22.22.0 官方 SHASUMS256.txt 的 SHA-256。+ *+ * 清单本身也要过摘要校验:镜像(npmmirror)提供的清单只有在与官方逐字节+ * 相同时才被接受,否则归档源兜底会把「镜像改过的运行时 + 镜像改过的清单」+ * 当成一次合法安装。升级 NODE_VERSION 时必须同步更新本常量。+ */+export const NODE_SHASUMS256_SHA256 = '782c13291346fa7b5ac3ce3d6f0a466a1c9c317a30471a3f65f9ab7f6f7156c0'+export function pnpmDownloadUrls(): string[] {return [`${PNPM_BASE_URL}pnpm-${PNPM_VERSION}.tgz`,