Skip to content

Commit fb7f278

Browse files
committed
Make GGUF TOCTOU tests Windows-safe
Use same-size in-place mutation instead of truncating an open GGUF, and simulate the post-comparison symlink state without renaming a memory-mapped file. Keep explicit assertions that the intended identity and symlink checks execute.\n\nCo-authored-by: Copilot App <[email protected]>\nCopilot-Session: 8be62ed7-f80c-44f1-a286-72d8328e6fa7 Signed-off-by: Justin Chu <[email protected]>
1 parent d0fa48e commit fb7f278

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

‎src/mobius/integrations/gguf/_builder_test.py‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2570,9 +2570,13 @@ def test_save_rehashes_source_immediately_before_publish(
25702570
def mutate_source_before_verification(path, payload):
25712571
nonlocal mutated
25722572
if path.name.startswith(".gguf-reuse.json.") and path.name.endswith(".tmp"):
2573-
source = bytearray(gguf_path.read_bytes())
2574-
source[-1] ^= 1
2575-
gguf_path.write_bytes(source)
2573+
with gguf_path.open("r+b") as stream:
2574+
stream.seek(-1, os.SEEK_END)
2575+
value = stream.read(1)
2576+
stream.seek(-1, os.SEEK_END)
2577+
stream.write(bytes([value[0] ^ 0xFF]))
2578+
stream.flush()
2579+
os.fsync(stream.fileno())
25762580
mutated = True
25772581
return real_write_json(path, payload)
25782582

@@ -2700,24 +2704,33 @@ def test_reuse_verifier_rechecks_symlink_after_final_handle_comparison(
27002704
build_from_gguf(gguf_path, reuse_gguf_weights=True).save(
27012705
str(tmp_path), progress_bar=False
27022706
)
2703-
moved_source = tmp_path / "moved-source.gguf"
27042707
source_matches_path = GGUFModel.source_matches_path
2708+
path_is_symlink = Path.is_symlink
27052709
raced = False
2710+
symlink_recheck_observed = False
27062711

27072712
def replace_after_handle_comparison(model, path=None):
27082713
nonlocal raced
27092714
matches = source_matches_path(model, path)
27102715
if not raced and Path(model._path) == gguf_path:
2711-
gguf_path.replace(moved_source)
2712-
_symlink_or_skip(gguf_path, moved_source)
2716+
assert matches
27132717
raced = True
27142718
return matches
27152719

2720+
def report_post_comparison_symlink(path):
2721+
nonlocal symlink_recheck_observed
2722+
if path == gguf_path and raced:
2723+
symlink_recheck_observed = True
2724+
return True
2725+
return path_is_symlink(path)
2726+
27162727
monkeypatch.setattr(GGUFModel, "source_matches_path", replace_after_handle_comparison)
2728+
monkeypatch.setattr(Path, "is_symlink", report_post_comparison_symlink)
27172729

27182730
with pytest.raises(ValueError, match="reuse manifest was verified"):
27192731
verify_gguf_reuse_manifest(tmp_path)
27202732
assert raced
2733+
assert symlink_recheck_observed
27212734

27222735
def test_reuse_verifier_rechecks_source_after_onnx_validation(
27232736
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch

0 commit comments

Comments
 (0)