Skip to content

Commit cbe7a8b

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

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎mcp_server/core/backup_manifest.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ def resources_for(backend: str) -> tuple[ResourceSpec, ...]:
117117
return tuple(spec for spec in RESOURCES if backend in spec.backends)
118118

119119

120+
def _is_canonical(path: str) -> bool:
121+
"""A relative POSIX path with no empty, ``.`` or ``..`` component and no ``\\``.
122+
123+
The prefix check below is only containment for such a path: a restore that
124+
joins ``methodology/wiki/../../x`` to the root would write outside it.
125+
"""
126+
parts = path.split("/")
127+
return "\\" not in path and not any(p in ("", ".", "..") for p in parts)
128+
129+
120130
def _checked_files(
121131
spec: ResourceSpec, records: Sequence[FileRecord]
122132
) -> list[dict[str, Any]]:
@@ -125,6 +135,8 @@ def _checked_files(
125135
raise ManifestError(f"{spec.path} is a file resource: one record, its own path")
126136
files = []
127137
for path, sha256, size in sorted(records):
138+
if not _is_canonical(path):
139+
raise ManifestError(f"{path!r} is not a canonical relative path")
128140
if spec.kind == "dir" and not path.startswith(spec.path + "/"):
129141
raise ManifestError(f"{path} is not inside {spec.path}")
130142
if not _SHA256_HEX.fullmatch(sha256) or size < 0:

‎tests_py/core/test_backup_manifest.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,29 @@ def test_size_zero_is_valid():
215215
"sqlite", _sqlite_scan(wiki=[("methodology/wiki/a.md", DIGEST, 0)]), "t"
216216
)
217217
assert entry["entries"][2]["files"][0]["size"] == 0
218+
219+
220+
@pytest.mark.parametrize(
221+
"path",
222+
[
223+
"methodology/wiki/../../outside.txt",
224+
"methodology/wiki/./a.md",
225+
"methodology/wiki//a.md",
226+
"methodology/wiki/a.md/",
227+
"/methodology/wiki/a.md",
228+
"methodology/wiki/sub\\..\\..\\x",
229+
"methodology/wiki/..",
230+
],
231+
)
232+
def test_a_path_that_is_not_canonical_is_refused(path):
233+
with pytest.raises(bm.ManifestError, match=r"is not a canonical relative path$"):
234+
bm.build_manifest("sqlite", _sqlite_scan(wiki=[(path, DIGEST, 1)]), "t")
235+
236+
237+
def test_dots_inside_a_name_are_canonical():
238+
records = [
239+
("methodology/wiki/..a.md", DIGEST, 1),
240+
("methodology/wiki/a..b/.c", DIGEST, 1),
241+
]
242+
entry = bm.build_manifest("sqlite", _sqlite_scan(wiki=records), "t")["entries"][2]
243+
assert [f["path"] for f in entry["files"]] == [r[0] for r in records]

‎tests_py/core/test_backup_manifest_parse.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,14 @@ def test_parse_refuses_a_required_entry_marked_absent():
154154
def test_parse_accepts_absent_and_present_entries_built_by_the_table():
155155
text = _entry_text("wiki")
156156
assert bm.parse_manifest(text) == json.loads(text)
157+
158+
159+
def test_parse_refuses_a_parent_traversal_in_a_file_path():
160+
text = _entry_text(
161+
"wiki",
162+
files=[
163+
{"path": "methodology/wiki/../../outside.txt", "sha256": DIGEST, "size": 1}
164+
],
165+
)
166+
with pytest.raises(bm.ManifestError, match=r"is not a canonical relative path$"):
167+
bm.parse_manifest(text)

0 commit comments

Comments
 (0)