Skip to content

Commit bcac032

Browse files
feat: add option to replace traceback on errors
Showing a traceback can be a security risk in a context such as Voila. (for instance showing paths, usernames or values in the exception message) Similar to what we do at notebook execution time at: voila-dashboards/voila#758 here we also disable the transfer of tracebacks at runtime.
1 parent 9ef90bb commit bcac032

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

jupyter_server/services/kernels/handlers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ def write_stderr(error_message):
323323
channel = getattr(stream, 'channel', None)
324324
msg_type = msg['header']['msg_type']
325325

326+
if channel == 'iopub' and msg_type == 'error':
327+
self._on_error(msg)
328+
329+
326330
if channel == 'iopub' and msg_type == 'status' and msg['content'].get('execution_state') == 'idle':
327331
# reset rate limit counter on status=idle,
328332
# to avoid 'Run All' hitting limits prematurely.
@@ -475,6 +479,12 @@ def on_restart_failed(self):
475479
logging.error("kernel %s restarted failed!", self.kernel_id)
476480
self._send_status_message('dead')
477481

482+
def _on_error(self, msg):
483+
if self.kernel_manager.allow_tracebacks:
484+
return
485+
msg['content']['ename'] = 'ExecutionError'
486+
msg['content']['evalue'] = 'Execution error'
487+
msg['content']['traceback'] = [self.kernel_manager.traceback_replacement_message]
478488

479489
#-----------------------------------------------------------------------------
480490
# URL to handler mappings

jupyter_server/services/kernels/kernelmanager.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ def __init__(self, **kwargs):
131131
"""
132132
)
133133

134+
allow_tracebacks = Bool(True, config=True, help=(
135+
'Whether to send tracebacks to clients on exceptions.'
136+
))
137+
138+
traceback_replacement_message = Unicode(
139+
'An exception occurred at runtime, which is not shown due to security reasons.',
140+
config=True,
141+
help=(
142+
'Message to print when allow_tracebacks is False, and an exception occurs'
143+
)
144+
)
145+
134146
#-------------------------------------------------------------------------
135147
# Methods for managing kernels and sessions
136148
#-------------------------------------------------------------------------

0 commit comments

Comments
 (0)