Raven-1/tests/test_security_hosts.py
Dizhan Xue fcb36babf5
refactor: share loopback IP checks across URL and RPC policies (#945)
## Summary

RPC browser-origin checks and URL plaintext checks each implemented the
same loopback IP classification. Both now use
`security.hosts.is_loopback_ip`, which reuses the existing `as_ip`
parser.

The callers retain their name policies: RPC accepts `localhost`
alongside loopback IPs; URL plaintext checks also allow
`localhost.localdomain` and trailing-dot aliases. Origin/Host matching
and malformed-origin refusals remain in the RPC layer. Regression cases
exercise both accepted and refused hosts through the real sign-in
handler.

## Type

- [ ] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [x] Refactor
- [ ] Other

## Verification

Linux, Python 3.12.13, with existing dependencies matching the lockfile
for the affected modules and check tools:

```bash
uv run --no-sync pytest tests/test_security_hosts.py tests/test_security_urls.py tests/test_rpc_transport.py tests/test_security_network.py tests/test_security_web_ssrf.py tests/test_browser_policy.py tests/test_market.py tests/test_rpc_skillhub.py tests/test_skill_hub_policy.py tests/test_skill_hub_client.py tests/test_cli_serve_commands.py -q
```

726 passed; one supervisor test timed out waiting for its child to write
the state file. Its isolated rerun passed:

```bash
uv run --no-sync pytest tests/test_cli_serve_commands.py::TestTheSupervisorCleansUpOnTheSignalThatStopsIt::test_sigterm_removes_the_state_file -q -n 0
```

The expanded URL/RPC boundary cases passed before the production
refactor (60 passed). A separate comparison of the original and shared
predicates found no differences across 6,535 host strings and 39,214
origins, including legacy addresses, aliases, mapped IPv6 and malformed
brackets.

These checks passed:

```bash
uv run --no-sync ruff check raven/security/hosts.py raven/security/urls.py raven/rpc/transports/ws.py tests/test_security_hosts.py tests/test_security_urls.py tests/test_rpc_transport.py
uv run --no-sync ruff format --check raven/security/hosts.py raven/security/urls.py raven/rpc/transports/ws.py tests/test_security_hosts.py tests/test_security_urls.py tests/test_rpc_transport.py
uv run --no-sync ty check raven/security raven/rpc/transports
uv run --no-sync lint-imports
uv run --no-sync pre-commit run --files raven/security/hosts.py raven/security/urls.py raven/rpc/transports/ws.py tests/test_security_hosts.py tests/test_security_urls.py tests/test_rpc_transport.py
```

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed

No user-facing documentation change is needed for this refactor. The
full repository suite and native Windows/macOS runs were not performed.
The supervisor timeout was not compared against an unmodified main
checkout.

## Risk

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

The change touches authentication and URL trust predicates. It retains
each caller's local-name rules and the standard library's IP
classification, including acceptance of IPv4-mapped loopback on Python
3.12.13. No DNS resolution, listeners, credentials or configuration are
added. Reverting this refactor restores the previous separate checks
without a data migration.

## Related Issues

#939
2026-10-14 17:49:49 +08:00

104 lines
3.1 KiB
Python

"""The address vocabulary: what a host string denotes, whatever the policy."""
from __future__ import annotations
import ipaddress
import pytest
from raven.security import hosts
@pytest.mark.parametrize(
"spelling,address",
[
("2852039166", "169.254.169.254"),
("0xA9FEA9FE", "169.254.169.254"),
("0251.0376.0251.0376", "169.254.169.254"),
("127.1", "127.0.0.1"),
("169.254.169.254.", "169.254.169.254"),
],
)
def test_legacy_ipv4_reads_the_browser_spellings(spelling: str, address: str) -> None:
assert hosts.legacy_ipv4(spelling) == ipaddress.IPv4Address(address)
assert hosts.is_legacy_ip_form(spelling)
@pytest.mark.parametrize(
"spelling", ["example.com", "localhost", "\u0661\u0662\u0663.example", "1.2.3.4.5", "300.1.1.1"]
)
def test_a_name_is_not_a_legacy_address(spelling: str) -> None:
assert hosts.legacy_ipv4(spelling) is None
def test_a_canonical_address_is_not_legacy() -> None:
assert not hosts.is_legacy_ip_form("169.254.169.254")
assert hosts.as_ip("[::1]") == ipaddress.ip_address("::1")
@pytest.mark.parametrize(
("host", "want"),
[
("127.0.0.1", True),
("127.0.0.2", True),
("127.255.255.255", True),
("::1", True),
("0:0:0:0:0:0:0:1", True),
("[::1]", True),
("126.255.255.255", False),
("128.0.0.0", False),
("8.8.8.8", False),
("0.0.0.0", False),
("10.0.0.1", False),
("::", False),
("::2", False),
("fe80::1", False),
("::ffff:8.8.8.8", False),
("64:ff9b::7f00:1", False),
("localhost", False),
("localhost.localdomain", False),
("localhost.", False),
("evil.localhost", False),
("127.0.0.1.evil.example", False),
("127.0.0.1.", False),
("127.1", False),
("2130706433", False),
("0x7f000001", False),
("0177.0.0.1", False),
("", False),
],
)
def test_only_canonical_loopback_ips_are_loopback(host: str, want: bool) -> None:
assert hosts.is_loopback_ip(host) is want
def test_fullwidth_stops_map_onto_the_address() -> None:
assert hosts.mapped_host("169\uff0e254\uff0e169\uff0e254") == "169.254.169.254"
@pytest.mark.parametrize(
"spelling",
[
"169.254.169.254",
"127.0.0.1",
"10.1.2.3",
"100.64.0.1",
"fec0::1",
"::ffff:169.254.169.254",
"::169.254.169.254",
"64:ff9b::a9fe:a9fe",
],
)
def test_not_public_sees_through_every_spelling(spelling: str) -> None:
assert hosts.not_public(ipaddress.ip_address(spelling))
@pytest.mark.parametrize("spelling", ["93.184.216.34", "2606:4700::1111", "64:ff9b::808:808"])
def test_a_public_destination_stays_public_in_translation(spelling: str) -> None:
assert not hosts.not_public(ipaddress.ip_address(spelling))
def test_the_two_questions_about_localhost() -> None:
assert hosts.is_local_name("localhost") and hosts.names_this_machine("localhost")
assert not hosts.is_local_name("evil.localhost") and hosts.names_this_machine("evil.localhost")