fix(build): harden the Collection archive check and refuse source symlinks
check_tarball.py now compares path components case-insensitively (.GIT, Molecule), rejects backslashes and control or zero-width characters, duplicate members, an empty archive and a missing REQUIRED file, and reports an unreadable archive as one line. Drop the shebang and the executable bit and pass check= to subprocess.run so ansible-test sanity passes. ansible-galaxy dereferences symlinks, which the archive check cannot see, so the Build workflow fails when the source tree holds a symlink. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
580df0b892
commit
b5751e14d6
4 files changed
+143
-23
No files matched your search
Executable → Regular
+59
-17
@@ -1,25 +1,42 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-FileCopyrightText: Defame1297 and contributors
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""Assert that a built Collection tarball holds only files meant for consumers.
|
||||
|
||||
scripts/check_tarball.py dist/mortis-kamino-X.Y.Z.tar.gz
|
||||
python3 scripts/check_tarball.py dist/mortis-kamino-X.Y.Z.tar.gz
|
||||
|
||||
galaxy.yml `build_ignore` is a deny list: a new file at the repository root (a tool's
|
||||
cache, an editor's directory, an agent's settings) ships silently. This check is the
|
||||
allow list. A top-level entry that is not in ALLOWED_TOP, or a path under an entry that
|
||||
matches FORBIDDEN, fails the build, and so does a member that is not a regular file or
|
||||
directory, or whose path is absolute or holds an empty, '.', '..' or '.git' component. When the Collection gains a plugin type or a
|
||||
directory consumers should get, add it to ALLOWED_TOP in the same change.
|
||||
allow list. It fails the build for:
|
||||
|
||||
* a top-level entry that is not in ALLOWED_TOP, or a path under an entry that matches
|
||||
FORBIDDEN (both compared case-insensitively, so `.GIT` and `Molecule` do not slip by);
|
||||
* a member that is not a regular file or directory;
|
||||
* a path that is absolute or holds an empty, '.', '..' or '.git' component, a backslash,
|
||||
or a control or zero-width character;
|
||||
* two members with the same name;
|
||||
* an empty archive, or one that lacks a file of REQUIRED;
|
||||
* an archive that cannot be read (one line, exit 1).
|
||||
|
||||
ansible-galaxy dereferences symlinks while building, so a symlink in the source tree
|
||||
reaches the archive as a regular file and is invisible here; the Build workflow checks
|
||||
the source tree for symlinks separately.
|
||||
|
||||
When the Collection gains a plugin type or a directory consumers should get, add it to
|
||||
ALLOWED_TOP in the same change.
|
||||
"""
|
||||
import fnmatch
|
||||
import sys
|
||||
import tarfile
|
||||
import unicodedata
|
||||
|
||||
ALLOWED_TOP = {
|
||||
"COPYING", "FILES.json", "LICENSES", "MANIFEST.json", "NOTICE", "README.md",
|
||||
"REUSE.toml", "changelogs", "docs", "meta", "plugins", "roles", "tests",
|
||||
}
|
||||
# Every Collection archive built by ansible-galaxy holds these.
|
||||
REQUIRED = {
|
||||
"MANIFEST.json", "FILES.json", "COPYING", "README.md", "meta/runtime.yml", "changelogs/changelog.yaml",
|
||||
}
|
||||
# Checked on every member, whatever its top-level directory.
|
||||
FORBIDDEN = [
|
||||
"*/molecule/*", "*/molecule", "tests/scripts*", "*.pyc", "*__pycache__*", "*.tar.gz",
|
||||
@@ -27,25 +44,33 @@ FORBIDDEN = [
|
||||
]
|
||||
|
||||
|
||||
def normalize(name):
|
||||
"""The member name without a leading './' and trailing '/'."""
|
||||
path = name.rstrip("/")
|
||||
return path[2:] if path.startswith("./") else path
|
||||
|
||||
|
||||
def path_problems(name):
|
||||
"""Problems of one member path, as a list of messages (empty when the path is fine)."""
|
||||
if name.startswith("/"):
|
||||
return [f"{name}: absolute path"]
|
||||
path = name.rstrip("/")
|
||||
if path.startswith("./"):
|
||||
path = path[2:]
|
||||
if not path:
|
||||
return []
|
||||
return [f"{name!r}: absolute path"]
|
||||
if "\\" in name:
|
||||
return [f"{name!r}: backslash in the path"]
|
||||
odd = [c for c in name if unicodedata.category(c) in ("Cc", "Cf", "Zl", "Zp")]
|
||||
if odd:
|
||||
return [f"{name!r}: control or invisible character in the path"]
|
||||
path = normalize(name)
|
||||
parts = path.split("/")
|
||||
if "" in parts:
|
||||
return [f"{path}: empty path component ('//')"]
|
||||
if "." in parts or ".." in parts:
|
||||
return [f"{path}: '.' or '..' path component"]
|
||||
if ".git" in parts:
|
||||
folded = [part.casefold() for part in parts]
|
||||
if ".git" in folded:
|
||||
return [f"{path}: '.git' path component"]
|
||||
if parts[0] not in ALLOWED_TOP:
|
||||
return [f"{path}: top-level entry '{parts[0]}' is not allowed in the Collection archive"]
|
||||
if any(fnmatch.fnmatch(path, pat) for pat in FORBIDDEN):
|
||||
if any(fnmatch.fnmatchcase("/".join(folded), pat) for pat in FORBIDDEN):
|
||||
return [f"{path}: matches a forbidden pattern"]
|
||||
return []
|
||||
|
||||
@@ -74,13 +99,30 @@ def member_problems(members):
|
||||
return found
|
||||
|
||||
|
||||
def archive_problems(members):
|
||||
"""All problems of an archive: members, duplicates, emptiness and the REQUIRED files."""
|
||||
if not members:
|
||||
return ["the archive is empty"]
|
||||
found = member_problems(members)
|
||||
names = [normalize(m.name) for m in members]
|
||||
for name in sorted({n for n in names if names.count(n) > 1}):
|
||||
found.append(f"{name}: appears more than once in the archive")
|
||||
present = {normalize(m.name) for m in members if m.isreg()}
|
||||
found += [f"{name}: required file is missing" for name in sorted(REQUIRED - present)]
|
||||
return found
|
||||
|
||||
|
||||
def main(argv):
|
||||
if len(argv) != 2:
|
||||
sys.stderr.write(__doc__)
|
||||
return 2
|
||||
with tarfile.open(argv[1]) as tar:
|
||||
members = tar.getmembers()
|
||||
bad = member_problems(members)
|
||||
try:
|
||||
with tarfile.open(argv[1]) as tar:
|
||||
members = tar.getmembers()
|
||||
except (tarfile.TarError, OSError, EOFError) as exc:
|
||||
sys.stderr.write(f"cannot read {argv[1]}: {' '.join(str(exc).split())}\n")
|
||||
return 1
|
||||
bad = archive_problems(members)
|
||||
if bad:
|
||||
sys.stderr.write("Unexpected files in the Collection archive:\n " + "\n ".join(bad) + "\n")
|
||||
return 1
|
||||
|
||||
Reference in new issue
Block a user