Skip to content

The agent that grows with you

fix(install): run the installed hermes under the Restricted policy

ethernete8d1fc76ec25
diff --git a/scripts/install.ps1 b/scripts/install.ps1
index f0417982ff0..6e62d9b39e1 100644
--- a/scripts/install.ps1
+++ b/scripts/install.ps1
@@ -882,10 +882,17 @@ function Stage-Config {
}
function Invoke-InstalledHermes([string[]]$CommandArgs) {
- . (Join-Path $InstallDir 'scripts/desktop-update/runtime.ps1')
- $command = @(Get-HermesRuntimeCommand -InstallRoot $InstallDir)
- $runtimeArgs = @($command | Select-Object -Skip 1) + $CommandArgs
- Invoke-Native { & $command[0] @runtimeArgs }
+ # Load the helper from its text, not its path. Under `irm | iex` this
+ # installer runs as a string that execution policy never checks, but
+ # dot-sourcing a .ps1 from disk is a file load. The default Restricted
+ # policy (Windows Sandbox, fresh machines) refuses that load.
+ $runtimeHelper = Join-Path $InstallDir 'scripts/desktop-update/runtime.ps1'
+ . ([ScriptBlock]::Create([IO.File]::ReadAllText($runtimeHelper)))
+ # Not `$command`: Invoke-Native's `$Command` parameter shadows it
+ # (names are case-insensitive) and the block would invoke itself.
+ $runtimeCommand = @(Get-HermesRuntimeCommand -InstallRoot $InstallDir)
+ $runtimeArgs = @($runtimeCommand | Select-Object -Skip 1) + $CommandArgs
+ Invoke-Native { & $runtimeCommand[0] @runtimeArgs }
if ($LASTEXITCODE) { Fail "hermes $($CommandArgs -join ' ') failed (exit $LASTEXITCODE)" }
}
diff --git a/tests/scripts/install/test_install_ps1_execution_policy.py b/tests/scripts/install/test_install_ps1_execution_policy.py
new file mode 100644
index 00000000000..6f1f5e5842a
--- /dev/null
+++ b/tests/scripts/install/test_install_ps1_execution_policy.py
@@ -0,0 +1,40 @@
+"""The iex'd installer must reach its runtime helper under the Restricted policy.
+
+`iex (irm .../install.ps1)` is a string, so execution policy never checks it.
+Loading a helper .ps1 from disk is a file load, which the default Restricted
+policy (Windows Sandbox, fresh machines) refuses: "runtime.ps1 cannot be
+loaded because running scripts is disabled on this system". Past the load,
+the resolved runtime command must actually run (a `$command` local once
+collided with Invoke-Native's `$Command` parameter and recursed forever).
+"""
+from pathlib import Path
+import shutil
+import subprocess
+
+import pytest
+
+pytestmark = pytest.mark.platforms("windows")
+INSTALLER = Path(__file__).resolve().parents[3] / "scripts" / "install.ps1"
+
+
+def test_installed_hermes_runs_under_restricted_policy(tmp_path):
+ powershell = shutil.which("powershell")
+ assert powershell
+ helper = tmp_path / "install" / "scripts" / "desktop-update" / "runtime.ps1"
+ helper.parent.mkdir(parents=True)
+ helper.write_text("function Get-HermesRuntimeCommand([string]$InstallRoot) {\n"
+ " @('cmd.exe', '/c', 'echo', 'runtime-from', $InstallRoot)\n}\n",
+ encoding="utf-8")
+ install_dir = str(helper.parents[2]).replace("'", "''")
+ # Definitions load under Bypass (the iex'd text is never policy-checked);
+ # the session then drops to Restricted, the policy a fresh machine has.
+ command = (f". '{str(INSTALLER).replace(chr(39), chr(39) * 2)}'; "
+ "Set-ExecutionPolicy -Scope Process Restricted -Force; "
+ f"$InstallDir = '{install_dir}'; "
+ "Invoke-InstalledHermes @('setup')")
+ result = subprocess.run([powershell, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass",
+ "-Command", command],
+ cwd=tmp_path, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=60)
+ output = result.stdout + result.stderr
+ assert "running scripts is disabled" not in output, output
+ assert f"runtime-from {helper.parents[2]} setup" in result.stdout, output