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.

9.0 KB
Raw Blame
"""The response limit must run before Requests buffers a response body."""

import contextlib
import gc
import gzip
import threading
import weakref
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

import pytest
import requests
import urllib3

from local_deep_research.security import safe_requests


@pytest.fixture
def capped_server(monkeypatch):
    monkeypatch.setattr(safe_requests, "MAX_RESPONSE_SIZE", 1024)

    class Handler(BaseHTTPRequestHandler):
        protocol_version = "HTTP/1.1"

        def do_GET(self):
            self._respond()

        def do_POST(self):
            self._respond()

        def _respond(self):
            if self.path == "/gzip":
                # Built per request, so only a test that asks for /gzip
                # compresses anything.
                compressed = gzip.compress(b"g" * 2048)
                self.send_response(200)
                self.send_header("Content-Encoding", "gzip")
                self.send_header("Content-Length", str(len(compressed)))
                self.end_headers()
                self.wfile.write(compressed)
            elif self.path == "/small":
                self.send_response(200)
                self.send_header("Content-Length", "512")
                self.end_headers()
                self.wfile.write(b"s" * 512)
            elif self.path == "/small-chunked":
                self.send_response(200)
                self.send_header("Transfer-Encoding", "chunked")
                self.end_headers()
                self.wfile.write(b"200\r\n" + b"s" * 512 + b"\r\n0\r\n\r\n")
            else:
                self.send_response(200)
                self.send_header("Transfer-Encoding", "chunked")
                self.end_headers()
                self.wfile.write(b"800\r\n" + b"x" * 2048 + b"\r\n0\r\n\r\n")
            self.wfile.flush()

        def log_message(self, *args):
            pass

    server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
    thread = threading.Thread(target=server.serve_forever, daemon=True)
    thread.start()
    try:
        yield f"http://127.0.0.1:{server.server_port}"
    finally:
        server.shutdown()
        server.server_close()
        thread.join(timeout=2)


def _fetch(client, url, **kwargs):
    if client == "safe_get":
        return safe_requests.safe_get(url, allow_localhost=True, **kwargs)
    if client == "safe_post":
        return safe_requests.safe_post(url, allow_localhost=True, **kwargs)
    with safe_requests.SafeSession(allow_localhost=True) as session:
        return session.request(
            "POST" if client == "session_post" else "GET", url, **kwargs
        )


@pytest.mark.parametrize(
    "client", ["safe_get", "safe_post", "session_get", "session_post"]
)
def test_chunked_body_over_cap_is_rejected_before_default_fetch_returns(
    capped_server, client
):
    with pytest.raises(ValueError, match="too large"):
        _fetch(client, f"{capped_server}/chunked")


@pytest.mark.parametrize("client", ["safe_get", "session_get"])
def test_compressed_body_is_capped_by_decoded_size(capped_server, client):
    with pytest.raises(ValueError, match="too large"):
        _fetch(client, f"{capped_server}/gzip")


@pytest.mark.parametrize("client", ["safe_get", "safe_post", "session_get"])
def test_small_body_remains_available(capped_server, client):
    response = _fetch(client, f"{capped_server}/small")
    try:
        assert response.content == b"s" * 512
    finally:
        response.close()


@pytest.mark.parametrize("requested", [None, 1_000_000])
def test_direct_raw_read_is_limited_before_allocation(monkeypatch, requested):
    monkeypatch.setattr(safe_requests, "MAX_RESPONSE_SIZE", 1024)

    class Raw:
        consumed = 0

        def read(self, amount=None):
            self.consumed += amount
            return b"x" * amount

        def close(self):
            pass

    raw = Raw()
    response = requests.Response()
    response.raw = raw
    safe_requests._check_response_size(response)

    with pytest.raises(ValueError, match="too large"):
        response.raw.read(requested)
    assert raw.consumed == 1025


@contextlib.contextmanager
def _cyclic_gc_off():
    """Leave reference counting as the only way objects are freed."""
    was_enabled = gc.isenabled()
    gc.disable()
    try:
        yield
    finally:
        if was_enabled:
            gc.enable()


@pytest.mark.parametrize("path", ["/small", "/small-chunked"])
@pytest.mark.parametrize(
    "client", ["safe_get", "safe_post", "session_get", "session_post"]
)
def test_dropped_guarded_response_is_freed_by_refcount(
    capped_server, client, path
):
    """A guarded response, its ``raw`` and its body are freed once dropped.

    The guard's state is stored on ``response.raw``. If it referred back
    to the response, the cycle Response -> raw -> guard -> Response would
    keep every response, with a body of up to MAX_RESPONSE_SIZE, alive
    until the cyclic garbage collector ran. If it referred back to
    ``raw`` (a wrapper holding the bound ``raw.read`` it replaces), the
    cycle raw -> wrapper -> raw would keep ``raw`` and urllib3's decoder
    state alive the same way. With that collector off, only reference
    counting can free them.
    """
    with _cyclic_gc_off():
        response = _fetch(client, f"{capped_server}{path}")
        body = response.content
        alive = weakref.ref(response)
        raw_alive = weakref.ref(response.raw)
        del response
        freed = alive() is None
        raw_freed = raw_alive() is None
    assert body == b"s" * 512
    assert freed, "the body guard keeps a dropped response alive"
    assert raw_freed, "the body guard keeps a dropped response's raw alive"


def test_dropped_response_with_a_second_guard_is_freed_by_refcount(
    capped_server,
):
    """A second guard, as ``_ensure_decoded_body_cap`` adds, keeps no cycle."""
    with safe_requests.SafeSession(allow_localhost=True) as session:
        with _cyclic_gc_off():
            response = session.get(f"{capped_server}/small", stream=True)
            safe_requests._install_body_guard(response)
            body = response.content
            alive = weakref.ref(response)
            raw_alive = weakref.ref(response.raw)
            del response
            freed = alive() is None
            raw_freed = raw_alive() is None
    assert body == b"s" * 512
    assert freed, "the stacked body guards keep a dropped response alive"
    assert raw_freed, "the stacked body guards keep a dropped raw alive"


@pytest.mark.parametrize("path", ["/small", "/small-chunked"])
@pytest.mark.parametrize(
    "client", ["safe_get", "safe_post", "session_get", "session_post"]
)
def test_chained_raw_read_of_a_streamed_response_still_works(
    capped_server, client, path
):
    """``safe_get(url, stream=True).raw.read()`` still returns the body.

    Nothing but the call itself holds ``raw`` here. A guard that kept no
    reference to ``raw`` but put its reader on the instance would find
    ``raw`` already freed when that reader ran: for an attribute found on
    the instance, CPython releases the object it was looked up on before
    making the call.
    """
    with _cyclic_gc_off():
        body = _fetch(client, f"{capped_server}{path}", stream=True).raw.read()
    assert body == b"s" * 512


@pytest.mark.parametrize(
    "client", ["safe_get", "safe_post", "session_get", "session_post"]
)
def test_chained_raw_read_over_cap_is_rejected(capped_server, client):
    """A chained direct read past the cap is still rejected."""
    with _cyclic_gc_off():
        with pytest.raises(ValueError, match="too large"):
            _fetch(client, f"{capped_server}/chunked", stream=True).raw.read()


def test_guarded_raws_of_one_class_share_one_subclass(capped_server):
    """Guarding a response does not create a class per response.

    A class is freed only by the cyclic garbage collector, so a new one
    per response would bring back the garbage this guard avoids.
    """
    first = _fetch("safe_get", f"{capped_server}/small")
    second = _fetch("safe_get", f"{capped_server}/small-chunked")
    try:
        assert type(first.raw) is type(second.raw)
        assert isinstance(first.raw, urllib3.response.HTTPResponse)
    finally:
        first.close()
        second.close()


def test_over_cap_read_closes_raw_after_the_response_is_dropped(
    capped_server,
):
    """Code that keeps only ``raw`` still hits the cap, and raw is closed.

    The guard holds the response weakly, so here the response is already
    gone when the cap is crossed, and the guard closes ``raw`` itself.
    """
    with safe_requests.SafeSession(allow_localhost=True) as session:
        with _cyclic_gc_off():
            response = session.get(f"{capped_server}/chunked", stream=True)
            raw = response.raw
            alive = weakref.ref(response)
            del response
            freed = alive() is None
            with pytest.raises(ValueError, match="too large"):
                raw.read()
            closed = raw.closed
    assert freed, "the body guard keeps a dropped response alive"
    assert closed, "the guard left the over-cap stream open"