From 41f8aae78136c9611701f6fc5cd0c78724138d7e Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Tue, 8 Sep 2026 15:40:16 -0700 Subject: [PATCH] Refuse a Tree Source That Names the Repository Root However It Is Spelled `spec/validate.py` compared a tree source against `"."` as a raw string, so `"./"` and `".///."` passed the validator and were then refused by `scripts/carry.py`, which compares `PurePosixPath` objects. A manifest author got a green validator and a failing carrier, and the validator is the gate they run first. The comparison is now normalized, in a named helper beside `escapes_repo_root`, so the rule reads the way `carry.py` already resolves it. The helper is scoped to POSIX path rules and says so, since backslash and drive-letter spellings are the neighbouring function's subject rather than this one's. The diagnostic no longer enumerates the shapes it rejects, since the enumeration described the rule this change replaced. The test asserts the helper for five spellings of the root and seven ordinary sources, three of them carrying a redundant separator or a `./` prefix, so an over-eager normalizer that refused `./docs` would fail rather than pass. What it does not assert is the validator's own refusal of a manifest: the tree check sits inside `main()`, which this module has no fixture for, so reverting the call site while leaving the helper intact would leave every test green. That gap is real and is the reason this says what the test covers rather than claiming it covers the defect. Two defects of the same class remain in the same check and are filed rather than fixed here, since each is an escape rather than a root reduction and neither is what #1445 reports: a backslash spelling passes the validator and is refused by the carrier on Windows, and a symlinked source passes `is_dir()` here and is refused by the carrier everywhere. Both exist because this check hand-rolls a weaker subset of this file's own `escapes_repo_root`. Co-Authored-By: Claude Opus 5 (1M context) --- scripts/tests/test_spec_validate.py | 22 ++++++++++++++++++++++ spec/validate.py | 12 ++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/scripts/tests/test_spec_validate.py b/scripts/tests/test_spec_validate.py index 6b9d21371..8f7e826c3 100755 --- a/scripts/tests/test_spec_validate.py +++ b/scripts/tests/test_spec_validate.py @@ -258,5 +258,27 @@ def test_over_the_cap_is_rejected(self) -> None: ) +class TreeSourceRootCase(unittest.TestCase): + """A tree source naming the repository root is refused however it is spelled.""" + + def test_rejects_every_spelling_that_reduces_to_the_root(self) -> None: + for value in (".", "./", ".//.", ".///.", "./."): + with self.subTest(value=value): + self.assertTrue(validate.reduces_to_repo_root(value)) + + def test_accepts_a_source_below_the_root(self) -> None: + for value in ( + ".github/skills", + "spec", + "a/b/c", + ".github", + "./docs", + "docs//sub", + "docs/./sub", + ): + with self.subTest(value=value): + self.assertFalse(validate.reduces_to_repo_root(value)) + + if __name__ == "__main__": unittest.main() diff --git a/spec/validate.py b/spec/validate.py index 078ee2b86..0e79d16a0 100755 --- a/spec/validate.py +++ b/spec/validate.py @@ -127,6 +127,14 @@ def escapes_repo_root(value): ) +def reduces_to_repo_root(value): + """Whether `value` reduces to the repository root under POSIX path rules. + + A raw-string compare against "." accepts "./" and ".///.", which the schema also allows and which `scripts/carry.py` then refuses, so a manifest author gets a green validator and a failing carrier. Backslash and drive-letter spellings are `escapes_repo_root`'s subject and are not read here. + """ + return pathlib.PurePosixPath(value) == pathlib.PurePosixPath(".") + + def canonical_file_in_root(rel_path): """Whether `ROOT / rel_path` resolves, symlinks followed, to an existing file under ROOT. @@ -756,9 +764,9 @@ def check_selector(where, applies_to): continue for field, value in (("source", source), ("target", target)): parts = pathlib.PurePosixPath(value).parts - if value == "." or value.startswith("/") or ".." in parts: + if reduces_to_repo_root(value) or value.startswith("/") or ".." in parts: errors.append( - f"files.json: tree {source} {field} '{value}' must be below the repository root (no leading /, . or ..)" + f"files.json: tree {source} {field} '{value}' must be a path below the repository root" ) if tree.get("fidelity") != "verbatim-tree": errors.append(f"files.json: tree {source} fidelity must be 'verbatim-tree'")