From 2465b6657b02c8eed308759b7e800e295ae01888 Mon Sep 17 00:00:00 2001
From: hertschuh <1091026+hertschuh@users.noreply.github.com>
Date: Tue, 17 Feb 2026 23:06:13 +0000
Subject: [PATCH] Don't let unit test modify the source folder. (#22194)
Changed some unit tests to use a temporary folder instead of the current one.
Also fixed `file_utils.filter_safe_zipinfos` and `filter_safe_tarinfos` to resolve links within the context of the destination folder instead of hardcoding `"."` as the destination path.
Reference:https://github.com/keras-team/keras/commit/2465b6657b02c8eed308759b7e800e295ae01888
Conflict:Context adaptation in keras/utils/data_utils.py. And delete the modifications to unit tests.
keras/utils/data_utils.py | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
@@ -116,8 +116,11 @@ def _is_link_in_dir(info, base):
return _is_path_in_dir(info.linkname, base_dir=tip)
-def _filter_safe_paths(members):
- base_dir = _resolve_path(".")
+def _filter_safe_paths(members, base_dir=None):
+ if base_dir is None:
+ base_dir = _resolve_path(".")
+ else:
+ base_dir = _resolve_path(base_dir)
for finfo in members:
valid_path = False
if finfo.issym() or finfo.islnk():
@@ -175,11 +178,13 @@ def _extract_archive(file_path, path=".", archive_format="auto"):
try:
if zipfile.is_zipfile(file_path):
# Zip archive.
- archive.extractall(path)
+ archive.extractall(
+ path, members=_filter_safe_paths(archive.infolist(), path)
+ )
else:
# Tar archive, perhaps unsafe. Filter paths.
archive.extractall(
- path, members=_filter_safe_paths(archive)
+ path, members=_filter_safe_paths(archive, path)
)
except (tarfile.TarError, RuntimeError, KeyboardInterrupt):
if os.path.exists(path):
--
2.43.0