diff --git a/surfaces/blender/brokkr_bridge/scene_actions.py b/surfaces/blender/brokkr_bridge/scene_actions.py index fcaa7fe..0847c52 100644 --- a/surfaces/blender/brokkr_bridge/scene_actions.py +++ b/surfaces/blender/brokkr_bridge/scene_actions.py @@ -84,7 +84,12 @@ def resolve_inside(root: str, candidate: str, label: str, *, new_file: bool = Fa current = base for part in parts: step = current / part - if os.path.lexists(step) and not _same(step.resolve(), current / part): + try: + linked = os.path.lexists(step) and not _same(step.resolve(), current / part) + except OSError: + # Windows refuses to traverse some junctions outright (WinError 448, untrusted mount point). + linked = True + if linked: raise ActionRefused(f"{label} path goes through a link at {part!r}.") current = step try: diff --git a/surfaces/blender/docs/install.md b/surfaces/blender/docs/install.md index 0b764c9..a61be34 100644 --- a/surfaces/blender/docs/install.md +++ b/surfaces/blender/docs/install.md @@ -133,3 +133,23 @@ The scene actions were run against Blender 5.2.3 LTS headless with `surfaces/blender/tests/live/headless_smoke.py` (usage in its docstring). One real failure it found: Blender's `engine` enum is dynamic, so `bl_rna` lists only `BLENDER_EEVEE`; `renderStill` now asks Blender by assignment instead. + +### Path rules and the residual race + +Both directories take plain relative names only, judged as text the same on every OS: +no drive (`C:`, `C:foo`), UNC or `\\?\` form, leading separator, `..`, NUL, `:` anywhere +(also NTFS streams), trailing dot or space, or Windows device name. No existing component +may be a symlink or junction (Windows' "untrusted mount point" error on a junction counts +as one). `renderStill` refuses any output name that already exists, including symlinks +(even dangling) and hardlinks. `importGltf` parses the `.gltf` JSON (or a `.glb`'s first +chunk) and refuses every `buffers[].uri` / `images[].uri` that is not `data:` or a +relative path inside the import directory; `.gltf` files from the same folder tree work. + +**Residual TOCTOU.** The checks run in the add-on, and the path is re-checked (resolved +name, plus size and modification time of the file and its references) immediately before +use, but Blender's own importer and renderer then open the files themselves. Someone who +can write to the import or output directory in that last gap can still swap a file or +link in. The add-on cannot close that window from Python: make both directories writable +only by trusted users, and treat the checks as protection against a malicious *intent*, +not against a hostile local writer. A same-size, same-mtime content swap is also not +detected by the re-check. diff --git a/surfaces/blender/tests/live/headless_smoke.py b/surfaces/blender/tests/live/headless_smoke.py index 62ea574..085df0c 100644 --- a/surfaces/blender/tests/live/headless_smoke.py +++ b/surfaces/blender/tests/live/headless_smoke.py @@ -5,9 +5,11 @@ Writes a tiny .glb into /in, runs every scene action against real bpy, and prints RESULT lines (accepted/failed) plus the state each action should have produced. -Last run: Blender 5.2.3 LTS on Windows, all actions accepted, both escapes refused. +Windows only: also makes real mklink /J junctions (one pointing outside the root, one inside it) and +expects importGltf and renderStill to refuse paths through them, plus an existing-output refusal. +Last run: see the PR. """ -import json, struct, sys, types, os, traceback +import json, struct, sys, types, os, subprocess, shutil, traceback from pathlib import Path import bpy @@ -71,5 +73,27 @@ print("FILE pano", (ROOT / "out" / "pano.png").exists(), (ROOT / "out" / "pano.p run("renderStill_workbench", action="renderStill", outputPath="wb/shot.jpg", engine="BLENDER_WORKBENCH", resolutionX=64, resolutionY=64) print("FILE wb", (ROOT / "out" / "wb" / "shot.jpg").exists()) run("renderStill_escape", action="renderStill", outputPath="../evil.png") + +def junction(link, target): + link = Path(link) + if link.exists() or os.path.lexists(link): + os.rmdir(link) + subprocess.run(["cmd", "/c", "mklink", "/J", str(link), str(target)], check=True, capture_output=True) + +if os.name == "nt": + elsewhere = ROOT.parent / (ROOT.name + "-elsewhere"); elsewhere.mkdir(exist_ok=True) + shutil.copy(ROOT / "in" / "tiny.glb", elsewhere / "tiny.glb") + (ROOT / "in" / "realdir").mkdir(exist_ok=True); shutil.copy(ROOT / "in" / "tiny.glb", ROOT / "in" / "realdir" / "tiny.glb") + junction(ROOT / "in" / "jout", elsewhere) # junction leaving the root + junction(ROOT / "in" / "jin", ROOT / "in" / "realdir") # junction staying inside the root + junction(ROOT / "out" / "jout", elsewhere) + for label, cmd in (("junction_import_outside", dict(action="importGltf", filepath="jout/tiny.glb")), + ("junction_import_inside", dict(action="importGltf", filepath="jin/tiny.glb")), + ("junction_render_outside", dict(action="renderStill", outputPath="jout/shot.png", engine="BLENDER_WORKBENCH", resolutionX=8, resolutionY=8))): + r = run(label, **cmd) + print("JUNCTION_RULE", label, r["status"] == "failed" and "link" in r.get("message", "")) + print("JUNCTION_LEAK", (elsewhere / "shot.png").exists()) + run("render_existing_output", action="renderStill", outputPath="wb/shot.jpg", engine="BLENDER_WORKBENCH", resolutionX=8, resolutionY=8) + os.rmdir(ROOT / "in" / "jout"); os.rmdir(ROOT / "in" / "jin"); os.rmdir(ROOT / "out" / "jout") print("RESTORED", sc.render.engine, sc.render.resolution_x, sc.render.resolution_y, sc.camera and sc.camera.name) print("SUMMARY", results) diff --git a/surfaces/blender/tests/test_scene_actions.py b/surfaces/blender/tests/test_scene_actions.py index 493a273..1a8a4b0 100644 --- a/surfaces/blender/tests/test_scene_actions.py +++ b/surfaces/blender/tests/test_scene_actions.py @@ -391,6 +391,29 @@ class PathTests(unittest.TestCase): with self.assertRaises(sa.ActionRefused, msg=name): sa.resolve_inside(tmp, name, "output") + def test_check_relative_itself_refuses_dotdot_and_nul(self): + # Each rule on its own: resolve_inside's link walk also trips on '..', so only a + # direct call proves the text rule. + for name in ("a/../b", "..", "a\\..\\b", "a\x00b"): + with self.assertRaises(sa.ActionRefused, msg=repr(name)): + sa.check_relative(name, "output") + self.assertEqual(sa.check_relative("a\\b/./c", "output"), ["a", "b", "c"]) + + def test_a_component_the_os_refuses_to_resolve_counts_as_a_link(self): + # Windows raises WinError 448 resolving some junctions; that is a link refusal, not a crash. + with tempfile.TemporaryDirectory() as tmp: + (Path(tmp) / "j").mkdir() + real = Path.resolve + + def resolve(self, *args, **kwargs): + if self.name == "j": + raise OSError(448, "untrusted mount point") + return real(self, *args, **kwargs) + + with mock.patch.object(Path, "resolve", resolve): + with self.assertRaisesRegex(sa.ActionRefused, "link"): + sa.resolve_inside(tmp, "j/x.png", "output") + if __name__ == "__main__": unittest.main()