Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions scripts/tests/test_spec_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
12 changes: 10 additions & 2 deletions spec/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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'")
Expand Down