-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable the use of anchors when parsing yaml
This can be used as a DDoS attack Closes-Bug: 1785657 Change-Id: Icf460fea113e9279715cae87df3ef88a77575e04
- Loading branch information
Showing
6 changed files
with
146 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright 2019 - Nokia Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from unittest import TestCase | ||
|
||
from mistral.utils import safe_yaml | ||
|
||
|
||
class TestSafeLoader(TestCase): | ||
def test_safe_load(self): | ||
yaml_text = """ | ||
version: '2.0' | ||
wf1: | ||
type: direct | ||
input: | ||
- a: &a ["lol","lol","lol","lol","lol"] | ||
- b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a] | ||
- c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b] | ||
- d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c] | ||
- e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d] | ||
- f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e] | ||
- g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f] | ||
- h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g] | ||
- i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h] | ||
tasks: | ||
hello: | ||
action: std.echo output="Hello" | ||
wait-before: 1 | ||
publish: | ||
result: <% task(hello).result %> | ||
""" | ||
|
||
result = { | ||
'version': '2.0', | ||
'wf1': | ||
{'type': 'direct', | ||
'input': [ | ||
{'a': '&a ["lol","lol","lol","lol","lol"]'}, | ||
{'b': '&b [*a,*a,*a,*a,*a,*a,*a,*a,*a]'}, | ||
{'c': '&c [*b,*b,*b,*b,*b,*b,*b,*b,*b]'}, | ||
{'d': '&d [*c,*c,*c,*c,*c,*c,*c,*c,*c]'}, | ||
{'e': '&e [*d,*d,*d,*d,*d,*d,*d,*d,*d]'}, | ||
{'f': '&f [*e,*e,*e,*e,*e,*e,*e,*e,*e]'}, | ||
{'g': '&g [*f,*f,*f,*f,*f,*f,*f,*f,*f]'}, | ||
{'h': '&h [*g,*g,*g,*g,*g,*g,*g,*g,*g]'}, | ||
{'i': '&i [*h,*h,*h,*h,*h,*h,*h,*h,*h]'}], | ||
'tasks': | ||
{'hello': { | ||
'action': 'std.echo output="Hello"', | ||
'wait-before': 1, 'publish': | ||
{'result': '<% task(hello).result %>'} | ||
}} | ||
} | ||
} | ||
self.assertEqual(result, safe_yaml.load(yaml_text)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2019 - Nokia Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import yaml | ||
from yaml import * # noqa | ||
|
||
yaml.SafeDumper.ignore_aliases = lambda *args: True | ||
|
||
|
||
class SafeLoader(yaml.SafeLoader): | ||
"""Treat '@', '&', '*' as plain string. | ||
Anchors are not used in mistral workflow. It's better to | ||
disable them completely. Anchors can be used as an exploit to a | ||
Denial of service attack through expansion (Billion Laughs) | ||
see https://en.wikipedia.org/wiki/Billion_laughs_attack. | ||
Also this module uses the safe loader by default which is always | ||
a better loader. | ||
When using yaml module to load a yaml file or a string use this | ||
module instead of yaml. | ||
Example: | ||
import mistral.utils.safe_yaml as safe_yaml | ||
... | ||
... | ||
safe_yaml.load(...) | ||
""" | ||
|
||
def fetch_alias(self): | ||
return self.fetch_plain() | ||
|
||
def fetch_anchor(self): | ||
return self.fetch_plain() | ||
|
||
def check_plain(self): | ||
# Modified: allow '@' | ||
if self.peek() == '@': | ||
return True | ||
else: | ||
return super(SafeLoader, self).check_plain() | ||
|
||
|
||
def load(stream): | ||
return yaml.load(stream, SafeLoader) | ||
|
||
|
||
def safe_load(stream): | ||
return load(stream) |