test_log_reader_redacts_old_lines.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Log lines written before a redaction existed are masked when read.
  2. The filters only mask lines written from now on; ``bambuddy.log`` can still
  3. hold older ones with a camera login (an access line with the camera URL in
  4. its query, a URL in a shape an older pattern missed). Lines are masked as
  5. they are read, before the search filter, so a search cannot confirm a secret
  6. either.
  7. """
  8. from backend.app.core.config import settings
  9. from backend.app.services.log_reader import read_log_entries
  10. ACCESS = (
  11. "2026-10-01 10:00:00,000 INFO [uvicorn.access] [-] 127.0.0.1:1 - "
  12. '"POST /api/v1/printers/1/camera/external/test'
  13. '?url=rtsp%3A%2F%2Fadmin%3Aold-pw-1%40cam%2Fs&camera_type=rtsp HTTP/1.1" 200'
  14. )
  15. APP = (
  16. "2026-10-01 10:00:01,000 INFO [backend.app.services.external_camera] [x] "
  17. "Testing camera connection: type=rtsp, url=rtsp://:old-pw-2@10.0.0.5/s..."
  18. )
  19. TRACE = [
  20. "2026-10-01 10:00:02,000 ERROR [backend.app.services.external_camera] [x] Stream failed",
  21. " Input #0, rtsp, from 'http://old-token-3@10.0.0.5/snap.jpg':",
  22. ]
  23. def _write(tmp_path, monkeypatch):
  24. (tmp_path / "bambuddy.log").write_text("\n".join([ACCESS, APP, *TRACE]) + "\n", encoding="utf-8")
  25. monkeypatch.setattr(settings, "log_dir", tmp_path)
  26. def test_old_lines_come_back_masked(tmp_path, monkeypatch):
  27. _write(tmp_path, monkeypatch)
  28. entries, _total = read_log_entries(limit=50)
  29. text = "\n".join(e.message for e in entries)
  30. assert len(entries) == 3
  31. for secret in ("old-pw-1", "old-pw-2", "old-token-3"):
  32. assert secret not in text
  33. assert "camera_type=rtsp" in text
  34. def test_a_search_cannot_confirm_a_secret(tmp_path, monkeypatch):
  35. _write(tmp_path, monkeypatch)
  36. for secret in ("old-pw-1", "old-pw-2", "old-token-3"):
  37. entries, _total = read_log_entries(limit=50, search=secret)
  38. assert entries == [], secret