Skip to content

~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.

2.8 KB
Raw Blame
"""The OpenAI-compatible provider never logs a credential embedded in its
base URL.

``create_llm`` logs ``endpoint: <base_url>`` at INFO on every LLM creation,
and ``list_models_for_api`` logs the URL at DEBUG. For the custom
``openai_endpoint`` provider the base URL is operator input returned
unchanged by ``normalize_url``, so a key placed in it (userinfo or
``?api_key=``) reached the logs verbatim.

``create_llm`` runs for real (``ChatOpenAI`` construction makes no request).
The only stubbed seam is ``openai.OpenAI`` for model listing, which would
otherwise make a network call. The endpoint is a private IP literal, so
SSRF validation needs no DNS lookup.
"""

from unittest.mock import MagicMock, patch

from loguru import logger

from local_deep_research.llm.providers.implementations.custom_openai_endpoint import (
    CustomOpenAIEndpointProvider,
)

KEY = "sk-leakedkey1234567890"
PASSWORD = "pw-leaked-9876"
ENDPOINT = f"http://user:{PASSWORD}@192.168.1.50:8000/v1?api_key={KEY}"


def _capture():
    messages = []
    logger.enable("local_deep_research")
    sink_id = logger.add(
        lambda m: messages.append(m.record["message"]), level="DEBUG"
    )
    return messages, sink_id


def _release(sink_id):
    logger.remove(sink_id)
    logger.disable("local_deep_research")


def _assert_no_credential(messages):
    for message in messages:
        assert KEY not in message, message
        assert PASSWORD not in message, message


def test_create_llm_endpoint_log_is_redacted():
    snapshot = {
        "llm.openai_endpoint.url": {"value": ENDPOINT},
        "llm.openai_endpoint.api_key": {"value": "not-the-logged-key"},
    }
    messages, sink_id = _capture()
    try:
        llm = CustomOpenAIEndpointProvider.create_llm(
            model_name="some-model", settings_snapshot=snapshot
        )
    finally:
        _release(sink_id)

    assert llm is not None
    creating = [m for m in messages if "LLM with model: some-model" in m]
    assert creating, f"create_llm never logged its endpoint: {messages}"
    assert (
        "endpoint: http://192.168.1.50:8000/v1?api_key=REDACTED"
        in (creating[0])
    ), creating[0]
    _assert_no_credential(messages)


def test_list_models_endpoint_log_is_redacted():
    client = MagicMock()
    client.models.list.return_value = MagicMock(data=[])
    messages, sink_id = _capture()
    try:
        with patch("openai.OpenAI", return_value=client) as openai_cls:
            CustomOpenAIEndpointProvider.list_models_for_api(
                api_key="not-the-logged-key", base_url=ENDPOINT
            )
    finally:
        _release(sink_id)

    openai_cls.assert_called_once()
    fetching = [m for m in messages if m.startswith("Fetching models from")]
    assert fetching, f"list_models_for_api never logged its URL: {messages}"
    assert "api_key=REDACTED" in fetching[0], fetching[0]
    _assert_no_credential(messages)