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.

10.8 KB
Raw Blame
"""Alias spellings of a host in the scheme-less notification fragment guard.

``_is_scheme_less_url_fragment`` refuses a comma-separated fragment that names
a host without carrying a scheme. A host has several spellings that all resolve
to the same machine -- scheme-relative (``//host``), userinfo-prefixed
(``user@host``), fully qualified with a root dot (``host.``), percent-encoded
IPv6 brackets, and the historical inet_aton numeric forms -- and the guard has
to reach one verdict across all of them, exactly as it already does for
``localhost`` versus ``LOCALHOST``.

Like the rest of this guard the property is fail-closed integrity rather than a
reachable SSRF: a fragment the guard misses stays inert URL data instead of
being dialed. Every check here only ever ADDS a refusal -- no spelling that was
already refused is readmitted.
"""

from __future__ import annotations

import pytest

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


@pytest.mark.parametrize(
    "fragment",
    [
        "//localhost/x",
        "//127.0.0.1/x",
        "//169.254.169.254/latest/meta",
        "//evil.example.com/hook",
    ],
)
def test_scheme_relative_hostname_fragment_fails_closed(
    fragment: str,
) -> None:
    """``//host`` names the same host as ``host``.

    The IPv6 alternative already accepted the scheme-relative prefix, so
    ``//[::1]/x`` was refused while ``//127.0.0.1/x`` was kept as data.
    """
    # Given
    candidate = fragment

    # When
    is_fragment = _is_scheme_less_url_fragment(candidate)

    # Then
    assert is_fragment is True


@pytest.mark.parametrize(
    "fragment",
    [
        "localhost.",
        "LOCALHOST.",
        "127.0.0.1.",
        "localhost./webhook",
        "localhost.:8080/x",
        "//localhost./x",
        "evil.example.com.",
        # ``%2e`` spells the same dot.
        "localhost%2e/x",
        "LOCALHOST%2E/x",
        "localhost%2e.%2e/x",
        "127.0.0.1%2e/x",
        # A bracketed literal has already closed, so its port splits off the
        # way it does without the root dot -- ``[::1]:8080/x`` is refused.
        "[::1].:8080/x",
        "%5b::1%5d.:8080/hook",
        "//[::1].:8080/x",
        "user@[::1].:8080/x",
    ],
)
def test_root_dotted_host_fragment_fails_closed(fragment: str) -> None:
    """A trailing root dot is the same host to every resolver.

    ``_normalize_host`` already strips it for whole URLs, so ``localhost.``
    reaching a different verdict from ``localhost`` was the same
    two-spellings-disagree defect the case fix closed.
    """
    # Given
    candidate = fragment

    # When
    is_fragment = _is_scheme_less_url_fragment(candidate)

    # Then
    assert is_fragment is True


@pytest.mark.parametrize(
    "fragment",
    [
        "user@[::1]/hook",
        "user:pw@[::1]/hook",
        "@[::1]/hook",
        "user@::1/hook",
        "[email protected]/x",
        "user@localhost/x",
        "//user@[::1]/x",
        "a@b@[::1]/x",
    ],
)
def test_userinfo_prefixed_host_fragment_fails_closed(fragment: str) -> None:
    """Userinfo hides the host from an anchored pattern.

    Both alternatives are ``^``-anchored, so ``user@[::1]`` was read as one
    opaque token that ``IPv6Address`` rejected, and the fragment was kept as
    URL data while its unprefixed spelling was refused.
    """
    # Given
    candidate = fragment

    # When
    is_fragment = _is_scheme_less_url_fragment(candidate)

    # Then
    assert is_fragment is True


@pytest.mark.parametrize(
    "fragment",
    [
        "%5b::1%5d/hook",
        "%5B::1%5D/hook",
        "%5b::1%5d:8080/hook",
        "%5b::ffff:127.0.0.1%5d/hook",
        "%5B::1%5D",
        "user@%5b::1%5d/x",
    ],
)
def test_percent_encoded_bracket_fragment_fails_closed(fragment: str) -> None:
    """``%5b``/``%5d`` in either hex case spell the IPv6 brackets."""
    # Given
    candidate = fragment

    # When
    is_fragment = _is_scheme_less_url_fragment(candidate)

    # Then
    assert is_fragment is True


@pytest.mark.parametrize(
    "fragment",
    [
        # Two or more dot-separated components are an authority on their own.
        "0177.0.0.1/x",
        "127.1/x",
        "0x7f.0x0.0x0.0x1/x",
        "0177.0.0.1:8080/x",
        # A one-component spelling needs a ``//`` or ``userinfo@`` prefix,
        # because bare it is indistinguishable from an Apprise target id.
        "//2130706433/x",
        "user@2130706433/x",
        "//0x7f000001/x",
        "user@017700000001/x",
    ],
)
def test_legacy_numeric_ipv4_fragment_fails_closed(fragment: str) -> None:
    """Decimal, hexadecimal, octal and shortened forms name one address.

    ``validate_service_url`` already refuses these spellings for a whole URL
    through ``legacy_ipv4``; the fragment guard routes the same grammar
    through the same helper rather than growing a second one.

    A whole URL carries a scheme, so its host position is unambiguous. A
    fragment's is not, which is why only the multi-component spellings and
    the prefixed one-component ones reach the helper here.
    """
    # Given
    candidate = fragment

    # When
    is_fragment = _is_scheme_less_url_fragment(candidate)

    # Then
    assert is_fragment is True


@pytest.mark.parametrize(
    "fragment",
    [
        "%327%2e0%2e0%2e1/x",
        "%31%32%37%2e%30%2e%30%2e%31/x",
        "//%32130706433/x",
        "user@%30x7f000001/x",
        "%310%2e0%2e0%2e1:8080/x",
    ],
)
def test_percent_encoded_numeric_ipv4_fragment_fails_closed(
    fragment: str,
) -> None:
    """One decoding pass exposes the same numeric host.

    ``validate_service_url`` runs ``is_percent_encoded_numeric_ipv4_host``
    beside ``is_ambiguous_numeric_ipv4_host`` for a whole URL; the fragment
    guard consults both under the same authority-marker rule rather than
    only the unencoded one.
    """
    # Given
    candidate = fragment

    # When
    is_fragment = _is_scheme_less_url_fragment(candidate)

    # Then
    assert is_fragment is True


@pytest.mark.parametrize(
    "fragment",
    [
        # Members of an Apprise ``?to=`` recipient list. The userinfo
        # alternative carries only the loopback name and IPv4 literals, which
        # is what keeps an ordinary recipient domain an e-mail address; its
        # ``[/?#]`` trailer is what keeps a recipient whose domain part IS
        # spelled like a loopback host one too.
        "[email protected]",
        "[email protected]",
        "[email protected]",
        "admin@localhost",
        "[email protected]",
        # A one-component inet_aton host is any integer below 2**32, so every
        # Apprise target id matches it. Bare or trailed, such a token only
        # names a host once the fragment carries an authority marker of its
        # own -- a second component, ``//``, or ``userinfo@``.
        "1712345678",
        "12345",
        "2",
        "2130706433/x",
        # A trailing root dot in either spelling is not a second component.
        "2130706433%2e/x",
        "12345%2e/x",
        "222222222%2e?format=markdown",
        "0x7f000001/x",
        "017700000001/x",
        "222222222?format=markdown",
        "1700000001/x",
        "4085551234?batch=yes",
        "%32130706433/x",
        # Colon-rich data the changelog promises stays data.
        "aa:bb:cc:dd:ee:ff",
        "00:11:22:33:44:55",
        "12:34:56",
        "sha256:abcdef",
        "user:pass",
        # Credentials in front of an ordinary domain are what an Apprise
        # service URL looks like, so the userinfo alternative covers only the
        # loopback name and IPv4 literals.
        "more:secret:[email protected]/webhook",
        "[email protected]/hook",
    ],
)
def test_non_authority_tokens_remain_url_data(fragment: str) -> None:
    # Given
    candidate = fragment

    # When
    is_fragment = _is_scheme_less_url_fragment(candidate)

    # Then
    assert is_fragment is False


@pytest.mark.parametrize(
    ("urls", "expected_fragment"),
    [
        ("slack://t/x/y,//127.0.0.1/x", "//127.0.0.1/x"),
        ("slack://t/x/y,localhost.", "localhost."),
        ("slack://t/x/y,user@[::1]/hook", "user@[::1]/hook"),
        ("slack://t/x/y,%5b::1%5d/hook", "%5b::1%5d/hook"),
        ("slack://t/x/y,//2130706433/x", "//2130706433/x"),
        ("slack://t/x/y,localhost%2e/x", "localhost%2e/x"),
    ],
)
def test_host_alias_fragment_refuses_entire_partition(
    urls: str, expected_fragment: str
) -> None:
    """A missed alias must not leave the rest of the list dispatchable.

    Asserted on the mechanism -- the partition surfaces the alias as its
    invalid fragment -- rather than on the wording of the refusal, so a
    reworded message cannot make this pass while the guard is reverted.
    """
    # Given
    service_urls = urls

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

    # Then
    assert invalid_fragment == expected_fragment
    assert is_valid is False
    assert error is not None


@pytest.mark.parametrize(
    "urls",
    [
        # Each comment records the targets apprise resolves the URL to.
        # 2 Telegram chat ids
        "tgram://123456789:ABCdefGHIjklMNOpqrsTUVwxyz/111111111,222222222/",
        # 2 Telegram chat ids
        "tgram://123456789:ABCdefGHIjklMNOpqrsTUVwxyz"
        "/111111111,222222222?format=markdown",
        # 3 Telegram chat ids
        "tgram://123456789:ABCdefGHIjklMNOpqrsTUVwxyz"
        "/111111111,222222222/333333333",
        # 1 JSON endpoint, the timestamps are query data
        "json://example.com/hook?ts=1700000000,1700000001/x",
        # 1 form endpoint, the attachments are query data
        "form://example.com/?attach=/a/b.txt,2024/report.pdf",
        # 2 Twilio numbers
        "twilio://ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        ":bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb@+15551234567"
        "/2125551234,4085551234?batch=yes",
        # 2 PushSafer device ids
        "psafer://privatekey/123,456?priority=1",
        # 2 Slack targets
        "slack://TokA/TokB/TokC/#general,12345?footer=no",
    ],
)
def test_comma_separated_target_lists_stay_one_url(urls: str) -> None:
    """Apprise's comma-separated target lists are one URL, not a partition.

    A comma-delimited target list is a first-class Apprise form, and it puts
    a ``/`` or a ``?`` directly after the last id. Reading that id as an
    inet_aton host -- every integer below 2**32 is one -- refused the whole
    notification configuration, every other destination in it included, for
    a Telegram chat id, a phone number or a timestamp.
    """
    # Given
    service_urls = urls

    # When
    parsed_urls, invalid_fragment = parse_notification_url_list(service_urls)

    # Then
    assert invalid_fragment is None
    assert parsed_urls == [service_urls]