fix(ci): tighten the Collection archive check and make its guards testable
The forbidden names widen to anything containing "vault", SSH keys, .envrc and .env* files. Names that Windows or macOS extraction would read differently are rejected (trailing space or dot, reserved device names, "::", 8.3 aliases of .git, non-ASCII spaces, surrogate/private-use/unassigned characters, components over 255 bytes), and the .git test compares after NFKC and case folding. Duplicates are compared case-insensitively, a name that is both a file and a directory is rejected, and only one slash is stripped. A unit test builds a real tarball with ansible-galaxy, another asserts the directories build.yml prunes are in galaxy.yml build_ignore, lint.yml runs tests/scripts, and the Build workflow fails unless exactly one tarball matches. The docstring states that file symlinks are invisible to the archive check. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
b5751e14d6
commit
cab2187c89
5 files changed
+215
-20
No files matched your search
+65
-13
@@ -11,20 +11,32 @@ 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;
|
||||
* a path that is absolute or holds an empty, '.', '..' or '.git' component (compared
|
||||
after NFKC and case folding, so `.GIT`, a full-width `.git` and the 8.3 alias `GIT~1`
|
||||
are caught), a backslash, or a control, zero-width, non-ASCII-space, surrogate,
|
||||
private-use or unassigned character;
|
||||
* a name that extraction on Windows or macOS reads differently: a component that ends in
|
||||
a space or a dot, a reserved device name (CON, NUL, COM1, ... with or without an
|
||||
extension), '::' (an NTFS stream), or a component longer than 255 bytes;
|
||||
* two members with the same name, compared case-insensitively (a case-insensitive file
|
||||
system would merge them), or a name that is both a file and a directory (COPYING and
|
||||
COPYING/x);
|
||||
* 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.
|
||||
ansible-galaxy dereferences FILE symlinks while building, so a symlink to a file reaches
|
||||
the archive as a regular file with the target's content and this check cannot see it. The
|
||||
`Refuse symlinks in the source tree` step of .gitea/workflows/build.yml is the only guard
|
||||
against that. Its `find` prunes ./.venv and ./.git only because both are in galaxy.yml
|
||||
`build_ignore` (or excluded by ansible-galaxy itself, as .git is): the two lists must stay
|
||||
in step, and tests/scripts/test_check_tarball.py fails when a pruned directory is not
|
||||
ignored by the build.
|
||||
|
||||
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 re
|
||||
import sys
|
||||
import tarfile
|
||||
import unicodedata
|
||||
@@ -40,23 +52,48 @@ REQUIRED = {
|
||||
# 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",
|
||||
"*.pem", "*.key", "*vault*", "*/.git/*", "*.env", "*importer_result.json",
|
||||
"*id_rsa*", "*id_ed25519*", "*.envrc", "*/.ssh", "*/.ssh/*", "*.env.*", "*/.env*", ".env*",
|
||||
]
|
||||
# Names Windows treats as devices, with or without an extension (and with trailing spaces).
|
||||
WINDOWS_RESERVED = {"con", "prn", "aux", "nul", *(f"com{i}" for i in range(1, 10)), *(f"lpt{i}" for i in range(1, 10))}
|
||||
MAX_COMPONENT_BYTES = 255
|
||||
|
||||
|
||||
def normalize(name):
|
||||
"""The member name without a leading './' and trailing '/'."""
|
||||
path = name.rstrip("/")
|
||||
"""The member name without one leading './' and one trailing '/' (so 'a//' stays odd and is rejected)."""
|
||||
path = name[:-1] if name.endswith("/") else name
|
||||
return path[2:] if path.startswith("./") else path
|
||||
|
||||
|
||||
def fold(text):
|
||||
"""The comparison form of a name: NFKC and case folded."""
|
||||
return unicodedata.normalize("NFKC", text).casefold()
|
||||
|
||||
|
||||
def component_problems(path, part):
|
||||
"""Names that mean something else once extracted on Windows or macOS."""
|
||||
if part.endswith((" ", ".")):
|
||||
return [f"{path}: component {part!r} ends in a space or a dot (dropped on Windows)"]
|
||||
if fold(part).split(".")[0].rstrip(" ") in WINDOWS_RESERVED:
|
||||
return [f"{path}: component {part!r} is a reserved Windows device name"]
|
||||
if "::" in part:
|
||||
return [f"{path}: component {part!r} holds '::' (an NTFS stream)"]
|
||||
if re.fullmatch(r"git~[0-9]+", fold(part)):
|
||||
return [f"{path}: component {part!r} is an 8.3 alias of '.git'"]
|
||||
if len(part.encode("utf-8", "surrogateescape")) > MAX_COMPONENT_BYTES:
|
||||
return [f"{path}: a path component is longer than {MAX_COMPONENT_BYTES} bytes"]
|
||||
return []
|
||||
|
||||
|
||||
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")]
|
||||
odd = [c for c in name if unicodedata.category(c) in ("Cc", "Cf", "Cs", "Co", "Cn", "Zl", "Zp")
|
||||
or (unicodedata.category(c) == "Zs" and c != " ")]
|
||||
if odd:
|
||||
return [f"{name!r}: control or invisible character in the path"]
|
||||
path = normalize(name)
|
||||
@@ -65,9 +102,13 @@ def path_problems(name):
|
||||
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]
|
||||
folded = [fold(part) for part in parts]
|
||||
if ".git" in folded:
|
||||
return [f"{path}: '.git' path component"]
|
||||
for part in parts:
|
||||
found = component_problems(path, part)
|
||||
if found:
|
||||
return found
|
||||
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):
|
||||
@@ -105,8 +146,19 @@ def archive_problems(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")
|
||||
keys = [fold(n) for n in names]
|
||||
for key in sorted({k for k in keys if keys.count(k) > 1}):
|
||||
same = sorted({n for n, k in zip(names, keys) if k == key})
|
||||
found.append(f"{' / '.join(same)}: appears more than once in the archive (names are compared "
|
||||
"ignoring case)")
|
||||
file_names = {fold(n): n for n, m in zip(names, members) if not m.isdir()}
|
||||
for name in sorted(set(names)):
|
||||
parts = name.split("/")
|
||||
for i in range(1, len(parts)):
|
||||
prefix = "/".join(parts[:i])
|
||||
message = f"{prefix}: is both a file and a directory in the archive"
|
||||
if fold(prefix) in file_names and message not in found:
|
||||
found.append(message)
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user