Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevents arbitrary code execution during python/object/new constructor #386

Merged
merged 3 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/yaml/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def check_data(self):
# If there are more documents available?
return self.check_node()

def check_state_key(self, key):
"""Block special attributes/methods from being set in a newly created
object, to prevent user-controlled methods from being called during
deserialization"""
if self.get_state_keys_blacklist_regexp().match(key):
raise ConstructorError(None, None,
"blacklisted key '%s' in instance state found" % (key,), None)

def get_data(self):
# Construct and return the next document.
if self.check_node():
Expand Down Expand Up @@ -495,6 +503,16 @@ def construct_undefined(self, node):
SafeConstructor.construct_undefined)

class FullConstructor(SafeConstructor):
# 'extend' is blacklisted because it is used by
# construct_python_object_apply to add `listitems` to a newly generate
# python instance
def get_state_keys_blacklist(self):
return ['^extend$', '^__.*__$']

def get_state_keys_blacklist_regexp(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this needs to be public method. But I'll go ahead and approve it. :)

if not hasattr(self, 'state_keys_blacklist_regexp'):
self.state_keys_blacklist_regexp = re.compile('(' + '|'.join(self.get_state_keys_blacklist()) + ')')
return self.state_keys_blacklist_regexp

def construct_python_str(self, node):
return self.construct_scalar(node).encode('utf-8')
Expand Down Expand Up @@ -590,18 +608,23 @@ def make_python_instance(self, suffix, node,
else:
return cls(*args, **kwds)

def set_python_instance_state(self, instance, state):
def set_python_instance_state(self, instance, state, unsafe=False):
if hasattr(instance, '__setstate__'):
instance.__setstate__(state)
else:
slotstate = {}
if isinstance(state, tuple) and len(state) == 2:
state, slotstate = state
if hasattr(instance, '__dict__'):
if not unsafe and state:
for key in state.keys():
self.check_state_key(key)
instance.__dict__.update(state)
elif state:
slotstate.update(state)
for key, value in slotstate.items():
if not unsafe:
self.check_state_key(key)
setattr(instance, key, value)

def construct_python_object(self, suffix, node):
Expand Down Expand Up @@ -723,6 +746,10 @@ def make_python_instance(self, suffix, node, args=None, kwds=None, newobj=False)
return super(UnsafeConstructor, self).make_python_instance(
suffix, node, args, kwds, newobj, unsafe=True)

def set_python_instance_state(self, instance, state):
return super(UnsafeConstructor, self).set_python_instance_state(
instance, state, unsafe=True)

UnsafeConstructor.add_multi_constructor(
u'tag:yaml.org,2002:python/object/apply:',
UnsafeConstructor.construct_python_object_apply)
Expand Down
29 changes: 28 additions & 1 deletion lib3/yaml/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def check_data(self):
# If there are more documents available?
return self.check_node()

def check_state_key(self, key):
"""Block special attributes/methods from being set in a newly created
object, to prevent user-controlled methods from being called during
deserialization"""
if self.get_state_keys_blacklist_regexp().match(key):
raise ConstructorError(None, None,
"blacklisted key '%s' in instance state found" % (key,), None)

def get_data(self):
# Construct and return the next document.
if self.check_node():
Expand Down Expand Up @@ -472,6 +480,16 @@ def construct_undefined(self, node):
SafeConstructor.construct_undefined)

class FullConstructor(SafeConstructor):
# 'extend' is blacklisted because it is used by
# construct_python_object_apply to add `listitems` to a newly generate
# python instance
def get_state_keys_blacklist(self):
return ['^extend$', '^__.*__$']

def get_state_keys_blacklist_regexp(self):
if not hasattr(self, 'state_keys_blacklist_regexp'):
self.state_keys_blacklist_regexp = re.compile('(' + '|'.join(self.get_state_keys_blacklist()) + ')')
return self.state_keys_blacklist_regexp

def construct_python_str(self, node):
return self.construct_scalar(node)
Expand Down Expand Up @@ -574,18 +592,23 @@ def make_python_instance(self, suffix, node,
else:
return cls(*args, **kwds)

def set_python_instance_state(self, instance, state):
def set_python_instance_state(self, instance, state, unsafe=False):
if hasattr(instance, '__setstate__'):
instance.__setstate__(state)
else:
slotstate = {}
if isinstance(state, tuple) and len(state) == 2:
state, slotstate = state
if hasattr(instance, '__dict__'):
if not unsafe and state:
for key in state.keys():
self.check_state_key(key)
instance.__dict__.update(state)
elif state:
slotstate.update(state)
for key, value in slotstate.items():
if not unsafe:
self.check_state_key(key)
setattr(instance, key, value)

def construct_python_object(self, suffix, node):
Expand Down Expand Up @@ -711,6 +734,10 @@ def make_python_instance(self, suffix, node, args=None, kwds=None, newobj=False)
return super(UnsafeConstructor, self).make_python_instance(
suffix, node, args, kwds, newobj, unsafe=True)

def set_python_instance_state(self, instance, state):
return super(UnsafeConstructor, self).set_python_instance_state(
instance, state, unsafe=True)

UnsafeConstructor.add_multi_constructor(
'tag:yaml.org,2002:python/object/apply:',
UnsafeConstructor.construct_python_object_apply)
Expand Down
5 changes: 5 additions & 0 deletions tests/data/myfullloader.subclass_blacklist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- !!python/object/new:yaml.MappingNode
args:
state:
mymethod: test
wrong_method: test2
5 changes: 5 additions & 0 deletions tests/data/overwrite-state-new-constructor.loader-error
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- !!python/object/new:yaml.MappingNode
args:
state:
extend: test
__test__: test
18 changes: 17 additions & 1 deletion tests/lib/test_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def _make_objects():
global MyLoader, MyDumper, MyTestClass1, MyTestClass2, MyTestClass3, YAMLObject1, YAMLObject2, \
AnObject, AnInstance, AState, ACustomState, InitArgs, InitArgsWithState, \
NewArgs, NewArgsWithState, Reduce, ReduceWithState, Slots, MyInt, MyList, MyDict, \
FixedOffset, today, execute
FixedOffset, today, execute, MyFullLoader

class MyLoader(yaml.Loader):
pass
Expand Down Expand Up @@ -235,6 +235,10 @@ def tzname(self, dt):
def dst(self, dt):
return datetime.timedelta(0)

class MyFullLoader(yaml.FullLoader):
def get_state_keys_blacklist(self):
return super(MyFullLoader, self).get_state_keys_blacklist() + ['^mymethod$', '^wrong_.*$']

today = datetime.date.today()

def _load_code(expression):
Expand Down Expand Up @@ -289,6 +293,18 @@ def test_constructor_types(data_filename, code_filename, verbose=False):

test_constructor_types.unittest = ['.data', '.code']

def test_subclass_blacklist_types(data_filename, verbose=False):
_make_objects()
try:
yaml.load(open(data_filename, 'rb').read(), MyFullLoader)
except yaml.YAMLError as exc:
if verbose:
print("%s:" % exc.__class__.__name__, exc)
else:
raise AssertionError("expected an exception")

test_subclass_blacklist_types.unittest = ['.subclass_blacklist']

if __name__ == '__main__':
import sys, test_constructor
sys.modules['test_constructor'] = sys.modules['__main__']
Expand Down
18 changes: 17 additions & 1 deletion tests/lib3/test_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def _make_objects():
global MyLoader, MyDumper, MyTestClass1, MyTestClass2, MyTestClass3, YAMLObject1, YAMLObject2, \
AnObject, AnInstance, AState, ACustomState, InitArgs, InitArgsWithState, \
NewArgs, NewArgsWithState, Reduce, ReduceWithState, Slots, MyInt, MyList, MyDict, \
FixedOffset, today, execute
FixedOffset, today, execute, MyFullLoader

class MyLoader(yaml.Loader):
pass
Expand Down Expand Up @@ -222,6 +222,10 @@ def tzname(self, dt):
def dst(self, dt):
return datetime.timedelta(0)

class MyFullLoader(yaml.FullLoader):
def get_state_keys_blacklist(self):
return super().get_state_keys_blacklist() + ['^mymethod$', '^wrong_.*$']

today = datetime.date.today()

def _load_code(expression):
Expand Down Expand Up @@ -274,6 +278,18 @@ def test_constructor_types(data_filename, code_filename, verbose=False):

test_constructor_types.unittest = ['.data', '.code']

def test_subclass_blacklist_types(data_filename, verbose=False):
_make_objects()
try:
yaml.load(open(data_filename, 'rb').read(), MyFullLoader)
except yaml.YAMLError as exc:
if verbose:
print("%s:" % exc.__class__.__name__, exc)
else:
raise AssertionError("expected an exception")

test_subclass_blacklist_types.unittest = ['.subclass_blacklist']

if __name__ == '__main__':
import sys, test_constructor
sys.modules['test_constructor'] = sys.modules['__main__']
Expand Down