You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just wanted to let you guys know that there is a work around for executing os module methods without the need of __import__. I'm aware that RestrictedPython is not a "sandbox" or a "secure environment", but since there was "Problematic code example" in the docs, I just wanted to show you how this can be bypassed. I'm not really sure if this is a "bug" or not, but I just wanted to share :)
Here, I used getattr since I couldn't access whatever that starts with an "_". 78 is the index of <class 'codecs.IncrementalEncoder'> and this index can vary. If you are locally testing this out make sure to first find the index of the class codecs.IncrementalEncoder. I also had to use pop because __getitem__ was not available.
@s0cket7 Thank you for your report, it has shown two weaknesses:
in the documentation: compile_restricted has to be called with dict(__builtins__=safe_builtins) instead of safe_builtins as the latter one keeps the getattr implementation from Python which allows all sorts of access. Using dict(__builtins__=safe_builtins) in your example results in NameError: name 'getattr' is not defined as RestrictedPython does not provided a default getattr implementation.
in the implementation of safer_getattr: Even using the provided safer_getattr implementation allows the kind of access you have shown. (It can be used using dict(__builtins__=safe_builtins, getattr=RestrictedPython.Guards.safer_getattr).)
@djay RestrictedPython 3.x is not affected because it does not provide an example getattr implementation.
Zope and Plone are not effected because they provide their own getattr implementation which do not allow to access attribute names starting with an underscore.
Activity
idea for fixing bug #142
loechel commentedon Oct 25, 2018
@s0cket7 thanks for your report, I have make a proposal to fix that in future releases, might you want to review #143
djay commentedon Oct 26, 2018
@loechel is this a bug in the new version of RestrictedPython or affects older versions too?
icemac commentedon Oct 26, 2018
@s0cket7 Thank you for your report, it has shown two weaknesses:
in the documentation:
compile_restrictedhas to be called withdict(__builtins__=safe_builtins)instead ofsafe_builtinsas the latter one keeps thegetattrimplementation from Python which allows all sorts of access. Usingdict(__builtins__=safe_builtins)in your example results inNameError: name 'getattr' is not definedasRestrictedPythondoes not provided a defaultgetattrimplementation.in the implementation of
safer_getattr: Even using the providedsafer_getattrimplementation allows the kind of access you have shown. (It can be used usingdict(__builtins__=safe_builtins, getattr=RestrictedPython.Guards.safer_getattr).)Both are fixed in #143.
PwnFunction commentedon Oct 26, 2018
My pleasure and thank you for fixing it quickly :)
icemac commentedon Oct 26, 2018
@djay RestrictedPython 3.x is not affected because it does not provide an example
getattrimplementation.Zope and Plone are not effected because they provide their own
getattrimplementation which do not allow to access attribute names starting with an underscore.Safer getattr (#143)