Skip to content

Commit

Permalink
Use pathlib for path resolution and relative path check
Browse files Browse the repository at this point in the history
  • Loading branch information
sjagoe committed Dec 3, 2023
1 parent 1a9df30 commit edce0b5
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions haas/plugins/discoverer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from fnmatch import fnmatch
from importlib import import_module
from pathlib import Path
import logging
import os
import sys
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit edce0b5

Please sign in to comment.