Skip to content

Commit 71bc5e1

Browse files
🔨 Fix Arbitrary imports in get_datamodule() (#2736)
* fix semgrep in get_datamodule() Signed-off-by: Rajesh Gangireddy <rajesh.gangireddy@intel.com> * Update CHANGELOG.md Signed-off-by: Rajesh Gangireddy <rajesh.gangireddy@intel.com> --------- Signed-off-by: Rajesh Gangireddy <rajesh.gangireddy@intel.com>
1 parent 754119a commit 71bc5e1

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1616

1717
### Changed
1818

19-
- Update import location for `get_exportable_transform` from `anomalib.utils.transform` to `anomalib.pre_processing.utils.transform`. This update aligns with the reorganization of the `anomalib` project in `v2.0.0`, and the previous path no longer exists.
19+
- 🔨Fix semgrep security issue for `get_datamodule()` method in https://github.com/open-edge-platform/anomalib/pull/2736
20+
- 🔨Update doc for `get_exportable_transform()` method in https://github.com/open-edge-platform/anomalib/pull/2731
2021

2122
### Deprecated
2223

src/anomalib/data/__init__.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,29 @@ def get_datamodule(config: DictConfig | ListConfig | dict) -> AnomalibDataModule
119119
... })
120120
>>> datamodule = get_datamodule(config)
121121
"""
122-
logger.info("Loading the datamodule")
122+
logger.info("Loading the datamodule and dataset class from the config.")
123123

124+
# Getting the datamodule class from the config.
124125
if isinstance(config, dict):
125126
config = DictConfig(config)
127+
_config = config.data if "data" in config else config
128+
129+
# All the sub data modules are imported to anomalib.data. So need to import the module dynamically using paths.
130+
module = importlib.import_module("anomalib.data")
131+
data_class_name = _config.class_path.split(".")[-1]
132+
# check if the data_class exists in the module
133+
if not hasattr(module, data_class_name):
134+
logger.error(
135+
f"Dataclass '{data_class_name}' not found in module '{module.__name__}'. "
136+
f"Available classes are {AnomalibDataModule.__subclasses__()}",
137+
)
138+
error_str = f"Dataclass '{data_class_name}' not found in module '{module.__name__}'."
139+
raise UnknownDatamoduleError(error_str)
140+
dataclass = getattr(module, data_class_name)
126141

127-
try:
128-
_config = config.data if "data" in config else config
129-
if len(_config.class_path.split(".")) > 1:
130-
module = importlib.import_module(".".join(_config.class_path.split(".")[:-1]))
131-
else:
132-
module = importlib.import_module("anomalib.data")
133-
except ModuleNotFoundError as exception:
134-
logger.exception(f"ModuleNotFoundError: {_config.class_path}")
135-
raise UnknownDatamoduleError from exception
136-
dataclass = getattr(module, _config.class_path.split(".")[-1])
137142
init_args = {**_config.get("init_args", {})} # get dict
138143
if "image_size" in init_args:
139144
init_args["image_size"] = to_tuple(init_args["image_size"])
140-
141145
return dataclass(**init_args)
142146

143147

0 commit comments

Comments
 (0)