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.

24.0 KB
Raw Blame
"""Regression tests for the resolve-vs-connect pin (``security.dns_pinning``).

These prove that the address the SSRF guard validated is the address the
outbound connection actually targets, closing the window where
``requests``/``urllib3`` would re-resolve the hostname independently at
connect time.

The gap is simulated by driving the resolver seam
(``dns_pinning._real_getaddrinfo``): validation sees one answer while a
naive connect-time re-resolve would see a different, attacker-chosen one.
The pin makes the connection reuse the validated answer, so:

* a host that validated as public but "rebinds" to a metadata/private IP
  is rejected at connect time (no connection to the rebind target), and
* a legitimate host connects to exactly the validated address and is never
  re-resolved at connect time.
"""

import importlib
import socket
import threading
from http.server import BaseHTTPRequestHandler, HTTPServer
from unittest.mock import patch

import pytest
import requests

from local_deep_research.security import dns_pinning
from local_deep_research.security.safe_requests import (
    safe_get,
    safe_post,
    SafeSession,
)


# --------------------------------------------------------------------------
# Test doubles
# --------------------------------------------------------------------------
class _Handler(BaseHTTPRequestHandler):
    marker = b"PINNED-OK"

    def do_GET(self):
        body = self.marker
        self.send_response(200)
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

    def do_POST(self):
        self.do_GET()

    def log_message(self, *args):  # silence the test server
        pass


def _start_server(bind_ip: str, marker: bytes = b"PINNED-OK") -> HTTPServer:
    """Start a loopback HTTP server bound to a specific 127.0.0.0/8 IP.

    Binding distinct loopback IPs (127.0.0.1 vs 127.0.0.2) lets a test tell
    apart "connected to the pinned IP" from "connected to the rebind IP":
    only one of them has a listener.
    """
    handler = type("BoundHandler", (_Handler,), {"marker": marker})
    server = HTTPServer((bind_ip, 0), handler)
    thread = threading.Thread(target=server.serve_forever, daemon=True)
    thread.start()
    return server


class _Resolver:
    """Stand-in for ``dns_pinning._real_getaddrinfo``.

    ``answers`` maps host -> list of successive answers; each ``getaddrinfo``
    call for a host consumes the next answer, and the final answer sticks
    for every later call (a rebind that then stays put). Each answer is a
    list of IP-address strings. ``calls`` records the (host, port) of every
    lookup so a test can assert that no extra connect-time resolution
    happened.
    """

    def __init__(self, answers: dict):
        self._answers = {
            h.lower().rstrip("."): list(v) for h, v in answers.items()
        }
        self.calls: list = []

    def host_calls(self, host: str) -> int:
        key = host.lower().rstrip(".")
        return sum(
            1 for h, _ in self.calls if (h or "").lower().rstrip(".") == key
        )

    def __call__(self, host, port, family=0, type=0, proto=0, flags=0):
        self.calls.append((host, port))
        seq = self._answers.get((host or "").lower().rstrip("."))
        if not seq:
            raise socket.gaierror(socket.EAI_NONAME, f"no answer for {host}")
        ips = seq[0] if len(seq) == 1 else seq.pop(0)
        results = []
        for ip in ips:
            fam = socket.AF_INET6 if ":" in ip else socket.AF_INET
            if fam == socket.AF_INET6:
                sockaddr = (ip, port or 0, 0, 0)
            else:
                sockaddr = (ip, port or 0)
            results.append(
                (fam, socket.SOCK_STREAM, socket.IPPROTO_TCP, "", sockaddr)
            )
        return results


# --------------------------------------------------------------------------
# Core proofs
# --------------------------------------------------------------------------
def test_connection_consumes_pin_with_no_connect_time_relookup():
    """Happy path: the connection consumes the pinned resolution and does NOT
    perform a third, connect-time DNS lookup.

    Resolver answers: call #1 is ``validate_url``, call #2 is the pin; the
    fetch succeeds and the resolver is consulted exactly twice, so there is
    no separate connect-time re-resolution window for the happy path. (This
    asserts the mechanism — no redundant connect-time lookup — not that a
    rebind is defeated; the security discrimination that a post-validation
    rebind to a metadata/private IP is *refused* is covered by the
    ``test_rebind_to_*`` tests below, which fail without the pin.)
    """
    server = _start_server("127.0.0.1", b"PINNED-OK")
    port = server.server_address[1]
    resolver = _Resolver(
        {"pinned.example": [["127.0.0.1"], ["127.0.0.1"], ["127.0.0.2"]]}
    )
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            resp = safe_get(
                f"http://pinned.example:{port}/",
                allow_localhost=True,
                timeout=5,
            )
        assert resp.status_code == 200
        assert resp.content == b"PINNED-OK"
        # Exactly two lookups: validate_url + pin. Zero at connect time —
        # the rebind answer (127.0.0.2) was never consumed.
        assert resolver.host_calls("pinned.example") == 2
    finally:
        server.shutdown()


def test_rebind_to_metadata_ip_is_blocked_at_connect_time():
    """Validation sees a public IP; the pin's connect-time re-validation
    catches the rebind to a cloud-metadata IP and refuses to connect.
    """
    resolver = _Resolver(
        {"rebind.example": [["93.184.216.34"], ["169.254.169.254"]]}
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with patch(
            "local_deep_research.security.safe_requests.requests.get"
        ) as mock_get:
            with pytest.raises(ValueError, match="SSRF"):
                safe_get("http://rebind.example/", timeout=5)
            # The connection was never attempted: the pin rejected before
            # handing anything to requests.
            mock_get.assert_not_called()
    # Two lookups only: validate_url (public, passes) + pin (metadata,
    # rejects). No connect-time lookup.
    assert resolver.host_calls("rebind.example") == 2


def test_rebind_to_private_ip_is_blocked_at_connect_time():
    """Same as above but the rebind target is an RFC1918 address, with
    default flags (allow_private_ips=False)."""
    resolver = _Resolver({"rebind2.example": [["93.184.216.34"], ["10.1.2.3"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with patch(
            "local_deep_research.security.safe_requests.requests.get"
        ) as mock_get:
            with pytest.raises(ValueError, match="SSRF"):
                safe_get("http://rebind2.example/", timeout=5)
            mock_get.assert_not_called()


def test_host_resolving_only_to_blocked_ip_is_rejected():
    """A hostname that resolves solely to a blocked IP is rejected (at
    validation, before the pin) and never connected to."""
    resolver = _Resolver({"internal.example": [["169.254.169.254"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with patch(
            "local_deep_research.security.safe_requests.requests.get"
        ) as mock_get:
            with pytest.raises(ValueError, match="SSRF|security validation"):
                safe_get("http://internal.example/", timeout=5)
            mock_get.assert_not_called()


def test_legitimate_public_host_still_fetches_through_pin():
    """A permitted host resolves and fetches end-to-end through the pin,
    connecting to exactly the validated address."""
    server = _start_server("127.0.0.1", b"HELLO")
    port = server.server_address[1]
    resolver = _Resolver({"ok.example": [["127.0.0.1"]]})
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            resp = safe_get(
                f"http://ok.example:{port}/",
                allow_localhost=True,
                timeout=5,
            )
        assert resp.status_code == 200
        assert resp.content == b"HELLO"
    finally:
        server.shutdown()


def test_private_host_reachable_when_allow_private_ips_preserved():
    """PRIVATE_ONLY-style scope: with allow_private_ips=True a private
    address is intentionally reachable — the pin must not weaken that."""
    server = _start_server("127.0.0.1", b"LAB")
    port = server.server_address[1]
    # 127.0.0.1 is loopback; allow_private_ips permits loopback + RFC1918.
    resolver = _Resolver({"lab.example": [["127.0.0.1"]]})
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            resp = safe_get(
                f"http://lab.example:{port}/",
                allow_private_ips=True,
                timeout=5,
            )
        assert resp.status_code == 200
        assert resp.content == b"LAB"
    finally:
        server.shutdown()


def test_redirect_chain_repins_each_hop():
    """Each redirect hop is independently resolved, validated, and pinned.

    Host A (a real server on 127.0.0.1) redirects to host B. Host B's
    resolver answers good, good, then bad — so if the hop failed to pin,
    the connect-time re-resolve would hit the dead rebind IP. The chain
    completes against host B's real server, and host B is resolved exactly
    twice (validate + pin) with no connect-time lookup.
    """
    server_b = _start_server("127.0.0.1", b"HOP-B-OK")
    port_b = server_b.server_address[1]

    class _RedirectHandler(BaseHTTPRequestHandler):
        def do_GET(self):
            self.send_response(302)
            self.send_header("Location", f"http://host-b.example:{port_b}/")
            self.end_headers()

        def log_message(self, *args):
            pass

    server_a = HTTPServer(("127.0.0.1", 0), _RedirectHandler)
    threading.Thread(target=server_a.serve_forever, daemon=True).start()
    port_a = server_a.server_address[1]

    resolver = _Resolver(
        {
            "host-a.example": [["127.0.0.1"]],
            "host-b.example": [["127.0.0.1"], ["127.0.0.1"], ["127.0.0.2"]],
        }
    )
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            resp = safe_get(
                f"http://host-a.example:{port_a}/",
                allow_localhost=True,
                timeout=5,
            )
        assert resp.status_code == 200
        assert resp.content == b"HOP-B-OK"
        # Host B: validate + pin only, no connect-time re-resolution (the
        # 127.0.0.2 rebind answer stays unconsumed).
        assert resolver.host_calls("host-b.example") == 2
    finally:
        server_a.shutdown()
        server_b.shutdown()


# --------------------------------------------------------------------------
# safe_post proofs
# --------------------------------------------------------------------------
def test_safe_post_rebind_to_metadata_ip_is_blocked_at_connect_time():
    """``safe_post`` mirror of ``test_rebind_to_metadata_ip_is_blocked_at_connect_time``.

    Validation sees a public IP; the pin's connect-time re-validation
    catches the rebind to a cloud-metadata IP and refuses to connect —
    before ``requests.post`` is ever called. Without the pin (or with it
    disabled), this rebind would sail through: ``safe_post`` only validates
    the URL once up front, so a second, independent resolution at connect
    time would observe the rebind answer and POST to it.
    """
    resolver = _Resolver(
        {"rebind-post.example": [["93.184.216.34"], ["169.254.169.254"]]}
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with patch(
            "local_deep_research.security.safe_requests.requests.post"
        ) as mock_post:
            with pytest.raises(ValueError, match="SSRF"):
                safe_post("http://rebind-post.example/", timeout=5)
            # The connection was never attempted: the pin rejected before
            # handing anything to requests.
            mock_post.assert_not_called()
    # Two lookups only: validate_url (public, passes) + pin (metadata,
    # rejects). No connect-time lookup.
    assert resolver.host_calls("rebind-post.example") == 2


def test_safe_post_rebind_to_private_ip_is_blocked_at_connect_time():
    """Same as above but the rebind target is an RFC1918 address, with
    default flags (allow_private_ips=False)."""
    resolver = _Resolver(
        {"rebind2-post.example": [["93.184.216.34"], ["10.1.2.3"]]}
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with patch(
            "local_deep_research.security.safe_requests.requests.post"
        ) as mock_post:
            with pytest.raises(ValueError, match="SSRF"):
                safe_post("http://rebind2-post.example/", timeout=5)
            mock_post.assert_not_called()


class _MethodEchoHandler(BaseHTTPRequestHandler):
    """Replies 200 with the HTTP method name as the body.

    Lets a redirect test assert which method actually reached the final
    hop (GET after a 301/302/303 downgrade vs. POST preserved for 307/308)
    without needing to inspect request internals.
    """

    def _reply(self):
        body = self.command.encode()
        self.send_response(200)
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

    def do_GET(self):
        self._reply()

    def do_POST(self):
        length = int(self.headers.get("Content-Length", 0) or 0)
        if length:
            self.rfile.read(length)
        self._reply()

    def log_message(self, *args):  # silence the test server
        pass


@pytest.mark.parametrize(
    "status_code,expected_method",
    [
        (302, "GET"),  # POST -> GET downgrade
        (307, "POST"),  # method preserved
    ],
)
def test_safe_post_redirect_repins_hop_and_resolves_method(
    status_code, expected_method
):
    """Each ``safe_post`` redirect hop is independently pinned, and the
    method conversion (301/302/303 -> GET, 307/308 preserve POST) matches
    RFC 7231 / ``_resolve_redirect_method``.

    Mirrors ``test_redirect_chain_repins_each_hop``: host B's resolver
    answers good, good, then bad. If the hop's pin were skipped, urllib3's
    own connect-time re-resolution would observe the rebind (dead) IP and
    the fetch would fail instead of completing against the real server —
    so host B being resolved exactly twice (validate + pin, no connect-time
    lookup) is the discriminating assertion.

    The POST carries no body: a 307 leaving the scope of the URL the caller
    addressed is refused rather than re-sent (#6265, covered in
    ``test_ssrf_redirect_bypass.TestSafePostBodyScope``), and the method
    conversion asserted here does not depend on one.
    """
    server_b = HTTPServer(("127.0.0.1", 0), _MethodEchoHandler)
    threading.Thread(target=server_b.serve_forever, daemon=True).start()
    port_b = server_b.server_address[1]

    class _RedirectHandler(BaseHTTPRequestHandler):
        def do_POST(self):
            length = int(self.headers.get("Content-Length", 0) or 0)
            if length:
                self.rfile.read(length)
            self.send_response(status_code)
            self.send_header(
                "Location", f"http://host-b-post.example:{port_b}/"
            )
            self.end_headers()

        def log_message(self, *args):
            pass

    server_a = HTTPServer(("127.0.0.1", 0), _RedirectHandler)
    threading.Thread(target=server_a.serve_forever, daemon=True).start()
    port_a = server_a.server_address[1]

    resolver = _Resolver(
        {
            "host-a-post.example": [["127.0.0.1"]],
            "host-b-post.example": [
                ["127.0.0.1"],
                ["127.0.0.1"],
                ["127.0.0.2"],
            ],
        }
    )
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            resp = safe_post(
                f"http://host-a-post.example:{port_a}/",
                allow_localhost=True,
                timeout=5,
            )
        assert resp.status_code == 200
        assert resp.content == expected_method.encode()
        # Host B: validate + pin only, no connect-time re-resolution (the
        # 127.0.0.2 rebind answer stays unconsumed).
        assert resolver.host_calls("host-b-post.example") == 2
    finally:
        server_a.shutdown()
        server_b.shutdown()


def test_safesession_pins_and_blocks_rebind():
    """SafeSession.send pins per hop: a host validated as public but
    rebinding to a metadata IP is rejected before any connection."""
    resolver = _Resolver(
        {"sess.example": [["93.184.216.34"], ["169.254.169.254"]]}
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with SafeSession() as session:
            with pytest.raises(ValueError, match="SSRF|security validation"):
                session.get("http://sess.example/", timeout=5)


def test_safesession_fetches_through_pin():
    """SafeSession connects to the validated address end-to-end."""
    server = _start_server("127.0.0.1", b"SESSION-OK")
    port = server.server_address[1]
    resolver = _Resolver({"sess-ok.example": [["127.0.0.1"]]})
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            with SafeSession(allow_localhost=True) as session:
                resp = session.get(f"http://sess-ok.example:{port}/", timeout=5)
        assert resp.status_code == 200
        assert resp.content == b"SESSION-OK"
    finally:
        server.shutdown()


# --------------------------------------------------------------------------
# Unit-level checks on the pinning primitives
# --------------------------------------------------------------------------
def test_ip_literal_is_not_pinned():
    """IP-literal URLs have no DNS to rebind; the context manager is a
    no-op and never touches the resolver."""
    resolver = _Resolver({})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.pinned_request("http://93.184.216.34/"):
            pass
    assert resolver.calls == []


def test_pin_registry_is_cleared_after_context():
    """The pin is scoped strictly to the context and leaves no residue in
    the thread-local registry."""
    resolver = _Resolver({"scoped.example": [["93.184.216.34"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.pinned_request("http://scoped.example/"):
            assert "scoped.example" in dns_pinning._get_pins()
        assert "scoped.example" not in dns_pinning._get_pins()


def test_pinned_getaddrinfo_passes_through_when_unpinned():
    """With no active pin, the shim delegates unchanged to the real
    resolver (transparent for all non-pinned lookups)."""
    resolver = _Resolver({"passthrough.example": [["93.184.216.34"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        result = socket.getaddrinfo("passthrough.example", 80)
    assert result[0][4][0] == "93.184.216.34"
    assert resolver.host_calls("passthrough.example") == 1


def test_pinned_getaddrinfo_filters_by_requested_socktype():
    """A pinned entry is always resolved as SOCK_STREAM (see
    ``_resolve_and_validate``). A caller that explicitly asks for a
    different socket type (e.g. ``SOCK_DGRAM``) while the host happens to
    be pinned must NOT get back the SOCK_STREAM/IPPROTO_TCP entry relabeled
    with the requested type — it must fail closed instead, exactly like a
    requested-family mismatch does.
    """
    resolver = _Resolver({"socktype.example": [["93.184.216.34"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.pinned_request("http://socktype.example/"):
            # A SOCK_STREAM (or unspecified, 0) request is unaffected — it
            # is exactly what urllib3's own connection lookups request.
            stream_result = socket.getaddrinfo(
                "socktype.example", 80, 0, socket.SOCK_STREAM
            )
            assert stream_result[0][1] == socket.SOCK_STREAM
            any_result = socket.getaddrinfo("socktype.example", 80)
            assert any_result[0][1] == socket.SOCK_STREAM

            # A mismatched SOCK_DGRAM request must fail closed rather than
            # silently returning the SOCK_STREAM/IPPROTO_TCP entry
            # relabeled as SOCK_DGRAM.
            with pytest.raises(socket.gaierror):
                socket.getaddrinfo("socktype.example", 80, 0, socket.SOCK_DGRAM)
    # Only the real (pin-establishing) resolution happened; neither the
    # SOCK_STREAM lookups nor the rejected SOCK_DGRAM lookup fell through
    # to a fresh resolver call.
    assert resolver.host_calls("socktype.example") == 1


def test_gaierror_at_pin_time_surfaces_as_connection_error():
    """If the host stops resolving between validation and pin, the pin
    fails closed as a transport error (retryable), not a silent bypass."""

    class _FailSecondResolve:
        def __init__(self):
            self.n = 0

        def __call__(self, host, port, *a, **k):
            self.n += 1
            if self.n == 1:
                return [
                    (
                        socket.AF_INET,
                        socket.SOCK_STREAM,
                        socket.IPPROTO_TCP,
                        "",
                        ("93.184.216.34", port or 0),
                    )
                ]
            raise socket.gaierror(socket.EAI_NONAME, "gone")

    resolver = _FailSecondResolve()
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with pytest.raises(requests.ConnectionError):
            safe_get("http://transient.example/", timeout=5)


def test_reload_does_not_make_the_shim_call_itself():
    """Reloading the module must not turn every unpinned lookup into infinite
    recursion.

    The module captures the real resolver at import (``_real_getaddrinfo =
    socket.getaddrinfo``) and ``install()`` then swaps ``socket.getaddrinfo``
    for the shim. A naive ``importlib.reload`` re-runs that capture while the
    shim is already installed, re-capturing the shim as the "real" resolver so
    the shim's pass-through calls itself → ``RecursionError`` process-wide on
    every unpinned lookup. The capture guard must prevent that: after a reload
    the captured resolver is never our own shim, and a plain resolution still
    returns.
    """
    importlib.reload(dns_pinning)
    try:
        # The captured "real" resolver must not be our shim (which is what
        # ``socket.getaddrinfo`` now is).
        assert not getattr(
            dns_pinning._real_getaddrinfo, dns_pinning._SHIM_MARKER, False
        )
        assert dns_pinning._real_getaddrinfo is not socket.getaddrinfo
        # An unpinned loopback lookup must resolve without recursing. It goes
        # through the installed shim, falls through to the real resolver, and
        # returns instead of raising RecursionError.
        result = socket.getaddrinfo("127.0.0.1", 80)
        assert result and result[0][4][0] == "127.0.0.1"
    finally:
        # Re-establish a clean, self-consistent module state for later tests.
        importlib.reload(dns_pinning)


def test_pinned_request_forwards_block_link_local():
    """``block_link_local`` reaches the connect-time re-validation: a host
    whose answer sits in 169.254.0.0/16 is refused under
    ``allow_private_ips`` when the caller keeps link-local blocked, and is
    pinned as before when it does not."""
    resolver = _Resolver({"ll.example": [["169.254.10.5"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with pytest.raises(ValueError, match="SSRF|security validation"):
            with dns_pinning.pinned_request(
                "http://ll.example/",
                allow_private_ips=True,
                block_link_local=True,
            ):
                pass
        assert "ll.example" not in dns_pinning._get_pins()

    resolver = _Resolver({"ll.example": [["169.254.10.5"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.pinned_request(
            "http://ll.example/", allow_private_ips=True
        ):
            assert "ll.example" in dns_pinning._get_pins()