diff --git a/bin/prepare-workspace.ps1 b/bin/prepare-workspace.ps1 index fd347c48..9d6afeb2 100644 --- a/bin/prepare-workspace.ps1 +++ b/bin/prepare-workspace.ps1 @@ -4,21 +4,32 @@ $ErrorActionPreference = 'Stop' if ($ExecutionContext.SessionState.LanguageMode -ne 'FullLanguage') { throw 'DSH_OPS_CONSTRAINED_LANGUAGE: workspace ACL preparation requires PowerShell FullLanguage; no ACL preparation was completed. Keep the host security policy enabled and ask its administrator for a supported preparation path.' } +. (Join-Path $PSScriptRoot 'workspace-acl.ps1') $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = [Security.Principal.WindowsPrincipal]::new($identity) $item = Get-Item -LiteralPath $Workspace -Force if (!$item.PSIsContainer) { throw 'Workspace is not a directory' } -$cursor = $item -while ($null -ne $cursor) { - if ($cursor.Attributes -band [IO.FileAttributes]::ReparsePoint) { throw 'Workspace ancestry contains a reparse point; refusing ACL preparation' } - $cursor = $cursor.Parent -} +# \\wsl.localhost\ and \\wsl$\ are the WSL 9P redirector (#669): +# the filesystem behind them exposes no NTFS security descriptor, so Get-Acl +# cannot produce one and no DACL preparation has an object to write. Authority +# there is the distribution's own uid/gid mapping. The decision runs after the +# existence/container validation above — a missing path still fails exactly like +# any other workspace — and before Get-Acl below, so a live 9P root answers +# ready instead of dying inside it. This is a prefix decision, not a catch: +# every other path keeps its existing behaviour, and a real ACL error on those +# paths still fails closed. +if ((Get-DshOpsWorkspaceNtfsState -Path $item.FullName) -eq 'wsl-redirector') { Write-Output 'ready'; exit 0 } +# No reparse walk here: a folder that is already ready answers without any +# tag inspection, so an OneDrive placeholder root is not refused merely for +# being a reparse point (#744). The tag check lives on the one branch below +# that actually writes. $acl = Get-Acl -LiteralPath $item.FullName -. (Join-Path $PSScriptRoot 'workspace-acl.ps1') $decision = Get-DshOpsWorkspaceDecision -Acl $acl -Identity $identity -Principal $principal if ($decision.State -eq 'denied') { throw $decision.Reason } if ($decision.State -eq 'ready') { Write-Output 'ready'; exit 0 } if ($decision.State -eq 'foreign-owner') { throw $decision.Reason } +# Everything below is the preparation branch: the only code that writes, and +# therefore the only place a reparse tag is allowed to matter (#744). $needed = [Security.AccessControl.FileSystemRights]::TakeOwnership # WRITE_OWNER, needed to set the sandbox integrity label # Write only the root's own DACL through its handle. The added ACE is directory-only and the # inheritable ACEs are unchanged, so no descendant needs a new ACL; path-based SetAccessControl @@ -52,7 +63,47 @@ public static class DshOpsDirectoryDacl { } } } +public static class DshOpsReparseTag { + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + static extern SafeFileHandle CreateFileW(string name, uint access, uint share, IntPtr security, uint disposition, uint flags, IntPtr template); + [DllImport("kernel32.dll", SetLastError = true)] + static extern bool DeviceIoControl(SafeFileHandle handle, uint control, IntPtr inBuffer, uint inSize, byte[] outBuffer, uint outSize, out uint returned, IntPtr overlapped); + public static uint Get(string path) { + // Zero access, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT: + // the query opens the point itself, never its target (#744). + using (SafeFileHandle handle = CreateFileW(path, 0u, 7u, IntPtr.Zero, 3u, 0x02000000u | 0x00200000u, IntPtr.Zero)) { + if (handle.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); + // FSCTL_GET_REPARSE_POINT needs a full REPARSE_DATA_BUFFER-sized output: + // a four-byte slice is rejected with ERROR_INSUFFICIENT_BUFFER. + byte[] buffer = new byte[16384]; + // The first dword of the returned buffer is the tag. + // Windows PowerShell 5.1 compiles with C# 5, which has no inline out declarations. + uint returned; + if (!DeviceIoControl(handle, 0x900A8u, IntPtr.Zero, 0, buffer, (uint)buffer.Length, out returned, IntPtr.Zero) || returned < 4) + throw new Win32Exception(Marshal.GetLastWin32Error() != 0 ? Marshal.GetLastWin32Error() : unchecked((int)0x80070057)); + return BitConverter.ToUInt32(buffer, 0); + } + } +} '@ +# Cloud placeholders keep a real directory entry behind the point, so the proven +# cloud-files family (the exact 0x9000?01A shape) is allowed through; every other +# tag — including the rest of the 0x9xxxxxxx band — redirects the view and fails +# closed with the tag named, and a point whose tag cannot even be read fails closed +# too (#744). +$cursor = $item +while ($null -ne $cursor) { + if ($cursor.Attributes -band [IO.FileAttributes]::ReparsePoint) { + try { $tag = [DshOpsReparseTag]::Get($cursor.FullName) } catch { + throw "Workspace ancestry contains a reparse point at $($cursor.FullName) whose tag cannot be read ($($_.Exception.Message)); refusing ACL preparation. Cloud placeholders are allowed through; an unreadable point is not." + } + $family = Get-DshOpsReparseTagDecision -Tag $tag + if ($family.State -ne 'cloud') { + throw "Workspace ancestry contains an unsupported reparse point (tag 0x$($tag.ToString('X8'))) at $($cursor.FullName); refusing ACL preparation. Cloud placeholders pass; every other reparse tag fails closed." + } + } + $cursor = $cursor.Parent +} # Save only the DACL. Owner, SACL, existing ACEs and child ACLs are not changed. $before = $acl.GetSecurityDescriptorSddlForm([Security.AccessControl.AccessControlSections]::Access) [IO.Directory]::CreateDirectory($BackupDir) | Out-Null diff --git a/bin/workspace-acl.ps1 b/bin/workspace-acl.ps1 index eb7a6b31..cf5a88b7 100644 --- a/bin/workspace-acl.ps1 +++ b/bin/workspace-acl.ps1 @@ -62,3 +62,49 @@ function Get-DshOpsWorkspaceDecision { # foreign-owner instead. Both are the pre-existing behaviour, kept unchanged. [pscustomobject]@{ State = 'needs-preparation' } } + +<# +Classify one reparse tag for the preparation branch (#744). This is pure: it +never opens the point, it only judges the tag value the caller read. + +Only the proven cloud-files family is allowed through: the placeholders +(IO_REPARSE_TAG_CLOUD 0x9000001A, CLOUD_ROOT 0x9000101A, CLOUD_ON_DEMAND +0x9000201A, and family members sharing the exact 0x9000?01A shape) keep a +real directory entry behind the point, so a directory-only WRITE_OWNER write +still targets a real object. A high nibble of 9 alone is NOT enough: the rest +of Microsoft's reserved 0x90000000-0x9FFFFFFF band is unproven here and stays +out, exactly like every other tag (mount point, symlink, dedup, ...) that +redirects or transforms the view and fails closed with the tag named. A tag +that cannot be read at all is the caller's error to surface, not a decision +to soften. +#> +function Get-DshOpsReparseTagDecision { + param( + [Parameter(Mandatory = $true)][uint32]$Tag + ) + # Decimal constants on purpose: Windows PowerShell 5.1 folds in-band hex + # literals into a negative Int32, and a sign-extended Int64 mask never + # matches. 4294905855 is 0xFFFF0FFF (family mask); 2415919130 is + # 0x9000001A (IO_REPARSE_TAG_CLOUD). + if (($Tag -band 4294905855) -eq 2415919130) { return [pscustomobject]@{ State = 'cloud' } } + return [pscustomobject]@{ State = 'unsupported' } +} + +<# +Classify one workspace spelling for NTFS ACL evaluation (#669). Pure. + +\\wsl.localhost\ and \\wsl$\ are the WSL 9P redirector: the +filesystem behind it exposes no NTFS security descriptor, so Get-Acl cannot +produce one and no DACL write has an object. Authority there is the +distribution's own uid/gid mapping, so those prefixes must not be treated +as a failed preparation. This is a prefix decision, not a catch swallowing +ACL errors: everything else is evaluated as NTFS, where a real Get-Acl +failure still fails closed. +#> +function Get-DshOpsWorkspaceNtfsState { + param( + [Parameter(Mandatory = $true)][string]$Path + ) + if ($Path -match '^(\\\\(\?\\UNC\\)?wsl(\.localhost|\$)\\)') { return 'wsl-redirector' } + return 'ntfs' +} diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d66194e1..ca946312 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -11,6 +11,14 @@ this package adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - 原生工具构造与发布拒绝队列不再向组合根挂监听或生命周期 effect:证据观察者与 result admission 改为在调用方上下文订阅并声明 global 分发,拒绝队列在 root 拒绝 effect 时回落到调用方生命周期。dsh-tui 0.20 会拒绝插件激活期内的 `root.events.on` 与 `root.effect`,而原生工具行正是插件激活,导致每次 `dsh-tui --resume` 都在 agent 挂载前报 "cannot resume session"。 - The Bash tool description now states the parse contract of `stdout`/`stderr` where the model reads it: `text` may be a partial fragment, `truncated` marks one that must not be parsed as complete structured output, and `spillPath` is optional and not a completeness guarantee. The typed output schema is unchanged: repeating the contract there would cost every PTC request without saying anything the tool text does not. Command semantics, exit codes, result values, limits, spill files, permissions and rendering are unchanged. - Bash 工具说明现在在模型读取它的地方写明 `stdout`/`stderr` 的解析契约:文本可能只是片段,`truncated` 标记的片段不得当作完整结构化输出解析,`spillPath` 是可选项且不保证完整。输出类型 schema 保持不变:在那里重复这段契约只会让每个 PTC 请求更贵,并不会多说明什么。命令语义、退出码、结果值、上限、落盘、权限与渲染均不变。 +- The promoted Bash render no longer implies a job kill stops one command: it states that `job_kill` terminates the whole process tree, including daemons the command started and left attached to it (an adb server, a dev-server watcher), and points to stopping those on their own channel, starting them detached, or beginning daemon-like work with `run_in_background`. The host's subprocess/job layer still owns the actual kill semantics; this is actionable guidance, not a host fix (#814). +- 提升为后台的 Bash 渲染不再暗示一次任务终止只停掉一条命令:现在写明 `job_kill` 会终止整棵进程树,包括该命令启动后仍挂在其上的守护进程(如 adb server、dev-server watcher),并给出指引——先在守护进程自己的通道上停止、以分离方式启动,或对守护进程类工作直接使用 `run_in_background`。宿主的 subprocess/job 层仍拥有真实的终止语义;这是可操作的指引,不是对宿主的修复(#814)。 +- The workspace bootstrap no longer refuses a root for being a reparse point before the DACL decision runs: a folder that already grants effective `WRITE_OWNER` answers `ready` with no tag inspection at all, so an OneDrive placeholder root (IO_REPARSE_TAG_CLOUD, no target) stops failing every workspace-write tool (#744). The tag check now lives on the only branch that writes the DACL, and only the proven cloud-files family passes — the exact 0x9000?01A shape (IO_REPARSE_TAG_CLOUD, CLOUD_ROOT, CLOUD_ON_DEMAND), allowed because a placeholder keeps a real directory entry behind the point; the rest of the reserved 0x90000000-0x9FFFFFFF band is unproven and fails closed exactly like mount points, symlinks, and unknown or unreadable tags, naming the tag it refused. Cloud tags cannot be synthesized in a unit fixture, so the family rule is pinned on the pure decision function `Get-DshOpsReparseTagDecision` and the fail-closed walk is pinned end to end with a real junction. +- 工作区引导脚本不再在 DACL 判定之前就因为「是重解析点」而拒绝根目录:已授予有效 `WRITE_OWNER` 的文件夹在完全不检查 tag 的情况下直接回答 `ready`,OneDrive 占位符根目录(IO_REPARSE_TAG_CLOUD,无目标)不再让所有 workspace-write 工具失败(#744)。tag 检查现在只存在于唯一会写 DACL 的分支上,且只有已证实的云文件族可以通过——精确的 0x9000?01A 形状(IO_REPARSE_TAG_CLOUD、CLOUD_ROOT、CLOUD_ON_DEMAND),放行理由是占位点后面保留真实目录项;保留段 0x90000000-0x9FFFFFFF 的其余部分未经证实,与挂载点、符号链接、未知或不可读 tag 一样闭锁拒绝,并写明所拒绝的具体 tag。云 tag 无法在单元夹具中合成,因此家族规则固定在纯判定函数 `Get-DshOpsReparseTagDecision` 上,闭锁遍历则用真实 junction 做端到端验证。 +- A WSL UNC workspace (`\wsl.localhost\` or `\wsl$\`) no longer dies inside the workspace bootstrap: the 9P redirector behind those prefixes exposes no NTFS security descriptor, so `Get-Acl` threw and every workspace-write `run_code` call failed. Exactly those two Microsoft-fixed spellings now answer `ready` after the existence and container validation and before `Get-Acl` is touched — a prefix decision pinned on the pure classifier `Get-DshOpsWorkspaceNtfsState`, not a catch that swallows ACL errors: a missing path fails exactly like any other missing workspace, and a real ACL failure on any other path still fails closed (#669). +- WSL UNC 工作区(`\wsl.localhost\<发行版>` 或 `\wsl$\<发行版>`)不再死在工作区引导脚本里:这两个前缀背后的 9P 重定向器不暴露 NTFS 安全描述符,`Get-Acl` 因此抛错,workspace-write 下的每次 `run_code` 调用都失败。现在恰好这两个微软固定前缀会在完成存在性与目录验证之后、触碰 `Get-Acl` 之前直接回答 `ready`——判定固定在纯分类函数 `Get-DshOpsWorkspaceNtfsState` 上,是前缀判定而不是吞错 catch:不存在的路径与其他任何缺失工作区一样失败,其他任何路径保持原有行为,真实的 ACL 故障依然闭锁失败(#669)。 +- The optional WSL read pays one wsl.exe startup for preparation instead of one per drive mapping plus one for the payload digest (#576): preparation is now a single fixed `/bin/sh -c` script whose text is a constant, with every drive root and the payload path as positional arguments, so no Windows path can alter the command shape. A small delegated read drops from four wsl.exe invocations (two drive mappings, one sha256sum, one worker) to two (one merged preparation, one worker). The wslpath mappings are still resolved anew per call, the payload digest must still match the receipt, and a refused mapping (exit 9), refused digest (exit 8) or failed spawn still falls back to the Windows worker exactly as before; the distribution now needs `/bin/sh` beside `wslpath` and `sha256sum`. +- 可选 WSL 读取的准备开销从「每个盘符一次 wsl.exe 外加一次摘要校验」合并为一次 wsl.exe 启动(#576):准备现在是一次固定 `/bin/sh -c` 脚本,脚本文本是常量,所有盘符根与 worker 路径都以位置参数传递,任何 Windows 路径都无法改变命令形状。一次小的委派读取从四次 wsl.exe 调用(两次盘符映射、一次 sha256sum、一次 worker)降为两次(一次合并准备、一次 worker)。wslpath 映射仍按每次调用重新解析,载荷摘要仍须与凭据一致,映射被拒(退出码 9)、摘要被拒(退出码 8)或进程启动失败仍与从前完全一样回退到 Windows worker;发行版在 `wslpath` 与 `sha256sum` 之外现在还需要 `/bin/sh`。 - Replace the `global.forceDefaultOpsPtc` switch with one explicit action: the row's detail page carries a **Make OPS (PTC) the default** button that writes `selectedDefault: ops-ptc` once and answers `applied`/`no-update`/`unavailable`/`failed`. Apply, remount and unload no longer write the default, so a preset chosen on the settings page is not overridden on the next apply, and a saved `global.forceDefaultOpsPtc` key still loads with no effect. - 用一次显式操作取代 `global.forceDefaultOpsPtc` 开关:行详情页有一个「设为 OPS (PTC) 默认」按钮,点一次写 `selectedDefault: ops-ptc`,并回报 `applied`/`no-update`/`unavailable`/`failed`。apply、重挂载与卸载都不再改写默认,设置页选定的预设不会在下一次 apply 被覆盖;已保存的 `global.forceDefaultOpsPtc` 键仍可加载但不再生效。 diff --git "a/docs/WSL\345\217\252\350\257\273\346\226\207\344\273\266\346\223\215\344\275\234.md" "b/docs/WSL\345\217\252\350\257\273\346\226\207\344\273\266\346\223\215\344\275\234.md" index 7c4823fd..e1ebb45e 100644 --- "a/docs/WSL\345\217\252\350\257\273\346\226\207\344\273\266\346\223\215\344\275\234.md" +++ "b/docs/WSL\345\217\252\350\257\273\346\226\207\344\273\266\346\223\215\344\275\234.md" @@ -1,6 +1,6 @@ # 可选 WSL 只读文件操作 -实测 DSH rc.2 宿主无法通过 Windows `Get-Acl` 准备 WSL UNC 工作区。因此 workspace-write 下原有 PTC 运行时会拒绝启动,旧版缓存准备也可能不可用。内联 Native 读取仍可运行;字节裁剪的请求回退后与原 Windows 结果完全一致,包括输出不完整标志。OPS 不修改权限模式,也不绕过宿主拒绝。独立 full-access 夹具验证共享 PTC 文件绑定;这是测试范围说明,不是扩大用户权限的建议。 +Windows `Get-Acl` 无法准备 WSL UNC 工作区:`\\wsl.localhost\<发行版>` 与 `\\wsl$\<发行版>` 是 9P 重定向器,其后端文件系统根本不暴露 NTFS 安全描述符,未加区分的准备脚本因此在每个 WSL 工作区上直接死掉,workspace-write 下的 `run_code` 全部失败(#669)。工作区引导脚本现在会在完成存在性与目录验证之后、触碰 `Get-Acl` 之前,对恰好这两个微软固定前缀直接回答 `ready`——这是前缀判定,不是吞掉 ACL 错误的 catch:不存在的路径与其他任何缺失工作区一样失败,其他任何路径保持原有行为,真实的 ACL 故障依然闭锁失败。此类根目录上的旧版缓存准备仍可能不可用。内联 Native 读取可运行;字节裁剪的请求回退后与原 Windows 结果完全一致,包括输出不完整标志。OPS 不修改权限模式,也不绕过宿主拒绝。独立 full-access 夹具验证共享 PTC 文件绑定;这是测试范围说明,不是扩大用户权限的建议。 full-access 下,缺少 `sandbox.prepareResultStorage` 的宿主有意不启用可选存储,避免仅为缓存准备工作区 ACL(#411)。这两种旧宿主情形下,WSL 验收脚本会验证未授予存储权限,并逐字段对比关闭 WSL 开关后的 Windows 结果。宿主提供仅缓存准备能力时,缺少引用仍会导致验收失败,且必须能够分页。Windows 缓存契约套件另以该能力的测试实现验证真实私有 ACL 准备、溢出、分页和清理;这不代表已发布的宿主提供了该能力。 @@ -12,9 +12,9 @@ English: [Optional WSL file reads](wsl-readonly.md). 在 Windows x64 上,可在启动 DSH 的环境中设置 `DSH_OPS_WSL_READS=1`,为工作目录位于 `\\wsl.localhost\<发行版>\...` 或 `\\wsl$\<发行版>\...` 的会话启用 Linux 侧 `ops_glob`、`ops_grep` 和 `ops_read_files`。此功能主要减少 WSL 网络文件系统上的重复元数据访问。WSL 启动和校验也有固定开销,小文件读取可能更慢;应先测量实际工作目录再广泛启用。 -安装包必须同时包含普通 Windows worker,以及版本一致、凭据文件校验通过的 `runtime/linux-x64/dsh-ops-worker`。发行版须已安装、支持 Linux x64,并提供 `/usr/bin/wslpath` 和 `/usr/bin/sha256sum`。OPS 不安装 WSL,也不下载运行时。显式 `workerPath` 或 `DSH_OPS_WORKER_BIN` 选择仍使用原有 Windows 路径。关闭环境开关后,新请求恢复原有路径。 +安装包必须同时包含普通 Windows worker,以及版本一致、凭据文件校验通过的 `runtime/linux-x64/dsh-ops-worker`。发行版须已安装、支持 Linux x64,并提供 `/bin/sh`、`/usr/bin/wslpath` 与 `/usr/bin/sha256sum`。OPS 不安装 WSL,也不下载运行时。显式 `workerPath` 或 `DSH_OPS_WORKER_BIN` 选择仍使用原有 Windows 路径。关闭环境开关后,新请求恢复原有路径。 -worker 通过 `wsl.exe --distribution <名称> --exec <已校验的 worker>` 接收 JSON 数据,不运行命令 shell 或模型提供的代码。输入路径须属于会话所在发行版。每次调用都会重新解析 Windows 盘符挂载,包括凭据拒绝根和 worker 路径,并核对 Linux 侧可见文件的 SHA-256。Linux 遍历仍采用 Windows 不区分大小写的凭据拒绝规则和保留文件名限制。符号链接、特殊对象仍执行 no-follow;无效文件名属于扫描缺口,不得宣称计数完整。 +worker 通过 `wsl.exe --distribution <名称> --exec <已校验的 worker>` 接收 JSON 数据;worker 本身不运行命令 shell 或模型提供的代码。准备工作是每次调用一次固定的 `/bin/sh -c` 脚本(#576):脚本文本是常量,所有盘符根、凭据拒绝根与 worker 路径都以位置参数传递,任何 Windows 路径都无法改变命令形状——盘符挂载仍按每次调用重新解析,Linux 侧可见文件的 SHA-256 仍须与打包凭据一致。输入路径须属于会话所在发行版。Linux 遍历仍采用 Windows 不区分大小写的凭据拒绝规则和保留文件名限制。符号链接、特殊对象仍执行 no-follow;无效文件名属于扫描缺口,不得宣称计数完整。 工具名称、参数 schema、提示词、相对路径基准及宿主读取记录不变。只转换已定义的结果路径字段为 UNC,不改写文件内容。写入、Bash、PTC 执行和结果缓存所有权仍保留在原有 Windows 路径。 diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index b58646e9..cff05e5e 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -283,6 +283,10 @@ Before the first restricted `run_code` / `pwsh` / `bash` / `ops_bash` call in a This step runs the package's own `bin/prepare-workspace.ps1` with Windows PowerShell 5.1 **as the current user, before sandbox confinement applies**, only on Windows in workspace-write mode, and changes only the workspace root's own DACL. Read-only and danger-full-access sessions skip it. +A folder that is already ready answers without any reparse-point inspection, so a OneDrive placeholder root passes the fast path (#744). The reparse check runs only on the branch that must add the ACE: the proven cloud-files family — the exact 0x9000?01A shape (`IO_REPARSE_TAG_CLOUD`, `CLOUD_ROOT`, `CLOUD_ON_DEMAND`) — keeps a real directory entry behind the point and is allowed through, while the rest of Microsoft's 0x90000000-0x9FFFFFFF band is unproven and fails closed exactly like every other tag — mount point, symlink, unknown or unreadable — with the tag named. + +Both Microsoft-fixed WSL redirector prefixes (`\\wsl.localhost\…` and `\\wsl$\…`) answer `ready` after the existence and container validation and before any NTFS inspection, because the 9P filesystem behind them exposes no security descriptor at all and `Get-Acl` there used to abort every workspace-write call (#669). This is a prefix decision, not a catch: a missing path still fails, and on any other path a real ACL error still fails closed. + The message names the cause: - `PowerShell execution policy blocked dsh-ops's own preparation script …`: dsh-ops passes `-ExecutionPolicy Bypass` (earlier versions did not, so the Windows client default `Restricted` blocked every shell tool, #66). A refusal now means Group Policy (MachinePolicy/UserPolicy) enforces the policy; `Set-ExecutionPolicy -Scope CurrentUser` cannot override it. Check `powershell.exe -NoProfile -Command Get-ExecutionPolicy -List`, ask an administrator, or use danger-full-access for the session. diff --git a/docs/usage.md b/docs/usage.md index 1e441e54..48ddf4ee 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -275,7 +275,7 @@ Use `ops_bash`'s `command` for short commands and pipelines. For scripts longer `script_path` accepts only the script path, so the script body is not repeated in the `ops_bash` arguments. `command` and `script_path` are mutually exclusive and exactly one is required. This interface cannot repair JSON that the model or host failed to parse; an unparseable tool call still requires a host/model retry. -`ops_bash` requires a non-empty `description` and keeps the host Bash fields and result kinds: `foreground`, `background`, or `promoted`. Its default timeout is 120 seconds and the cap is 600 seconds. When the host jobs service is available, `run_in_background: true` returns a job ID immediately; a foreground timeout promotes the same running job. Use the host `job_output` and `job_kill` tools to collect or stop it. Without host jobs, the background parameter is absent and a foreground timeout stops the command. Unknown arguments are rejected before execution. +`ops_bash` requires a non-empty `description` and keeps the host Bash fields and result kinds: `foreground`, `background`, or `promoted`. Its default timeout is 120 seconds and the cap is 600 seconds. When the host jobs service is available, `run_in_background: true` returns a job ID immediately; a foreground timeout promotes the same running job. Use the host `job_output` and `job_kill` tools to collect or stop it. `job_kill` terminates the whole process tree: daemons the command started and left attached to its pipes (an adb server, a dev-server watcher) go down with it, so stop such daemons on their own channel first, start them detached, or begin daemon-like work with `run_in_background` so the tree decision stays deliberate. Without host jobs, the background parameter is absent and a foreground timeout stops the command. Unknown arguments are rejected before execution. The host job list and waiting view use `description` as the task label. Both `background` and `promoted` results include that same `label` beside `jobId`, and the rendered result quotes it. When migrating from the old `ops_run_background` / `ops_job_output` / `ops_job_kill` tools, start work with `ops_bash`'s `run_in_background` (or let a foreground call promote), then use the host `job_output` / `job_kill` with the returned ID. Keep `description` concise so the job is recognizable while waiting. diff --git a/docs/wsl-readonly.md b/docs/wsl-readonly.md index ed758ed6..9db70e11 100644 --- a/docs/wsl-readonly.md +++ b/docs/wsl-readonly.md @@ -1,6 +1,6 @@ # Optional WSL file reads -On the tested DSH rc.2 host, Windows `Get-Acl` cannot prepare a WSL UNC workspace. In workspace-write mode, the unchanged PTC runtime therefore refuses to start, and legacy cache preparation can be unavailable. Inline Native reads still work; a byte-clipped read falls back to exactly the original Windows result, including its incomplete-output flags. OPS does not change permission mode or bypass this host refusal. A separate full-access fixture verifies shared PTC file bindings; this is validation scope, not a recommendation to widen user authority. +Windows `Get-Acl` cannot prepare a WSL UNC workspace: `\\wsl.localhost\` and `\\wsl$\` are the 9P redirector, which exposes no NTFS security descriptor at all, so an unguarded preparation died on every WSL workspace and workspace-write `run_code` failed wholesale (#669). The workspace bootstrap now answers `ready` for exactly those two Microsoft-fixed prefixes after the existence and container validation and before `Get-Acl` is touched — a prefix decision, not a catch that swallows ACL errors: a missing path fails exactly like any other missing workspace, and on any other path a real ACL failure still fails closed. Legacy cache preparation can still be unavailable on such roots. Inline Native reads work; a byte-clipped read falls back to exactly the original Windows result, including its incomplete-output flags. OPS does not change permission mode or bypass a host refusal. A separate full-access fixture verifies shared PTC file bindings; this is validation scope, not a recommendation to widen user authority. In full-access mode, hosts without `sandbox.prepareResultStorage` intentionally omit optional storage rather than preparing workspace ACLs just for caching (#411). The WSL acceptance script verifies the absent storage authority and exact equality with the opt-out Windows result in either legacy-host case. When the host exposes cache-only preparation, a missing reference still fails acceptance; paging must succeed. The Windows cache-contract suite separately exercises real private ACL preparation, spill, paging and cleanup through a test implementation of that capability; it does not establish that a published host provides it. @@ -12,9 +12,9 @@ Apply the environment setting to the DSH daemon/desktop process, then restart th On Windows x64, set `DSH_OPS_WSL_READS=1` in the environment that starts DSH to opt in to Linux-side `ops_glob`, `ops_grep`, and `ops_read_files` for a session rooted at `\\wsl.localhost\\...` or `\\wsl$\\...`. This targets repeated metadata access through the WSL network filesystem. WSL startup and verification cost can make a small read slower; measure the actual workspace before enabling it broadly. -The installed package must contain the same-version, receipt-verified `runtime/linux-x64/dsh-ops-worker` as well as the normal Windows worker. The distribution must already be installed, support Linux x64, and provide `/usr/bin/wslpath` and `/usr/bin/sha256sum`. OPS neither installs WSL nor downloads a runtime. An explicit `workerPath` or `DSH_OPS_WORKER_BIN` selection keeps its existing Windows execution path. Turning the opt-in off restores the existing path for new calls. +The installed package must contain the same-version, receipt-verified `runtime/linux-x64/dsh-ops-worker` as well as the normal Windows worker. The distribution must already be installed, support Linux x64, and provide `/bin/sh`, `/usr/bin/wslpath` and `/usr/bin/sha256sum`. OPS neither installs WSL nor downloads a runtime. An explicit `workerPath` or `DSH_OPS_WORKER_BIN` selection keeps its existing Windows execution path. Turning the opt-in off restores the existing path for new calls. -The worker receives JSON data through `wsl.exe --distribution --exec `; no command shell or model-supplied code runs. Inputs must resolve to the same distribution as the session. Windows drive mappings are resolved anew for every call, including credential deny roots and the worker payload. The Linux-visible payload hash must match the packaged receipt. Windows case-insensitive credential matching and reserved-name restrictions remain enabled inside the Linux traversal. Links and special objects retain no-follow checks; an invalid filename is a scan gap, not a complete count. +The worker receives JSON data through `wsl.exe --distribution --exec `; the worker itself runs no command shell or model-supplied code. The preparation is one fixed `/bin/sh -c` script per call (#576): the script text is a constant, and every drive root, credential deny root and the worker payload travel as positional arguments, so no Windows path can alter the command shape — drive mappings are still resolved anew for every call, and the Linux-visible payload hash must still match the packaged receipt. Inputs must resolve to the same distribution as the session. Windows case-insensitive credential matching and reserved-name restrictions remain enabled inside the Linux traversal. Links and special objects retain no-follow checks; an invalid filename is a scan gap, not a complete count. Tools, argument schemas, prompt text, relative path bases, and host read observations stay the same. Only documented result path fields become UNC paths; file contents are untouched. Writes, Bash, PTC execution, and result-cache ownership remain on the existing Windows path. diff --git "a/docs/\344\275\277\347\224\250\346\214\207\345\215\227.md" "b/docs/\344\275\277\347\224\250\346\214\207\345\215\227.md" index 5422b6a2..03898874 100644 --- "a/docs/\344\275\277\347\224\250\346\214\207\345\215\227.md" +++ "b/docs/\344\275\277\347\224\250\346\214\207\345\215\227.md" @@ -242,7 +242,7 @@ dsh-ops panel --port 8123 # 固定回环端口;默认 0 由系统分配 `script_path` 只接收脚本路径,脚本内容不会重复进入 `ops_bash` 的工具参数;`command` 与 `script_path` 必须二选一。该接口不会替模型修复非法 JSON,工具调用本身若未能解析,仍需由宿主或模型重试。 -`ops_bash` 必须提供非空 `description`,输入字段和结果类型与宿主 Bash 保持一致:`foreground`、`background` 或 `promoted`。默认超时 120 秒,上限 600 秒。宿主 jobs 服务可用时,`run_in_background: true` 立即返回 job ID;前台等待超时会把同一个运行中任务交给宿主管理。用宿主 `job_output` 收集输出、`job_kill` 停止任务。没有 jobs 服务时不声明后台参数,前台超时会停止命令。未知参数在执行前被拒绝。 +`ops_bash` 必须提供非空 `description`,输入字段和结果类型与宿主 Bash 保持一致:`foreground`、`background` 或 `promoted`。默认超时 120 秒,上限 600 秒。宿主 jobs 服务可用时,`run_in_background: true` 立即返回 job ID;前台等待超时会把同一个运行中任务交给宿主管理。用宿主 `job_output` 收集输出、`job_kill` 停止任务。`job_kill` 会终止整棵进程树:该命令启动后仍挂在其管道上的守护进程(如 adb server、dev-server watcher)会被一并终止;请先在守护进程自己的通道上停止它们、以分离方式启动,或对守护进程类工作直接用 `run_in_background` 开始,让终止范围始终是明确决定。没有 jobs 服务时不声明后台参数,前台超时会停止命令。未知参数在执行前被拒绝。 宿主任务列表和等待视图使用 `description` 作为任务标签。`background` 和 `promoted` 结果在 `jobId` 旁返回相同的 `label`,显示文本也会引用该标签。从旧版 `ops_run_background` / `ops_job_output` / `ops_job_kill` 迁移时,用 `ops_bash` 的 `run_in_background` 启动任务(或让前台调用超时转后台),再将返回的 ID 交给宿主 `job_output` / `job_kill`。请用简短的 `description` 说明任务,便于等待时识别。 diff --git "a/docs/\345\270\270\350\247\201\351\227\256\351\242\230.md" "b/docs/\345\270\270\350\247\201\351\227\256\351\242\230.md" index 69fecb7b..99a1f7eb 100644 --- "a/docs/\345\270\270\350\247\201\351\227\256\351\242\230.md" +++ "b/docs/\345\270\270\350\247\201\351\227\256\351\242\230.md" @@ -220,6 +220,10 @@ argv 第一个元素解析后等于 Desktop 可执行文件——OPS 现在在 这一步用 Windows PowerShell 5.1 运行包内自带的 `bin/prepare-workspace.ps1`,**以当前用户身份、在沙箱约束生效之前执行**;只在 Windows 的工作区内修改模式下运行,且只修改工作区根目录自身的 DACL。只读与 danger-full-access 会话会跳过它。 +已经是 ready 的文件夹完全不检查重解析点,因此 OneDrive 占位符根目录可以直接通过快路径(#744)。重解析点检查只在必须补 ACE 的分支上运行:仅已证实的云文件族——精确的 0x9000?01A 形状(`IO_REPARSE_TAG_CLOUD`、`CLOUD_ROOT`、`CLOUD_ON_DEMAND`)——在占位点后面保留真实的目录项,允许通过;微软保留的 0x90000000-0x9FFFFFFF 段的其余部分未经证实,与挂载点、符号链接、未知或不可读一样闭锁拒绝,并在报错中写明具体 tag。 + +两个微软固定的 WSL 重定向器前缀(`\\wsl.localhost\…` 与 `\\wsl$\…`)会在完成存在性与目录验证之后、任何 NTFS 检查之前直接回答 `ready`,因为其后的 9P 文件系统根本不暴露安全描述符,此前 `Get-Acl` 在那里抛错会让每次 workspace-write 调用都中止(#669)。这是前缀判定而非 catch:不存在的路径照旧失败,其他任何路径上真实的 ACL 错误依然闭锁失败。 + 报错会写明原因: - `PowerShell execution policy blocked dsh-ops's own preparation script …`:dsh-ops 会传 `-ExecutionPolicy Bypass`(旧版本没有传,因此 Windows 客户端默认的 `Restricted` 会让所有 shell 工具失败,#66)。现在仍被拒绝,说明是组策略(MachinePolicy/UserPolicy)在强制执行策略,`Set-ExecutionPolicy -Scope CurrentUser` 无法覆盖。请运行 `powershell.exe -NoProfile -Command Get-ExecutionPolicy -List` 检查、联系管理员,或为该会话使用 danger-full-access。 diff --git a/src/execution/wsl-worker.ts b/src/execution/wsl-worker.ts index 59f0cc65..c1db0a37 100644 --- a/src/execution/wsl-worker.ts +++ b/src/execution/wsl-worker.ts @@ -56,7 +56,29 @@ export async function tryWslRead(subprocess: SubprocessRuntime, file: string, en if (remaining() === 0) { controller.abort(); combined.throwIfAborted() } if (!sameAuthority()) { authorityLost = true; throw new Error('Host authority changed') } } - const probe = async (argv: string[]): Promise => { + // One fixed /bin/sh -c preparation call (#576): the script text is a constant + // and every data value — one drive root per argument, the payload path last — + // travels as a positional parameter, so no Windows path can alter the command + // shape. The script maps each drive through wslpath, prints winlinux + // pairs, and ends with sha256sum over the mapped payload; a refused mapping + // exits 9 and a refused digest 8, so a failed preparation never masquerades + // as partial output. + const prepareScript = [ + 'i=0', + 'for a in "$@"', + 'do', + ' i=$((i+1))', + ' m=$(/usr/bin/wslpath -u "$a") || exit 9', + ' if [ "$i" -eq "$#" ]', + ' then', + ' /usr/bin/sha256sum -- "$m" || exit 8', + ' else', + " printf '%s\\t%s\\n' \"$a\" \"$m\"", + ' fi', + 'done', + 'exit 0', + ].join('\n') + const run = async (argv: string[]): Promise => { guard() const handle = subprocess.spawn({ argv: [...prefix, ...argv], cwd, stdio: { stdin: { data: '' }, stdout: { maxBytes: 8192 }, stderr: { maxBytes: 4096 } }, graceMs: 1000, signal: combined }) const done = await handle.done, output = handle.collected.stdout?.readFrom(0) @@ -65,14 +87,21 @@ export async function tryWslRead(subprocess: SubprocessRuntime, file: string, en } try { // Mount mappings are live authority, not a cache keyed only by a drive letter. + const driveRoots = [...new Set(paths.map(value => windowsDrive(String(value))).filter((value): value is string => Boolean(value)))] + const merged = await run(['/bin/sh', '-c', prepareScript, 'dsh-ops', ...driveRoots.map(drive => `${drive}\\`), payload]) + const lines = merged.split('\n').filter(line => line.length > 0) + if (lines.length !== driveRoots.length + 1) throw new Error('WSL drive mapping unavailable') const drives = new Map() - for (const drive of new Set(paths.map(value => windowsDrive(String(value))).filter((value): value is string => Boolean(value)))) { - const mapped = await probe(['/usr/bin/wslpath', '-u', `${drive}\\`]) - if (!mapped.startsWith('/') || mapped.includes('\n') || mapped.includes('\0')) throw new Error('WSL drive mapping unavailable') + for (const line of lines.slice(0, -1)) { + const at = line.indexOf('\t') + if (at <= 0) throw new Error('WSL drive mapping unavailable') + const drive = windowsDrive(line.slice(0, at)), mapped = line.slice(at + 1) + if (!drive || !mapped.startsWith('/') || mapped.includes('\n') || mapped.includes('\0')) throw new Error('WSL drive mapping unavailable') drives.set(drive, mapped) } + if (drives.size !== driveRoots.length) throw new Error('WSL drive mapping unavailable') const worker = linuxPath(payload, root.distro, drives) - if (!worker || !(await probe(['/usr/bin/sha256sum', '--', worker])).startsWith(`${digest} `)) throw new Error('WSL payload mismatch') + if (!worker || !lines[lines.length - 1].startsWith(`${digest} `)) throw new Error('WSL payload mismatch') const convert = (value: string): string => { const mapped = linuxPath(value, root.distro, drives) if (!mapped) throw new Error('WSL authority mapping unavailable') diff --git a/src/shells/host-contract.ts b/src/shells/host-contract.ts index 44ae8c79..7ba10719 100644 --- a/src/shells/host-contract.ts +++ b/src/shells/host-contract.ts @@ -41,7 +41,12 @@ export function hostFailure(message: string, timeoutMs = 120_000, effects: 'none export function renderHostBash(value: HostBashResult): string { const directory = value.resolvedFrom === undefined ? '' : `[cwd: resolvedFrom=${JSON.stringify(value.resolvedFrom)}; cwdSource=${JSON.stringify(value.cwdSource)}]\n` if (value.kind === 'background') return directory + `started background job ${value.jobId} (${JSON.stringify(value.label)})` - if (value.kind === 'promoted') return directory + `${value.output || '(no output)'}\n[still running after ${value.timeoutMs}ms; moved to background job ${value.jobId} (${JSON.stringify(value.label)})]\nUse job_output to collect output and job_kill to stop the command.` + // The host owns what a job kill reaches (its subprocess and job layer), so this + // render can only be honest about it: killing the job terminates the whole process + // tree, including daemons the command started and left attached to its pipes, and + // the model has to know that before job_kill takes out an adb server or a dev + // watcher that was supposed to survive (#814). + if (value.kind === 'promoted') return directory + `${value.output || '(no output)'}\n[still running after ${value.timeoutMs}ms; moved to background job ${value.jobId} (${JSON.stringify(value.label)})]\nCollect output with job_output, or stop the command with job_kill. job_kill terminates the whole process tree, including daemons this command started and left attached to it (an adb server, a dev-server watcher); stop such daemons on their own channel first, or start them detached, so a job kill cannot reach them. Daemon-like or long-lived work belongs in run_in_background from the start.` const { exitCode, signal, ...rest } = value const body = renderBashResult(rest) const markers = [] diff --git a/test/shells-job-promoted-guidance.test.mjs b/test/shells-job-promoted-guidance.test.mjs new file mode 100644 index 00000000..1e8e2955 --- /dev/null +++ b/test/shells-job-promoted-guidance.test.mjs @@ -0,0 +1,36 @@ +/** + * The promoted Bash render is the model's decision point for a command that + * outlived its foreground timeout (#814): job_output and job_kill are the only + * two handles left, and a job kill at the host layer terminates the whole + * process tree. The defect behind the issue lives in the host's subprocess and + * job layer (an inherited pipe keeps a daemon inside the tree, and a tree kill + * takes out daemons the command started, e.g. an adb server); this repository + * cannot fix the host, so its render must at least state what a job kill + * reaches and how to keep daemons out of the blast radius. + */ +import { assert, test, report } from './lib/harness.mjs' +import { renderHostBash } from '../lib/shells/host-contract.js' + +await test('the promoted render states that job_kill terminates the whole process tree', () => { + const value = { + kind: 'promoted', jobId: 'job-7', label: 'adb start-server', timeoutMs: 120000, + output: '* daemon not running; starting it now', + } + const text = renderHostBash(value) + // The existing handles stay named, and the moved-to-job line keeps its shape. + assert.match(text, /still running after 120000ms; moved to background job job-7/) + assert.match(text, /job_output/) + assert.match(text, /job_kill/) + // #814: the render must say what a job kill reaches before the model uses it. + assert.match(text, /whole process tree/) + // ... and which victims that implies, plus how to avoid them next time. + assert.match(text, /daemon/) + assert.match(text, /run_in_background/) +}) + +await test('the background render keeps its exact one-line shape', () => { + const text = renderHostBash({ kind: 'background', jobId: 'j-1', label: 'watcher' }) + assert.equal(text, 'started background job j-1 (\"watcher\")') +}) + +report('shells-job-promoted-guidance') diff --git a/test/workspace-reparse-ready.test.mjs b/test/workspace-reparse-ready.test.mjs new file mode 100644 index 00000000..ed23cfc7 --- /dev/null +++ b/test/workspace-reparse-ready.test.mjs @@ -0,0 +1,137 @@ +import fs from 'node:fs' +import path from 'node:path' +import { spawnSync } from 'node:child_process' +import { fileURLToPath } from 'node:url' +import { assert, test, report, withTempDir, TEST_HOME } from './lib/harness.mjs' +import { preparationEnvironment } from '../lib/workspace-preparation.js' + +/** + * Issue #744: a OneDrive folder carries IO_REPARSE_TAG_CLOUD, so the workspace + * bootstrap's reparse-point walk refused it before the DACL decision could + * say the folder was already ready, and every workspace-write tool failed. + * The reparse check belongs to the preparation branch alone — the only path + * that writes — and there it must let the cloud-files family through (they are + * ordinary directory entries to the ACL layer) and fail every other tag closed + * with the tag named. Cloud tags cannot be synthesized without the cloud + * filter API, so the family rule is pinned on the pure decision function and + * the walk itself is pinned with a junction, which is a real mount-point tag. + */ +if (process.platform === 'win32') await windowsReparseChecks() +else console.log(' Windows reparse fixture unavailable: real DACL and PowerShell checks require Windows') + +async function windowsReparseChecks() { + +const helper = fileURLToPath(new URL('../bin/workspace-acl.ps1', import.meta.url)) +const preparation = fileURLToPath(new URL('../bin/prepare-workspace.ps1', import.meta.url)) +const powershell = path.join(process.env.SystemRoot ?? 'C:\\Windows', 'System32/WindowsPowerShell/v1.0/powershell.exe') +const quote = (text) => "'" + text.replaceAll("'", "''") + "'" + +/** Run PowerShell 5.1 the way production does: scrubbed environment, bypassed policy. */ +function ps(code) { + return spawnSync(powershell, + ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', Buffer.from(code, 'utf16le').toString('base64')], + { encoding: 'utf8', env: preparationEnvironment(), windowsHide: true, timeout: 30_000 }) +} + +function psValue(code) { + const value = ps(code) + assert.equal(value.status, 0, value.stderr) + return value.stdout.trim() +} + +/** Run the shipped bootstrap exactly as production launches it. */ +function runScript(root, backups) { + return spawnSync(powershell, + ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', preparation, '-Workspace', root, '-BackupDir', backups], + { encoding: 'utf8', env: preparationEnvironment(), windowsHide: true, timeout: 30_000 }) +} + +function receipts(backups) { + return fs.existsSync(backups) ? fs.readdirSync(backups).length : 0 +} + +/** A junction under the temporary home: a real mount point (tag 0xA0000003). */ +function junctionFixture(base) { + const target = path.join(base, 'target') + fs.mkdirSync(target) + const root = path.join(base, 'root') + const made = spawnSync('cmd.exe', ['/c', 'mklink', '/J', root, target], { encoding: 'utf8' }) + assert.equal(made.status, 0, made.stderr) + return root +} + +await test('reparse tag classification lets the cloud-files family through and fails every other tag closed', () => { + const state = (tag) => psValue(`$ErrorActionPreference='Stop' +. ${quote(helper)} +(Get-DshOpsReparseTagDecision -Tag ${tag}).State`) + // Decimal literals on purpose: Windows PowerShell 5.1 folds hex literals in + // the 0x90000000-0xFFFFFFFF band into a negative Int32, which cannot convert + // to the function's [uint32] parameter. 2415919130 is IO_REPARSE_TAG_CLOUD, + // 2415923226 CLOUD_ROOT, 2415927322 CLOUD_ON_DEMAND: the proven cloud-files + // family has the exact shape 0x9000?01A, and only that family is allowed — + // because a placeholder keeps a real directory entry behind the point, so an + // ACL write targets a real object. 2415980570 (0x9000F01A) shares that family + // shape under the mask. + assert.equal(state(2415919130), 'cloud') + assert.equal(state(2415923226), 'cloud') + assert.equal(state(2415927322), 'cloud') + assert.equal(state(2415980570), 'cloud') + // A high nibble of 9 alone is NOT enough (#744 review): 2684354559 is + // 0x9FFFFFFF and 2415963085 is 0x9000ABCD, whose low 16 bits are not the + // ?01A family shape. Unproven tags must fail closed before a DACL write. + assert.equal(state(2684354559), 'unsupported') + assert.equal(state(2415963085), 'unsupported') + // 0xA0000003 is a mount point and 0xA000000C a symlink; unknown tags like + // 0x8000001B redirect or transform the view and must fail closed when + // preparation is about to write the DACL. + assert.equal(state(2684354563), 'unsupported') + assert.equal(state(2684354572), 'unsupported') + assert.equal(state(2147483675), 'unsupported') +}) + +await test('a junction workspace with an effective WRITE_OWNER takes the ready path without a reparse refusal (#744)', async () => { + await withTempDir('prepare-reparse-ready', async (base) => { + const root = junctionFixture(base) + const backups = path.join(TEST_HOME, 'prepare-reparse-ready-backups') + const value = runScript(root, backups) + assert.equal(value.status, 0, value.stderr) + assert.equal(value.stdout.trim(), 'ready') + assert.equal(receipts(backups), 0, 'the ready path writes no receipt') + }) +}) + +await test('a junction workspace that still needs the ACE fails closed and names its reparse tag (#744)', async () => { + await withTempDir('prepare-reparse-need', async (base) => { + const root = junctionFixture(base) + const backups = path.join(TEST_HOME, 'prepare-reparse-need-backups') + // Replace every inherited ACE with one caller-owned RX grant, so the + // folder no longer holds WRITE_OWNER but stays listable and cleanable: + // the decision must be needs-preparation, the one branch that writes and + // therefore the one branch allowed to look at tags. The owner is taken + // first because an elevated runner defaults new-object ownership to + // Administrators, which would make the decision foreign-owner instead. + const sid = psValue('[Security.Principal.WindowsIdentity]::GetCurrent().User.Value') + const owned = spawnSync('icacls.exe', [root, '/setowner', `*${sid}`], { encoding: 'utf8' }) + assert.equal(owned.status, 0, owned.stdout + owned.stderr) + const strip = spawnSync('icacls.exe', [root, '/inheritance:r', '/grant', `*${sid}:(RX)`], { encoding: 'utf8' }) + assert.equal(strip.status, 0, strip.stdout + strip.stderr) + const state = psValue(`$ErrorActionPreference='Stop' +. ${quote(helper)} +$acl=Get-Acl -LiteralPath ${quote(root)} +$identity=[Security.Principal.WindowsIdentity]::GetCurrent() +$principal=[Security.Principal.WindowsPrincipal]::new($identity) +(Get-DshOpsWorkspaceDecision -Acl $acl -Identity $identity -Principal $principal).State`) + assert.equal(state, 'needs-preparation') + const value = runScript(root, backups) + assert.notEqual(value.status, 0, JSON.stringify(value.stdout)) + // The refusal must name the actual tag, so the operator can tell a cloud + // placeholder from a mount point instead of a blanket reparse rejection. + assert.match(value.stderr, /reparse point/) + assert.match(value.stderr, /0xA0000003/) + assert.equal(receipts(backups), 0, 'a refused preparation writes no receipt') + }) +}) + +} + +report('workspace-reparse-ready') diff --git a/test/workspace-wsl-redirector.test.mjs b/test/workspace-wsl-redirector.test.mjs new file mode 100644 index 00000000..ac0eaa13 --- /dev/null +++ b/test/workspace-wsl-redirector.test.mjs @@ -0,0 +1,115 @@ +import fs from 'node:fs' +import path from 'node:path' +import { spawnSync } from 'node:child_process' +import { fileURLToPath } from 'node:url' +import { assert, test, report, wait, TEST_HOME } from './lib/harness.mjs' +import { preparationEnvironment } from '../lib/workspace-preparation.js' + +/** + * Issue #669: \\wsl.localhost and \\wsl$ are the WSL 9P redirector, and the + * filesystem behind it exposes no NTFS security descriptor at all. The + * bootstrap's Get-Acl therefore threw on every WSL workspace and all of + * run_code died. The fix is a prefix decision, not a catch that swallows ACL + * errors: those two Microsoft-fixed prefixes answer ready (authority there + * is the distribution's own uid/gid mapping), and every other path keeps its + * existing behaviour, where a real Get-Acl failure still fails closed. + */ +if (process.platform === 'win32') await windowsWslChecks() +else console.log(' Windows WSL redirector fixture unavailable: real DACL and PowerShell checks require Windows') + +async function windowsWslChecks() { + +const helper = fileURLToPath(new URL('../bin/workspace-acl.ps1', import.meta.url)) +const preparation = fileURLToPath(new URL('../bin/prepare-workspace.ps1', import.meta.url)) +const powershell = path.join(process.env.SystemRoot ?? 'C:\\Windows', 'System32/WindowsPowerShell/v1.0/powershell.exe') +const wsl = path.join(process.env.SystemRoot ?? 'C:\\Windows', 'System32', 'wsl.exe') +const quote = (text) => "'" + text.replaceAll("'", "''") + "'" + +/** Run PowerShell 5.1 the way production does: scrubbed environment, bypassed policy. */ +function ps(code) { + return spawnSync(powershell, + ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', Buffer.from(code, 'utf16le').toString('base64')], + { encoding: 'utf8', env: preparationEnvironment(), windowsHide: true, timeout: 30_000 }) +} + +function psValue(code) { + const value = ps(code) + assert.equal(value.status, 0, value.stderr) + return value.stdout.trim() +} + +/** Run the shipped bootstrap exactly as production launches it. */ +function runScript(root, backups) { + return spawnSync(powershell, + ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', preparation, '-Workspace', root, '-BackupDir', backups], + { encoding: 'utf8', env: preparationEnvironment(), windowsHide: true, timeout: 30_000 }) +} + + + +await test('NTFS state classification separates the two WSL redirector prefixes from everything else', () => { + const state = (value) => psValue(`$ErrorActionPreference='Stop' +. ${quote(helper)} +(Get-DshOpsWorkspaceNtfsState -Path ${quote(value)})`) + // Microsoft-fixed redirector spellings, case-insensitive, incl. the \\?\UNC form. + assert.equal(state('\\\\wsl.localhost\\Ubuntu\\home\\pc'), 'wsl-redirector') + assert.equal(state('\\\\wsl$\\Ubuntu\\tmp'), 'wsl-redirector') + assert.equal(state('\\\\?\\UNC\\wsl.localhost\\Ubuntu\\tmp'), 'wsl-redirector') + assert.equal(state('\\\\WSL.LOCALHOST\\Ubuntu\\tmp'), 'wsl-redirector') + // Everything else is NTFS business as usual: a real ACL error there must + // still fail closed, not be absorbed into a redirector allowance. + assert.equal(state('D:\\normal\\path'), 'ntfs') + assert.equal(state('\\\\server\\share\\file'), 'ntfs') + assert.equal(state('\\\\wsl.localhost2\\a'), 'ntfs') + assert.equal(state('\\\\wslx\\Ubuntu\\a'), 'ntfs') + assert.equal(state('wsl'), 'ntfs') +}) + +// The live 9P end-to-end case is an explicit opt-in: it needs a real installed +// distribution, so it registers only when DSH_OPS_TEST_WSL_DISTRO names one. It +// is reported as not run otherwise — an unavailable distribution never counts +// as a passing check, and the default suite keeps its honest coverage below. +// Registration order and the probe are host facts, not preferences: touching a +// nonexistent \\wsl.localhost distribution leaves the P9NP redirector unable to +// answer even valid paths for under two seconds (measured on this host), so +// this case registers before the negative one, probes at registration time so +// a check never silently passes as an empty body, and retries once within that +// window. A still-failing probe is reported as not run and not counted. +const liveDistro = process.env.DSH_OPS_TEST_WSL_DISTRO +let liveProvider = 'not-answering' +if (liveDistro) { + for (const attempt of [0, 1]) { + const probe = ps(`try { \$null = Get-Item -LiteralPath ${quote(`\\\\wsl.localhost\\${liveDistro}\\tmp`)} -Force -ErrorAction Stop; 'ok' } catch { 'no' }`) + if (probe.status === 0 && probe.stdout.trim() === 'ok') { liveProvider = 'answering'; break } + if (attempt === 0) await wait(2500) + } +} +if (liveDistro && liveProvider === 'answering') { + await test(`the live WSL UNC workspace ${liveDistro} answers ready instead of dying inside Get-Acl (#669)`, () => { + const root = `\\\\wsl.localhost\\${liveDistro}\\tmp` + const backups = path.join(TEST_HOME, 'prepare-wsl-redirector-live-backups') + const value = runScript(root, backups) + assert.equal(value.status, 0, value.stderr) + assert.equal(value.stdout.trim(), 'ready') + }) +} else if (liveDistro) { + console.log(' (live 9P end-to-end not run: the redirector is not answering right now; not counted as passed)') +} else { + console.log(' (live 9P end-to-end not run: set DSH_OPS_TEST_WSL_DISTRO= to include it; not counted as passed)') +} + +await test('a redirector-spelled workspace that does not exist fails instead of answering ready (#669)', () => { + // The prefix allowance must not widen into skipping existence: a 9P-spelled + // path that does not resolve still has to fail the same way any other + // missing workspace does, after the Get-Item/container validation. + const root = '\\\\wsl.localhost\\nonexistent-distro\\workspace' + const backups = path.join(TEST_HOME, 'prepare-wsl-redirector-backups') + const value = runScript(root, backups) + assert.notEqual(value.status, 0, 'a missing 9P workspace must not be answered ready') + assert.ok(!value.stdout.includes('ready')) + assert.equal(fs.existsSync(backups) ? fs.readdirSync(backups).length : 0, 0) +}) + +} + +report('workspace-wsl-redirector') diff --git a/test/wsl-prepare-merge.test.mjs b/test/wsl-prepare-merge.test.mjs new file mode 100644 index 00000000..da12641c --- /dev/null +++ b/test/wsl-prepare-merge.test.mjs @@ -0,0 +1,149 @@ +import fs from 'node:fs' +import path from 'node:path' +import { createHash } from 'node:crypto' +import { assert, test, report, withTempDir } from './lib/harness.mjs' +import { tryWslRead } from '../lib/execution/wsl-worker.js' +import { workerEnvelope } from '../lib/execution/protocol.js' + +/** + * Issue #576: the optional WSL read delegated after driving one wsl.exe call + * per drive mapping, one more for the payload sha256, and a last one for the + * worker, so a small read paid four WSL startups for one result. The + * preparation must be a single fixed /bin/sh -c script whose text never + * interpolates data: every drive root and the payload path travel as + * positional arguments, so no Windows path can alter the command shape. The + * digest check and the Windows fallback stay exactly as strict as before. + */ +const unc = '\\\\wsl.localhost\\Ubuntu' + +/** Stage a fake installed package with receipt-verified workers for both targets. */ +function stagePackage(dir) { + const version = '9.9.9-merge' + fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({ version })) + const digests = {} + for (const [target, name] of [['win32-x64', 'dsh-ops-worker.exe'], ['linux-x64', 'dsh-ops-worker']]) { + const file = path.join(dir, 'runtime', target, name) + fs.mkdirSync(path.dirname(file), { recursive: true }) + fs.writeFileSync(file, `payload-${target}`) + digests[target] = createHash('sha256').update(`payload-${target}`).digest('hex') + fs.writeFileSync(path.join(dir, 'runtime', target, 'provenance.json'), + JSON.stringify({ target, version, executable: name, sha256: digests[target] })) + } + return digests +} + +/** Record every spawn and answer the merged preparation or the worker delegation. */ +function fakeSubprocess(answers) { + const calls = [] + return { + calls, + spawn: (spec) => { + calls.push(spec) + const prepare = spec.argv[4] === '/bin/sh' && spec.argv[5] === '-c' + const answer = prepare ? answers.prepare(calls.length) : answers.worker + return { + done: Promise.resolve({ exitCode: answer.exitCode ?? 0, signal: null }), + collected: { + stdout: { readFrom: () => ({ text: answer.stdout ?? '', lossy: false }) }, + stderr: { readFrom: () => ({ text: answer.stderr ?? '', lossy: false }) }, + }, + } + }, + } +} + +async function attemptWith(dir, subprocess, digests, windowsWorker, payloadLinux, extraArgs = {}) { + const envelope = workerEnvelope('file', { tool: 'glob', args: { path: `${unc}\\project\\*.txt` }, + root: `${unc}\\project`, deny_roots: ['C:\\Users\\pc\\.ssh', 'D:\\private\\creds'], ...extraArgs }) + return tryWslRead(subprocess, windowsWorker, envelope, 'C:\\', new AbortController().signal, () => true, undefined, + { platform: 'win32', packageRoot: dir, env: { DSH_OPS_WSL_READS: '1', SystemRoot: 'C:\\Windows' } }) +} + +await test('WSL read preparation is one fixed positional-parameter shell call plus the worker (#576)', async () => { + if (process.platform !== 'win32') { + console.log(' (POSIX host: merged WSL preparation needs real Windows drive roots; this check needs Windows)') + return + } + await withTempDir('wsl-prepare-merge', async (dir) => { + const digests = stagePackage(dir) + const windowsWorker = path.join(dir, 'runtime', 'win32-x64', 'dsh-ops-worker.exe') + const payloadLinux = path.posix.join('/mnt/c', path.win32.relative('C:\\', path.join(dir, 'runtime', 'linux-x64', 'dsh-ops-worker')).replaceAll('\\', '/')) + const mergedOutput = [ + 'C:\\\t/mnt/c', + 'D:\\\t/mnt/d', + `${digests['linux-x64']} ${payloadLinux}`, + ].join('\n') + '\n' + const subprocess = fakeSubprocess({ + prepare: () => ({ stdout: mergedOutput }), + worker: { stdout: JSON.stringify({ status: 'completed', pageComplete: true, items: ['/project/a.txt'] }) }, + }) + const attempt = await attemptWith(dir, subprocess, digests, windowsWorker, payloadLinux) + assert.equal(attempt.handled, true, JSON.stringify(attempt)) + assert.deepEqual(attempt.value.items, [`${unc}\\project\\a.txt`]) + // #576: exactly one preparation call and one worker delegation. + assert.equal(subprocess.calls.length, 2, + `expected 2 wsl.exe invocations, saw ${subprocess.calls.length}: ${subprocess.calls.map(c => c.argv.join(' ')).join(' || ')}`) + const prepare = subprocess.calls[0] + assert.equal(prepare.argv[4], '/bin/sh') + assert.equal(prepare.argv[5], '-c') + const script = prepare.argv[6] + // The script is fixed text: it names its tools and interpolates nothing. + assert.ok(script.includes('wslpath') && script.includes('sha256sum'), script) + assert.ok(!script.includes('C:'), script) + assert.ok(!script.includes(dir), script) + // Data travels as positional arguments behind the $0 placeholder, in the + // order drive roots first and the payload path last. + assert.equal(prepare.argv[7], 'dsh-ops') + assert.deepEqual(prepare.argv.slice(8), ['C:\\', 'D:\\', path.join(dir, 'runtime', 'linux-x64', 'dsh-ops-worker')]) + // The delegation runs the receipt-verified Linux worker with mapped roots. + assert.equal(subprocess.calls[1].argv[4], payloadLinux) + const delegated = JSON.parse(subprocess.calls[1].stdio.stdin.data) + assert.deepEqual(delegated.request.deny_roots, ['/mnt/c/Users/pc/.ssh', '/mnt/d/private/creds']) + assert.equal(delegated.request.root, '/project') + }) +}) + +await test('a mismatched payload digest still falls back to the Windows worker (#576)', async () => { + if (process.platform !== 'win32') { + console.log(' (POSIX host: the digest fallback check needs real Windows drive roots; this check needs Windows)') + return + } + await withTempDir('wsl-prepare-digest', async (dir) => { + const digests = stagePackage(dir) + const windowsWorker = path.join(dir, 'runtime', 'win32-x64', 'dsh-ops-worker.exe') + const payloadLinux = path.posix.join('/mnt/c', path.win32.relative('C:\\', path.join(dir, 'runtime', 'linux-x64', 'dsh-ops-worker')).replaceAll('\\', '/')) + const mergedOutput = [ + 'C:\\\t/mnt/c', + 'D:\\\t/mnt/d', + `${'0'.repeat(64)} ${payloadLinux}`, + ].join('\n') + '\n' + const subprocess = fakeSubprocess({ + prepare: () => ({ stdout: mergedOutput }), + worker: { stdout: '{}' }, + }) + const attempt = await attemptWith(dir, subprocess, digests, windowsWorker, payloadLinux) + assert.equal(attempt.handled, false) + assert.equal(subprocess.calls.length, 1, 'a digest mismatch must not start a worker delegation') + }) +}) + +await test('a failing preparation script still falls back to the Windows worker (#576)', async () => { + if (process.platform !== 'win32') { + console.log(' (POSIX host: the preparation fallback check needs real Windows drive roots; this check needs Windows)') + return + } + await withTempDir('wsl-prepare-fail', async (dir) => { + const digests = stagePackage(dir) + const windowsWorker = path.join(dir, 'runtime', 'win32-x64', 'dsh-ops-worker.exe') + const payloadLinux = path.posix.join('/mnt/c', path.win32.relative('C:\\', path.join(dir, 'runtime', 'linux-x64', 'dsh-ops-worker')).replaceAll('\\', '/')) + const subprocess = fakeSubprocess({ + prepare: () => ({ exitCode: 9, stdout: '' }), + worker: { stdout: '{}' }, + }) + const attempt = await attemptWith(dir, subprocess, digests, windowsWorker, payloadLinux) + assert.equal(attempt.handled, false) + assert.equal(subprocess.calls.length, 1) + }) +}) + +report('wsl-prepare-merge')