Skip to content

[Enhance] Use SafeLoader for YAML config parsing - #1703

Open
tritsystem wants to merge 2 commits into
open-mmlab:mainfrom
tritsystem:safe-yaml-config-loader
Open

tritsystem wants to merge 2 commits into
open-mmlab:mainfrom
tritsystem:safe-yaml-config-loader

Conversation

@tritsystem

Copy link
Copy Markdown

Motivation

YamlHandler parses YAML with the full yaml.Loader/CLoader, which supports Python-object construction tags such as !!python/object/apply. YAML configs only need plain data (dicts, lists, scalars) — they don't require constructing arbitrary Python objects — so loading a YAML config via Config.fromfile('x.yaml') or mmengine.load(...) can trigger unintended code execution at load time.

This switches the YAML handler to SafeLoader/CSafeLoader, which parses the same plain-data YAML but refuses Python-object tags. It's a defense-in-depth change and matches how peer config libraries (jsonargparse, OmegaConf, albumentations) load YAML. Python (.py) configs are unaffected — they remain code by design.

Modification

  • mmengine/fileio/handlers/yaml_handler.py: use CSafeLoader/SafeLoader instead of CLoader/Loader.
  • Added a regression test asserting a !!python/object/apply tag is rejected.

BC-breaking (Optional)

A YAML file that relied on !!python/* tags to construct Python objects would now be rejected. Such usage is uncommon and unsafe; plain-data YAML (the documented config format) is unchanged.

YamlHandler parsed YAML with the full Loader/CLoader, which supports
Python-object construction tags (e.g. !!python/object/apply). YAML
configs only need plain data, so loading one via Config.fromfile or
mmengine.load could trigger unintended code execution. Use
SafeLoader/CSafeLoader instead, which parses the same plain-data YAML
but refuses object tags. Python (.py) configs are unaffected.

Add a regression test asserting a python-object tag is rejected.
@CLAassistant

CLAassistant commented Nov 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

The test string contained real line breaks instead of newline escapes, which is
a syntax error, so lint failed and the test never ran. Use newline escapes,
assert the specific yaml ConstructorError instead of any exception, and remove
the temporary file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants