# 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. 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. 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", "*.pem", "*.key", "*.vault*", "*vault-pass*", "*/.git/*", "*.env", "*importer_result.json", ] 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!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"] 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.fnmatchcase("/".join(folded), pat) for pat in FORBIDDEN): return [f"{path}: matches a forbidden pattern"] return [] def problems(names): found = [] for name in sorted(names): found += path_problems(name) return found def member_problems(members): """Check tarfile members: only regular files and directories, and only fine paths. Links, devices and fifos are rejected whatever their name: a symlink such as roles/a/link -> /etc/passwd would ship the target's content on the consumer's side (or make an extractor write outside its directory). """ found = [] for member in sorted(members, key=lambda m: m.name): if not (member.isreg() or member.isdir()): kind = ("symlink" if member.issym() else "hardlink" if member.islnk() else "device" if member.isdev() else "fifo" if member.isfifo() else "special file") found.append(f"{member.name}: {kind} is not allowed in the Collection archive") found += path_problems(member.name) 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 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 print(f"{argv[1]}: {len(members)} entries, all allowed") return 0 if __name__ == "__main__": sys.exit(main(sys.argv))