From b117ed823a258cc7d760f1f9614df39899bb988d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Billeter?= Date: Fri, 2 Oct 2026 10:30:39 +0200 Subject: [PATCH] utils.py: Fix `_ensure_real_directory()` `os.path.split()` splits the path into a `(head, tail)` pair, not into a list of all path segments. Use `pathlib` to iterate over all path segments and also reject absolute paths. `_ensure_real_directory()` is only called from `_process_list()`, though, where it's separately called for each level in the hierarchy. Due to this, each path segment was already checked and thus, the previous bug didn't cause any issues. --- src/buildstream/utils.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py index 8673813dc..59d97e956 100644 --- a/src/buildstream/utils.py +++ b/src/buildstream/utils.py @@ -34,7 +34,7 @@ import threading import itertools from contextlib import contextmanager -from pathlib import Path +from pathlib import Path, PurePath from typing import Callable, IO, Iterable, Iterator, Optional, Tuple, Union, Pattern from google.protobuf import timestamp_pb2 @@ -1064,7 +1064,12 @@ def _copy_directories(srcdir, destdir, target): # def _ensure_real_directory(root, path): destpath = root - for name in os.path.split(path): + purepath = PurePath(path) + if purepath.is_absolute(): + raise UtilError(f"Expected relative path, got absolute path: {path}") + for name in purepath.parts: + if name == "..": + raise UtilError(f"`..` not allowed in relative path: {path}") destpath = os.path.join(destpath, name) try: deststat = os.lstat(destpath)