From 5420bde963d7fcb5affe979934d12a7c035759e5 Mon Sep 17 00:00:00 2001 From: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com> Date: Thu, 2 May 2024 12:50:43 +0200 Subject: [PATCH 1/2] Sanitize sensitive data from .cli.his --- cli/openbb_cli/config/completer.py | 24 ++++++++++++++++++++++++ cli/openbb_cli/session.py | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/cli/openbb_cli/config/completer.py b/cli/openbb_cli/config/completer.py index f084c8214785..9cc50a9b7eb4 100644 --- a/cli/openbb_cli/config/completer.py +++ b/cli/openbb_cli/config/completer.py @@ -16,6 +16,7 @@ from prompt_toolkit.completion import CompleteEvent, Completer, Completion from prompt_toolkit.document import Document from prompt_toolkit.formatted_text import AnyFormattedText +from prompt_toolkit.history import FileHistory NestedDict = Mapping[str, Union[Any, Set[str], None, Completer]] @@ -401,3 +402,26 @@ def get_completions( # noqa: PLR0912 # This is a WordCompleter yield from completer.get_completions(document, complete_event) + + +class CustomFileHistory(FileHistory): + """Filtered file history.""" + + def sanitize_input(self, string: str) -> str: + """Sanitize sensitive information from the input string by parsing arguments.""" + keywords = ["--password", "--email", "--pat"] + string_list = string.split(" ") + + for kw in keywords: + if kw in string_list: + index = string_list.index(kw) + if len(string_list) > index + 1: + string_list[index + 1] = "********" + + result = " ".join(string_list) + return result + + def store_string(self, string: str) -> None: + """Store string in history.""" + string = self.sanitize_input(string) + super().store_string(string) diff --git a/cli/openbb_cli/session.py b/cli/openbb_cli/session.py index 96b63f10da96..ef63fd6e7f03 100644 --- a/cli/openbb_cli/session.py +++ b/cli/openbb_cli/session.py @@ -8,8 +8,8 @@ from openbb_core.app.model.abstract.singleton import SingletonMeta from openbb_core.app.model.user_settings import UserSettings as User from prompt_toolkit import PromptSession -from prompt_toolkit.history import FileHistory +from openbb_cli.config.completer import CustomFileHistory from openbb_cli.config.console import Console from openbb_cli.config.constants import HIST_FILE_PROMPT from openbb_cli.config.style import Style @@ -62,7 +62,7 @@ def _get_prompt_session(self) -> Optional[PromptSession]: try: if sys.stdin.isatty(): prompt_session: Optional[PromptSession] = PromptSession( - history=FileHistory(str(HIST_FILE_PROMPT)) + history=CustomFileHistory(str(HIST_FILE_PROMPT)) ) else: prompt_session = None From 683de15ef67b75d44c8d67eccbb6671152f93a3a Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Thu, 2 May 2024 12:18:28 +0100 Subject: [PATCH 2/2] fix long command providers --- cli/openbb_cli/config/menu_text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/openbb_cli/config/menu_text.py b/cli/openbb_cli/config/menu_text.py index d9bdc59656ca..0902e72bb695 100644 --- a/cli/openbb_cli/config/menu_text.py +++ b/cli/openbb_cli/config/menu_text.py @@ -204,7 +204,7 @@ def add_cmd(self, name: str, description: str = "", disable: bool = False): """ formatted_name = self._format_cmd_name(name) name_padding = (self.CMD_NAME_LENGTH - len(formatted_name)) * " " - providers = get_ordered_providers(f"{self.menu_path}{formatted_name}") + providers = get_ordered_providers(f"{self.menu_path}{name}") formatted_description = self._format_cmd_description( formatted_name, description,