~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Notify-time redaction of Apprise Telegram token-bearing diagnostics.
Apprise 1.12.0's Telegram plugin logs token-bearing POST URLs while
notifying (``telegram.py`` builds ``https://api.telegram.org/bot<token>/...``
and logs it in ``detect_bot_owner`` / the attachment path). The repo
installs a sanitizing log-record factory
(``notifications.apprise_log_utils``) that scrubs every ``apprise`` /
``apprise.*`` record before any handler renders it; these tests prove
both halves against the REAL notify path with the network mocked out.
"""
import logging
from collections.abc import Generator
from typing import List
from unittest.mock import patch
import pytest
import requests
from local_deep_research.notifications.apprise_log_utils import (
APPRISE_DIAGNOSTIC,
)
from local_deep_research.notifications.service import NotificationService
TG_FAKE_CREDENTIAL = "123456789:AAexample_token" # obviously fake constant
# Trigger path (chosen after reading apprise 1.12.0 telegram.py): a
# TARGET-LESS tgram:// URL parses zero chat ids, so notify() falls back to
# detect_owner -> detect_bot_owner(), which logs the token-bearing
# "Telegram User Detection POST URL" diagnostic (debug) BEFORE its HTTP
# POST — the most deterministic token-bearing path (no attachment file or
# phone-style target needed). requests.post is mocked to raise so Apprise
# emits its full diagnostics with zero network activity.
TGRAM_URL = f"tgram://{TG_FAKE_CREDENTIAL}/"
class _CapturingHandler(logging.Handler):
def __init__(self) -> None:
super().__init__(level=logging.DEBUG)
self.records: List[logging.LogRecord] = []
def emit(self, record: logging.LogRecord) -> None:
self.records.append(record)
@pytest.fixture(autouse=True)
def _restore_apprise_logging_state() -> Generator[None, None, None]:
apprise_logger = logging.getLogger("apprise")
original_factory = logging.getLogRecordFactory()
logger_state = (
tuple(apprise_logger.filters),
tuple(apprise_logger.handlers),
apprise_logger.level,
apprise_logger.propagate,
)
yield
logging.setLogRecordFactory(original_factory)
apprise_logger.filters[:] = logger_state[0]
apprise_logger.handlers[:] = logger_state[1]
apprise_logger.setLevel(logger_state[2])
apprise_logger.propagate = logger_state[3]
def _apprise_records_with_token(
records: List[logging.LogRecord],
) -> List[logging.LogRecord]:
return [
record
for record in records
if record.name == "apprise" or record.name.startswith("apprise.")
if TG_FAKE_CREDENTIAL in record.getMessage()
or TG_FAKE_CREDENTIAL in str(record.args or ())
]
def test_notify_time_telegram_token_diagnostics_are_redacted() -> None:
# Constructing the service installs the sanitizing record factory the
# production send path uses (see NotificationService.__init__).
NotificationService(outbound_allowed=True)
sanitizing_factory = logging.getLogRecordFactory()
handler = _CapturingHandler()
apprise_logger = logging.getLogger("apprise")
apprise_logger.addHandler(handler)
apprise_logger.setLevel(logging.DEBUG)
apprise_logger.propagate = False
def _notify_and_collect() -> List[logging.LogRecord]:
handler.records.clear()
apprise_obj = NotificationService._new_apprise()
assert apprise_obj.add(TGRAM_URL) is True
with patch(
"apprise.plugins.telegram.requests.post",
side_effect=requests.RequestException("mocked network failure"),
):
apprise_obj.notify(title="Test", body="Network-free body")
return list(handler.records)
# (a) Prove the diagnostic really fires and really carries the raw
# token in this scenario: temporarily replace the sanitizing factory
# with the default (unsanitized) LogRecord construction.
logging.setLogRecordFactory(
lambda *args, **kwargs: logging.LogRecord(*args, **kwargs)
)
try:
raw_pass = _notify_and_collect()
finally:
logging.setLogRecordFactory(sanitizing_factory)
assert _apprise_records_with_token(raw_pass), (
"expected the Telegram detect-owner diagnostic to carry the raw "
"bot token when the sanitizing factory is absent"
)
# (b) Production state: with the sanitizing factory installed, every
# captured record is free of the raw token (and Apprise records carry
# only the generic diagnostic).
sanitized_pass = _notify_and_collect()
assert sanitized_pass, "expected Apprise diagnostics during notify()"
assert not _apprise_records_with_token(sanitized_pass)
assert all(
record.msg == APPRISE_DIAGNOSTIC and record.args == ()
for record in sanitized_pass
)