Skip to content

Use yaml.safe_load and yaml.safe_dump for potential YAML and orbit HDF5 I/O - #690

Open
rpinho wants to merge 1 commit into
adrn:mainfrom
rpinho:fix-687-safe-yaml
Open

rpinho wants to merge 1 commit into
adrn:mainfrom
rpinho:fix-687-safe-yaml

Conversation

@rpinho

@rpinho rpinho commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Describe your changes

gala.potential.potential.io.load() and Orbit.from_hdf5() previously parsed YAML with yaml.load(..., Loader=yaml.Loader), which constructs arbitrary Python objects from !!python/object/apply, !!python/object/new, and !!python/name tags. save() and Orbit.to_hdf5() also used yaml.dump(...).

Who hits this: gala users loading .yml potential definitions or .hdf5 orbit files shared from external sources via gala.potential.load(...) or Orbit.from_hdf5(...) (#687).

Changes:

  • src/gala/potential/potential/io.py & src/gala/dynamics/orbit.py:
    • Switch load() (both path and file-like branches) and Orbit.from_hdf5() from yaml.load(..., Loader=yaml.Loader) to yaml.safe_load(...).
    • Switch save() and Orbit.to_hdf5() from yaml.dump(...) to yaml.safe_dump(...).
    • Add _to_plain_str() and _to_yaml_safe() in io.py so to_dict() emits plain YAML-safe Python types (handling numpy.str_ catalog labels, NumPy scalars/arrays, tuples, OrderedDict, and (str, Enum) component names without calling overridden __str__), raises ValueError loudly if two distinct keys collapse to the same normalized name instead of silently overwriting a component, and still fails fast on unexpected Quantity objects rather than stripping units.
  • tests/potential/potential/lm10.yml & tests/potential/potential/ccomposite.yml:
    • Migrate the legacy 2016 !!python/object/apply:collections.OrderedDict tags in these two fixtures to plain YAML mappings matching current save() output (a loader-only swap breaks test_read_lm10). Note: pre-2020 OrderedDict-tagged YAML files are intentionally no longer accepted by yaml.safe_load.
  • tests/potential/potential/test_io.py & tests/dynamics/test_orbit.py:
    • Add regression tests covering ConstructorError rejection of !!python/object/apply, !!python/object/new, and !!python/name tags across load(path), load(StringIO), and Orbit.from_hdf5(), plus round-trip and collision-guard tests for MilkyWayPotential, CompositePotential, CCompositePotential, np.str_ / (str, Enum) / str-subclass component names, and Orbit HDF5 I/O.

Verification:

  • Migrated lm10.yml loads an LM10Potential with identical class, component order, all 11 parameter values and units, energies, and gradients as main.
  • Full test suite passes (1,277 passed vs 1,244 on main, +33 new tests, 0 regressions) along with ruff check and ruff format --check.
  • Found and written with AI agents; a separate cross-family AI referee independently verified the SafeLoader rejection, lm10.yml fidelity, round-trip/collision guards, and full test suite before this PR.

Fixes #687

Checklist

  • Did you add tests?
  • Did you add documentation for your changes?
  • Did you reference any relevant issues?
  • Did you add a changelog entry? (see CHANGES.rst)
  • Are the CI tests passing?
  • Is the milestone set?

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

This branch has not been deployed

No deployments
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.

gala: arbitrary code execution when loading a potential YAML file or an orbit HDF5 file (CWE-502)

1 participant