diff --git a/README.md b/README.md index 18ce7b5..2970b9a 100644 --- a/README.md +++ b/README.md @@ -300,7 +300,7 @@ The update channel is this repository: a `releases` branch holding the installer Once a day at startup, and on demand from Settings -> **Updates**, Shift reads the manifest, compares versions with semver rules including prereleases, and offers a download. A release that has been found is announced at the top of the main window - **Shift x.y.z is available**, with one button that opens Settings -> Updates - and it stays announced across restarts, including the restart that follows an update, because the notice is remembered on disk rather than held in memory. Since the probe itself runs at most once a day, a notice that only lived in RAM would be visible on the morning it was discovered and invisible every launch after. A downloaded installer must match the manifest's exact byte length and recomputed SHA256, and both checks run again immediately before the handoff that installs it. A mismatch deletes the file and reports the error, so a truncated or replaced installer never launches. **Discard** removes the downloaded installer as well as the status. Publishing is one command, `scripts/publish-release.sh`, and `docs/RELEASE.md` covers the manifest format, the flags, and the difference between the integrity the hash proves and the authenticity a signed manifest adds (`--key` plus a key baked in with `SHIFT_UPDATE_PUBKEY`). -**Install & relaunch** finishes the update without leaving the app. macOS replaces its own `.app` bundle from the downloaded disk image and Windows runs the same installer silently, both from a helper script that starts once Shift has quit and then opens the new copy - so a conversion is interrupted, but nothing is left half-installed and a failed swap reopens the copy you had. The helper receives its paths as process arguments; they are never interpolated into the shell or batch script. The tab states your operating system, architecture, running version and the feed it reads above everything else, so one screenshot explains any report about an update. +**Install & relaunch** finishes the update without leaving the app. macOS replaces its own `.app` bundle from the downloaded disk image and Windows runs the same installer silently, both from a helper script that starts once Shift has quit and then opens the new copy - so a conversion is interrupted, but nothing is left half-installed and a failed swap reopens the copy you had. The helper receives its paths as process arguments or environment variables, never by splicing them into the shell or batch script; the Windows launcher also keeps the outer quote layer `cmd.exe /C` otherwise strips from a path containing spaces. The tab states your operating system, architecture, running version and the feed it reads above everything else, so one screenshot explains any report about an update. ## External dependencies diff --git a/README.zh-CN.md b/README.zh-CN.md index 1eb84e5..3388949 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -302,7 +302,7 @@ Shift 会持续维护局域网内其他 Shift 实例的列表,并据此提供 Shift 每天启动时检查一次(也可在设置 → **软件更新** 中手动检查),读取清单、按含预发布规则的 semver 比较版本,然后提供下载。发现的版本会显示在主窗口顶部——「Shift x.y.z 可以升级了」,旁边一个按钮直接跳到设置 → 软件更新——并且**跨启动保留**,包括升级装完之后的那一次启动:提示是记在磁盘上的,不是只存在内存里。探测本身一天最多跑一次,所以只活在内存里的话,新版本只在被发现的问题当天出现,之后每次启动都再也看不到。下载完成的安装包必须与清单的体积和重新计算的 SHA256 一致,而且这两项会在交接给安装脚本前**再次校验**。任一不符即删除文件并明确报错,因此半成品或被替换的安装包永远不会启动。点击「丢弃」会同时删除已下载的安装包和状态。发布只需一条命令 `scripts/publish-release.sh`;`docs/RELEASE.md` 说明清单格式、参数,以及可选的 ed25519 清单签名——签名用来证明清单的来源,而不只是下载内容与清单一致。 -**「安装并重启」让你在应用内完成升级**:macOS 会用下载好的磁盘镜像替换自己的 `.app`,Windows 会静默运行同一个安装程序,两者都由一个在 Shift 退出后才启动的辅助脚本执行,完成后自动打开新版本——正在进行的转换会被中断,但不会装到一半卡住,替换失败也会重新打开原来的版本。辅助脚本通过进程参数接收路径,路径不会被拼接进 shell 或批处理脚本文本。该标签页最上方依次显示本机系统、架构、当前版本与更新源,截一张图就能说明升级问题出在哪里。 +**「安装并重启」让你在应用内完成升级**:macOS 会用下载好的磁盘镜像替换自己的 `.app`,Windows 会静默运行同一个安装程序,两者都由一个在 Shift 退出后才启动的辅助脚本执行,完成后自动打开新版本——正在进行的转换会被中断,但不会装到一半卡住,替换失败也会重新打开原来的版本。辅助脚本通过进程参数或环境变量接收路径,路径不会被拼接进 shell 或批处理脚本文本;Windows 启动器还保留了一层外层引号,否则 `cmd.exe /C` 会把含空格路径的首尾引号剥掉。该标签页最上方依次显示本机系统、架构、当前版本与更新源,截一张图就能说明升级问题出在哪里。 ## 外部依赖 diff --git a/docs/ENGINEERING-NOTES.md b/docs/ENGINEERING-NOTES.md index dfee9cd..7184470 100644 --- a/docs/ENGINEERING-NOTES.md +++ b/docs/ENGINEERING-NOTES.md @@ -24,7 +24,7 @@ | 0.1.9 | 重新支持把「最近使用」拖回三个主操作按钮,并且**按钮是唯一落点**——松开在面板其他位置不执行任何操作(见「『最近使用』拖回主面板的三个按钮」);设置面板字号统一到 `--text-*` 阶梯;取消「截图后」行为选项,固定为结果卡片;修复 macOS「运行组件」把 AppKit 文档渲染器报成 Pandoc 缺失且没有安装入口的回归。 | | 0.1.10 | 拖放反馈收束到按钮:面板自身不再做拖拽动画,松开在非按钮处彻底静默(去掉 0.1.9 的提示浮层);「文字识别」对非图片直接拒绝,不再退回格式环;托盘「识别图片文字…」改为「文字识别」。 | | 0.1.11 | Windows 实测「最近使用」拖到三个按钮全部完不成:页内文件拖拽在 WebView2 上 DOM 与原生两条投递路径都可能不上报,改由 `dragEnd` 兜底;开始拖动时先让闲置圆环让开,它无边框且始终置顶,正压在按钮上(两节都在「『最近使用』拖回主面板的三个按钮」里)。三个按钮的可用性判定收进 `src/lib/dropCapabilities.ts`,两种语言、两条投递路径共用一条"能处理就处理,不能处理就禁止"的规则。同一节里另有两处:内部拖拽被原生 `enter` 报成"没有文件"、从而三个按钮全部点亮(`dragPathsKnown()` 改为合并),以及禁止态定为**按钮禁用+光标禁止符号**——光标原本由 wry 固定回报 Copy、页面改不动,现在由 `src-tauri/src/drop_cursor.rs` 在运行时接管那两条回报方法,把页面的判定翻译成原生拖放的拒绝(同一节也记了这条路上"被拒绝的手势没有收尾事件"这个坑)。局域网接收的文件从此也受「完成后在访达中显示」控制,一批只在最后一个落地时显示一次;「运行组件」缺失多项时可以一次装完。 | -| 0.1.13 | Windows 实测「运行组件」一键安装失败:`winget` 把“已经装过”报成非零退出码,界面就照着说安装失败(见「Windows 一键安装运行组件」)。同一节还记了 `--source` 未钉住导致的静默挂死、机器级组件必须提权、以及装完之后进程内 PATH 仍是旧值这三处。另有三处与系统资源相关的约束:托盘改用自绘的黑白模板图(`scripts/build-icons.py` 重建,应用图标同一份几何,`icon_as_template` 只在 macOS 生效);钉图窗口的放大尺寸与位置必须按 work area 与 DPI 换算后夹取,见「钉图窗口的放大与还原」;闲置时的取样节奏——伴生进程 `native/macos/drag-bridge.swift` 无按压时 10Hz、Windows 手势监视 `monitor_pause` 在圆环关闭时 250ms、发现线程静默时 1.6s——都要在“关掉了就别一直占 CPU”和“还听得见、还能过期”之间取平衡。另有两条:主面板顶部的升级提示与托盘那一行共用同一份状态,而这份状态必须落到磁盘上,否则探测一天只跑一次,用户在探测当天之后就再也看不到提示——包括升级重启后的那一次启动;落盘的提示在恢复时要重新判定它是否还算数,见「升级提示跨启动存活」。 | +| 0.1.13 | Windows 实测「运行组件」一键安装失败:`winget` 把“已经装过”报成非零退出码,界面就照着说安装失败(见「Windows 一键安装运行组件」)。同一节还记了 `--source` 未钉住导致的静默挂死、机器级组件必须提权、以及装完之后进程内 PATH 仍是旧值这三处。另有三处与系统资源相关的约束:托盘改用自绘的黑白模板图(`scripts/build-icons.py` 重建,应用图标同一份几何,`icon_as_template` 只在 macOS 生效);钉图窗口的放大尺寸与位置必须按 work area 与 DPI 换算后夹取,见「钉图窗口的放大与还原」;闲置时的取样节奏——伴生进程 `native/macos/drag-bridge.swift` 无按压时 10Hz、Windows 手势监视 `monitor_pause` 在圆环关闭时 250ms、发现线程静默时 1.6s——都要在“关掉了就别一直占 CPU”和“还听得见、还能过期”之间取平衡。另有两条:主面板顶部的升级提示与托盘那一行共用同一份状态,而这份状态必须落到磁盘上,否则探测一天只跑一次,用户在探测当天之后就再也看不到提示——包括升级重启后的那一次启动;落盘的提示在恢复时要重新判定它是否还算数,见「升级提示跨启动存活」。Windows 应用内更新随后实测失败:`cmd.exe /C` 从带空格的 `%TEMP%\Shift Updates` 路径上剥掉了首尾引号,辅助脚本从未启动;静默安装还会被没人能点的 WebView2 提示框永久卡住。现在脚本路径经环境变量传入并保留外层引号,静默模式不弹 WebView2 提示,安装器在 `taskkill` 后也会等进程真正释放 EXE。 | 历史提交可以这样定位: @@ -825,7 +825,7 @@ cd src-tauri && cargo test --lib update:: | 清单下载后限制 256 KiB | 清单是小型 JSON;损坏或被替换的 feed 不应无限占用内存 | | 下载后校验是普通文件、精确字节数和 SHA256 | 只比较哈希仍会接受声明尺寸与实际尺寸不一致的输入 | | 安装前再次执行同一组校验 | 下载文件位于可写的临时目录,下载完成到用户点击安装之间存在 TOCTOU 窗口;失败时必须删除文件 | -| macOS/Windows 辅助脚本使用固定脚本常量,路径作为进程参数传入 | 文件夹名不能改变 helper 的脚本文本;Windows `cmd.exe` 的参数解析仍需真机覆盖极端路径字符 | +| macOS/Windows 辅助脚本使用固定脚本常量,路径经进程参数或环境变量传入 | 文件夹名不能改变 helper 的脚本文本;Windows 启动器必须保留 `cmd.exe /C` 的外层引号,否则含空格路径会被静默截断 | | `Discard` 删除实际下载物 | 只清状态会留下可被误用的旧安装包 | 回归测试:`artifact_names_stay_inside_the_release_directory`、 diff --git a/docs/RELEASE.md b/docs/RELEASE.md index 888ddfb..3da3c0d 100644 --- a/docs/RELEASE.md +++ b/docs/RELEASE.md @@ -308,16 +308,19 @@ for that pid to disappear and only then touches the installed copy. Both scripts are written so a failure cannot end with Shift simply closed: every macOS failure path still `open`s the bundle it had, and the swap is `move aside, move in`, so a copy that fails halfway leaves the previous version where it was. The scripts themselves -are fixed constants; the pid, installer, bundle and log paths are passed as process -arguments, not interpolated into shell or batch source. Before the handoff starts, macOS +are fixed constants; the pid, installer, bundle and log paths arrive through process +arguments or environment variables, never interpolated into shell or batch source. The +Windows launcher also keeps the outer quote layer that `cmd.exe /C` would otherwise strip +from a script path containing spaces. Before the handoff starts, macOS writes a probe file next to the bundle — a root-owned copy in `/Applications` is reported as "open the downloaded installer yourself" rather than quietly quitting on an update that could not be applied. The Windows installer is the same one a user runs by hand, so the update path cannot drift from the manual one. Because the silent install comes from Shift itself, `${Silent}` skips -the interactive "Shift is running, close it?" box: the app already told the user it is -about to restart, and a box nobody can answer would abort the update. +the interactive "Shift is running, close it?" box and the WebView2 warning: the app +already told the user it is about to restart, and a box nobody can answer would abort or +hang the update. This is not a delta update and not a signature check on the running binary: the same SHA256 gate that guards a manual install is the only integrity gate, and the manifest diff --git a/docs/SECURITY.md b/docs/SECURITY.md index b710e66..d3cf919 100644 --- a/docs/SECURITY.md +++ b/docs/SECURITY.md @@ -103,8 +103,8 @@ Controls: - the installer is checked for exact byte length and SHA256 after download; - both checks run again immediately before installing; - the installer must be a regular file, not a symlink; -- macOS and Windows handoff scripts receive paths as process arguments, not - interpolated shell/batch text; +- macOS and Windows handoff scripts receive paths as process arguments or + environment variables, never interpolated shell/batch text; - `Discard` deletes the downloaded artifact, not just the UI state. Residual risk: diff --git a/scripts/windows-installer.nsi b/scripts/windows-installer.nsi index 2b88ef9..6d0d3ee 100644 --- a/scripts/windows-installer.nsi +++ b/scripts/windows-installer.nsi @@ -111,11 +111,15 @@ Function .onInit nsExec::Exec 'taskkill /IM shift.exe /F' Pop $0 + ; taskkill returns once termination is requested; give Windows a moment to + ; release the mapped EXE before File tries to overwrite it. + Sleep 800 FunctionEnd Function un.onInit nsExec::Exec 'taskkill /IM shift.exe /F' Pop $0 + Sleep 800 FunctionEnd Section "Install" @@ -162,7 +166,12 @@ Section "PostInstall" ReadRegStr $0 HKCU "${WEBVIEW_KEY}" "pv" ${EndIf} ${If} $0 == "" - MessageBox MB_OK|MB_ICONINFORMATION "$(webview_missing)" + ; A silent install is driven by Shift's updater and has nobody to dismiss a + ; dialog. The missing runtime is still worth saying out loud on a manual + ; install, but a box here would hold the update open forever. + ${IfNot} ${Silent} + MessageBox MB_OK|MB_ICONINFORMATION "$(webview_missing)" + ${EndIf} ${EndIf} SectionEnd diff --git a/src-tauri/src/update.rs b/src-tauri/src/update.rs index cc00fd9..01afeac 100644 --- a/src-tauri/src/update.rs +++ b/src-tauri/src/update.rs @@ -955,12 +955,13 @@ exit "$status" #[cfg(target_os = "windows")] const WINDOWS_HANDOFF_SCRIPT: &str = r#"@echo off setlocal EnableExtensions DisableDelayedExpansion -set "WAIT_FOR=%~1" -set "INSTALLER=%~2" -set "LOG=%~3" +set "WAIT_FOR=%SHIFT_UPDATE_PID%" +set "INSTALLER=%SHIFT_UPDATE_INSTALLER%" +set "LOG=%SHIFT_UPDATE_LOG%" if not defined WAIT_FOR exit /b 2 if not defined INSTALLER exit /b 2 if not defined LOG exit /b 2 +echo handoff started %DATE% %TIME% >> "%LOG%" set /a TRIES=0 :waitloop tasklist /FI "PID eq %WAIT_FOR%" /NH /FO CSV 2>nul | find /I "shift.exe" >nul || goto install @@ -972,12 +973,27 @@ set "TARGET=%LOCALAPPDATA%\Programs\Shift\shift.exe" for /f "tokens=1,2,*" %%a in ('reg query "HKCU\Software\Shift" /v InstallDirectory 2^>nul ^| find "InstallDirectory"') do set "TARGET=%%c\shift.exe" start /wait "" "%INSTALLER%" /S if errorlevel 1 echo installer exited with %ERRORLEVEL% >> "%LOG%" -if exist "%TARGET%" start "" "%TARGET%" +if exist "%TARGET%" ( + start "" "%TARGET%" +) else ( + echo target missing: %TARGET% >> "%LOG%" +) "#; +/// `cmd.exe /C` strips the first and last quote from its command when there is +/// more than one pair. Without the extra layer, the quoted path to this script +/// (which lives under `%TEMP%\Shift Updates`) loses its opening quote and the +/// helper never runs. The script path itself travels in an environment variable, +/// so no user-controlled path is spliced into the command line. +#[cfg(any(target_os = "windows", test))] +fn windows_handoff_launch() -> &'static str { + "\"\"%SHIFT_UPDATE_SCRIPT%\"\"" +} + /// Write a handoff script and start it detached, so it is still alive when this -/// process has exited. Paths travel as process arguments rather than interpolated -/// script text, so folder names cannot change the helper's script source. +/// process has exited. Paths travel as process arguments or environment variables +/// rather than interpolated script text, so folder names cannot change the +/// helper's script source. fn spawn_handoff(name: &str, script: &str, arguments: &[&OsStr]) -> Result<(), String> { let path = download_directory().join(name); if let Some(parent) = path.parent() { @@ -988,9 +1004,22 @@ fn spawn_handoff(name: &str, script: &str, arguments: &[&OsStr]) -> Result<(), S let _ = fs::remove_file(&path); fs::write(&path, script).map_err(|error| error.to_string())?; #[cfg(target_os = "windows")] - let mut command = crate::process::command("cmd.exe"); - #[cfg(target_os = "windows")] - command.args(["/D", "/C"]).arg(&path).args(arguments); + let mut command = { + use std::os::windows::process::CommandExt; + + let mut command = crate::process::command("cmd.exe"); + // Keep every path out of the command line. `cmd.exe` expands these + // variables after its `/C` quote handling, so spaces, `&` and `%` in a + // profile folder cannot change the command it runs. + command + .env("SHIFT_UPDATE_SCRIPT", &path) + .env("SHIFT_UPDATE_PID", arguments[0]) + .env("SHIFT_UPDATE_INSTALLER", arguments[1]) + .env("SHIFT_UPDATE_LOG", arguments[2]) + .args(["/D", "/S", "/C"]); + command.raw_arg(windows_handoff_launch()); + command + }; #[cfg(not(target_os = "windows"))] let mut command = crate::process::command("/bin/sh"); #[cfg(not(target_os = "windows"))] @@ -1573,8 +1602,8 @@ mod tests { fn the_windows_handoff_installs_only_after_this_process_is_gone() { let script = WINDOWS_HANDOFF_SCRIPT; assert!( - script.contains("set \"WAIT_FOR=%~1\""), - "the script lost our pid" + script.contains("set \"WAIT_FOR=%SHIFT_UPDATE_PID%\""), + "the script lost the environment variable carrying our pid" ); assert!(!script.contains("C:\\Users\\me\\Shift-Setup.exe")); let wait = script @@ -1585,6 +1614,34 @@ mod tests { assert!(script.contains("start \"\" \"%TARGET%\""), "never restarts"); } + #[test] + fn the_windows_handoff_command_keeps_its_outer_quote_layer() { + // `cmd.exe /C` strips the first and last quote when the command has more + // than one quoted token. The script path is under a folder with a space, + // so without this second layer the helper is silently never started. + let launch = windows_handoff_launch(); + assert_eq!(launch, "\"\"%SHIFT_UPDATE_SCRIPT%\"\""); + assert!(launch.starts_with("\"\"")); + assert!(launch.ends_with("\"\"")); + } + + #[test] + fn a_silent_windows_install_never_waits_on_a_webview_prompt() { + let installer = include_str!("../../scripts/windows-installer.nsi"); + let prompt = installer + .find("MessageBox MB_OK|MB_ICONINFORMATION \"$(webview_missing)\"") + .expect("the manual install lost its WebView2 warning"); + let before_prompt = &installer[prompt.saturating_sub(240)..prompt]; + assert!( + before_prompt.contains("${IfNot} ${Silent}"), + "a silent update would hang on a dialog nobody can dismiss" + ); + assert!( + installer.contains("Sleep 800"), + "the installer can race the terminated process for its EXE" + ); + } + #[cfg(target_os = "macos")] #[test] fn only_a_bundle_in_a_writable_folder_can_be_replaced() {