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.

5.5 KB
Raw Blame
import socket
from unittest.mock import patch

import pytest

from local_deep_research.security.notification_validator import (
    NotificationURLValidator,
    _is_scheme_less_url_fragment,
)


@pytest.mark.parametrize(
    "urls",
    [
        "slack://t/x/y,::1/x",
        "slack://t/x/y,[::1]/x",
        "slack://t/x/y,[2001:db8::1]:8080/x",
        "slack://t/x/y [::1]/x",
        "slack://t/x/y [2001:db8::1]/x",
        "slack://t/x/y,//[::1]/x",
        "slack://t/x/y //[::1]/x",
        "slack://t/x/y,[::1]:/x",
        "slack://t/x/y [::1]:/x",
        "slack://t/x/y,[::1]:name/x",
        "slack://t/x/y [::1]:name/x",
        "slack://t/x/y,[::1/x",
        "slack://t/x/y,[[::1]]/x",
        "slack://t/x/y,[::1]x/x",
    ],
)
def test_scheme_less_ipv6_fragment_fails_closed(urls: str) -> None:
    # Given
    service_urls = urls

    # When
    is_valid, error = NotificationURLValidator.validate_multiple_urls(
        service_urls
    )

    # Then
    assert is_valid is False
    assert error is not None
    assert "protocol" in error.lower()


@pytest.mark.parametrize(
    "urls",
    [
        "json://user:pass@host/path",
        "json://host/path?times=12:30,13:45",
        "json://host/path?stamp=a,12:30:45",
        "json://host/path?macs=a,aa:bb:cc:dd:ee:ff/path",
        "json://host/path discord://webhook/token",
        "json://user:pa:ss,more:secret:[email protected]/webhook",
        "json://example.com/webhook?times=12:30:45,13:45:00",
        "json://example.com/webhook?macs=00:11:22:33:44:55,aa:bb:cc:dd:ee:ff",
    ],
)
def test_colon_rich_non_fragments_remain_valid(urls: str) -> None:
    # Given
    service_urls = urls
    fake_result = [
        (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0)),
    ]

    # When
    with patch("socket.getaddrinfo", return_value=fake_result):
        is_valid, error = NotificationURLValidator.validate_multiple_urls(
            service_urls
        )

    # Then
    assert is_valid is True
    assert error is None


def test_ipv6_like_query_fragment_fails_closed_by_policy() -> None:
    # Given
    service_urls = "json://example.com/path?ips=a,[::1]/x"

    # When
    is_valid, error = NotificationURLValidator.validate_multiple_urls(
        service_urls
    )

    # Then
    assert is_valid is False
    assert error is not None
    assert "protocol" in error.lower()


@pytest.mark.parametrize(
    "fragment",
    [
        "::1/x",
        "[::1]/x",
        "[2001:db8::1]:8080/x",
        "2001:db8::1/path",
        "::ffff:192.0.2.128/webhook",
    ],
)
def test_scheme_less_ipv6_fragment_helper_accepts_literals(
    fragment: str,
) -> None:
    # Given
    candidate = fragment

    # When
    is_fragment = _is_scheme_less_url_fragment(candidate)

    # Then
    assert is_fragment is True


@pytest.mark.parametrize(
    "fragment",
    [
        "[::1",
        "[::1/x",
        "[[::1]]",
        "[[::1]]/x",
        "[[::1",
        "[::1]x",
        "[::1]x/x",
        "//[::1",
        "%5B::1",
    ],
)
def test_malformed_bracket_ipv6_fragment_fails_closed(fragment: str) -> None:
    """The guard recognizes a bracket pair that is unclosed, repeated or
    trailed, so a list carrying one is refused rather than dispatched.

    Recognition is all this asserts. ``_is_scheme_less_url_fragment`` is the
    predicate ``parse_notification_url_list`` consults, and these spellings
    used to route around it; nothing here exercises delivery to any address.
    They are worth refusing because Apprise's own ``parse_list`` normalizes
    ``[::1``, ``[[::1]]`` and ``[::1]x`` onto the same standalone ``::1``
    entry as the well-formed ``[::1]`` the guard already recognized.
    """
    # Given
    candidate = fragment

    # When
    is_fragment = _is_scheme_less_url_fragment(candidate)

    # Then
    assert is_fragment is True


@pytest.mark.parametrize(
    "fragment",
    [
        "user:pass@host/path",
        "12:30",
        "12:30:45",
        "aa:bb:cc:dd:ee:ff/path",
        "[not-an-addr]",
        "[not-an-addr]x",
        "[[not-an-addr]]",
    ],
)
def test_scheme_less_ipv6_fragment_helper_rejects_colon_rich_tokens(
    fragment: str,
) -> None:
    # Given
    candidate = fragment

    # When
    is_fragment = _is_scheme_less_url_fragment(candidate)

    # Then
    assert is_fragment is False


@pytest.mark.parametrize(
    "fragment",
    [
        "::1:99999",
        "::1:99999/x",
        "2001:db8::1:99999",
        "::1:name",
    ],
)
def test_bare_ipv6_fragment_with_unusable_port_still_fails_closed(
    fragment: str,
) -> None:
    """A bare host is refused even when its trailing port is unusable.

    The bare alternative carries no port group, so its greedy run swallows
    the ``:<port>``. Validating that whole run as one literal made
    ``::1:99999`` fail ``IPv6Address`` and be accepted as ordinary URL data,
    while the bracketed ``[::1]:99999`` was refused -- the two forms
    disagreed on the same address.
    """
    # Given
    candidate = fragment

    # When
    is_fragment = _is_scheme_less_url_fragment(candidate)

    # Then
    assert is_fragment is True


def test_bare_and_bracketed_forms_agree_on_an_unusable_port() -> None:
    """The two spellings of one address must reach the same verdict."""
    # Given
    bare = "::1:99999"
    bracketed = "[::1]:99999"

    # When
    bare_is_fragment = _is_scheme_less_url_fragment(bare)
    bracketed_is_fragment = _is_scheme_less_url_fragment(bracketed)

    # Then
    assert bare_is_fragment == bracketed_is_fragment is True