fix(build): reject links, special files and unsafe paths in the Collection archive
check_tarball.py now iterates the tar members instead of their names. Only regular files and directories are allowed, so a symlink or hardlink cannot smuggle a file in under an allowed directory. Absolute paths and paths with an empty, '.', '..' or '.git' component are rejected, so roles/../.venv/x no longer passes the top-level allow list. Tests build crafted tarballs and run the script on them. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
e3acab9ca2
commit
580df0b892
3 files changed
+118
-15
No files matched your search
+46
-14
@@ -8,7 +8,8 @@
|
||||
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. When the Collection gains a plugin type or a
|
||||
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.
|
||||
"""
|
||||
import fnmatch
|
||||
@@ -26,19 +27,50 @@ FORBIDDEN = [
|
||||
]
|
||||
|
||||
|
||||
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 []
|
||||
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:
|
||||
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):
|
||||
return [f"{path}: matches a forbidden pattern"]
|
||||
return []
|
||||
|
||||
|
||||
def problems(names):
|
||||
found = []
|
||||
for name in sorted(names):
|
||||
path = name.rstrip("/")
|
||||
if path.startswith("./"):
|
||||
path = path[2:]
|
||||
if not path:
|
||||
continue
|
||||
top = path.split("/", 1)[0]
|
||||
if top not in ALLOWED_TOP:
|
||||
found.append(f"{path}: top-level entry '{top}' is not allowed in the Collection archive")
|
||||
elif any(fnmatch.fnmatch(path, pat) for pat in FORBIDDEN):
|
||||
found.append(f"{path}: matches a forbidden pattern")
|
||||
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
|
||||
|
||||
|
||||
@@ -47,12 +79,12 @@ def main(argv):
|
||||
sys.stderr.write(__doc__)
|
||||
return 2
|
||||
with tarfile.open(argv[1]) as tar:
|
||||
names = tar.getnames()
|
||||
bad = problems(names)
|
||||
members = tar.getmembers()
|
||||
bad = member_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(names)} entries, all allowed")
|
||||
print(f"{argv[1]}: {len(members)} entries, all allowed")
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user