Skip to content

Commit 87c897d

Browse files
authored
🔒 Address checkmarx issues. (#1672)
* Fix metadata path * Ignore hidden directories in folder dataset * Add check for mask_dir for segmentation tasks in Folder dataset * Limit the gradio version to <4 * Add should exist flag to validate_path and validate the output paths Signed-off-by: Samet Akcay <[email protected]> --------- Signed-off-by: Samet Akcay <[email protected]>
1 parent 6412fe1 commit 87c897d

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/anomalib/data/utils/image.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ def generate_output_image_filename(input_path: str | Path, output_path: str | Pa
264264
Returns:
265265
Path: The output filename to save the output predictions from the inferencer.
266266
"""
267-
input_path = Path(input_path)
268-
output_path = Path(output_path)
267+
input_path = validate_path(input_path)
268+
output_path = validate_path(output_path, should_exist=False)
269269

270270
# Input validation: Check if input_path is a valid directory or file
271271
if input_path.is_file() is False:

src/anomalib/data/utils/path.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,13 @@ def contains_non_printable_characters(path: str | Path) -> bool:
143143
return not printable_pattern.match(str(path))
144144

145145

146-
def validate_path(path: str | Path, base_dir: str | Path | None = None) -> Path:
146+
def validate_path(path: str | Path, base_dir: str | Path | None = None, should_exist: bool = True) -> Path:
147147
"""Validate the path.
148148
149149
Args:
150150
path (str | Path): Path to validate.
151151
base_dir (str | Path): Base directory to restrict file access.
152+
should_exist (bool): If True, do not raise an exception if the path does not exist.
152153
153154
Returns:
154155
Path: Validated path.
@@ -215,15 +216,18 @@ def validate_path(path: str | Path, base_dir: str | Path | None = None) -> Path:
215216
msg = "Access denied: Path is outside the allowed directory"
216217
raise ValueError(msg)
217218

218-
# Check if the path exists
219-
if not path.exists():
220-
msg = f"Path does not exist: {path}"
221-
raise FileNotFoundError(msg)
222-
223-
# Check the read and execute permissions
224-
if not (os.access(path, os.R_OK) or os.access(path, os.X_OK)):
225-
msg = f"Read or execute permissions denied for the path: {path}"
226-
raise PermissionError(msg)
219+
# In case path ``should_exist``, the path is valid, and should be
220+
# checked for read and execute permissions.
221+
if should_exist:
222+
# Check if the path exists
223+
if not path.exists():
224+
msg = f"Path does not exist: {path}"
225+
raise FileNotFoundError(msg)
226+
227+
# Check the read and execute permissions
228+
if not (os.access(path, os.R_OK) or os.access(path, os.X_OK)):
229+
msg = f"Read or execute permissions denied for the path: {path}"
230+
raise PermissionError(msg)
227231

228232
return path
229233

0 commit comments

Comments
 (0)