Trusted root: lexical check before any filesystem access, cached env setting, stricter log escaping

Review (Heimdall): a path outside the pinned root (as resolved or as given) is refused by
string comparison before File.Exists or any link is followed; the trusted-root setting is
its own small type that takes the environment variable once, caches it and refuses blank
values; Printable also escapes backslash, format characters (bidi, zero-width) and the
line and paragraph separators; dev-watch matches the host ack by the unique slot leaf with
doubled backslashes; dev-reload prunes live-slots and rejects stale reopen state up front.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Aqua 2026-11-07 11:04:00 +00:00
commit 467a97f540
6 changed files with 196 additions and 46 deletions

View file

@ -22,6 +22,7 @@ $devRoot = Join-Path $repoRoot "artifacts\dev-reload"
# it is the host's trusted root, so state, cache and logs in $devRoot are not part of it.
$assemblyRoot = Join-Path $devRoot "assemblies"
$slotRoot = Join-Path $assemblyRoot "slots"
$liveSlotRoot = Join-Path $assemblyRoot "live-slots"
$visibleStatePath = Join-Path $devRoot "state.clixml"
$headlessStatePath = Join-Path $devRoot "headless-state.clixml"
$statePath = if ($Headless) { $headlessStatePath } else { $visibleStatePath }
@ -82,9 +83,12 @@ function Remove-OldSlots {
return
}
$slots = Get-ChildItem -Path $slotRoot -Directory -ErrorAction SilentlyContinue |
Sort-Object LastWriteTimeUtc -Descending |
Select-Object -Skip $RetainSlots
# Both roots hold client code the host may load; dev-watch adds a live slot per change and never prunes.
$slots = foreach ($root in @($slotRoot, $liveSlotRoot)) {
Get-ChildItem -Path $root -Directory -ErrorAction SilentlyContinue |
Sort-Object LastWriteTimeUtc -Descending |
Select-Object -Skip $RetainSlots
}
foreach ($slot in $slots) {
try {
@ -245,6 +249,17 @@ function Reopen-VisibleProcess {
$liveAssemblyPath = [string]$state.liveAssembly
}
# Stale state fails here, in words, instead of as a refused load inside the host.
$assemblyRootPrefix = [System.IO.Path]::GetFullPath($assemblyRoot).TrimEnd('\') + '\'
foreach ($recorded in @($slotPath, $liveAssemblyPath)) {
if (-not [System.IO.Path]::GetFullPath($recorded).StartsWith($assemblyRootPrefix, [System.StringComparison]::OrdinalIgnoreCase)) {
throw "Visible dev-reload state names '$recorded', outside the assemblies root '$assemblyRoot' (state from before the trusted-root layout, or the root moved). Run dev-reload without -Reopen to rebuild."
}
}
if (-not (Test-Path -LiteralPath $liveAssemblyPath)) {
throw "Visible dev-reload state names '$liveAssemblyPath', which no longer exists (pruned slot). Run dev-reload without -Reopen to rebuild."
}
$process = Start-AquariumProcess -SlotPath $slotPath -LiveAssemblyPath $liveAssemblyPath -RunHeadless:$false
Wait-ForStartedAquarium -Process $process -ExpectWindow:$true

View file

@ -455,12 +455,15 @@ function Wait-LiveReloadAcknowledged {
$stderrLog = if ($state.stderrLog) { [string]$state.stderrLog } else { $null }
$timeoutSeconds = [Math]::Max(1, $LiveReloadAckTimeoutSeconds)
$deadline = (Get-Date).AddSeconds($timeoutSeconds)
$appliedLine = "Client runtime reload applied: $liveAssemblyPath"
# The host logs the resolved path with backslashes doubled, which can differ from the path built here when the
# repo sits under a link. The slot directory name is unique, so match its leaf and the assembly name.
$slotLeaf = Split-Path -Leaf (Split-Path -Parent $liveAssemblyPath)
$appliedSuffix = ("$slotLeaf\" + (Split-Path -Leaf $liveAssemblyPath)).Replace("\", "\\")
while ((Get-Date) -lt $deadline) {
if (Test-Path $stdoutLog) {
$stdoutTail = (Get-Content -Path $stdoutLog -Tail 120 -ErrorAction SilentlyContinue) -join "`n"
if ($stdoutTail.Contains($appliedLine)) {
if ($stdoutTail -match ("(?m)^Client runtime reload applied: .*" + [regex]::Escape($appliedSuffix) + "\s*$")) {
Write-WatchLog "Live reload acknowledged by host: $liveAssemblyPath"
return
}