Skip to content

Commit 2d8e1b8

Browse files
committed
fixup! feat(backup): a versioned manifest digests every class A file of an install (#731, slice 2)
1 parent 5cc4524 commit 2d8e1b8

5 files changed

Lines changed: 239 additions & 52 deletions

File tree

‎mcp_server/core/backup_manifest.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,25 @@ def parse_manifest(text: str) -> dict[str, Any]:
203203
raise ManifestError(
204204
f"entries {names} do not match the {manifest['backend']} table"
205205
)
206+
for index, spec in enumerate(specs): # same length: the names matched above
207+
_check_entry(spec, entries[index], manifest["backend"])
206208
return manifest
209+
210+
211+
def _is_record(record: tuple[Any, Any, Any]) -> bool:
212+
path, sha256, size = record
213+
return isinstance(path, str) and isinstance(sha256, str) and type(size) is int
214+
215+
216+
def _check_entry(spec: ResourceSpec, entry: dict[str, Any], backend: str) -> None:
217+
"""Refuse an entry that is not exactly what its spec and its own files build."""
218+
files, present = entry.get("files"), entry.get("present")
219+
if not isinstance(present, bool) or not isinstance(files, list):
220+
raise ManifestError(f"entry {spec.name}: present or files malformed")
221+
if not all(isinstance(f, dict) for f in files):
222+
raise ManifestError(f"entry {spec.name}: present or files malformed")
223+
records = [(f.get("path"), f.get("sha256"), f.get("size")) for f in files]
224+
if not all(_is_record(record) for record in records):
225+
raise ManifestError(f"entry {spec.name}: present or files malformed")
226+
if _entry(spec, records if present else None, backend) != entry:
227+
raise ManifestError(f"entry {spec.name} differs from the table or its files")

‎mcp_server/infrastructure/backup_scan.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import hashlib
1616
import os
1717
import stat
18-
from pathlib import Path
18+
from pathlib import Path, PurePosixPath
1919

2020

2121
class BackupScanError(OSError):
@@ -50,13 +50,29 @@ def _walk(root: Path, directory: Path) -> list[tuple[str, str, int]]:
5050
return records
5151

5252

53+
def _check_ancestors(root: Path, relative: str) -> None:
54+
"""Refuse a directory between ``root`` and the resource that is not a real one.
55+
56+
A linked ``methodology`` would otherwise digest a store outside the root
57+
under a name inside it. The walk stops at the first one that is absent.
58+
"""
59+
current = root
60+
for part in PurePosixPath(relative).parts[:-1]:
61+
current = current / part
62+
if not current.exists() and not current.is_symlink():
63+
return
64+
if not stat.S_ISDIR(_checked_mode(current)):
65+
raise BackupScanError(f"{current} is expected to be a dir")
66+
67+
5368
def scan_resource(
5469
root: Path, relative: str, kind: str
5570
) -> list[tuple[str, str, int]] | None:
5671
"""The records of ``root/relative``; ``None`` when it does not exist.
5772
5873
``kind`` is ``"file"`` or ``"dir"``; a resource of the other kind is refused.
5974
"""
75+
_check_ancestors(root, relative)
6076
target = root / relative
6177
if not target.exists() and not target.is_symlink():
6278
return None

‎tests_py/core/test_backup_manifest.py‎

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
import json
65

76
import pytest
87

@@ -216,53 +215,3 @@ def test_size_zero_is_valid():
216215
"sqlite", _sqlite_scan(wiki=[("methodology/wiki/a.md", DIGEST, 0)]), "t"
217216
)
218217
assert entry["entries"][2]["files"][0]["size"] == 0
219-
220-
221-
# --- canonical text and parse ----------------------------------------------------
222-
223-
224-
def test_to_json_is_canonical_and_parses_back_unchanged():
225-
manifest = bm.build_manifest("sqlite", _sqlite_scan(), "t")
226-
text = bm.to_json(manifest)
227-
assert text.endswith("}\n") and not text.endswith("\n\n")
228-
assert text == json.dumps(manifest, sort_keys=True, indent=2) + "\n"
229-
assert bm.to_json({"b": "é", "a": 1}) == '{\n "a": 1,\n "b": "\\u00e9"\n}\n'
230-
assert bm.parse_manifest(text) == manifest
231-
232-
233-
def _text(**override) -> str:
234-
manifest = bm.build_manifest("sqlite", _sqlite_scan(), "t")
235-
manifest.update(override)
236-
return json.dumps(manifest)
237-
238-
239-
@pytest.mark.parametrize(
240-
("text", "message"),
241-
[
242-
("{", r"^not JSON: "),
243-
("[]", r"^not a cortex-backup-manifest document$"),
244-
(
245-
_text(format="cortex-memory-export"),
246-
r"^not a cortex-backup-manifest document$",
247-
),
248-
(_text(version=2), r"^manifest version 2, this Cortex reads 1$"),
249-
(_text(version="1"), r"^manifest version '1', this Cortex reads 1$"),
250-
(_text(backend="auto"), r"^unknown backend 'auto'"),
251-
(_text(entries={}), r"^entries must be a list of objects$"),
252-
(_text(entries=["wiki"]), r"^entries must be a list of objects$"),
253-
(
254-
_text(entries=[{"name": "wiki"}]),
255-
r"^entries \['wiki'\] do not match the sqlite table$",
256-
),
257-
],
258-
)
259-
def test_parse_refuses_another_format_version_or_table(text, message):
260-
with pytest.raises(bm.ManifestError, match=message):
261-
bm.parse_manifest(text)
262-
263-
264-
def test_parse_refuses_a_manifest_without_entries():
265-
manifest = json.loads(_text())
266-
del manifest["entries"]
267-
with pytest.raises(bm.ManifestError, match=r"^entries must be a list of objects$"):
268-
bm.parse_manifest(json.dumps(manifest))
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
"""Canonical text and parse of the backup manifest (issue #731, slice 2)."""
2+
3+
from __future__ import annotations
4+
5+
import json
6+
7+
import pytest
8+
9+
from mcp_server.core import backup_manifest as bm
10+
11+
DIGEST = "a" * 64
12+
OTHER = "0123456789abcdef" * 4
13+
14+
15+
def _scan(backend: str, **present) -> dict:
16+
scanned = {spec.name: None for spec in bm.resources_for(backend)}
17+
scanned.update(present)
18+
return scanned
19+
20+
21+
def _sqlite_scan(**present) -> dict:
22+
return _scan(
23+
"sqlite", sqlite_store=[("methodology/memory.db", DIGEST, 10)], **present
24+
)
25+
26+
27+
# --- canonical text and parse ----------------------------------------------------
28+
29+
30+
def test_to_json_is_canonical_and_parses_back_unchanged():
31+
manifest = bm.build_manifest("sqlite", _sqlite_scan(), "t")
32+
text = bm.to_json(manifest)
33+
assert text.endswith("}\n") and not text.endswith("\n\n")
34+
assert text == json.dumps(manifest, sort_keys=True, indent=2) + "\n"
35+
assert bm.to_json({"b": "é", "a": 1}) == '{\n "a": 1,\n "b": "\\u00e9"\n}\n'
36+
assert bm.parse_manifest(text) == manifest
37+
38+
39+
def _text(**override) -> str:
40+
manifest = bm.build_manifest("sqlite", _sqlite_scan(), "t")
41+
manifest.update(override)
42+
return json.dumps(manifest)
43+
44+
45+
@pytest.mark.parametrize(
46+
("text", "message"),
47+
[
48+
("{", r"^not JSON: "),
49+
("[]", r"^not a cortex-backup-manifest document$"),
50+
(
51+
_text(format="cortex-memory-export"),
52+
r"^not a cortex-backup-manifest document$",
53+
),
54+
(_text(version=2), r"^manifest version 2, this Cortex reads 1$"),
55+
(_text(version="1"), r"^manifest version '1', this Cortex reads 1$"),
56+
(_text(backend="auto"), r"^unknown backend 'auto'"),
57+
(_text(entries={}), r"^entries must be a list of objects$"),
58+
(_text(entries=["wiki"]), r"^entries must be a list of objects$"),
59+
(
60+
_text(entries=[{"name": "wiki"}]),
61+
r"^entries \['wiki'\] do not match the sqlite table$",
62+
),
63+
],
64+
)
65+
def test_parse_refuses_another_format_version_or_table(text, message):
66+
with pytest.raises(bm.ManifestError, match=message):
67+
bm.parse_manifest(text)
68+
69+
70+
def test_parse_refuses_a_manifest_without_entries():
71+
manifest = json.loads(_text())
72+
del manifest["entries"]
73+
with pytest.raises(bm.ManifestError, match=r"^entries must be a list of objects$"):
74+
bm.parse_manifest(json.dumps(manifest))
75+
76+
77+
# --- parse checks every entry against its spec and its files (review of #960) ---
78+
79+
80+
def _entry_text(name: str, **override) -> str:
81+
manifest = bm.build_manifest(
82+
"sqlite",
83+
_sqlite_scan(wiki=[("methodology/wiki/a.md", DIGEST, 1)]),
84+
"t",
85+
)
86+
entry = next(e for e in manifest["entries"] if e["name"] == name)
87+
entry.update(override)
88+
return json.dumps(manifest)
89+
90+
91+
@pytest.mark.parametrize(
92+
("name", "override"),
93+
[
94+
("sqlite_store", {"path": "/outside/other.db"}),
95+
("sqlite_store", {"kind": "dir"}),
96+
("sqlite_store", {"secret": True}),
97+
("sqlite_store", {"extra": 1}),
98+
("wiki", {"present": False}),
99+
("backend_marker", {"files": [{"path": "x", "sha256": DIGEST, "size": 1}]}),
100+
],
101+
)
102+
def test_parse_refuses_an_entry_that_differs_from_its_spec_or_files(name, override):
103+
with pytest.raises(bm.ManifestError, match=rf"^entry {name} "):
104+
bm.parse_manifest(_entry_text(name, **override))
105+
106+
107+
def test_parse_refuses_unsorted_files():
108+
manifest = bm.build_manifest(
109+
"sqlite",
110+
_sqlite_scan(
111+
wiki=[
112+
("methodology/wiki/a.md", DIGEST, 1),
113+
("methodology/wiki/b.md", DIGEST, 1),
114+
]
115+
),
116+
"t",
117+
)
118+
manifest["entries"][2]["files"].reverse()
119+
with pytest.raises(
120+
bm.ManifestError, match=r"^entry wiki differs from the table or its files$"
121+
):
122+
bm.parse_manifest(json.dumps(manifest))
123+
124+
125+
@pytest.mark.parametrize(
126+
"override",
127+
[
128+
{"present": "yes"},
129+
{"present": None},
130+
{"files": {}},
131+
{"files": ["methodology/wiki/a.md"]},
132+
{"files": [{"path": 1, "sha256": DIGEST, "size": 1}]},
133+
{"files": [{"path": "methodology/wiki/a.md", "sha256": 1, "size": 1}]},
134+
{"files": [{"path": "methodology/wiki/a.md", "sha256": DIGEST, "size": 1.0}]},
135+
{"files": [{"path": "methodology/wiki/a.md", "sha256": DIGEST, "size": True}]},
136+
{"files": [{"path": "methodology/wiki/a.md", "sha256": DIGEST}]},
137+
],
138+
)
139+
def test_parse_refuses_malformed_presence_or_files(override):
140+
with pytest.raises(
141+
bm.ManifestError, match=r"^entry wiki: present or files malformed$"
142+
):
143+
bm.parse_manifest(_entry_text("wiki", **override))
144+
145+
146+
def test_parse_refuses_a_required_entry_marked_absent():
147+
text = _entry_text("sqlite_store", present=False, files=[])
148+
with pytest.raises(
149+
bm.ManifestError, match=r"memory\.db is required for a sqlite backup"
150+
):
151+
bm.parse_manifest(text)
152+
153+
154+
def test_parse_accepts_absent_and_present_entries_built_by_the_table():
155+
text = _entry_text("wiki")
156+
assert bm.parse_manifest(text) == json.loads(text)

‎tests_py/infrastructure/test_backup_scan.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,48 @@ def test_a_special_file_inside_a_directory_is_refused(tmp_path):
105105
scan_resource(tmp_path, "spool", "dir")
106106
finally:
107107
server.close()
108+
109+
110+
# --- ancestors of a resource (review of #960) ---
111+
112+
113+
@needs_symlink
114+
def test_a_symlinked_ancestor_is_refused(tmp_path, tmp_path_factory):
115+
outside = tmp_path_factory.mktemp("outside")
116+
(outside / "memory.db").write_bytes(b"not this install's store")
117+
os.symlink(outside, tmp_path / "methodology")
118+
with pytest.raises(BackupScanError, match=r"methodology is a symbolic link"):
119+
scan_resource(tmp_path, "methodology/memory.db", "file")
120+
121+
122+
@needs_symlink
123+
def test_a_dangling_symlinked_ancestor_is_refused_not_absent(tmp_path):
124+
os.symlink(tmp_path / "missing", tmp_path / "methodology")
125+
with pytest.raises(BackupScanError, match=r"methodology is a symbolic link"):
126+
scan_resource(tmp_path, "methodology/wiki", "dir")
127+
128+
129+
def test_a_deep_ancestor_that_is_a_file_is_refused(tmp_path):
130+
(tmp_path / "methodology").mkdir()
131+
(tmp_path / "methodology" / "managed-pg").write_bytes(b"x")
132+
with pytest.raises(BackupScanError, match=r"managed-pg is expected to be a dir$"):
133+
scan_resource(tmp_path, "methodology/managed-pg/password", "file")
134+
135+
136+
def test_an_absent_ancestor_makes_the_resource_absent(tmp_path):
137+
(tmp_path / "methodology").mkdir()
138+
assert scan_resource(tmp_path, "methodology/managed-pg/password", "file") is None
139+
assert scan_resource(tmp_path, ".capture-worker/spool", "dir") is None
140+
141+
142+
def test_two_absent_ancestors_make_the_resource_absent(tmp_path):
143+
assert scan_resource(tmp_path, "methodology/managed-pg/password", "file") is None
144+
assert scan_resource(tmp_path, "methodology/managed-pg/cluster", "dir") is None
145+
146+
147+
def test_a_resource_below_real_ancestors_is_read(tmp_path):
148+
(tmp_path / "methodology" / "managed-pg").mkdir(parents=True)
149+
(tmp_path / "methodology" / "managed-pg" / "password").write_bytes(b"pw")
150+
assert scan_resource(tmp_path, "methodology/managed-pg/password", "file") == [
151+
("methodology/managed-pg/password", _sha(b"pw"), 2)
152+
]

0 commit comments

Comments
 (0)