fix: move sensitive log to debug mode (#7230)

This commit is contained in:
Tobias Wochinger 2024-02-28 09:45:50 +01:00 committed by GitHub
parent 380052a963
commit 419009b495
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 3 deletions

View File

@ -67,7 +67,9 @@ class TextLanguageRouter:
def detect_language(self, text: str) -> Optional[str]:
try:
language = langdetect.detect(text)
except langdetect.LangDetectException:
logger.warning("Langdetect cannot detect the language of text: %s", text)
except langdetect.LangDetectException as exception:
logger.warning("Langdetect cannot detect the language of text. Error: %s", exception)
# Only log the text in debug mode, as it might contain sensitive information
logger.debug("Langdetect cannot detect the language of text: %s", text)
language = None
return language

View File

@ -0,0 +1,12 @@
---
security:
- |
Remove the text value from a warning log in the `TextLanguageRouter` to avoid logging sensitive information.
The text can be still be shown by switching to the `debug` log level.
```python
import logging
logging.basicConfig(format="%(levelname)s - %(name)s - %(message)s", level=logging.WARNING)
logging.getLogger("haystack").setLevel(logging.DEBUG)
```

View File

@ -1,5 +1,6 @@
import logging
import pytest
from _pytest.logging import LogCaptureFixture
from haystack import Document
from haystack.components.routers import TextLanguageRouter
@ -38,8 +39,15 @@ class TestTextLanguageRouter:
result = classifier.run(text=german_sentence)
assert result == {"unmatched": german_sentence}
def test_warning_if_no_language_detected(self, caplog):
def test_warning_if_no_language_detected(self, caplog: LogCaptureFixture):
with caplog.at_level(logging.WARNING):
classifier = TextLanguageRouter()
classifier.run(text=".")
assert "Langdetect cannot detect the language of text. Error: No features in text." in caplog.text
def test_warning_if_no_language_detected_if_debug(self, caplog: LogCaptureFixture):
with caplog.at_level(logging.DEBUG):
classifier = TextLanguageRouter()
classifier.run(text=".")
assert "Langdetect cannot detect the language of text. Error: No features in text." in caplog.text
assert "Langdetect cannot detect the language of text: ." in caplog.text