From a1c85b03824bf5b01868ad08c866c79501db5356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Billeter?= Date: Tue, 21 Apr 2020 13:59:48 +0200 Subject: _casbaseddirectory.py: Validate path components This catches incorrect use of the `Directory` API. --- src/buildstream/storage/_casbaseddirectory.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/buildstream/storage/_casbaseddirectory.py b/src/buildstream/storage/_casbaseddirectory.py index 0a5a557b4..484dc0789 100644 --- a/src/buildstream/storage/_casbaseddirectory.py +++ b/src/buildstream/storage/_casbaseddirectory.py @@ -329,6 +329,8 @@ class CasBasedDirectory(Directory): if not path: continue + self.__validate_path_component(path) + entry = current_dir.index.get(path) if entry: @@ -726,6 +728,7 @@ class CasBasedDirectory(Directory): @contextmanager def open_file(self, *path: str, mode: str = "r"): subdir = self.descend(*path[:-1]) + self.__validate_path_component(path[-1]) entry = subdir.index.get(path[-1]) if entry and entry.type != _FileType.REGULAR_FILE: @@ -825,6 +828,7 @@ class CasBasedDirectory(Directory): def exists(self, *path, follow_symlinks=False): try: subdir = self.descend(*path[:-1], follow_symlinks=follow_symlinks) + self.__validate_path_component(path[-1]) target = subdir.index.get(path[-1]) if target is not None: if follow_symlinks and target.type == _FileType.SYMLINK: @@ -864,3 +868,7 @@ class CasBasedDirectory(Directory): subdir.__add_files_to_result(path_prefix=relative_pathname, result=result) else: result.files_written.append(relative_pathname) + + def __validate_path_component(self, path): + if "/" in path: + raise VirtualDirectoryError("Invalid path component: '{}'".format(path)) -- cgit v1.2.1