Skip to content

Commit 5d05753

Browse files
😱 Fix vulnerability of load_yaml and load_yamlf (#12)
1 parent 409c30c commit 5d05753

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

owlmixin/util.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import json
1010
import yaml
1111
from urllib.request import urlopen
12-
from yaml import Loader, SafeLoader
12+
from yaml import SafeLoader
1313

1414
import csv
1515
from csv import register_dialect, Dialect, QUOTE_MINIMAL
@@ -44,7 +44,6 @@ def increase_indent(self, flow=False, indentless=False):
4444
def construct_yaml_str(self, node):
4545
return self.construct_scalar(node)
4646

47-
Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
4847
SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
4948

5049

@@ -93,7 +92,7 @@ def load_yaml(yaml_str):
9392
:param unicode yaml_str:
9493
:rtype: dict | list
9594
"""
96-
return yaml.load(yaml_str)
95+
return yaml.safe_load(yaml_str)
9796

9897

9998
def load_yamlf(fpath, encoding):
@@ -103,7 +102,7 @@ def load_yamlf(fpath, encoding):
103102
:rtype: dict | list
104103
"""
105104
with codecs.open(fpath, encoding=encoding) as f:
106-
return yaml.load(f)
105+
return yaml.safe_load(f)
107106

108107

109108
def load_csvf(fpath, fieldnames, encoding):

tests/test_util.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
from __future__ import division, absolute_import, unicode_literals
44

5+
from yaml.constructor import ConstructorError
56
from owlmixin import util
67

8+
import pytest
9+
710

811
class TestReplaceKeys:
912
def test_need_not_snake(self):
@@ -82,3 +85,38 @@ def test_docopt(self):
8285
assert util.to_snake("<file_list>") == "file_list"
8386
assert util.to_snake("-o") == "o"
8487
assert util.to_snake("--detail-option") == "detail_option"
88+
89+
90+
class TestLoadYaml:
91+
def test(self):
92+
actual = util.load_yaml('''
93+
id: 1
94+
names:
95+
- tadashi
96+
- aikawa
97+
''')
98+
assert actual == {
99+
"id": 1,
100+
"names": ['tadashi', 'aikawa']
101+
}
102+
103+
def test_yaml_load_vulnerability(self):
104+
with pytest.raises(ConstructorError):
105+
util.load_yaml('!!python/object/apply:os.system ["calc.exe"]')
106+
107+
108+
class TestLoadYamlf:
109+
def test(self):
110+
assert util.load_yamlf('tests/yaml/spots_utf8.yaml', 'utf-8') == [
111+
{
112+
"address": {"name": "address1"},
113+
"names": ["spot1"]
114+
},
115+
{
116+
"names": ["スポット21", "スポット22"]
117+
}
118+
]
119+
120+
def test_yaml_load_vulnerability(self):
121+
with pytest.raises(ConstructorError):
122+
util.load_yamlf('tests/yaml/vulnerability.yaml', 'utf-8')

tests/yaml/vulnerability.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!!python/object/apply:os.system ["calc.exe"]

0 commit comments

Comments
 (0)