Skip to content

Personal AI, On Personal Devices

Python

feat: SanitizingFormatter — auto-redact credentials in all log output

Jon Saad-Falcon687ad30f9ee4
diff --git a/src/openjarvis/cli/log_config.py b/src/openjarvis/cli/log_config.py
index 17174423..0bdf1a32 100644
--- a/src/openjarvis/cli/log_config.py
+++ b/src/openjarvis/cli/log_config.py
@@ -7,6 +7,18 @@ from logging.handlers import RotatingFileHandler
from pathlib import Path
from typing import Optional, Union
+from openjarvis.security.credential_stripper import CredentialStripper
+
+_stripper = CredentialStripper()
+
+
+class SanitizingFormatter(logging.Formatter):
+ """Formatter that redacts credentials from log messages."""
+
+ def format(self, record: logging.LogRecord) -> str:
+ msg = super().format(record)
+ return _stripper.strip(msg)
+
def setup_logging(
verbose: bool = False,
@@ -47,7 +59,7 @@ def setup_logging(
# Console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(level)
- fmt = logging.Formatter("%(levelname)s %(name)s: %(message)s")
+ fmt = SanitizingFormatter("%(levelname)s %(name)s: %(message)s")
console_handler.setFormatter(fmt)
logger.addHandler(console_handler)
@@ -65,7 +77,7 @@ def setup_logging(
backupCount=3,
)
file_handler.setLevel(logging.DEBUG)
- file_fmt = logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s")
+ file_fmt = SanitizingFormatter("%(asctime)s %(levelname)s %(name)s: %(message)s")
file_handler.setFormatter(file_fmt)
logger.addHandler(file_handler)
diff --git a/tests/security/test_log_sanitization.py b/tests/security/test_log_sanitization.py
new file mode 100644
index 00000000..33acc8a8
--- /dev/null
+++ b/tests/security/test_log_sanitization.py
@@ -0,0 +1,74 @@
+"""Tests for log sanitization (Section 5)."""
+
+from __future__ import annotations
+
+import logging
+
+
+class TestSanitizingFormatter:
+ """SanitizingFormatter should redact secrets in log messages."""
+
+ def test_redacts_openai_key(self) -> None:
+ from openjarvis.cli.log_config import SanitizingFormatter
+
+ fmt = SanitizingFormatter("%(message)s")
+ record = logging.LogRecord(
+ name="test",
+ level=logging.INFO,
+ pathname="",
+ lineno=0,
+ msg="Key is sk-proj-abc123def456ghi789jkl012mno345pqr678stu",
+ args=(),
+ exc_info=None,
+ )
+ result = fmt.format(record)
+ assert "sk-proj-" not in result
+ assert "[REDACTED" in result
+
+ def test_redacts_aws_key(self) -> None:
+ from openjarvis.cli.log_config import SanitizingFormatter
+
+ fmt = SanitizingFormatter("%(message)s")
+ record = logging.LogRecord(
+ name="test",
+ level=logging.INFO,
+ pathname="",
+ lineno=0,
+ msg="AWS: AKIAIOSFODNN7EXAMPLE",
+ args=(),
+ exc_info=None,
+ )
+ result = fmt.format(record)
+ assert "AKIAIOSFODNN7EXAMPLE" not in result
+
+ def test_clean_message_unchanged(self) -> None:
+ from openjarvis.cli.log_config import SanitizingFormatter
+
+ fmt = SanitizingFormatter("%(message)s")
+ record = logging.LogRecord(
+ name="test",
+ level=logging.INFO,
+ pathname="",
+ lineno=0,
+ msg="Server started on port 8000",
+ args=(),
+ exc_info=None,
+ )
+ result = fmt.format(record)
+ assert result == "Server started on port 8000"
+
+ def test_redacts_slack_token(self) -> None:
+ from openjarvis.cli.log_config import SanitizingFormatter
+
+ fmt = SanitizingFormatter("%(message)s")
+ record = logging.LogRecord(
+ name="test",
+ level=logging.INFO,
+ pathname="",
+ lineno=0,
+ msg="Token: xoxb-1234-5678-abcdefghij",
+ args=(),
+ exc_info=None,
+ )
+ result = fmt.format(record)
+ assert "xoxb-" not in result