Skip to content

Commit ca15e57

Browse files
shengyfuCopilot
andcommitted
Preflight legacy archive file and directory conflicts
Reject archive entries beneath a file entry, including the generated probe path, before creating the destination so extraction cannot fail partway and leave output that blocks a retry. Co-authored-by: Copilot App <[email protected]>
1 parent 3b98aa7 commit ca15e57

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎scripts/qualification/prepare_legacy.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
BASELINE = "1120aca41dd192ae61bdd996e9886cb354a78a27"
1818
MAX_ENTRIES = 50000
1919
MAX_BYTES = 256 * 1024 * 1024
20+
PROBE_PATH = "tgrep-core/examples/legacy_reader.rs"
2021
PROBE = r"""// Copyright (c) Microsoft Corporation. All rights reserved.
2122
use std::path::PathBuf;
2223
@@ -49,6 +50,7 @@ def prepare(archive, destination):
4950
if len(members) > MAX_ENTRIES or sum(item.file_size for item in members) > MAX_BYTES:
5051
raise ValueError("baseline archive exceeds the extraction bounds")
5152
seen = set()
53+
files = set()
5254
for item in members:
5355
path = PurePosixPath(item.filename)
5456
kind = stat.S_IFMT(item.external_attr >> 16)
@@ -61,9 +63,21 @@ def prepare(archive, destination):
6163
if key in seen:
6264
raise ValueError(f"archive has aliased entries: {item.filename}")
6365
seen.add(key)
66+
if not item.is_dir():
67+
files.add(key)
6468
required = ("cargo.toml", "cargo.lock", "tgrep-core/cargo.toml", "tgrep-cli/cargo.toml")
6569
if not all(name in seen for name in required):
6670
raise ValueError("baseline archive lacks the complete locked workspace")
71+
if PROBE_PATH.casefold() in seen:
72+
raise ValueError(f"baseline archive already has {PROBE_PATH}")
73+
# Reject every file/child conflict, including the generated probe,
74+
# before creating the destination, so extraction cannot fail partway.
75+
for key in seen | {PROBE_PATH.casefold()}:
76+
parent = PurePosixPath(key).parent
77+
while parent.parts:
78+
if str(parent) in files:
79+
raise ValueError(f"archive entry is beneath a file entry: {key}")
80+
parent = parent.parent
6781
destination.mkdir(parents=True, exist_ok=False)
6882
for item in members:
6983
path = destination.joinpath(*PurePosixPath(item.filename).parts)
@@ -73,7 +87,7 @@ def prepare(archive, destination):
7387
path.parent.mkdir(parents=True, exist_ok=True)
7488
with source.open(item) as reader, path.open("xb") as writer:
7589
shutil.copyfileobj(reader, writer, length=1024 * 1024)
76-
example = destination / "tgrep-core" / "examples" / "legacy_reader.rs"
90+
example = destination.joinpath(*PurePosixPath(PROBE_PATH).parts)
7791
example.parent.mkdir(parents=True, exist_ok=True)
7892
with example.open("x", encoding="utf-8", newline="\n") as writer:
7993
writer.write(PROBE)

‎scripts/qualification/test_prepare_legacy.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ def test_rejects_unsafe_or_aliased_members_before_writing_any_source(self):
5151
prepare_legacy.prepare(archive, root / "baseline")
5252
self.assertFalse((root / "baseline").exists())
5353

54+
def test_rejects_file_and_child_conflicts_before_writing_any_source(self):
55+
for members in (
56+
("conflict", "conflict/child"),
57+
("conflict/child", "conflict"),
58+
("Conflict", "conflict/nested/child"),
59+
("tgrep-core/examples",),
60+
("tgrep-core/examples/legacy_reader.rs",),
61+
("TGREP-CORE/Examples/Legacy_Reader.rs",),
62+
):
63+
with self.subTest(members=members), tempfile.TemporaryDirectory() as directory:
64+
root = Path(directory)
65+
archive = self.archive(root, members)
66+
with self.assertRaises(ValueError):
67+
prepare_legacy.prepare(archive, root / "baseline")
68+
self.assertFalse((root / "baseline").exists())
69+
with tempfile.TemporaryDirectory() as directory:
70+
root = Path(directory)
71+
archive = self.archive(root, ("tgrep-core/examples/", "nested/", "nested/child"))
72+
result = prepare_legacy.prepare(archive, root / "baseline")
73+
self.assertTrue(Path(result["probe"]).is_file())
74+
self.assertTrue((root / "baseline" / "nested" / "child").is_file())
75+
5476

5577
if __name__ == "__main__":
5678
unittest.main()

0 commit comments

Comments
 (0)