Skip to content

Commit

Permalink
[Enhancement] - Sanitize sensitive data from .cli.his (#6361)
Browse files Browse the repository at this point in the history
* Sanitize sensitive data from .cli.his

* fix long command providers

---------

Co-authored-by: Diogo Sousa <[email protected]>
  • Loading branch information
IgorWounds and montezdesousa authored May 2, 2024
1 parent 4097033 commit 18115e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
24 changes: 24 additions & 0 deletions cli/openbb_cli/config/completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]

Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion cli/openbb_cli/config/menu_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions cli/openbb_cli/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 18115e4

Please sign in to comment.