From dd9d53634faa4236155454df30d462ceb7387141 2024-10-29 00:15:52 From: Hacker-Dom <18601956+hacker-DOM@users.noreply.github.com> Date: 2024-10-29 00:15:52 Subject: [PATCH] Modify binding mechanism to allow binding to one of a set of whitelisted commands, Add five such commands ("beginning_of_buffer", "end_of_buffer", "end_of_line", "forward_word", "unix_line_discard"). --- diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py index 40e2c9a..fd42843 100644 --- a/IPython/terminal/interactiveshell.py +++ b/IPython/terminal/interactiveshell.py @@ -52,6 +52,7 @@ from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook from .ptutils import IPythonPTCompleter, IPythonPTLexer from .shortcuts import ( KEY_BINDINGS, + UNASSIGNED_ALLOWED_COMMANDS, create_ipython_shortcuts, create_identifier, RuntimeBinding, @@ -508,19 +509,23 @@ class TerminalInteractiveShell(InteractiveShell): # rebuild the bindings list from scratch key_bindings = create_ipython_shortcuts(self) - # for now we only allow adding shortcuts for commands which are already - # registered; this is a security precaution. - known_commands = { + # for now we only allow adding shortcuts for a specific set of + # commands; this is a security precution. + allowed_commands = { create_identifier(binding.command): binding.command for binding in KEY_BINDINGS } + allowed_commands.update({ + create_identifier(command): command + for command in UNASSIGNED_ALLOWED_COMMANDS + }) shortcuts_to_skip = [] shortcuts_to_add = [] for shortcut in user_shortcuts: command_id = shortcut["command"] - if command_id not in known_commands: - allowed_commands = "\n - ".join(known_commands) + if command_id not in allowed_commands: + allowed_commands = "\n - ".join(allowed_commands) raise ValueError( f"{command_id} is not a known shortcut command." f" Allowed commands are: \n - {allowed_commands}" @@ -544,7 +549,7 @@ class TerminalInteractiveShell(InteractiveShell): new_keys = shortcut.get("new_keys", None) new_filter = shortcut.get("new_filter", None) - command = known_commands[command_id] + command = allowed_commands[command_id] creating_new = shortcut.get("create", False) modifying_existing = not creating_new and ( diff --git a/IPython/terminal/shortcuts/__init__.py b/IPython/terminal/shortcuts/__init__.py index 12890f4..ba6d405 100644 --- a/IPython/terminal/shortcuts/__init__.py +++ b/IPython/terminal/shortcuts/__init__.py @@ -628,3 +628,11 @@ KEY_BINDINGS = [ *SIMPLE_CONTROL_BINDINGS, *ALT_AND_COMOBO_CONTROL_BINDINGS, ] + +UNASSIGNED_ALLOWED_COMMANDS = [ + nc.beginning_of_buffer, + nc.end_of_buffer, + nc.end_of_line, + nc.forward_word, + nc.unix_line_discard, +]