Skip to content

Commit

Permalink
Add some regression tests for this change
Browse files Browse the repository at this point in the history
  • Loading branch information
shreve committed Feb 8, 2023
1 parent fd34cf5 commit 6a8c8c3
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions IPython/core/tests/test_interactiveshell.py
Expand Up @@ -1110,3 +1110,38 @@ def foo(*args, **kwargs):

# clean up
ip.Completer.custom_matchers.pop()

class TestShowTracebacksAttack(unittest.TestCase):
"""Test that the interactive shell is resilient against the client attack of
manipulating the showtracebacks method. These attacks shouldn't result in an
unhandled exception in the kernel."""

def test_set_show_tracebacks_none(self):
"""Test the case of the client setting showtracebacks to None"""

result = ip.run_cell("""
import IPython.core.interactiveshell
IPython.core.interactiveshell.InteractiveShell.showtraceback = None
assert False, "This should not raise an exception"
""")
print(result)

assert result.result is None
assert isinstance(result.error_in_exec, TypeError)
assert str(result.error_in_exec) == "'NoneType' object is not callable"

def test_set_show_tracebacks_noop(self):
"""Test the case of the client setting showtracebacks to a no op lambda"""

result = ip.run_cell("""
import IPython.core.interactiveshell
IPython.core.interactiveshell.InteractiveShell.showtraceback = lambda *args, **kwargs: None
assert False, "This should not raise an exception"
""")
print(result)

assert result.result is None
assert isinstance(result.error_in_exec, AssertionError)
assert str(result.error_in_exec) == "This should not raise an exception"

0 comments on commit 6a8c8c3

Please sign in to comment.