From edce0b577c6e675ef563682178e383c39e301426 Mon Sep 17 00:00:00 2001 From: Simon Jagoe Date: Sun, 3 Dec 2023 17:23:13 +0200 Subject: [PATCH] Use pathlib for path resolution and relative path check --- haas/plugins/discoverer.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/haas/plugins/discoverer.py b/haas/plugins/discoverer.py index a8d2ab0..392a895 100644 --- a/haas/plugins/discoverer.py +++ b/haas/plugins/discoverer.py @@ -8,6 +8,7 @@ from fnmatch import fnmatch from importlib import import_module +from pathlib import Path import logging import os import sys @@ -41,9 +42,11 @@ def test_error(self): def get_relpath(top_level_directory, fullpath): - normalized = os.path.normpath(fullpath) - relpath = os.path.relpath(normalized, top_level_directory) - if os.path.isabs(relpath) or relpath.startswith('..'): + top_level = Path(top_level_directory).resolve() + normalized = Path(fullpath).resolve() + try: + relpath = str(normalized.relative_to(top_level)) + except ValueError: raise ValueError('Path not within project: {0}'.format(fullpath)) return relpath