use std::path::Path;
use super::protocol::{self, DONE, ENDED, READY};
pub(super) fn ps_quote(s: &str) -> String {
let mut out = String::with_capacity(s.len() + 2);
out.push('\'');
for c in s.chars() {
if matches!(c, '\'' | '\u{2018}' | '\u{2019}' | '\u{201A}' | '\u{201B}') {
out.push(c);
}
out.push(c);
}
out.push('\'');
out
}
pub(super) fn passes_intact(arg: &str) -> bool {
!(arg.is_empty() || arg.contains(['"', '\0', '\n', '\r']) || arg == "--%")
}
pub(super) fn broker(dir: &Path, token: &str, runner: u32) -> String {
let [dir, token, done, ended] = [&*dir.display().to_string(), token, DONE, ENDED].map(ps_quote);
let preamble = format!(
"$ErrorActionPreference = 'Continue'\n\
$ProgressPreference = 'SilentlyContinue'\n\
try {{ [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding -ArgumentList $false }} catch {{}}\n\
$d = {dir}\n\
$token = {token}\n\
function Write-Ended($why) {{ if (-not (Test-Path -LiteralPath (Join-Path $d {done}))) {{ try {{ [System.IO.File]::WriteAllText((Join-Path $d {ended}), $why) }} catch {{}} }} }}\n\
function Remove-Quietly($paths) {{ foreach ($p in $paths) {{ Remove-Item -LiteralPath $p -Force -ErrorAction SilentlyContinue }} }}\n\
function Test-Over {{ (-not (Test-Path -LiteralPath $d)) -or (Test-Path -LiteralPath (Join-Path $d {done})) }}\n"
);
let watcher = format!(
"$null = Start-Job -ArgumentList $PID, {runner}, $d -ScriptBlock {{\n\
\x20 param($main, $runnerId, $d)\n\
\x20 $runner = Get-Process -Id $runnerId -ErrorAction SilentlyContinue\n\
\x20 while (-not (Test-Path -LiteralPath (Join-Path $d {done}))) {{\n\
\x20 $why = $null\n\
\x20 if (-not (Test-Path -LiteralPath $d)) {{ $why = 'its folder is gone' }}\n\
\x20 elseif ($null -eq $runner -or $runner.HasExited) {{ $why = 'the runner is gone' }}\n\
\x20 if ($null -ne $why) {{\n\
\x20 try {{ [System.IO.File]::WriteAllText((Join-Path $d {ended}), $why) }} catch {{}}\n\
\x20 & taskkill.exe /T /F /PID $main | Out-Null; return\n\
\x20 }}\n\
\x20 Start-Sleep -Milliseconds 500\n\
\x20 }}\n\
}}\n"
);
[preamble.as_str(), &allowed(), watcher.as_str(), REQUEST_JOB, &request_loop()].concat()
}
const TRUSTED_OWNERS: [&str; 3] = ["S-1-5-32-544", "S-1-5-18", "S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464"];
fn allowed() -> String {
let list = |items: &[&str]| items.iter().map(|i| ps_quote(i)).collect::<Vec<_>>().join(", ");
let [owners, vagrant_vars, system_vars, loaders] = [
&TRUSTED_OWNERS[..],
&protocol::VAGRANT_VARS[..],
&protocol::SYSTEM_VARS[..],
&protocol::LOADER_PREFIXES[..],
]
.map(list);
format!(
"$sys = [System.Environment]::SystemDirectory\n\
$programFiles = [System.Environment]::GetFolderPath('ProgramFiles')\n\
$programs = @{{\n\
\x20 'vagrant' = @((Join-Path $programFiles 'Vagrant\\bin\\vagrant.exe'), (Join-Path $env:SystemDrive 'HashiCorp\\Vagrant\\bin\\vagrant.exe'))\n\
\x20 'powershell' = @((Join-Path $sys 'WindowsPowerShell\\v1.0\\powershell.exe'))\n\
}}\n\
$systemPath = @($sys, (Join-Path $sys 'Wbem'), (Join-Path $sys 'WindowsPowerShell\\v1.0'), (Join-Path $sys 'OpenSSH'), [System.Environment]::GetFolderPath('Windows'))\n\
$modulePath = @((Join-Path $sys 'WindowsPowerShell\\v1.0\\Modules'), (Join-Path $programFiles 'WindowsPowerShell\\Modules')) -join ';'\n\
[System.Environment]::SetEnvironmentVariable('PSModulePath', $modulePath, 'Process')\n\
$trustedOwners = @({owners})\n\
function Test-Owned($path) {{\n\
\x20 $p = $path\n\
\x20 while ($p -and $p -ne [System.IO.Path]::GetPathRoot($p)) {{\n\
\x20 try {{ $owner = (Get-Acl -LiteralPath $p).GetOwner([System.Security.Principal.SecurityIdentifier]).Value }} catch {{ return $false }}\n\
\x20 if ($trustedOwners -notcontains $owner) {{ return $false }}\n\
\x20 $p = Split-Path -Parent $p\n\
\x20 }}\n\
\x20 $true\n\
}}\n\
function Resolve-Program($name) {{\n\
\x20 if ($name -isnot [string] -or -not $programs.ContainsKey($name)) {{ return $null }}\n\
\x20 foreach ($p in $programs[$name]) {{ if ((Test-Path -LiteralPath $p -PathType Leaf) -and (Test-Owned $p)) {{ return $p }} }}\n\
\x20 $null\n\
}}\n\
$vagrantVars = @({vagrant_vars})\n\
$systemVars = @({system_vars})\n\
$loaderPrefixes = @({loaders})\n\
function Test-Loader($k) {{\n\
\x20 $u = $k.ToUpperInvariant()\n\
\x20 if ($u.StartsWith('VAGRANT_')) {{ return $vagrantVars -notcontains $u }}\n\
\x20 foreach ($p in $loaderPrefixes) {{ if ($u.StartsWith($p)) {{ return $true }} }}\n\
\x20 $false\n\
}}\n\
function Test-Var($k) {{\n\
\x20 if ($k -isnot [string] -or $k -eq '' -or $k.Contains('=')) {{ return $false }}\n\
\x20 -not (($systemVars -contains $k.ToUpperInvariant()) -or (Test-Loader $k))\n\
}}\n\
foreach ($v in @(Get-ChildItem Env:)) {{ if (Test-Loader $v.Name) {{ [System.Environment]::SetEnvironmentVariable($v.Name, $null, 'Process') }} }}\n"
)
}
const REQUEST_JOB: &str = "$run = {\n\
\x20 param($r, $e, $exe, $path, $modules)\n\
\x20 [pscustomobject]@{ isothrumPid = $PID }\n\
\x20 try { [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding -ArgumentList $false } catch {}\n\
\x20 [System.Environment]::SetEnvironmentVariable('PATH', $path, 'Process')\n\
\x20 [System.Environment]::SetEnvironmentVariable('PSModulePath', $modules, 'Process')\n\
\x20 foreach ($p in $e.env) { [System.Environment]::SetEnvironmentVariable($p.k, $p.v, 'Process') }\n\
\x20 try {\n\
\x20 Set-Location -LiteralPath $r.cwd -ErrorAction Stop\n\
\x20 $global:LASTEXITCODE = 0\n\
\x20 & $exe @($r.args) 2>&1 | ForEach-Object { [string]$_ }\n\
\x20 [pscustomobject]@{ isothrumExit = $LASTEXITCODE }\n\
\x20 } catch { [string]$_; [pscustomobject]@{ isothrumExit = 1 } }\n\
}\n";
fn request_loop() -> String {
let ready = ps_quote(READY);
let [request, variables, cancel, output, exit] =
[protocol::request, protocol::variables, protocol::cancel, protocol::output, protocol::exit].map(|name| name("$n"));
format!(
"function Read-Once($path) {{ $text = [System.IO.File]::ReadAllText($path); Remove-Item -LiteralPath $path -Force; $text | ConvertFrom-Json }}\n\
[System.IO.File]::WriteAllText((Join-Path $d {ready}), '')\n\
$n = 1\n\
try {{\n\
while (-not (Test-Over)) {{\n\
\x20 $req = Join-Path $d \"{request}\"\n\
\x20 $envFile = Join-Path $d \"{variables}\"\n\
\x20 $cancel = Join-Path $d \"{cancel}\"\n\
\x20 if (Test-Path -LiteralPath $cancel) {{ Remove-Quietly @($req, $envFile, $cancel); $n++; continue }}\n\
\x20 if (-not (Test-Path -LiteralPath $req -PathType Leaf)) {{ Start-Sleep -Milliseconds 200; continue }}\n\
\x20 $r = Read-Once $req\n\
\x20 if ($r.session -ne $token -or $r.seq -ne $n) {{ continue }}\n\
\x20 $e = $null\n\
\x20 if (Test-Path -LiteralPath $envFile) {{ $e = Read-Once $envFile }}\n\
\x20 $out = Join-Path $d \"{output}\"\n\
\x20 $f = [System.IO.File]::Open($out, [System.IO.FileMode]::Append, [System.IO.FileAccess]::Write, [System.IO.FileShare]::ReadWrite)\n\
\x20 $log = New-Object System.IO.StreamWriter -ArgumentList $f, (New-Object System.Text.UTF8Encoding -ArgumentList $false)\n\
\x20 $log.AutoFlush = $true\n\
\x20 $rc = 1\n\
\x20 $cancelled = $false\n\
\x20 $exe = Resolve-Program $r.program\n\
\x20 if ($null -eq $e -or $e.session -ne $token -or $e.seq -ne $n) {{\n\
\x20 $log.WriteLine('isothrum: refused: the request came without its own variables')\n\
\x20 }} elseif ($null -eq $exe) {{\n\
\x20 $log.WriteLine('isothrum: refused: the elevated session runs only Vagrant and Windows PowerShell, from where an administrator installed them')\n\
\x20 }} elseif (@($e.env | Where-Object {{ -not (Test-Var $_.k) }}).Count -gt 0) {{\n\
\x20 $log.WriteLine('isothrum: refused: the request sets a variable an elevated program is never given')\n\
\x20 }} else {{\n\
\x20 $path = (@((Split-Path -Parent $exe)) + $systemPath) -join ';'\n\
{WATCH_REQUEST}\
\x20 }}\n\
\x20 $log.Close()\n\
\x20 if ($cancelled) {{\n\
\x20 Remove-Quietly @($out, $cancel)\n\
\x20 }} else {{\n\
\x20 $exit = Join-Path $d \"{exit}\"\n\
\x20 [System.IO.File]::WriteAllText(\"$exit.tmp\", [string]$rc)\n\
\x20 Move-Item -LiteralPath \"$exit.tmp\" -Destination $exit -Force\n\
\x20 }}\n\
\x20 $n++\n\
}}\n\
}} finally {{ Write-Ended 'the elevated broker stopped' }}\n"
)
}
const WATCH_REQUEST: &str = "\x20 $job = Start-Job -ScriptBlock $run -ArgumentList $r, $e, $exe, $path, $modulePath\n\
\x20 $child = $null\n\
\x20 while ($true) {\n\
\x20 foreach ($o in @(Receive-Job -Job $job)) {\n\
\x20 if ($o -is [string]) { $log.WriteLine($o) }\n\
\x20 elseif ($null -ne $o.isothrumPid) { $child = $o.isothrumPid }\n\
\x20 elseif ($null -ne $o.isothrumExit) { $rc = $o.isothrumExit }\n\
\x20 else { $log.WriteLine([string]$o) }\n\
\x20 }\n\
\x20 if ($job.State -in 'Completed', 'Failed', 'Stopped') { if (-not $job.HasMoreData) { break }; continue }\n\
\x20 if ((Test-Path -LiteralPath $cancel) -or (Test-Over)) {\n\
\x20 $cancelled = $true\n\
\x20 for ($i = 0; $null -eq $child -and $i -lt 50; $i++) { foreach ($o in @(Receive-Job -Job $job)) { if ($null -ne $o.isothrumPid) { $child = $o.isothrumPid } }; Start-Sleep -Milliseconds 100 }\n\
\x20 if ($null -ne $child) { & taskkill.exe /T /F /PID $child | Out-Null; $job = $null } else { Stop-Job -Job $job }\n\
\x20 break\n\
\x20 }\n\
\x20 Start-Sleep -Milliseconds 100\n\
\x20 }\n\
\x20 if ($null -ne $job) { Remove-Job -Job $job -Force }\n";
pub(super) fn encoded(script: &str) -> Vec<String> {
let bytes: Vec<u8> = script.encode_utf16().flat_map(u16::to_le_bytes).collect();
["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-EncodedCommand"]
.into_iter()
.map(str::to_owned)
.chain(std::iter::once(crate::encoding::base64(&bytes)))
.collect()
}
pub(super) fn launcher(script: &str) -> String {
start_elevated(script, false)
}
pub(super) fn waited(script: &str) -> String {
start_elevated(script, true)
}
fn start_elevated(script: &str, wait: bool) -> String {
let args = encoded(script).iter().map(|a| ps_quote(a)).collect::<Vec<_>>().join(",");
let (flags, end) = if wait { ("-Wait -PassThru ", "; exit $p.ExitCode") } else { ("", "") };
format!(
"try {{ $p = Start-Process -FilePath powershell.exe -Verb RunAs -WindowStyle Hidden {flags}-ErrorAction Stop -ArgumentList {args}{end} }} \
catch {{ $e = $_.Exception; while ($e -and -not ($e -is [System.ComponentModel.Win32Exception])) {{ $e = $e.InnerException }}; \
if ($e) {{ exit $e.NativeErrorCode }}; throw }}"
)
}
pub(super) const ERROR_CANCELLED: i32 = 1223;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn quoting_doubles_every_single_quote_powershell_reads() {
assert_eq!(ps_quote("it's"), "'it''s'");
assert_eq!(ps_quote("a\u{2019}b"), "'a\u{2019}\u{2019}b'");
assert_eq!(ps_quote("C:\\Users\\ada"), "'C:\\Users\\ada'");
}
#[test]
fn only_arguments_powershell_passes_intact_are_taken() {
assert!(passes_intact("up") && passes_intact("--provider") && passes_intact("C:\\a b"));
for bad in ["", "a\"b", "--%", "a\nb"] {
assert!(!passes_intact(bad), "{bad:?}");
}
}
#[test]
fn the_broker_follows_its_session_folder() {
let s = broker(Path::new("C:\\Users\\ada\\runs\\r\\elevated\\0f1e"), "0f1e2d", 4242);
for part in [
"$d = 'C:\\Users\\ada\\runs\\r\\elevated\\0f1e'",
"$token = '0f1e2d'",
"req-$n.json",
"env-$n.json",
"out-$n.log",
"exit-$n",
"'ready'",
"cancel-$n",
"'done'",
"taskkill.exe /T /F /PID $main",
"taskkill.exe /T /F /PID $child",
"4242",
] {
assert!(s.contains(part), "{part}: {s}");
}
}
#[test]
fn a_broker_that_stops_other_than_at_done_says_why_and_has_no_deadline() {
let s = broker(Path::new("C:\\e\\t"), "t", 1);
let why = s.find("WriteAllText((Join-Path $d 'ended'), $why)").unwrap();
assert!(why < s.find("taskkill.exe").unwrap(), "{s}");
assert!(s.contains("} finally { Write-Ended 'the elevated broker stopped' }"), "{s}");
assert!(
s.contains("function Write-Ended($why) { if (-not (Test-Path -LiteralPath (Join-Path $d 'done')))"),
"{s}"
);
assert!(!s.contains("AddSeconds") && !s.contains("Get-Date"), "{s}");
}
#[test]
fn the_broker_runs_only_this_sessions_next_request_and_keeps_no_values() {
let s = broker(Path::new("C:\\e\\t"), "t", 1);
assert!(
s.contains("$r = Read-Once $req\n if ($r.session -ne $token -or $r.seq -ne $n) { continue }"),
"{s}"
);
let read = s.find("$e = Read-Once $envFile").unwrap();
assert!(read < s.find("$job = Start-Job -ScriptBlock $run").unwrap(), "{s}");
assert!(s.contains("if ($null -eq $e -or $e.session -ne $token -or $e.seq -ne $n)"));
assert!(s.contains("Remove-Item -LiteralPath $path -Force"));
let job = s.find("$run = {").unwrap();
for set in [
"SetEnvironmentVariable('PATH', $path, 'Process')",
"SetEnvironmentVariable($p.k, $p.v, 'Process')",
] {
assert!(s.find(set).unwrap() > job, "{set}: {s}");
}
assert!(s.contains("Move-Item -LiteralPath \"$exit.tmp\" -Destination $exit -Force"));
}
#[test]
fn the_elevated_programs_load_modules_from_the_systems_folders_only() {
let s = broker(Path::new("C:\\e\\t"), "t", 1);
let pinned = s.find("SetEnvironmentVariable('PSModulePath', $modulePath, 'Process')").unwrap();
assert!(pinned < s.find("$run = {").unwrap(), "the broker's own first: {s}");
assert!(
s.contains(
"$modulePath = @((Join-Path $sys 'WindowsPowerShell\\v1.0\\Modules'), (Join-Path $programFiles 'WindowsPowerShell\\Modules')) -join ';'"
),
"{s}"
);
assert!(!s.contains("Documents") && !s.contains("$env:PSModulePath"), "never the user's: {s}");
let job = s.find("$run = {").unwrap();
assert!(
s.find("SetEnvironmentVariable('PSModulePath', $modules, 'Process')").unwrap() > job,
"the request's too: {s}"
);
assert!(s.contains("-ArgumentList $r, $e, $exe, $path, $modulePath"), "{s}");
}
#[test]
fn a_cancel_ends_its_request_only_and_the_broker_takes_the_next() {
let s = broker(Path::new("C:\\e\\t"), "t", 1);
assert!(
s.contains("if (Test-Path -LiteralPath $cancel) { Remove-Quietly @($req, $envFile, $cancel); $n++; continue }"),
"{s}"
);
let kill = s.find("taskkill.exe /T /F /PID $child").unwrap();
assert!(s.find("[pscustomobject]@{ isothrumPid = $PID }").unwrap() < s.find("& $exe").unwrap());
assert!(s.find("$cancelled = $true").unwrap() < kill, "{s}");
assert!(s.contains("if ($cancelled) {\n Remove-Quietly @($out, $cancel)"), "{s}");
assert!(!s.contains("'cancel'") && !s.contains("the runner cancelled it"), "{s}");
}
#[test]
fn the_broker_runs_only_what_it_found_itself_never_what_a_request_names() {
let s = broker(Path::new("C:\\e\\t"), "t", 1);
assert!(s.contains("& $exe @($r.args)") && !s.contains("& $r.program"), "{s}");
assert!(s.contains("$exe = Resolve-Program $r.program"), "{s}");
for program in protocol::PROGRAMS {
assert!(s.contains(&format!("'{program}' = @(")), "{program}: {s}");
}
assert!(s.contains("[System.Environment]::GetFolderPath('ProgramFiles')") && s.contains("[System.Environment]::SystemDirectory"));
for sid in TRUSTED_OWNERS {
assert!(s.contains(sid), "{sid}");
}
assert!(s.contains("(Test-Path -LiteralPath $p -PathType Leaf) -and (Test-Owned $p)"), "{s}");
let path = s.find("SetEnvironmentVariable('PATH', $path, 'Process')").unwrap();
assert!(path < s.find("foreach ($p in $e.env)").unwrap(), "{s}");
assert!(s.contains("Where-Object { -not (Test-Var $_.k) }"), "{s}");
for refused in protocol::SYSTEM_VARS.iter().chain(&protocol::LOADER_PREFIXES) {
assert!(s.contains(&format!("'{refused}'")), "{refused}");
}
let scrub = s
.find("if (Test-Loader $v.Name) { [System.Environment]::SetEnvironmentVariable($v.Name, $null, 'Process') }")
.unwrap();
assert!(scrub < s.find("$job = Start-Job").unwrap(), "{s}");
let refused = s.find("isothrum: refused: the elevated session runs only").unwrap();
assert!(refused < s.find("$job = Start-Job").unwrap(), "{s}");
}
#[test]
fn the_broker_is_its_snapshot_byte_for_byte() {
let s = broker(Path::new("C:\\Users\\ada\\runs\\r\\elevated\\0f1e"), "0f1e2d", 4242);
let file = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/process/elevated/broker.snapshot.ps1");
if std::env::var_os("ISOTHRUM_WRITE_BROKER").is_some() {
std::fs::write(&file, &s).unwrap();
}
let snapshot = std::fs::read_to_string(&file).unwrap_or_default();
assert!(
s == snapshot,
"the broker's PowerShell changed: review it, then `ISOTHRUM_WRITE_BROKER=1 cargo test -p isothrum-runner broker_is_its_snapshot`"
);
}
#[test]
fn the_script_travels_encoded() {
let args = encoded("echo 1");
assert_eq!(args.last().map(String::as_str), Some("ZQBjAGgAbwAgADEA"));
let start = launcher("echo 1");
assert!(
start
.starts_with("try { $p = Start-Process -FilePath powershell.exe -Verb RunAs -WindowStyle Hidden -ErrorAction Stop -ArgumentList '-NoProfile',"),
"{start}"
);
assert!(
start.contains("[System.ComponentModel.Win32Exception]") && start.contains("exit $e.NativeErrorCode"),
"{start}"
);
assert!(!start.contains("-Wait"), "the broker's start doesn't wait for it: {start}");
}
#[test]
fn a_waited_script_s_exit_code_is_the_command_s() {
let start = waited("exit 3");
assert!(
start.starts_with("try { $p = Start-Process -FilePath powershell.exe -Verb RunAs -WindowStyle Hidden -Wait -PassThru -ErrorAction Stop -ArgumentList '-NoProfile',"),
"{start}"
);
assert!(start.contains("; exit $p.ExitCode }") && start.contains("exit $e.NativeErrorCode"), "{start}");
}
}