mirror of
https://github.com/PaddlePaddle/PaddleOCR.git
synced 2026-11-25 17:29:39 +08:00
fix: use safe YAML loaders for config parsing (#17930)
* fix: use safe yaml loaders for config parsing * test: restore YAML stubs after tests --------- Co-authored-by: Tao Luo <luotao02@baidu.com> Co-authored-by: Lin Manhui <mhlin425@whu.edu.cn>
This commit is contained in:
5 files changed
+90
-9
No files matched your search
@@ -281,7 +281,7 @@ class Config(object):
|
||||
assert ext in [".yml", ".yaml"], "only support yaml files for now"
|
||||
|
||||
with open(file_path) as f:
|
||||
file_cfg = yaml.load(f, Loader=yaml.Loader)
|
||||
file_cfg = yaml.load(f, Loader=yaml.SafeLoader)
|
||||
|
||||
# NOTE: cfgs outside have higher priority than cfgs in _BASE_
|
||||
if self.BASE_KEY in file_cfg:
|
||||
@@ -344,7 +344,7 @@ class ArgsParser(ArgumentParser):
|
||||
s = s.strip()
|
||||
k, v = s.split("=", 1)
|
||||
if "." not in k:
|
||||
config[k] = yaml.load(v, Loader=yaml.Loader)
|
||||
config[k] = yaml.load(v, Loader=yaml.SafeLoader)
|
||||
else:
|
||||
keys = k.split(".")
|
||||
if keys[0] not in config:
|
||||
@@ -352,7 +352,7 @@ class ArgsParser(ArgumentParser):
|
||||
cur = config[keys[0]]
|
||||
for idx, key in enumerate(keys[1:]):
|
||||
if idx == len(keys) - 2:
|
||||
cur[key] = yaml.load(v, Loader=yaml.Loader)
|
||||
cur[key] = yaml.load(v, Loader=yaml.SafeLoader)
|
||||
else:
|
||||
cur[key] = {}
|
||||
cur = cur[key]
|
||||
|
||||
@@ -131,7 +131,7 @@ You can download it from \
|
||||
https://github.com/PaddlePaddle/PaddleOCR/tree/dygraph/configs/rec/multi_language/"
|
||||
|
||||
global_config = yaml.load(
|
||||
open("./rec_multi_language_lite_train.yml", "rb"), Loader=yaml.Loader
|
||||
open("./rec_multi_language_lite_train.yml", "rb"), Loader=yaml.SafeLoader
|
||||
)
|
||||
project_path = os.path.abspath(os.path.join(os.getcwd(), "../../../"))
|
||||
|
||||
@@ -180,7 +180,7 @@ class ArgsParser(ArgumentParser):
|
||||
for s in opts:
|
||||
s = s.strip()
|
||||
k, v = s.split("=")
|
||||
config[k] = yaml.load(v, Loader=yaml.Loader)
|
||||
config[k] = yaml.load(v, Loader=yaml.SafeLoader)
|
||||
return config
|
||||
|
||||
def _set_language(self, type):
|
||||
|
||||
@@ -38,7 +38,7 @@ class ArgsParser(ArgumentParser):
|
||||
for s in opts:
|
||||
s = s.strip()
|
||||
k, v = s.split("=")
|
||||
config[k] = yaml.load(v, Loader=yaml.Loader)
|
||||
config[k] = yaml.load(v, Loader=yaml.SafeLoader)
|
||||
return config
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ def load_config(file_path):
|
||||
merge_config(default_config)
|
||||
_, ext = os.path.splitext(file_path)
|
||||
assert ext in [".yml", ".yaml"], "only support yaml files for now"
|
||||
merge_config(yaml.load(open(file_path, "rb"), Loader=yaml.Loader))
|
||||
merge_config(yaml.load(open(file_path, "rb"), Loader=yaml.SafeLoader))
|
||||
return global_config
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import importlib.util
|
||||
import sys
|
||||
import types
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def _stub_module(monkeypatch, name, **attrs):
|
||||
module = types.ModuleType(name)
|
||||
for key, value in attrs.items():
|
||||
setattr(module, key, value)
|
||||
monkeypatch.setitem(sys.modules, name, module)
|
||||
return module
|
||||
|
||||
|
||||
def _load_tools_program_module(monkeypatch):
|
||||
_stub_module(monkeypatch, "paddle")
|
||||
_stub_module(monkeypatch, "paddle.distributed")
|
||||
_stub_module(monkeypatch, "cv2")
|
||||
_stub_module(monkeypatch, "numpy")
|
||||
_stub_module(monkeypatch, "tqdm", tqdm=lambda *args, **kwargs: None)
|
||||
_stub_module(monkeypatch, "ppocr")
|
||||
_stub_module(monkeypatch, "ppocr.utils")
|
||||
_stub_module(monkeypatch, "ppocr.utils.stats", TrainingStats=object)
|
||||
_stub_module(
|
||||
monkeypatch, "ppocr.utils.save_load", save_model=lambda *args, **kwargs: None
|
||||
)
|
||||
_stub_module(
|
||||
monkeypatch,
|
||||
"ppocr.utils.utility",
|
||||
print_dict=lambda *args, **kwargs: None,
|
||||
AverageMeter=object,
|
||||
)
|
||||
_stub_module(
|
||||
monkeypatch, "ppocr.utils.logging", get_logger=lambda *args, **kwargs: None
|
||||
)
|
||||
_stub_module(monkeypatch, "ppocr.utils.loggers", WandbLogger=object, Loggers=object)
|
||||
_stub_module(monkeypatch, "ppocr.utils.profiler")
|
||||
_stub_module(
|
||||
monkeypatch, "ppocr.data", build_dataloader=lambda *args, **kwargs: None
|
||||
)
|
||||
_stub_module(
|
||||
monkeypatch, "ppocr.utils.export_model", export=lambda *args, **kwargs: None
|
||||
)
|
||||
|
||||
module_name = "paddleocr_tools_program"
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
module_name, REPO_ROOT / "tools" / "program.py"
|
||||
)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
monkeypatch.setitem(sys.modules, module_name, module)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def test_tools_program_load_config_rejects_python_object_tags(tmp_path, monkeypatch):
|
||||
module = _load_tools_program_module(monkeypatch)
|
||||
payload = (
|
||||
'!!python/object/apply:os.system ["echo SHOULD_NOT_RUN > '
|
||||
'/tmp/paddleocr_tools_program_test"]\n'
|
||||
)
|
||||
config_path = tmp_path / "malicious.yml"
|
||||
config_path.write_text(payload, encoding="utf-8")
|
||||
|
||||
with pytest.raises(yaml.constructor.ConstructorError):
|
||||
module.load_config(str(config_path))
|
||||
|
||||
|
||||
def test_tools_program_parse_opt_rejects_python_object_tags(monkeypatch):
|
||||
parser = _load_tools_program_module(monkeypatch).ArgsParser()
|
||||
malicious_opt = [
|
||||
'Global.debug=!!python/object/apply:os.system ["echo SHOULD_NOT_RUN > '
|
||||
'/tmp/paddleocr_opt_test"]'
|
||||
]
|
||||
|
||||
with pytest.raises(yaml.constructor.ConstructorError):
|
||||
parser._parse_opt(malicious_opt)
|
||||
+2
-2
@@ -68,7 +68,7 @@ class ArgsParser(ArgumentParser):
|
||||
for s in opts:
|
||||
s = s.strip()
|
||||
k, v = s.split("=")
|
||||
config[k] = yaml.load(v, Loader=yaml.Loader)
|
||||
config[k] = yaml.load(v, Loader=yaml.SafeLoader)
|
||||
return config
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ def load_config(file_path):
|
||||
"""
|
||||
_, ext = os.path.splitext(file_path)
|
||||
assert ext in [".yml", ".yaml"], "only support yaml files for now"
|
||||
config = yaml.load(open(file_path, "rb"), Loader=yaml.Loader)
|
||||
config = yaml.load(open(file_path, "rb"), Loader=yaml.SafeLoader)
|
||||
return config
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user