Skip to content

Commit f038891

Browse files
committed
Reject canonical removal junctions and read UTF-8 manifests
Reject junctions alongside symlinks in the existing path-component preflight and revalidation. Decode removal manifests explicitly as UTF-8. Add regressions for whole-batch refusal, revalidation, and Unicode dry-run/apply/idempotence. Assisted-by: OpenAI Codex:model version unavailable
1 parent df0dec5 commit f038891

2 files changed

Lines changed: 94 additions & 3 deletions

File tree

‎scripts/update_lib/cmd_migrate.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,8 @@ def relative(name):
10121012
return pathlib.Path(path)
10131013

10141014
def no_symlinks(path):
1015-
if any(p.is_symlink() for p in (path, *path.parents)):
1016-
raise ValueError(f"Symlink in removal path: {path}")
1015+
if any(p.is_symlink() or p.is_junction() for p in (path, *path.parents)):
1016+
raise ValueError(f"Symlink or junction in removal path: {path}")
10171017

10181018
def git(*args):
10191019
result = subprocess.run(
@@ -1031,7 +1031,9 @@ def git(*args):
10311031
raise ValueError("Cannot verify pinned canonical source")
10321032
return result.stdout.decode().strip()
10331033

1034-
document = json.loads(manifest_path.read_text(), object_pairs_hook=unique_object)
1034+
document = json.loads(
1035+
manifest_path.read_text(encoding="utf-8"), object_pairs_hook=unique_object
1036+
)
10351037
if not isinstance(document, dict) or set(document) != {"source", "removals"}:
10361038
raise ValueError("Expected source and removals metadata")
10371039
source, removals = document["source"], document["removals"]

‎scripts/update_lib/tests/test_migrate.py‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,95 @@ def test_dry_run_explicit_scope_receipt_and_idempotence(self):
13691369
self.assertEqual((self.target / "a.py").exists(), dry_run)
13701370
self.assertEqual((self.target / "unselected").read_text(), "keep")
13711371

1372+
def test_junction_components_reject_the_whole_batch(self):
1373+
before = {
1374+
name: (self.target / name).read_bytes()
1375+
for name in self.document["removals"]
1376+
}
1377+
for junction in (
1378+
self.source,
1379+
self.source.parent,
1380+
self.source / "data",
1381+
self.target,
1382+
self.target.parent,
1383+
self.target / "data",
1384+
):
1385+
for dry_run in (True, False):
1386+
with self.subTest(junction=junction, dry_run=dry_run):
1387+
self.assertFalse(junction.is_symlink())
1388+
with (
1389+
patch.object(
1390+
Path,
1391+
"is_junction",
1392+
autospec=True,
1393+
side_effect=lambda path: path == junction,
1394+
),
1395+
self.assertRaisesRegex(ValueError, "[Jj]unction"),
1396+
):
1397+
self.run_removal(dry_run)
1398+
self.assertEqual(
1399+
{name: (self.target / name).read_bytes() for name in before},
1400+
before,
1401+
)
1402+
1403+
def test_junction_revalidation_prevents_partial_removal(self):
1404+
for junction in (self.target / "data", self.source / "data"):
1405+
checks = 0
1406+
1407+
def becomes_junction(path):
1408+
nonlocal checks
1409+
if path == junction:
1410+
checks += 1
1411+
return checks > 1
1412+
return False
1413+
1414+
with self.subTest(junction=junction):
1415+
with (
1416+
patch.object(
1417+
Path, "is_junction", autospec=True, side_effect=becomes_junction
1418+
),
1419+
self.assertRaisesRegex(ValueError, "[Jj]unction"),
1420+
):
1421+
self.run_removal()
1422+
self.assertEqual(checks, 2)
1423+
self.assertEqual((self.target / "a.py").read_bytes(), b"old\n")
1424+
self.assertEqual((self.target / "data/z.bin").read_bytes(), b"\x00\xff")
1425+
1426+
def test_utf8_manifest_ignores_non_utf8_default_encoding(self):
1427+
name = "caf\u00e9.py"
1428+
target = self.target / name
1429+
target.write_bytes(b"old\n")
1430+
self.document["removals"][name] = dict(self.document["removals"]["a.py"])
1431+
self.manifest.write_bytes(
1432+
json.dumps(self.document, ensure_ascii=False).encode("utf-8")
1433+
)
1434+
with (
1435+
patch(
1436+
"pathlib.io.text_encoding",
1437+
side_effect=lambda encoding, *args: (
1438+
"latin-1" if encoding is None else encoding
1439+
),
1440+
),
1441+
contextlib.chdir(self.target.parents[2]),
1442+
contextlib.redirect_stdout(io.StringIO()),
1443+
):
1444+
self.assertNotEqual(
1445+
self.manifest.read_text(), self.manifest.read_text(encoding="utf-8")
1446+
)
1447+
for dry_run, expected in (
1448+
(True, "would_remove"),
1449+
(False, "removed"),
1450+
(False, "already_absent"),
1451+
):
1452+
receipt = remove_canonical_files(
1453+
self.source, self.manifest, dry_run=dry_run
1454+
)
1455+
self.assertEqual(
1456+
set(receipt["removals"]), set(self.document["removals"])
1457+
)
1458+
self.assertEqual(receipt["removals"][name]["status"], expected)
1459+
self.assertEqual(target.exists(), dry_run)
1460+
13721461
def test_cli_dry_run_never_migrates_or_changes_destination_bytes(self):
13731462
def snapshot():
13741463
return {

0 commit comments

Comments
 (0)