|
| 1 | +"""Unit tests for app_logging's record sanitization. |
| 2 | +
|
| 3 | +Covers the two jobs of ``_sanitize_log_text`` / ``LogSanitizingFilter``: |
| 4 | +console-safety (emoji and non-Latin-1 symbol stripping for Windows code-pages) |
| 5 | +and log-injection safety (CR/LF and other control characters are neutralised so |
| 6 | +an attacker-controlled value cannot forge or split log lines -- CWE-117). |
| 7 | +
|
| 8 | +Emoji and accented characters are built with ``chr()`` so this source file |
| 9 | +stays pure ASCII. |
| 10 | +""" |
| 11 | +import logging |
| 12 | + |
| 13 | +from app_logging import _sanitize_log_text, LogSanitizingFilter, configure_logging |
| 14 | + |
| 15 | +_CHECK_MARK = chr(0x2705) |
| 16 | +_MUSIC_NOTE = chr(0x1F3B5) |
| 17 | +_ACCENTED = "caf" + chr(0xE9) + " se" + chr(0xF1) + "or " + chr(0xFC) + "ber" |
| 18 | + |
| 19 | + |
| 20 | +class TestSanitizeLogText: |
| 21 | + def test_removes_emoji_and_symbols(self): |
| 22 | + assert _sanitize_log_text("done " + _CHECK_MARK) == "done" |
| 23 | + assert _sanitize_log_text("track " + _MUSIC_NOTE + " ready") == "track ready" |
| 24 | + |
| 25 | + def test_newline_becomes_space(self): |
| 26 | + assert _sanitize_log_text("hello\nworld") == "hello world" |
| 27 | + |
| 28 | + def test_crlf_collapses_to_single_space(self): |
| 29 | + assert _sanitize_log_text("hello\r\nworld") == "hello world" |
| 30 | + |
| 31 | + def test_control_chars_become_space(self): |
| 32 | + assert _sanitize_log_text("a\x00b\x07c\x7f") == "a b c" |
| 33 | + |
| 34 | + def test_tab_is_preserved(self): |
| 35 | + assert _sanitize_log_text("col1\tcol2") == "col1\tcol2" |
| 36 | + |
| 37 | + def test_latin1_accents_pass_through(self): |
| 38 | + assert _sanitize_log_text(_ACCENTED) == _ACCENTED |
| 39 | + |
| 40 | + def test_log_injection_cannot_forge_a_line(self): |
| 41 | + forged = "user42\n[INFO]-[fake]-dropped all tables" |
| 42 | + result = _sanitize_log_text(forged) |
| 43 | + assert "\n" not in result |
| 44 | + assert "\r" not in result |
| 45 | + assert result == "user42 [INFO]-[fake]-dropped all tables" |
| 46 | + |
| 47 | + def test_non_string_returned_unchanged(self): |
| 48 | + assert _sanitize_log_text(123) == 123 |
| 49 | + assert _sanitize_log_text(None) is None |
| 50 | + |
| 51 | + |
| 52 | +class TestLogSanitizingFilter: |
| 53 | + def _record(self, msg, args=None): |
| 54 | + return logging.LogRecord( |
| 55 | + name="test", level=logging.INFO, pathname=__file__, lineno=1, |
| 56 | + msg=msg, args=args, exc_info=None, |
| 57 | + ) |
| 58 | + |
| 59 | + def test_sanitizes_msg(self): |
| 60 | + record = self._record("oops\ninjected " + _CHECK_MARK) |
| 61 | + LogSanitizingFilter().filter(record) |
| 62 | + assert record.msg == "oops injected" |
| 63 | + |
| 64 | + def test_sanitizes_tuple_args_leaving_non_strings(self): |
| 65 | + record = self._record("%s %s", args=("a\nb", 7)) |
| 66 | + LogSanitizingFilter().filter(record) |
| 67 | + assert record.args == ("a b", 7) |
| 68 | + |
| 69 | + def test_sanitizes_dict_args(self): |
| 70 | + record = self._record("%(x)s") |
| 71 | + record.args = {"x": "p\nq", "n": 3} |
| 72 | + LogSanitizingFilter().filter(record) |
| 73 | + assert record.args == {"x": "p q", "n": 3} |
| 74 | + |
| 75 | + def test_filter_always_returns_true(self): |
| 76 | + assert LogSanitizingFilter().filter(self._record("hi")) is True |
| 77 | + |
| 78 | + |
| 79 | +class TestConfigureLogging: |
| 80 | + def test_attaches_sanitizing_filter_once(self): |
| 81 | + root = logging.getLogger() |
| 82 | + saved = {handler: list(handler.filters) for handler in root.handlers} |
| 83 | + try: |
| 84 | + configure_logging() |
| 85 | + configure_logging() |
| 86 | + assert root.handlers |
| 87 | + for handler in root.handlers: |
| 88 | + count = sum(isinstance(f, LogSanitizingFilter) for f in handler.filters) |
| 89 | + assert count == 1 |
| 90 | + finally: |
| 91 | + for handler in root.handlers: |
| 92 | + if handler in saved: |
| 93 | + handler.filters = saved[handler] |
| 94 | + else: |
| 95 | + handler.filters = [ |
| 96 | + f for f in handler.filters |
| 97 | + if not isinstance(f, LogSanitizingFilter) |
| 98 | + ] |
0 commit comments