Skip to content

Commit ee1f659

Browse files
samet-akcayAshwin Vaidya
authored andcommitted
🔒 v1 - Address security issues (open-edge-platform#1637)
* Address path traversal issues 1-3 * address traversal path 6 * Address traverse path 8 * modify the comment to make it more descriptive
1 parent 928058f commit ee1f659

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

src/anomalib/data/utils/image.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ def get_image_filenames(path: str | Path) -> list[Path]:
2929
list[Path]: List of image filenames
3030
3131
"""
32+
path = Path(path).resolve()
3233
image_filenames: list[Path]
3334

34-
if isinstance(path, str):
35-
path = Path(path)
36-
3735
if path.is_file() and path.suffix in IMG_EXTENSIONS:
3836
image_filenames = [path]
3937

@@ -67,8 +65,10 @@ def duplicate_filename(path: str | Path) -> Path:
6765
Returns:
6866
Path: Duplicated output path.
6967
"""
70-
if isinstance(path, str):
71-
path = Path(path)
68+
path = Path(path)
69+
70+
if not path.exists():
71+
return path
7272

7373
i = 0
7474
while True:
@@ -114,32 +114,29 @@ def generate_output_image_filename(input_path: str | Path, output_path: str | Pa
114114
Returns:
115115
Path: The output filename to save the output predictions from the inferencer.
116116
"""
117-
if isinstance(input_path, str):
118-
input_path = Path(input_path)
117+
input_path = Path(input_path)
118+
output_path = Path(output_path)
119119

120-
if isinstance(output_path, str):
121-
output_path = Path(output_path)
122-
123-
# This function expects an ``input_path`` that is a file. This is to check if output_path
120+
# Input validation: Check if input_path is a valid directory or file
124121
if input_path.is_file() is False:
125122
msg = "input_path is expected to be a file to generate a proper output filename."
126123
raise ValueError(msg)
127124

128125
# If the output is a directory, then add parent directory name
129126
# and filename to the path. This is to ensure we do not overwrite
130127
# images and organize based on the categories.
131-
file_path = output_path / input_path.parent.name / input_path.name if output_path.is_dir() else output_path
132-
133-
# This new ``file_path`` might contain a directory path yet to be created.
134-
# Create the parent directory to avoid such cases.
135-
file_path.parent.mkdir(parents=True, exist_ok=True)
136-
137-
if file_path.is_file():
128+
if output_path.is_dir():
129+
output_image_filename = output_path / input_path.parent.name / input_path.name
130+
elif output_path.is_file() and output_path.exists():
138131
msg = f"{output_path} already exists. Renaming the file to avoid overwriting."
139132
logger.warning(msg)
140-
file_path = duplicate_filename(file_path)
133+
output_image_filename = duplicate_filename(output_path)
134+
else:
135+
output_image_filename = output_path
136+
137+
output_image_filename.parent.mkdir(parents=True, exist_ok=True)
141138

142-
return file_path
139+
return output_image_filename
143140

144141

145142
def get_image_height_and_width(image_size: int | Sequence[int]) -> tuple[int, int]:

0 commit comments

Comments
 (0)