From 3ff5d52c221ba82d92dfe7b5c247829367f103d3 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Mon, 14 Oct 2024 09:23:42 +0200 Subject: [PATCH 1/3] app: Re-use constant WEST_DIR Use the literal constant WEST_DIR instead of using the same string value scattered around the project. Signed-off-by: Pieter De Gendt --- src/west/app/project.py | 4 ++-- src/west/configuration.py | 4 ++-- src/west/util.py | 6 ++++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/west/app/project.py b/src/west/app/project.py index f3caf66c..9a3005c5 100644 --- a/src/west/app/project.py +++ b/src/west/app/project.py @@ -222,7 +222,7 @@ def do_run(self, args, _): This forces west to search for a workspace there. Try unsetting ZEPHYR_BASE and re-running this command.''') else: - west_dir = Path(self.topdir) / '.west' + west_dir = Path(self.topdir) / WEST_DIR msg = ("\n Hint: if you do not want a workspace there, \n" " remove this directory and re-run this command:\n\n" f" {west_dir}") @@ -2094,7 +2094,7 @@ def projects_unknown(manifest, projects): # # Top-level west directory, containing west itself and the manifest. -WEST_DIR = '.west' +WEST_DIR = util.WEST_DIR # Default manifest repository URL. MANIFEST_URL_DEFAULT = 'https://github.com/zephyrproject-rtos/zephyr' diff --git a/src/west/configuration.py b/src/west/configuration.py index ae90f5a4..251171de 100644 --- a/src/west/configuration.py +++ b/src/west/configuration.py @@ -43,7 +43,7 @@ from typing import Any, Dict, Iterable, List, Optional, Tuple, TYPE_CHECKING import warnings -from west.util import west_dir, WestNotFound, PathType +from west.util import WEST_DIR, west_dir, WestNotFound, PathType class MalformedConfig(Exception): '''The west configuration was malformed. @@ -616,7 +616,7 @@ def _location(cfg: ConfigFile, topdir: Optional[PathType] = None, return env['WEST_CONFIG_LOCAL'] if topdir: - return os.fspath(Path(topdir) / '.west' / 'config') + return os.fspath(Path(topdir) / WEST_DIR / 'config') if find_local: # Might raise WestNotFound! diff --git a/src/west/util.py b/src/west/util.py index fd577d3e..3a225e94 100644 --- a/src/west/util.py +++ b/src/west/util.py @@ -20,6 +20,8 @@ # otherwise, but it doesn't seem worth it. PathType = Union[str, os.PathLike] +WEST_DIR = '.west' + def escapes_directory(path: PathType, directory: PathType) -> bool: '''Returns True if `path` escapes parent directory `directory`. @@ -58,7 +60,7 @@ def west_dir(start: Optional[PathType] = None) -> str: Raises WestNotFound if no .west directory is found. ''' - return os.path.join(west_topdir(start), '.west') + return os.path.join(west_topdir(start), WEST_DIR) def west_topdir(start: Optional[PathType] = None, fall_back: bool = True) -> str: @@ -69,7 +71,7 @@ def west_topdir(start: Optional[PathType] = None, cur_dir = pathlib.Path(start or os.getcwd()) while True: - if (cur_dir / '.west').is_dir(): + if (cur_dir / WEST_DIR).is_dir(): return os.fspath(cur_dir) parent_dir = cur_dir.parent From 5a23158db3df72413f850e3fd31ba93a9f227c49 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Mon, 14 Oct 2024 09:25:02 +0200 Subject: [PATCH 2/3] manifest: Do not allow projects inside the west directory It's not allowed to have projects inside the .west directory, add a check when parsing the manifest. Signed-off-by: Pieter De Gendt --- src/west/manifest.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/west/manifest.py b/src/west/manifest.py index 42caeb15..6b3b7d6e 100644 --- a/src/west/manifest.py +++ b/src/west/manifest.py @@ -2435,6 +2435,10 @@ def _load_project(self, pd: Dict, url_bases: Dict[str, str], f'normalizes to {ret_norm}, which escapes ' f'the workspace topdir') + if Path(ret_norm).parts[0] == util.WEST_DIR: + self._malformed(f'project "{name}" path {ret.path} ' + f'is in the {util.WEST_DIR} directory') + return ret def _validate_project_groups(self, project_name: str, From d57283e69b8b3f585f74889b89f8143edab02f39 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Mon, 14 Oct 2024 09:28:39 +0200 Subject: [PATCH 3/3] tests: Add test cases for invalid west projects manifests Add tests for cases where projects are inside the .west directory. Signed-off-by: Pieter De Gendt --- tests/manifests/invalid_project_in_west_dir.yml | 7 +++++++ tests/manifests/invalid_project_in_west_reldir.yml | 7 +++++++ tests/manifests/invalid_project_in_west_relsubdir.yml | 7 +++++++ tests/manifests/invalid_project_in_west_subdir.yml | 7 +++++++ 4 files changed, 28 insertions(+) create mode 100644 tests/manifests/invalid_project_in_west_dir.yml create mode 100644 tests/manifests/invalid_project_in_west_reldir.yml create mode 100644 tests/manifests/invalid_project_in_west_relsubdir.yml create mode 100644 tests/manifests/invalid_project_in_west_subdir.yml diff --git a/tests/manifests/invalid_project_in_west_dir.yml b/tests/manifests/invalid_project_in_west_dir.yml new file mode 100644 index 00000000..8fd4091a --- /dev/null +++ b/tests/manifests/invalid_project_in_west_dir.yml @@ -0,0 +1,7 @@ +# Project paths can't be .west. + +manifest: + projects: + - name: nope + url: ignore-me + path: .west diff --git a/tests/manifests/invalid_project_in_west_reldir.yml b/tests/manifests/invalid_project_in_west_reldir.yml new file mode 100644 index 00000000..aae9889d --- /dev/null +++ b/tests/manifests/invalid_project_in_west_reldir.yml @@ -0,0 +1,7 @@ +# Project paths can't be .west after normalization. + +manifest: + projects: + - name: nope + url: ignore-me + path: foo/../.west diff --git a/tests/manifests/invalid_project_in_west_relsubdir.yml b/tests/manifests/invalid_project_in_west_relsubdir.yml new file mode 100644 index 00000000..289dab36 --- /dev/null +++ b/tests/manifests/invalid_project_in_west_relsubdir.yml @@ -0,0 +1,7 @@ +# Project paths can't be in .west normalized. + +manifest: + projects: + - name: nope + url: ignore-me + path: foo/../.west/some/path diff --git a/tests/manifests/invalid_project_in_west_subdir.yml b/tests/manifests/invalid_project_in_west_subdir.yml new file mode 100644 index 00000000..bc37a236 --- /dev/null +++ b/tests/manifests/invalid_project_in_west_subdir.yml @@ -0,0 +1,7 @@ +# Project paths can't be in .west/. + +manifest: + projects: + - name: nope + url: ignore-me + path: .west/some/path