Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] - Sanitize sensitive data from .cli.his #6361

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading