diff options
| author | Ingy döt Net <ingy@ingy.net> | 2019-02-17 19:22:58 -0800 |
|---|---|---|
| committer | Ingy döt Net <ingy@ingy.net> | 2019-03-08 09:09:48 -0800 |
| commit | 0cedb2a0697b2bc49e4f3841b8d4590b6b15657e (patch) | |
| tree | eccffeba44a0f56368834ee8d8f3504d641ca0ec /tests | |
| parent | d13a3d0f9684351b357b7bf55df2cef050ddfbc6 (diff) | |
| download | pyyaml-git-0cedb2a0697b2bc49e4f3841b8d4590b6b15657e.tar.gz | |
Deprecate/warn usage of yaml.load(input)
The `load` and `load_all` methods will issue a warning when they are
called without the 'Loader=' parameter. The warning will point to a URL
that is always up to date with the latest information on the usage of
`load`.
There are several ways to stop the warning:
* Use `full_load(input)` - sugar for `yaml.load(input, FullLoader)`
* FullLoader is the new safe but complete loader class
* Use `safe_load(input)` - sugar for `yaml.load(input, SafeLoader)`
* Make sure your input YAML consists of the 'safe' subset
* Use `unsafe_load(input)` - sugar for `yaml.load(input, UnsafeLoader)`
* Make sure your input YAML consists of the 'safe' subset
* Use `yaml.load(input, Loader=yaml.<loader>)`
* Or shorter `yaml.load(input, yaml.<loader>)`
* Where '<loader>' can be:
* FullLoader - safe, complete Python YAML loading
* SafeLoader - safe, partial Python YAML loading
* UnsafeLoader - more explicit name for the old, unsafe 'Loader' class
* yaml.warnings({'YAMLLoadWarning': False})
* Use this when you use third party modules that use `yaml.load(input)`
* Only do this if input is trusted
The above `load()` expressions all have `load_all()` counterparts.
You can get the original unsafe behavior with:
* `yaml.unsafe_load(input)`
* `yaml.load(input, Loader=yaml.UnsafeLoader)`
In a future release, `yaml.load(input)` will raise an exception.
The new loader called FullLoader is almost entirely complete as
Loader/UnsafeLoader but it does it avoids all known code execution
paths. It is the preferred YAML loader, and the current default for
`yaml.load(input)` when you get the warning.
Here are some of the exploits that can be triggered with UnsafeLoader
but not with FullLoader:
```
python -c 'import os, yaml; yaml.full_load("!!python/object/new:os.system [echo EXPLOIT!]")'`
python -c 'import yaml; print yaml.full_load("!!python/object/new:abs [-5]")'
python -c 'import yaml; yaml.full_load("!!python/object/new:eval [exit(5)]")' ; echo $?
python -c 'import yaml; yaml.full_load("!!python/object/new:exit [5]")' ; echo $?
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/lib/test_errors.py | 6 | ||||
| -rw-r--r-- | tests/lib/test_input_output.py | 10 | ||||
| -rw-r--r-- | tests/lib/test_recursive.py | 2 |
3 files changed, 9 insertions, 9 deletions
diff --git a/tests/lib/test_errors.py b/tests/lib/test_errors.py index 7dc9388..32423c1 100644 --- a/tests/lib/test_errors.py +++ b/tests/lib/test_errors.py @@ -3,7 +3,7 @@ import yaml, test_emitter def test_loader_error(error_filename, verbose=False): try: - list(yaml.load_all(open(error_filename, 'rb'))) + list(yaml.load_all(open(error_filename, 'rb'), yaml.FullLoader)) except yaml.YAMLError, exc: if verbose: print "%s:" % exc.__class__.__name__, exc @@ -14,7 +14,7 @@ test_loader_error.unittest = ['.loader-error'] def test_loader_error_string(error_filename, verbose=False): try: - list(yaml.load_all(open(error_filename, 'rb').read())) + list(yaml.load_all(open(error_filename, 'rb').read(), yaml.FullLoader)) except yaml.YAMLError, exc: if verbose: print "%s:" % exc.__class__.__name__, exc @@ -25,7 +25,7 @@ test_loader_error_string.unittest = ['.loader-error'] def test_loader_error_single(error_filename, verbose=False): try: - yaml.load(open(error_filename, 'rb').read()) + yaml.load(open(error_filename, 'rb').read(), yaml.FullLoader) except yaml.YAMLError, exc: if verbose: print "%s:" % exc.__class__.__name__, exc diff --git a/tests/lib/test_input_output.py b/tests/lib/test_input_output.py index 3277a88..12e95b1 100644 --- a/tests/lib/test_input_output.py +++ b/tests/lib/test_input_output.py @@ -17,7 +17,7 @@ def _unicode_open(file, encoding, errors='strict'): def test_unicode_input(unicode_filename, verbose=False): data = open(unicode_filename, 'rb').read().decode('utf-8') value = ' '.join(data.split()) - output = yaml.load(_unicode_open(StringIO.StringIO(data.encode('utf-8')), 'utf-8')) + output = yaml.full_load(_unicode_open(StringIO.StringIO(data.encode('utf-8')), 'utf-8')) assert output == value, (output, value) for input in [data, data.encode('utf-8'), codecs.BOM_UTF8+data.encode('utf-8'), @@ -25,9 +25,9 @@ def test_unicode_input(unicode_filename, verbose=False): codecs.BOM_UTF16_LE+data.encode('utf-16-le')]: if verbose: print "INPUT:", repr(input[:10]), "..." - output = yaml.load(input) + output = yaml.full_load(input) assert output == value, (output, value) - output = yaml.load(StringIO.StringIO(input)) + output = yaml.full_load(StringIO.StringIO(input)) assert output == value, (output, value) test_unicode_input.unittest = ['.unicode'] @@ -40,14 +40,14 @@ def test_unicode_input_errors(unicode_filename, verbose=False): codecs.BOM_UTF8+data.encode('utf-16-le')]: try: - yaml.load(input) + yaml.full_load(input) except yaml.YAMLError, exc: if verbose: print exc else: raise AssertionError("expected an exception") try: - yaml.load(StringIO.StringIO(input)) + yaml.full_load(StringIO.StringIO(input)) except yaml.YAMLError, exc: if verbose: print exc diff --git a/tests/lib/test_recursive.py b/tests/lib/test_recursive.py index 6707fd4..312204e 100644 --- a/tests/lib/test_recursive.py +++ b/tests/lib/test_recursive.py @@ -30,7 +30,7 @@ def test_recursive(recursive_filename, verbose=False): output2 = None try: output1 = yaml.dump(value1) - value2 = yaml.load(output1) + value2 = yaml.load(output1, yaml.FullLoader) output2 = yaml.dump(value2) assert output1 == output2, (output1, output2) finally: |
