Repository navigation
fix: load YAML files with the safe loader - #66
Merged
Merged
Conversation
CLoader is PyYAML's full loader: a YAML file with Python tags such as !!python/object/apply can make it create arbitrary Python objects and call functions while the file is read. Metadata repositories are often shared, so reading one should not run code. CSafeLoader only builds standard YAML types. Assisted-by: Generative AI
Owner
Author
|
@ggmarshall this speeds up the read and I'm rather confident that it should not break anything. do you agree? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
load_dict(and thereforeTextDB,Props.read_fromandCatalog.read_from) read YAML withyaml.CLoader, which is PyYAML's full loader. A YAML file containing Python tags such as!!python/object/apply:os.system [...]makes it create arbitrary Python objects and call functions while the file is read. Metadata repositories are shared and cloned from remotes, so reading one should never run code. This PR switches toyaml.CSafeLoader(falling back toyaml.SafeLoaderwithout libyaml) and adds a test that a!!python/...tag is rejected.What stops working: only the
!!python/*tags:!!python/tuple,!!python/complex,!!python/name,!!python/module,!!python/object,!!python/object/new,!!python/object/apply, and the!!python/str|unicode|bytes|int|long|float|bool|none|dict|listvariants. Loading such a file now raisesyaml.constructor.ConstructorError. All standard YAML keeps working, including timestamps (datetime),!!set,!!binary,!!omap,!!pairs, anchors/aliases and merge keys (<<).Round-trip caveat:
write_dict(used byProps.write_toandCatalog.write_to) still uses the defaultyaml.dump, which emits Python tags for some objects. Such files can no longer be read back:AttrsDict(or anydictsubclass) at any level, e.g. the result ofProps.add_to, is written as!!python/object/new:dbetto.attrsdict.AttrsDict!!python/tuple)np.float64andnp.int64(!!python/object/apply:numpy._core.multiarray.scalar)Plain
dict/list/str/int/float/bool/None/datetimevalues are unaffected (Catalog.write_toonly writes those). Calling code should convert to plain types before writing (e.g.attrs.to_dict(),float(x),list(t)). A follow-up could makewrite_dictdo this itself, so it never writes tags the loader rejects.Verified:
nox -s tests(32 passed) and pre-commit on the changed files. I also checked by hand which types written byyaml.dumpfail to load withCSafeLoader.🤖 Generated with Claude Code