Skip to content

Commit

Permalink
Update pyyaml to prevent arbitrary code execution
Browse files Browse the repository at this point in the history
Before `pyyaml==5.1` the `yaml.load` function was vulnerable to
arbitrary code execution, because it loaded the full set of YAML. There
was an alternative `safe_load` but this was not the default and could
only load a sub set of the markup language. The new version of pyyaml
deprecates the old vulnerable code and provides the `FullLoader` that
can load the full set without being vulnerable.
  • Loading branch information
sphuber committed Dec 16, 2019
1 parent bf9bb72 commit 3a92119
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 11 deletions.
5 changes: 2 additions & 3 deletions aiida/cmdline/params/options/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
.. py:module::config
:synopsis: Convenience class for configuration file option
"""

import yaml
import click_config_file
import yaml

from .overridable import OverridableOption


def yaml_config_file_provider(file_path, cmd_name): # pylint: disable=unused-argument
"""Read yaml config file."""
with open(file_path, 'r') as handle:
return yaml.load(handle)
return yaml.safe_load(handle)


class ConfigFileOption(OverridableOption):
Expand Down
3 changes: 1 addition & 2 deletions aiida/manage/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""Definition of caching mechanism and configuration for calculations."""

import os
import copy
import warnings
Expand Down Expand Up @@ -55,7 +54,7 @@ def _get_config(config_file):

try:
with open(config_file, 'r', encoding='utf8') as handle:
config = yaml.load(handle)[profile.name]
config = yaml.safe_load(handle)[profile.name]
except (OSError, IOError, KeyError):
# No config file, or no config for this profile
return DEFAULT_CONFIG
Expand Down
13 changes: 10 additions & 3 deletions aiida/orm/utils/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
checkpoints and messages in the RabbitMQ queue so do so with caution. It is fine to add representers
for new types though.
"""

from functools import partial
import yaml

Expand Down Expand Up @@ -177,8 +176,14 @@ def represent_data(self, data):
return super().represent_data(data)


class AiiDALoader(yaml.Loader):
"""AiiDA specific yaml loader"""
class AiiDALoader(yaml.FullLoader):
"""AiiDA specific yaml loader
.. note:: we subclass the `FullLoader` which is the one that since `pyyaml>=5.1` is the loader that prevents
arbitrary code execution. Even though this is in principle only used internally, one could imagine someone
sharing a database with a maliciously crafted process instance dump, which when reloaded could execute arbitrary
code. This load prevents this: https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation
"""


yaml.add_representer(Bundle, represent_bundle, Dumper=AiiDADumper)
Expand Down Expand Up @@ -217,6 +222,8 @@ def serialize(data, encoding=None):
def deserialize(serialized):
"""Deserialize a yaml dump that represents a serialized data structure.
.. note:: no need to use `yaml.safe_load` here because the `Loader` will ensure that loading is safe.
:param serialized: a yaml serialized string representation
:return: the deserialized data structure
"""
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements_for_rtd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pytest~=5.3
python-dateutil~=2.8
python-memcached~=1.59
pytz~=2019.3
pyyaml~=3.13
pyyaml~=5.1
reentry~=1.3
seekpath~=1.9,>=1.9.3
simplejson~=3.16
Expand Down
2 changes: 2 additions & 0 deletions docs/source/nitpick-exceptions
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ py:class yaml.Dumper
py:class yaml.Loader
py:class yaml.dumper.Dumper
py:class yaml.loader.Loader
py:class yaml.FullLoader
py:class yaml.loader.FullLoader

py:class uuid.UUID

Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies:
- psycopg2~=2.8,>=2.8.3
- python-dateutil~=2.8
- pytz~=2019.3
- pyyaml~=3.13
- pyyaml~=5.1
- reentry~=1.3
- simplejson~=3.16
- sqlalchemy-utils~=0.34.2
Expand Down
2 changes: 1 addition & 1 deletion setup.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"pyblake2~=1.1; python_version<'3.6'",
"python-dateutil~=2.8",
"pytz~=2019.3",
"pyyaml~=3.13",
"pyyaml~=5.1",
"reentry~=1.3",
"simplejson~=3.16",
"sqlalchemy-utils~=0.34.2",
Expand Down

0 comments on commit 3a92119

Please sign in to comment.