Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ”¨ Remove access denied error #1797

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions src/anomalib/data/utils/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,6 @@ def validate_path(path: str | Path, base_dir: str | Path | None = None, should_e
>>> validate_path("./datasets/MVTec/bottle/train/good/000.png", base_dir="./datasets/MVTec")
PosixPath('/abs/path/to/anomalib/datasets/MVTec/bottle/train/good/000.png')

Path to an outside file/directory should raise ValueError:

>>> validate_path("/usr/local/lib")
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 18, in validate_path
ValueError: Access denied: Path is outside the allowed directory

Path to a non-existing file should raise FileNotFoundError:

>>> validate_path("/path/to/unexisting/file")
Traceback (most recent call last):
File "<string>", line 1, in <module>
Expand Down Expand Up @@ -211,11 +201,6 @@ def validate_path(path: str | Path, base_dir: str | Path | None = None, should_e
path = Path(path).resolve()
base_dir = Path(base_dir).resolve() if base_dir else Path.home()

# Check if the resolved path is within the base directory
if not str(path).startswith(str(base_dir)):
msg = "Access denied: Path is outside the allowed directory"
raise ValueError(msg)

# In case path ``should_exist``, the path is valid, and should be
# checked for read and execute permissions.
if should_exist:
Expand Down
12 changes: 0 additions & 12 deletions tests/unit/data/utils/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ def test_existing_directory_within_base_dir(self, dataset_path: Path) -> None:
validated_path = validate_path(directory_path, base_dir=dataset_path)
assert validated_path == directory_path.resolve()

def test_symlinks_to_a_path_outside_base_dir_raises_error(self, dataset_path: Path) -> None:
"""Test ``validate_path`` raises ValueError for a symlink to a path outside the base directory."""
symlink_path = dataset_path / "mvtec/dummy/train/good/symlink"
symlink_path.symlink_to("/usr/local/lib")
with pytest.raises(ValueError, match=r"Access denied: Path is outside the allowed directory"):
validate_path(symlink_path)

def test_nonexistent_file(self, dataset_path: Path) -> None:
"""Test ``validate_path`` raises FileNotFoundError for a nonexistent file."""
with pytest.raises(FileNotFoundError):
Expand All @@ -58,11 +51,6 @@ def test_nonexistent_directory(self, dataset_path: Path) -> None:
with pytest.raises(FileNotFoundError):
validate_path(dataset_path / "nonexistent/directory")

def test_outside_base_dir(self) -> None:
"""Test ``validate_path`` raises ValueError for a path outside the base directory."""
with pytest.raises(ValueError, match=r"Access denied: Path is outside the allowed directory"):
validate_path("/usr/local/lib")

def test_no_read_permission(self) -> None:
"""Test ``validate_path`` raises PermissionError for a file without read permission."""
with TemporaryDirectory() as tmp_dir:
Expand Down
Loading