Files
akyldash/infra/deploy/test_windows_release_launcher.py
codex 3d7df25d00
CI / tests-and-images (pull_request) Successful in 2m13s
Runner smoke test / runner-smoke (pull_request) Successful in 2s
fix Windows ACL checks to enumerate SIDs directly
2026-11-03 11:52:10 +03:00

416 lines
25 KiB
Python

import os
import re
import shlex
import subprocess
import sys
import tempfile
import unittest
import xml.etree.ElementTree as ET
import zipfile
import io
import hashlib
import json
from pathlib import Path
import prepare_windows_release_bundle as release_bundle
from windows_release_launcher import WINDOWS_PATH, WINDOWS_TOOLS, configure_windows_tool_environment
ROOT = Path(__file__).resolve().parents[2]
class WindowsReleaseBootstrapTests(unittest.TestCase):
def _installer_mutation_mask(self):
script = (ROOT / "infra/deploy/install_windows_release_client.ps1").read_text(encoding="utf-8")
match = re.search(r"\$mutationRights\s*=\s*\[System\.Security\.AccessControl\.FileSystemRights\]\((.*?)\n\)", script, re.S)
self.assertIsNotNone(match, "installer must declare one explicit FileSystemRights mutation mask")
names = set(re.findall(r"FileSystemRights\]::([A-Za-z]+)", match.group(1)))
values = {
"WriteData": 0x0002,
"AppendData": 0x0004,
"WriteExtendedAttributes": 0x0010,
"WriteAttributes": 0x0100,
"DeleteSubdirectoriesAndFiles": 0x0040,
"Delete": 0x10000,
"ChangePermissions": 0x40000,
"TakeOwnership": 0x80000,
}
self.assertEqual(names, set(values), "mutation mask must name each mutating filesystem right explicitly")
mask = 0
for name in names:
mask |= values[name]
return script, mask, values
def test_installer_acl_guard_rejects_each_mutation_right(self):
script, mask, rights = self._installer_mutation_mask()
trusted = {"S-1-5-18", "S-1-5-32-544", "S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464"}
def safe_acl(owner, entries):
if owner not in trusted:
return False
for sid, access_type, granted, inherit_only in entries:
if inherit_only:
continue
if access_type == "Allow" and granted & mask and sid not in trusted:
return False
return True
for name, right in rights.items():
with self.subTest(right=name):
self.assertNotEqual(mask & right, 0)
self.assertFalse(safe_acl("S-1-5-18", [("S-1-5-11", "Allow", right, False)]))
self.assertTrue(safe_acl(
"S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464",
[("S-1-5-32-544", "Allow", mask, False), ("S-1-5-11", "Allow", 0x0001, False)],
))
self.assertTrue(safe_acl("S-1-5-18", [("S-1-5-11", "Allow", mask, True)]))
self.assertIn("([int]$_.FileSystemRights -band [int]$mutationRights)", script)
self.assertIn("([int]$rule.FileSystemRights -band [int]$checkedRights)", script)
self.assertIn("PropagationFlags]::InheritOnly", script)
owner_branch = re.search(
r"if \(\$AllowOwnerWrite\) \{\s*\$approvedOwners = @\((.*?)\)\s*\} else \{\s*\$approvedOwners = @\((.*?)\)\s*\}",
script, re.S,
)
self.assertIsNotNone(owner_branch)
self.assertIn("$identity.User.Value", owner_branch.group(1))
self.assertNotIn("$identity.User.Value", owner_branch.group(2))
self.assertIn("$trustedInstaller", owner_branch.group(2))
def test_parent_path_guard_allows_create_sibling_but_rejects_replacement_rights(self):
script = (ROOT / "infra/deploy/install_windows_release_client.ps1").read_text(encoding="utf-8")
match = re.search(r"\$pathReplacementRights\s*=\s*\[System\.Security\.AccessControl\.FileSystemRights\]\((.*?)\n\)", script, re.S)
self.assertIsNotNone(match)
names = set(re.findall(r"FileSystemRights\]::([A-Za-z]+)", match.group(1)))
values = {"DeleteSubdirectoriesAndFiles": 0x40, "Delete": 0x10000,
"ChangePermissions": 0x40000, "TakeOwnership": 0x80000}
self.assertEqual(names, set(values))
mask = sum(values.values())
self.assertEqual(mask & 0x0004, 0) # AppendData/CreateDirectories on C:\ creates a sibling.
for right in values.values():
self.assertNotEqual(mask & right, 0)
def test_installer_source_is_hash_bound_locked_and_never_runs_git(self):
script = (ROOT / "infra/deploy/install_windows_release_client.ps1").read_text(encoding="utf-8")
self.assertIn("[IO.FileShare]::Read", script)
self.assertIn("[System.IO.Compression.ZipArchive]::new($bundleStream", script)
self.assertIn("Assert-ReceiverBundleEntryMode -Entry $zipEntry", script)
self.assertIn("$entryMode -ne 0x8180", script)
self.assertIn("$bundleHash -ne $ExpectedBundleSha256", script)
self.assertIn("manifest.commit -cne $ExpectedCommit", script)
self.assertIn("Get-GitObjectHex -Kind 'blob' -Bytes $data", script)
self.assertIn("Get-GitObjectHex -Kind 'commit' -Bytes $commitBytes", script)
self.assertIn("Get-GitObjectHex -Kind 'tree' -Bytes $treeData", script)
self.assertIn("Receiver bundle commit tree does not contain the exact source file", script)
self.assertIn("objects/commit.bin", script)
self.assertIn("objects/root-tree.bin", script)
self.assertIn("$treeHeaders[0].Groups[1].Value -cne $seenTrees['']", script)
self.assertIn("$verifiedFiles[$file.path] = $data", script)
self.assertNotIn("(& $git", script)
self.assertNotIn("Start-Process", script)
self.assertNotIn("Copy-Item", script)
self.assertLess(script.index("$bundleHash -ne $ExpectedBundleSha256"),
script.index("[IO.File]::WriteAllBytes($destination, $verifiedFiles[$name])"))
self.assertLess(script.index("foreach ($tool in @($python, $git, $ssh, $powershell)) { Assert-TrustedPath $tool }"),
script.index("$bundleStream = [IO.File]::Open"))
self.assertIn("$approvedOwners = @('S-1-5-18', 'S-1-5-32-544', $trustedInstaller)", script)
self.assertIn("$approvedOwners = @('S-1-5-18', 'S-1-5-32-544', $identity.User.Value)", script)
self.assertIn("$acl.GetOwner([Security.Principal.SecurityIdentifier]).Value", script)
self.assertGreaterEqual(script.count("$acl.GetAccessRules($true, $true, [Security.Principal.SecurityIdentifier])"), 2)
self.assertIn("$rule.IdentityReference.Value", script)
self.assertNotIn(".Translate(", script)
self.assertIn("Assert-TrustedPath $appRoot", script)
self.assertIn("Assert-TrustedPath $programData -ParentsOnly $true", script)
self.assertIn("Set-TrustedCodeOwner $app", script)
self.assertIn("Set-TrustedCodeOwner $destination", script)
for name in release_bundle.FILES:
self.assertIn(f"files/{name}", script)
def test_windows_acl_harness_exercises_unmapped_sid_with_real_installer_guards(self):
harness = (ROOT / "infra/deploy/test_windows_release_bundle_mode.ps1").read_text(encoding="utf-8")
self.assertIn("Assert-ProtectedPath", harness)
self.assertIn("Assert-TrustedPath", harness)
self.assertIn("IdentityNotMappedException", harness)
self.assertIn("$unknownSid", harness)
self.assertIn("$guardAst.Extent.Text", harness)
self.assertIn("Unmapped SID was not rejected by the protected-path guard", harness)
def test_release_bundle_uses_exact_commit_objects_not_dirty_worktree_or_git_hooks(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
repository = root / "repo"
repository.mkdir()
self._git(repository, "init", "-b", "main")
self._git(repository, "config", "user.name", "Fixture")
self._git(repository, "config", "user.email", "fixture@example.invalid")
for name, relative in release_bundle.FILES.items():
path = repository / relative
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(f"committed:{name}\n".encode())
self._git(repository, "add", "--", *release_bundle.FILES.values())
self._git(repository, "commit", "-m", "fixture")
commit = self._git(repository, "rev-parse", "HEAD").decode().strip()
self._git(repository, "switch", "-c", "not-main")
self._git(repository, "commit", "--allow-empty", "-m", "alternate")
alternate = self._git(repository, "rev-parse", "HEAD").decode().strip()
self._git(repository, "switch", "main")
with self.assertRaisesRegex(ValueError, "local main branch tip"):
release_bundle.create_bundle(repository, alternate)
executable_path = repository / release_bundle.FILES["archive.py"]
self._git(repository, "switch", "-c", "executable-mode")
executable_path.chmod(0o755)
self._git(repository, "add", "--", release_bundle.FILES["archive.py"])
self._git(repository, "commit", "-m", "executable source")
executable_commit = self._git(repository, "rev-parse", "HEAD").decode().strip()
self._git(repository, "update-ref", "refs/heads/main", executable_commit)
with self.assertRaisesRegex(ValueError, "not a regular Git blob"):
release_bundle.create_bundle(repository, executable_commit)
self._git(repository, "update-ref", "refs/heads/main", commit)
self._git(repository, "switch", "main")
symlink_path = repository / release_bundle.FILES["archive.py"]
self._git(repository, "switch", "-c", "symlink-source")
symlink_path.unlink()
symlink_path.symlink_to("missing-target")
self._git(repository, "add", "--", release_bundle.FILES["archive.py"])
self._git(repository, "commit", "-m", "symlink source")
symlink_commit = self._git(repository, "rev-parse", "HEAD").decode().strip()
self._git(repository, "update-ref", "refs/heads/main", symlink_commit)
with self.assertRaisesRegex(ValueError, "not a regular Git blob"):
release_bundle.create_bundle(repository, symlink_commit)
self._git(repository, "update-ref", "refs/heads/main", commit)
self._git(repository, "switch", "main")
edited = repository / release_bundle.FILES["receive_external.py"]
edited.write_bytes(b"uncommitted attacker-controlled edit\n")
hook_marker = root / "fsmonitor-ran"
hook = root / "fsmonitor.sh"
hook.write_text(f"#!/bin/sh\nprintf ran > {shlex.quote(str(hook_marker))}\n", encoding="utf-8")
hook.chmod(0o700)
self._git(repository, "config", "core.fsmonitor", str(hook))
payload = release_bundle.create_bundle(repository, commit)
expected_sha256 = hashlib.sha256(payload).hexdigest()
self.assertEqual(set(release_bundle.verify_bundle(payload, commit, expected_sha256)),
set(release_bundle.FILES))
self.assertFalse(hook_marker.exists(), "bundle creation must not invoke config-driven fsmonitor hooks")
with zipfile.ZipFile(io.BytesIO(payload)) as bundle:
manifest = json.loads(bundle.read("manifest.json"))
self.assertEqual(manifest["commit"], commit)
self.assertEqual(set(member.filename for member in bundle.infolist()),
{"manifest.json", *(f"files/{name}" for name in release_bundle.FILES),
*release_bundle.TREE_OBJECTS.values(), "objects/commit.bin"})
self.assertEqual({(member.external_attr >> 16) & 0xFFFF for member in bundle.infolist()},
{0o100600})
self.assertEqual(release_bundle._git_object_sha("commit", bundle.read("objects/commit.bin")), commit)
for tree in manifest["trees"]:
object_name = release_bundle.TREE_OBJECTS[tree["path"]]
self.assertEqual(release_bundle._git_object_sha("tree", bundle.read(object_name)), tree["sha1"])
for name in release_bundle.FILES:
self.assertEqual(bundle.read(f"files/{name}"), f"committed:{name}\n".encode())
for member in ("objects/commit.bin", "objects/infra-tree.bin", "files/receive_external.py"):
with self.subTest(member=member):
corrupted = self._rewrite_bundle(payload, member, lambda content: content[:-1] + bytes([content[-1] ^ 1]))
with self.assertRaises(ValueError):
release_bundle.verify_bundle(corrupted, commit, hashlib.sha256(corrupted).hexdigest())
traversal = self._rewrite_bundle(payload, "../escape", lambda _unused: b"outside")
with self.assertRaisesRegex(ValueError, "unexpected entries"):
release_bundle.verify_bundle(traversal, commit, hashlib.sha256(traversal).hexdigest())
duplicate = self._rewrite_bundle(payload, "manifest.json", lambda content: content, duplicate=True)
with self.assertRaisesRegex(ValueError, "duplicate"):
release_bundle.verify_bundle(duplicate, commit, hashlib.sha256(duplicate).hexdigest())
symlink_entry = self._rewrite_bundle(payload, "files/archive.py", lambda content: content,
external_attr=0o120777 << 16)
with self.assertRaisesRegex(ValueError, "unsafe mode"):
release_bundle.verify_bundle(symlink_entry, commit, hashlib.sha256(symlink_entry).hexdigest())
altered_mode = self._rewrite_bundle(payload, "files/archive.py", lambda content: content,
external_attr=0o100755 << 16)
with self.assertRaisesRegex(ValueError, "unsafe mode"):
release_bundle.verify_bundle(altered_mode, commit, hashlib.sha256(altered_mode).hexdigest())
with self.assertRaisesRegex(ValueError, "invalid size"):
release_bundle.verify_bundle(b"x" * (release_bundle.MAX_BUNDLE_BYTES + 1), commit, "0" * 64)
@staticmethod
def _git(repository, *arguments):
return subprocess.run(["git", "-C", str(repository), *arguments], check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout
@staticmethod
def _rewrite_bundle(payload, target, transform, *, duplicate=False, external_attr=None):
output = io.BytesIO()
with zipfile.ZipFile(io.BytesIO(payload)) as original, zipfile.ZipFile(output, "w") as altered:
for item in original.infolist():
content = original.read(item)
if item.filename == target:
content = transform(content)
info = zipfile.ZipInfo(item.filename, item.date_time)
info.compress_type = zipfile.ZIP_STORED
info.external_attr = external_attr if item.filename == target and external_attr is not None else item.external_attr
altered.writestr(info, content)
if duplicate and item.filename == target:
altered.writestr(info, content)
if target not in {item.filename for item in original.infolist()}:
info = zipfile.ZipInfo(target)
info.compress_type = zipfile.ZIP_STORED
info.external_attr = 0o100600 << 16
altered.writestr(info, transform(b""))
return output.getvalue()
def test_installer_has_no_child_processes_and_checks_tools_before_bundle_read(self):
script = (ROOT / "infra/deploy/install_windows_release_client.ps1").read_text(encoding="utf-8")
trusted_tools = script.index("foreach ($tool in @($python, $git, $ssh, $powershell)) { Assert-TrustedPath $tool }")
bundle_open = script.index("$bundleStream = [IO.File]::Open")
self.assertLess(trusted_tools, bundle_open)
self.assertNotIn("(& $git", script)
self.assertNotIn("Start-Process", script)
self.assertNotIn("Process]::Start", script)
self.assertIn("while ($null -ne $current)", script)
self.assertIn("$current = $current.Parent", script)
self.assertIn("$ownerSid -notin @('S-1-5-18', 'S-1-5-32-544', $trustedInstaller)", script)
def test_windows_bundle_mode_harness_uses_safe_python_and_validates_cleanup_target(self):
harness = (ROOT / "infra/deploy/test_windows_release_bundle_mode.ps1").read_text(encoding="utf-8")
self.assertIn("-B -E -s -S", harness)
self.assertIn("$item.CreationTimeUtc -ne $temporaryRootCreated", harness)
self.assertIn("$ownerSid -cne $temporaryOwnerSid", harness)
self.assertIn("[IO.FileAttributes]::ReparsePoint", harness)
self.assertIn("[IO.Directory]::Delete($resolvedRoot, $false)", harness)
self.assertNotIn("[IO.Directory]::Delete($resolvedRoot, $true)", harness)
self.assertIn("$wrongRootRejected", harness)
self.assertIn("$readonlyObject", harness)
self.assertIn("-bnot [int][IO.FileAttributes]::ReadOnly", harness)
self.assertIn("[IO.File]::Delete($verified.FullName)", harness)
self.assertIn("-not $resolved.StartsWith($ResolvedRoot + '\\'", harness)
self.assertLess(harness.index("if ($alteredMode -ne 0x81A0)"),
harness.index("Assert-ReceiverBundleEntryMode -Entry $alteredEntry"))
def test_windows_tool_environment_discards_path_and_git_overrides(self):
environment = {
"PATH": r"C:\Users\attacker\bin",
"PATHEXT": ".COM;.EXE;.BAT",
"PSModulePath": r"C:\Users\attacker\modules",
"GIT_CONFIG_COUNT": "1",
"GIT_CONFIG_KEY_0": "core.sshCommand",
"GIT_CONFIG_VALUE_0": "attacker.exe",
"SAFE_SETTING": "preserved",
}
calls = []
def which(command):
calls.append(command)
return str(WINDOWS_TOOLS[command])
configure_windows_tool_environment(environ=environment, which=which)
self.assertEqual(environment["PATH"], WINDOWS_PATH)
self.assertEqual(environment["PATHEXT"], ".EXE")
self.assertEqual(environment["SystemRoot"], r"C:\Windows")
self.assertEqual(environment["PSModulePath"], r"C:\Windows\System32\WindowsPowerShell\v1.0\Modules")
self.assertEqual(environment["SAFE_SETTING"], "preserved")
self.assertFalse(any(key.upper().startswith("GIT_") for key in environment))
self.assertEqual(set(calls), set(WINDOWS_TOOLS))
def test_windows_tool_environment_rejects_unexpected_resolution(self):
environment = {"PATH": r"C:\attacker"}
with self.assertRaisesRegex(RuntimeError, "protected path"):
configure_windows_tool_environment(environ=environment, which=lambda _command: r"C:\attacker\ssh.exe")
def test_launcher_ignores_poisoned_working_directory_and_pythonpath(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
app = root / "Program Files" / "Akyldash"
poison = root / "poison"
app.mkdir(parents=True)
poison.mkdir()
launcher = app / "windows_release_launcher.py"
launcher.write_bytes((ROOT / "infra/deploy/windows_release_launcher.py").read_bytes())
(app / "receive_external.py").write_text("def main(): return 0\n", encoding="utf-8")
(app / "archive.py").write_text("ORIGIN = __file__\n", encoding="utf-8")
(app / "external_session.py").write_text("ORIGIN = __file__\n", encoding="utf-8")
marker = root / "sitecustomize-ran"
(poison / "json.py").write_text("raise RuntimeError('poison json imported')\n", encoding="utf-8")
(poison / "sitecustomize.py").write_text(
"import os\nfrom pathlib import Path\nPath(os.environ['POISON_MARKER']).touch()\n",
encoding="utf-8",
)
driver = app / "driver.py"
driver.write_text(
"import sys\n"
"from windows_release_launcher import configure_import_path\n"
"configure_import_path(executable=sys.executable, app_dir=__import__('pathlib').Path(__file__).parent, "
"python_root=__import__('pathlib').Path(sys.prefix), expected_executable=__import__('pathlib').Path(sys.executable), "
"protected_app_root=__import__('pathlib').Path(__file__).parents[1])\n"
"import json, receive_external, archive, external_session\n"
"from pathlib import Path\n"
"assert Path(json.__file__).resolve().is_relative_to(Path(sys.prefix).resolve())\n"
"assert Path(receive_external.__file__).resolve().parent == Path(__file__).parent.resolve()\n"
"assert Path(archive.__file__).resolve().parent == Path(__file__).parent.resolve()\n"
"assert Path(external_session.__file__).resolve().parent == Path(__file__).parent.resolve()\n",
encoding="utf-8",
)
environment = {**os.environ, "PYTHONPATH": str(poison), "POISON_MARKER": str(marker)}
result = subprocess.run(
[sys.executable, "-E", "-s", "-S", str(driver)], cwd=poison,
env=environment, text=True, capture_output=True,
)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertFalse(marker.exists())
def test_disabled_task_uses_pinned_python_and_hardened_launcher(self):
path = ROOT / "infra/deploy/windows-release-trigger.example.xml"
tree = ET.parse(path)
namespace = {"t": "http://schemas.microsoft.com/windows/2004/02/mit/task"}
task = tree.getroot()
self.assertEqual(task.findtext("t:Settings/t:Enabled", namespaces=namespace), "false")
trigger = task.find("t:Triggers/t:CalendarTrigger", namespace)
self.assertEqual(trigger.findtext("t:Enabled", namespaces=namespace), "false")
self.assertEqual(task.findtext("t:Actions/t:Exec/t:Command", namespaces=namespace), r"C:\Python313\python.exe")
arguments = task.findtext("t:Actions/t:Exec/t:Arguments", namespaces=namespace)
self.assertIn("-B -E -s -S", arguments)
self.assertIn("windows_release_launcher.py", arguments)
def test_installer_never_registers_or_enables_task_and_uses_canonical_trigger(self):
script = (ROOT / "infra/deploy/install_windows_release_client.ps1").read_text(encoding="utf-8")
self.assertIn("release-trigger.json", script)
self.assertIn("release-auto-apply.json", script)
self.assertIn('"enabled":false', script)
self.assertIn("C:\\Python313\\python.exe", script)
self.assertIn("Get-AuthenticodeSignature", script)
self.assertIn(".Status -ne 'Valid'", script)
self.assertIn("client-$ExpectedCommit", script)
self.assertIn("--auto-apply-prepared", script)
self.assertIn("Akyldash-ReleaseAutoApply-disabled.xml", script)
self.assertIn("Assert-TrustedPath $tool", script)
self.assertIn(r"$git = 'C:\Program Files\Git\cmd\git.exe'", script)
self.assertIn(r"$ssh = 'C:\Windows\System32\OpenSSH\ssh.exe'", script)
self.assertIn(r"$powershell = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'", script)
self.assertIn("$current = $current.Parent", script)
self.assertIn("PropagationFlags]::InheritOnly", script)
self.assertNotIn("Register-ScheduledTask", script)
self.assertNotIn("Enable-ScheduledTask", script)
if __name__ == "__main__":
if len(sys.argv) == 3 and sys.argv[1] == "--write-bundle-fixture":
output_path = Path(sys.argv[2])
repository = output_path.parent / "fixture-repository"
repository.mkdir()
subprocess.run(["git", "-C", str(repository), "init", "-b", "main"], check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
subprocess.run(["git", "-C", str(repository), "config", "user.name", "Akyldash Test"], check=True)
subprocess.run(["git", "-C", str(repository), "config", "user.email", "test@example.invalid"], check=True)
for name, relative in release_bundle.FILES.items():
path = repository / relative
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(f"fixture:{name}\n".encode("ascii"))
subprocess.run(["git", "-C", str(repository), "add", "--", *release_bundle.FILES.values()], check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
subprocess.run(["git", "-C", str(repository), "commit", "-m", "bundle fixture"], check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
commit = subprocess.run(["git", "-C", str(repository), "rev-parse", "HEAD"], check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True).stdout.strip()
payload = release_bundle.create_bundle(repository, commit)
output_path.write_bytes(payload)
print(json.dumps({"commit": commit, "sha256": hashlib.sha256(payload).hexdigest()}))
else:
unittest.main()