Skip to content

fix(windows): stop main-thread PowerShell ACL storm on env-store reads - #5011

Merged
nwparker merged 3 commits into
stablyai:mainfrom
LesleyMurfin:fix/windows-env-store-acl-harden-storm
Jun 9, 2026
Merged

nwparker merged 3 commits into
stablyai:mainfrom
LesleyMurfin:fix/windows-env-store-acl-harden-storm

Conversation

@LesleyMurfin

@LesleyMurfin LesleyMurfin commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On Windows (v1.4.52+), connecting to a remote/paired runtime made the app extremely
slow during load and timed out session-tabs loading
([web-session-tabs-sync] … RemoteRuntimeClientError: Timed out waiting for the remote Orca runtime to respond). The env-store read path was re-hardening the parent
directory's NTFS ACL on every read by spawning powershell.exe synchronously on the
Electron main thread — ~1.8 spawns/sec, continuous through the whole load window — which
saturated the main thread so it could not service runtimeEnvironments:call.

This PR makes env-store reads stop storming the main thread while keeping credential
files locked down. No visual change.

Fixes #5006.

Root cause

readEnvironmentStore calls hardenExistingSecureFile on every read. Hardening was
keyed on directory mtime/ctime, but the env-store parent directory's mtime churns
constantly (every secure write updates it), so the idempotency cache never matched and
bestEffortRestrictWindowsPath (PowerShell Get-Acl/Set-Acl, ~1–1.5 s cold-start) ran
on every call. The remote-runtime tab-sync loop reads the store ~2×/s, turning sporadic
mtime misses into a continuous synchronous-PowerShell storm on the main thread. Regressed
in v1.4.52 (worked in v1.4.51). Instrumented Windows process creation showed 49
powershell.exe spawns in 27 s on a cold launch.

The fix

Two coordinated changes in src/shared/secure-file.ts, applying the right
caching + execution strategy per path type:

  1. Path-cached directory hardening (kills the storm). Directories are now cached as
    hardened by path for the process lifetime (hardenedDirectoryPathsThisProcess),
    not by mtime. A directory's required ACL does not change because a child write bumped
    its mtime, so once hardened in this process it is trusted. Directory hardening is
    async (execFile, fire-and-forget) so it never blocks the main thread.

    • Known limitation documented in code: a deleted+recreated directory is not
      re-hardened until the next process restart (the .orca secure dirs are not deleted
      at runtime).
  2. Synchronous credential-file ACL on the write path (closes a security window).
    Switching the file ACL to async would have left a regression: writeFileSync({ mode })
    is a no-op on Windows, so a freshly written credential file carries the parent
    directory's inherited (broader) ACL until PowerShell finishes ~1–1.5 s later. To avoid
    writeSecureFile returning with the credential briefly readable under inherited ACLs,
    the file ACL (temp file + renamed target) is applied synchronously
    (execFileSync) on the write path, and cached as hardened only on confirmed
    success
    so a failed apply retries. The write path is infrequent; it was the read
    path that stormed, and that stays async + cached.

The read path's existing-file hardening stays async + metadata-cached (fires at most once
per file per process, so it does not storm; it only re-asserts an ACL on a file that
already exists).

Test plan

Unit tests — src/shared/secure-file.test.ts (vitest run, 13 passing)

New / updated coverage:

Run: vitest run --config config/vitest.config.ts src/shared/secure-file.test.ts

CI gates (run locally on the changed files)

  • oxlint (lint) — clean
  • oxlint switch-exhaustiveness on src/shared — clean
  • oxfmt --check — clean
  • tsgo typecheck (node / web / cli configs) — all clean

End-to-end — manual on Windows (no automated coverage possible here)

The Playwright e2e harness runs on ubuntu-latest, where
applySecurePathRestriction short-circuits to chmodSync and never reaches the
PowerShell path, so the Windows ACL storm cannot be reproduced in the cross-platform
harness. Manual Windows plan documented in
docs/windows-secure-file-acl-hardening.md:

  • PowerShell watcher counting powershell.exe processes during load.
  • Before: count oscillates ~1–2/sec continuously + session-tabs timeout.
  • After: count stays 0; app loads without the timeout.
  • icacls on the credential file immediately after a write shows only current user /
    SYSTEM / Administrators (no inherited entries) — confirms the synchronous file-ACL apply
    closed the async window.

AI Review Report

Adversarial review covered: cross-platform — all PowerShell paths are guarded by
platform === 'win32'; POSIX behavior (chmodSync) unchanged; a test asserts no
PowerShell on non-win32. SSH/remote/local — change is in the shared secure-file
writer used by local and remote runtimes alike; behavior is identical, only the Windows
ACL execution model changed. Performance — this PR removes a hot-path main-thread
stall (the whole point); the only added synchronous work is on the infrequent write path.
Security — the review flagged that a naive async switch would publish credential files
under inherited ACLs for ~1–1.5 s; addressed by applying the file ACL synchronously on the
write path and caching only on confirmed success. No UI change.

Security Audit

  • Command execution: PowerShell is invoked with -NoProfile -NonInteractive -ExecutionPolicy Bypass, windowsHide, a 5 s timeout, and a fixed embedded script;
    arguments (path, SID, isDirectory flag) are passed as execFile/execFileSync argv (no
    shell interpolation). No change to the argument-passing model.
  • Path handling: uses path utilities and the existing getWindowsSystemToolPath
    helper (resolves powershell.exe/whoami.exe under %SystemRoot%\System32); no
    user-controlled tool paths.
  • Secrets / credential exposure: the core security improvement — credential files now
    have their restrictive ACL applied synchronously before writeSecureFile returns,
    removing the async window where they would carry inherited (broader) ACLs.
  • Failure handling: ACL hardening remains best-effort (won't crash startup where
    PowerShell ACL APIs are locked down); a failed file-ACL apply is left uncached so it
    retries rather than being silently trusted.
  • Follow-up: none required.

Notes

  • Platform-specific: Windows-only behavior change; macOS/Linux paths
    (chmodSync) are untouched and covered by the non-win32 guard test.
  • Risk / rollback: low. Self-contained to src/shared/secure-file.ts and its test.
    Rollback = revert the commit; restores prior behavior (the storm). The directory cache
    is process-lifetime only (no persisted state), so there is no migration concern.
  • No visual change.

LesleyMurfin and others added 3 commits June 9, 2026 01:42
Two changes fix the v1.4.52+ Windows performance regression (stablyai#4901 regression
against stablyai#4840) where 49 powershell.exe processes were spawned in 27 seconds
during load, saturating the Electron main thread and causing black terminals
and runtimeEnvironments:call timeouts.

Root cause: `readEnvironmentStore` calls `hardenExistingSecureFile` on every
read. The env-store parent directory's mtime churns constantly (every secure
write updates it), so the mtime-keyed idempotency cache never matched →
`bestEffortRestrictWindowsPath` (powershell, ~1-1.5s synchronous) fired on
every call. After stablyai#4901, the remote-runtime tab-sync loop reads the store
~2×/s, turning sporadic mtime misses into a continuous main-thread storm.

Fix 1 – path-cached directory hardening: add `hardenedDirectoryPathsThisProcess
(Set<string>)` that caches directory hardening by PATH for the process lifetime.
A directory's required ACL does not change when its mtime changes; only file
hardening retains the metadata-keyed cache so post-rename inode changes are
detected correctly.

Fix 2 – async ACL application: replace `execFileSync(powershell.exe, ...)` with
`execFile` (fire-and-forget). PowerShell cold-start is ~1-1.5s; the function is
already named `bestEffortRestrictWindowsPath` so async/optimistic caching is
correct. `applySecurePathRestriction` returns `true` optimistically on win32 so
the cache entry is written before the background process completes.

Tests: new regression tests verify the directory is hardened exactly once even
when its mtime changes between calls, that unchanged files are not re-hardened,
and that ACL application goes through async execFile (not execFileSync).
Follow-up rigor on the env-store PowerShell ACL storm fix (stablyai#5006). The
read-path storm fix (path-cached async directory hardening + async file
re-harden) is retained, but switching ALL ACL application to async opened a
narrow Windows-only security window: because writeFileSync({mode}) is a no-op
on Windows, writeSecureFile returned with the credential file still carrying
the parent directory's inherited (broader) ACL for the ~1-1.5s PowerShell
cold-start, affecting the e2ee keypair, device registry, and runtime env auth
store.

Fix: apply the credential FILE's ACL synchronously (execFileSync) on the
infrequent write path, before the atomic rename publishes it, and cache the
path as hardened only on confirmed success so a failed apply retries. Keep the
DIRECTORY hardening async + path-cached for the process lifetime (that is what
killed the stablyai#4901/stablyai#5006 main-thread storm). The read path's existing-file
re-harden stays async + metadata-cached (fires at most once per file, no storm).

Also:
- Document the dir-path cache process-lifetime known limitation (deleted+
  recreated dir not re-hardened until restart).
- Remove the redundant double dir-cache write in writeSecureFile.
- Add docs/windows-secure-file-acl-hardening.md describing the sync-file/
  async-dir model and a manual Windows e2e test plan (the cross-platform
  Playwright harness runs on Linux and cannot reach the PowerShell path).

Tests (src/shared/secure-file.test.ts, 13 passing): credential file hardened
synchronously while dir stays async (no async file-ACL window); failed sync
file-ACL apply is not cached and retries; dir hardened exactly once across many
writes despite mtime churn; no PowerShell spawned on non-win32.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@nwparker
nwparker force-pushed the fix/windows-env-store-acl-harden-storm branch from 36e3894 to f2ca3b3 Compare June 9, 2026 08:47
@nwparker
nwparker merged commit 4d6c291 into stablyai:main Jun 9, 2026
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
stablyai#5011)

* fix(windows): stop main-thread PowerShell storm on env-store reads

Two changes fix the v1.4.52+ Windows performance regression (stablyai#4901 regression
against stablyai#4840) where 49 powershell.exe processes were spawned in 27 seconds
during load, saturating the Electron main thread and causing black terminals
and runtimeEnvironments:call timeouts.

Root cause: `readEnvironmentStore` calls `hardenExistingSecureFile` on every
read. The env-store parent directory's mtime churns constantly (every secure
write updates it), so the mtime-keyed idempotency cache never matched →
`bestEffortRestrictWindowsPath` (powershell, ~1-1.5s synchronous) fired on
every call. After stablyai#4901, the remote-runtime tab-sync loop reads the store
~2×/s, turning sporadic mtime misses into a continuous main-thread storm.

Fix 1 – path-cached directory hardening: add `hardenedDirectoryPathsThisProcess
(Set<string>)` that caches directory hardening by PATH for the process lifetime.
A directory's required ACL does not change when its mtime changes; only file
hardening retains the metadata-keyed cache so post-rename inode changes are
detected correctly.

Fix 2 – async ACL application: replace `execFileSync(powershell.exe, ...)` with
`execFile` (fire-and-forget). PowerShell cold-start is ~1-1.5s; the function is
already named `bestEffortRestrictWindowsPath` so async/optimistic caching is
correct. `applySecurePathRestriction` returns `true` optimistically on win32 so
the cache entry is written before the background process completes.

Tests: new regression tests verify the directory is hardened exactly once even
when its mtime changes between calls, that unchanged files are not re-hardened,
and that ACL application goes through async execFile (not execFileSync).

* fix(windows): apply credential-file ACL synchronously on write path

Follow-up rigor on the env-store PowerShell ACL storm fix (stablyai#5006). The
read-path storm fix (path-cached async directory hardening + async file
re-harden) is retained, but switching ALL ACL application to async opened a
narrow Windows-only security window: because writeFileSync({mode}) is a no-op
on Windows, writeSecureFile returned with the credential file still carrying
the parent directory's inherited (broader) ACL for the ~1-1.5s PowerShell
cold-start, affecting the e2ee keypair, device registry, and runtime env auth
store.

Fix: apply the credential FILE's ACL synchronously (execFileSync) on the
infrequent write path, before the atomic rename publishes it, and cache the
path as hardened only on confirmed success so a failed apply retries. Keep the
DIRECTORY hardening async + path-cached for the process lifetime (that is what
killed the stablyai#4901/stablyai#5006 main-thread storm). The read path's existing-file
re-harden stays async + metadata-cached (fires at most once per file, no storm).

Also:
- Document the dir-path cache process-lifetime known limitation (deleted+
  recreated dir not re-hardened until restart).
- Remove the redundant double dir-cache write in writeSecureFile.
- Add docs/windows-secure-file-acl-hardening.md describing the sync-file/
  async-dir model and a manual Windows e2e test plan (the cross-platform
  Playwright harness runs on Linux and cannot reach the PowerShell path).

Tests (src/shared/secure-file.test.ts, 13 passing): credential file hardened
synchronously while dir stays async (no async file-ACL window); failed sync
file-ACL apply is not cached and retries; dir hardened exactly once across many
writes despite mtime churn; no PowerShell spawned on non-win32.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* fix: keep POSIX secure directory hardening metadata-aware

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Neil <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants