From dd0ebcb51210e64a0f3b303f90b393d34b6ee06f Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 6 Sep 2026 20:18:57 +0800 Subject: [PATCH 1/2] fix(design-critique): refuse a junctioned dir in the served signature _served_signature digests the files capture-build.mjs will actually serve, and that script's header calls the two enumerations a contract: its readdir Dirent test counts neither a symlinked directory nor a symlinked file, so nothing behind one is ever reachable over the preview server. os.path.islink cannot hold up that contract on Windows. It reports False for a directory junction, so the walk descended into one and signed bytes the server does not serve. Measured: Node reports the same junction as isSymbolicLink=true and indexes only the real file, while the walk's digest moved when a file behind the junction changed. That is the "false mismatches and needless re-captures" the docstring rules out; it also lets an unserved tree raise newest_mtime_ns, which feeds the discover-time mid-capture check, and counts those files against _SIGNATURE_MAX_FILES. Both guards in the function now use platform_compat.is_link_or_junction, which tries os.path.islink first, so symlink behaviour is unchanged. The files-loop guard is behaviour-preserving on its own -- a junction is a directory reparse point and never lands in os.walk's files list -- and is changed so both halves of one contract read through one predicate. The existing symlink test skips on Windows for want of SeCreateSymbolicLinkPrivilege, which is the one platform that has junctions; the new test uses a junction there and a symlink elsewhere. Co-Authored-By: Claude Opus 5 --- .../design_critique/backend/routes.py | 15 ++++- .../tests/test_backend_routes.py | 55 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/kiro_crew/apps/builtins/design_critique/backend/routes.py b/src/kiro_crew/apps/builtins/design_critique/backend/routes.py index 797f3f41ee0d..b029c9f4194b 100644 --- a/src/kiro_crew/apps/builtins/design_critique/backend/routes.py +++ b/src/kiro_crew/apps/builtins/design_critique/backend/routes.py @@ -330,6 +330,16 @@ def _served_signature(build_dir: Path) -> _ServedToken | None: symlinked directory nor a symlinked file as one, and skips dot-entries, so none of those is ever reachable over the preview server — and none is signed here. + Matching that test needs ``is_link_or_junction``, not ``os.path.islink``. Node's + Dirent reports a Windows junction as a symbolic link and therefore serves nothing + behind it, while ``os.path.islink`` calls the same junction a plain directory — + so the bare check descended into it and signed bytes the server will not serve. + Nothing outside ``build_dir`` is disclosed by that (the token is a digest, and it + is never served), but it is exactly the "false mismatches and needless + re-captures" this paragraph rules out, it lets an unserved tree raise + ``newest_mtime_ns`` for the mid-capture check, and its files count against + ``_SIGNATURE_MAX_FILES``. + Returns ``None`` whenever the served set cannot be read in full: missing, unreadable, or larger than ``_SIGNATURE_MAX_FILES``. A caller MUST treat ``None`` as "unknown" rather than "unchanged" and refuse reuse — a token over part of a @@ -353,7 +363,8 @@ def _fail(exc: OSError) -> None: dirs[:] = sorted( d for d in dirs - if not d.startswith(".") and not os.path.islink(os.path.join(root, d)) + if not d.startswith(".") + and not platform_compat.is_link_or_junction(os.path.join(root, d)) ) # Each directory's own mtime feeds ``newest_mtime_ns`` but NOT the digest. # It has to feed the former because a DELETION leaves no file behind to @@ -366,7 +377,7 @@ def _fail(exc: OSError) -> None: rel_root = os.path.relpath(root, build_dir) for name in sorted(files): path = os.path.join(root, name) - if name.startswith(".") or os.path.islink(path): + if name.startswith(".") or platform_compat.is_link_or_junction(path): continue seen += 1 if seen > _SIGNATURE_MAX_FILES: diff --git a/src/kiro_crew/apps/builtins/design_critique/tests/test_backend_routes.py b/src/kiro_crew/apps/builtins/design_critique/tests/test_backend_routes.py index e071dd82e8bd..9b727783822e 100644 --- a/src/kiro_crew/apps/builtins/design_critique/tests/test_backend_routes.py +++ b/src/kiro_crew/apps/builtins/design_critique/tests/test_backend_routes.py @@ -13,6 +13,7 @@ import pytest from aiohttp import web +from kiro_crew import platform_compat from kiro_crew.apps.builtins.design_critique import register_routes from kiro_crew.apps.builtins.design_critique.backend import routes @@ -1185,6 +1186,60 @@ def test_served_signature_ignores_what_the_server_will_not_serve(tmp_path) -> No assert behind is not None and behind.digest == sig.digest +def _make_dir_link(link: Path, target: Path) -> None: + # A directory SYMLINK needs SeCreateSymbolicLinkPrivilege on Windows (WinError + # 1314 unelevated), which is why the test above can only skip there. A junction + # needs no privilege and is the reparse point a real build tree would carry, so + # the Windows half of this contract stays exercised instead of being skipped. + if platform_compat.IS_WINDOWS: + import _winapi + + _winapi.CreateJunction(str(target), str(link)) + return + link.symlink_to(target, target_is_directory=True) + + +def test_served_signature_refuses_a_junctioned_directory(tmp_path) -> None: + # capture-build.mjs's Dirent test reports a junction as a symbolic link, so it + # walks nothing behind one and the preview server serves nothing from it. The + # token has to agree, and `os.path.islink` cannot make it agree: it calls a + # junction a plain directory, so the walk descended and signed bytes that are + # not served. Windows is the only platform with junctions and the only one where + # the symlink test above can be skipped for want of a privilege. + build = tmp_path / "dist" + _build_tree(build) + sig = routes._served_signature(build) + assert sig is not None + + outside = tmp_path / "outside" + outside.mkdir() + (outside / "extra.js").write_text("x", encoding="utf-8") + _make_dir_link(build / "vendor", outside) + + # Guard the guard: on Windows the link must really be the shape `os.path.islink` + # misreads. Without this the test could pass on a plain directory and prove + # nothing about the fix. + if platform_compat.IS_WINDOWS: + assert not os.path.islink(build / "vendor") + assert os.path.isdir(build / "vendor") + assert platform_compat.is_link_or_junction(build / "vendor") + + # Only the DIGEST can hold still across the link's creation: that writes a new + # entry into dist/, and newest_mtime_ns reads directory mtimes on purpose. + linked = routes._served_signature(build) + assert linked is not None and linked.digest == sig.digest + + # A change BEHIND the link moves neither field. Rewriting a file leaves its + # parent directory's mtime alone, so newest_mtime_ns is pinned exactly here — + # it feeds the discover-time mid-capture check, which an unserved tree must not + # be able to trip. + (outside / "extra.js").write_text("changed-and-longer", encoding="utf-8") + _bump(outside / "extra.js") + behind = routes._served_signature(build) + assert behind is not None and behind.digest == sig.digest + assert behind.newest_mtime_ns == linked.newest_mtime_ns + + def test_probe_build_dir_rejects_a_path_outside_the_project(tmp_path) -> None: # A manifest path is only ever stat()ed, but a token taken over an unrelated # tree would stand still and permit reuse of a stale PNG for the whole TTL. From d8a01a3d1b1c44d02b87bcdedf7ae0766e941011 Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 6 Sep 2026 21:24:30 +0800 Subject: [PATCH 2/2] test(design-critique): resolve CreateJunction without a typed attribute Backend Lint & Type Check (3.12) failed on this branch's own test file: typeshed guards _winapi.CreateJunction behind sys.platform == "win32", so the direct reference is an attr-defined error whenever mypy checks this in-package test on Linux. Reproduced locally with `mypy --platform linux` (the default platform on a Windows box hides it) and confirmed gone after. The import is now a guarded module-level one, which is the shape the top-level-imports rule exempts and closes the review's advisory finding on the function-local import at the same time. CreateJunction is resolved with getattr, mirroring platform_compat's own `getattr(os.path, "isjunction", None)`. The Windows path still creates a real JUNCTION and nothing else. It is not routed through a helper that may fall back to os.symlink: symlink creation succeeds on a runner with Developer Mode enabled, and a symlink is the shape os.path.islink already refused, so the fallback would turn the red-before green for the wrong reason. A missing CreateJunction now asserts instead of silently degrading. Red-before re-verified against unpatched origin/main with the new helper in place; the containment test still fails there. Co-Authored-By: Claude Opus 5 --- .../tests/test_backend_routes.py | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/kiro_crew/apps/builtins/design_critique/tests/test_backend_routes.py b/src/kiro_crew/apps/builtins/design_critique/tests/test_backend_routes.py index 9b727783822e..d0dbbbaca620 100644 --- a/src/kiro_crew/apps/builtins/design_critique/tests/test_backend_routes.py +++ b/src/kiro_crew/apps/builtins/design_critique/tests/test_backend_routes.py @@ -17,6 +17,17 @@ from kiro_crew.apps.builtins.design_critique import register_routes from kiro_crew.apps.builtins.design_critique.backend import routes +if platform_compat.IS_WINDOWS: + import _winapi + + # Resolved at runtime, not as a typed attribute: typeshed guards CreateJunction + # behind sys.platform == "win32", so a direct reference is an attr-defined error + # when mypy checks this in-package test file on Linux. Same shape as + # platform_compat's own `getattr(os.path, "isjunction", None)`. + _create_junction = getattr(_winapi, "CreateJunction", None) +else: # pragma: no cover - junctions exist on Windows only + _create_junction = None + def test_register_routes_mounts_the_three_endpoints() -> None: app = web.Application() @@ -1191,10 +1202,14 @@ def _make_dir_link(link: Path, target: Path) -> None: # 1314 unelevated), which is why the test above can only skip there. A junction # needs no privilege and is the reparse point a real build tree would carry, so # the Windows half of this contract stays exercised instead of being skipped. + # + # A junction, never "a junction OR a symlink": os.symlink SUCCEEDS on a runner + # with Developer Mode on, and a symlink is the shape os.path.islink already + # refused. Degrading to it would turn the Windows red-before green for the + # wrong reason. if platform_compat.IS_WINDOWS: - import _winapi - - _winapi.CreateJunction(str(target), str(link)) + assert _create_junction is not None, "_winapi.CreateJunction missing on Windows" + _create_junction(str(target), str(link)) return link.symlink_to(target, target_is_directory=True)