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.

4.1 KB
Raw Blame
"""Preserve credential coverage when query scrubbing precedes log sinks."""

import pytest
from loguru import logger

from local_deep_research.security.log_sanitizer import (
    redact_log_record,
    scrub_error,
)
from tests.test_utils import restored_loguru_state


@pytest.mark.parametrize("control", ["\u2060", "\x00", "\n"])
@pytest.mark.parametrize(
    "template",
    [
        "x-api-key:{secret}.a{control}pikey=P",
        "redis://:{secret}_passwd={control}@db.example",
    ],
)
@pytest.mark.parametrize("known_secrets", [(), ("unrelated-known-secret",)])
def test_control_view_keeps_outer_credential_values_hidden(
    template, control, known_secrets
):
    # A credential-looking name inside a header/URL value must not shrink
    # the outer redaction to its label while leaving its value visible.
    secret = "Canary731xy"
    text = "Request failed: " + template.format(secret=secret, control=control)
    direct = {"message": text, "extra": {}}
    redact_log_record(direct)
    assert secret not in direct["message"]

    scrubbed = scrub_error(text, *known_secrets)
    with restored_loguru_state():
        logger.remove()
        logger.configure(patcher=redact_log_record)
        seen = []
        logger.add(seen.append, format="{message}", diagnose=False)
        logger.error(scrubbed)
    assert len(seen) == 1
    assert secret not in str(seen[0])
    assert str(seen[0]).startswith("Request failed: ")
    assert scrub_error(scrubbed, *known_secrets) == scrubbed


@pytest.mark.parametrize("padding", ["none", "before", "after"])
@pytest.mark.parametrize(
    ("message", "secret"),
    [
        (
            "secret=b_password='s://p?\u2028r apik\rey=\"Authorization=Z\"' 'Canary1310x5Z",
            "Canary1310x5Z",
        ),
        ("s://p?pw\u2028d=s://x?1\u2028p\twd= Canary1382x5Z", "Canary1382x5Z"),
        ('s://p?="secret=" Canary1690x4Z', "Canary1690x4Z"),
        ("s://p?='ApiKey=''Canary2311x4Z", "Canary2311x4Z"),
        ("s://p?secret=\x1cpw\x1cd='secret=Z' Canary4109x3Z", "Canary4109x3Z"),
        ("s://x?='ApiKey=' Canary5416x5Z", "Canary5416x5Z"),
        ('s://x?="secret=" Canary6671x6Z', "Canary6671x6Z"),
        (
            "s://p?password=\nAu\u2028thorization: Canary7190x1Z",
            "Canary7190x1Z",
        ),
        ("s://x?}passwd=\u2028passwor\nd= Canary8032x3Z'pwd=", "Canary8032x3Z"),
        ('s://x?="pwd="\'Canary8504x3Z', "Canary8504x3Z"),
        ('s://{?="Authorization=""Canary10405x4Z', "Canary10405x4Z"),
        (
            '\rapi_key="x\rCanary128920x0Z\u2028?https://h/p?ap\ti_key=Canary128920x1Z\r\x85OPENAI_API_KEY: "Canary128920x2Z"\x1c\u2028Bearer Canary128920x3Zabcdefghijk"\u200bpass\u2028word= \'Canary128920x4Z\'\x00{\rBearer Canary128920x5Zabcdefghijk:[https://u:Canary128920x6Z@h/x?q=1',
            "Canary128920x4Z",
        ),
        (
            "https://h/p?api_key=Canary133226x0Z{{\t\u200bBearer Canary133226x1Zabcdefghijk{/api_key='x;Bearer='Canary133226x2Z'@https://h/p?O\tPENAI_API_KEY=Canary133226x3Z?\r\tclient_secret = \"Canary133226x4Z\"\tpas\x00sword = \"Canary133226x5Z\"\u2028?\"password = 'Canary133226x6Z'",
            "Canary133226x5Z",
        ),
    ],
)
def test_query_scrubbing_keeps_later_credentials_redacted(
    message, secret, padding
):
    # Query guards must still protect values beyond the control-view budget.
    if padding == "before":
        message = "context " * 600 + message
    elif padding == "after":
        message += " diagnostic" * 600
    record = {"message": scrub_error(message), "extra": {}}
    redact_log_record(record)
    assert secret not in record["message"]


@pytest.mark.parametrize("repetitions", [1, 128])
def test_repeated_split_credentials_keep_no_value_after_scrubbing(repetitions):
    secret = "CanaryBounded12345678"
    message = (
        "Request failed: "
        + ("s://p?pw\u2028d=s://x?1\u2028p\twd= " + secret + "\n") * repetitions
        + " diagnostic-end"
    )
    record = {"message": scrub_error(message), "extra": {}}
    redact_log_record(record)
    assert secret not in record["message"]
    assert record["message"].startswith("Request failed: ")
    if repetitions == 1:
        assert record["message"].endswith(" diagnostic-end")