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

Security Fix for Arbitrary Code Execution - huntr.dev #637

Closed
wants to merge 2 commits into from
Closed
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
28 changes: 27 additions & 1 deletion petastorm/etl/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@

from six.moves import cPickle as pickle

import io
import builtins

safe_builtins = {
'range',
'complex',
'set',
'frozenset',
'slice',
}


class RestrictedUnpickler(pickle.Unpickler):

def find_class(self, module, name):
"""Only allow safe classes from builtins"""
if module == "builtins" and name in safe_builtins:
return getattr(builtins, name)
"""Forbid everything else"""
raise pickle.UnpicklingError("global '%s.%s' is forbidden" %
(module, name))

def restricted_loads(s):
"""Helper function analogous to pickle.loads()"""
return RestrictedUnpickler(io.BytesIO(s)).load()

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -43,5 +69,5 @@ def depickle_legacy_package_name_compatible(pickled_string):
'Regenerate metadata.', legacy_package_name, legacy_module, legacy_module)

pickled_string = modified_pickled_string

restricted_loads(pickled_string)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We are ignoring the return value here. Also, the implementation fails the tests. Started #640 to resolve test issues.

return pickle.loads(pickled_string)