Skip to content

Commit

Permalink
Fix escape via generators etc.
Browse files Browse the repository at this point in the history
Yes - we need to do allow-lists not deny-lists... 2.0

Co-authored-by: decorator-factory <decorator-factory@protonmail.com>
  • Loading branch information
danthedeckie and decorator-factory committed Oct 4, 2024
1 parent 9c90b10 commit 1ff1bda
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion simpleeval.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
- kurtmckee (Kurt McKee) Infrastructure updates
- edgarrmondragon (Edgar Ramírez-Mondragón) Address Python 3.12+ deprecation warnings
- cedk (Cédric Krier) <ced@b2ck.com> Allow running tests with Werror
- decorator-factory <decorator-factory@protonmail.com> More security fixes
-------------------------------------
Basic Usage:
Expand Down Expand Up @@ -115,7 +116,16 @@
MAX_SHIFT = 10000 # highest << or >> (lshift / rshift)
MAX_SHIFT_BASE = int(sys.float_info.max) # highest on left side of << or >>
DISALLOW_PREFIXES = ["_", "func_"]
DISALLOW_METHODS = ["format", "format_map", "mro"]
DISALLOW_METHODS = [
"format",
"format_map",
"mro",
"tb_frame",
"gi_frame",
"ag_frame",
"cr_frame",
"exec",
]

# Disallow functions:
# This, strictly speaking, is not necessary. These /should/ never be accessable anyway,
Expand Down
11 changes: 11 additions & 0 deletions test_simpleeval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,17 @@ def test_functions_are_disallowed_in_expressions(self):

simpleeval.DEFAULT_FUNCTIONS = DF.copy()

def test_breakout_via_generator(self):
# Thanks decorator-factory
class Foo:
def bar(self):
yield "Hello, world!"

evil = "foo.bar().gi_frame.f_globals['__builtins__'].exec('raise RuntimeError(\"Oh no\")')"

with self.assertRaises(FeatureNotAvailable):
simple_eval(evil, names={"foo": Foo()})


@unittest.skipIf(platform.python_implementation() == "PyPy", "GC set_debug not available in PyPy")
class TestReferenceCleanup(DRYTest):
Expand Down

0 comments on commit 1ff1bda

Please sign in to comment.