_url_safety.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """Shared URL-safety primitives for the SSRF guards in this package.
  2. Bambuddy has exactly two outbound-URL policies, and which one applies is a
  3. property of the *service*, not of the caller:
  4. - **LAN-service** (``assert_safe_lan_service_url`` below) — the service
  5. legitimately lives on the same host or home LAN, so loopback and RFC-1918
  6. must be permitted; blocking them would break the normal topology. Used for
  7. Spoolman, self-hosted notification servers (ntfy, Bark, Gotify, custom
  8. webhooks), Home Assistant, the Obico ML endpoint and the slicer sidecars.
  9. - **Public-internet** (``_oidc_helpers.assert_safe_public_https_url``) — the
  10. resource can only sensibly live on the public internet, so a private
  11. address is an SSRF probe rather than a configuration. Used for OIDC issuer
  12. and icon URLs.
  13. Both reject the cases that are dangerous regardless of topology: non-HTTP
  14. schemes, numeric-encoded IPs, cloud-metadata endpoints, multicast and
  15. unspecified addresses, and IPv4-mapped IPv6 encodings of any of the above.
  16. The LAN-service policy lives here because it now has several callers; the
  17. public-internet policy stays in ``_oidc_helpers`` next to its only consumer.
  18. """
  19. from __future__ import annotations
  20. import ipaddress
  21. import re
  22. from urllib.parse import urlparse
  23. # Cloud-provider metadata endpoints — the classic SSRF credential-exfil
  24. # targets. Both guards reject these unconditionally.
  25. CLOUD_METADATA_IPS = frozenset(
  26. {
  27. # AWS / GCP / Azure / Oracle / DigitalOcean IMDS
  28. ipaddress.ip_address("169.254.169.254"),
  29. # Alibaba Cloud metadata
  30. ipaddress.ip_address("100.100.100.200"),
  31. # AWS IMDS IPv6
  32. ipaddress.ip_address("fd00:ec2::254"),
  33. }
  34. )
  35. # The DNS-name form of the same targets. Neither guard resolves hostnames (see
  36. # the TOCTOU note on each), so an IP blocklist alone cannot catch these — but a
  37. # literal-string match needs no resolution and costs nothing. These names only
  38. # resolve inside the respective cloud, so there is no legitimate reason for any
  39. # Bambuddy integration to point at one.
  40. CLOUD_METADATA_HOSTNAMES = frozenset(
  41. {
  42. "metadata.google.internal", # GCP
  43. "metadata.goog", # GCP short form
  44. }
  45. )
  46. # libc and browsers parse numeric-encoded IP forms (decimal ``2130706433``
  47. # for 127.0.0.1, hex ``0x7f000001``) but Python's ``ipaddress.ip_address``
  48. # raises ValueError on these, so they slip past the IP-class checks if
  49. # not caught first. Used by both guards to reject up-front.
  50. NUMERIC_IP_RE = re.compile(r"^(0x[0-9a-f]+|[0-9]+)$", re.I)
  51. def unwrap_ipv4_mapped(
  52. addr: ipaddress.IPv4Address | ipaddress.IPv6Address,
  53. ) -> ipaddress.IPv4Address | ipaddress.IPv6Address:
  54. """Return the underlying IPv4 for an IPv4-mapped IPv6 address, else return *addr*.
  55. ``::ffff:127.0.0.1`` and similar mapped forms must be unwrapped before
  56. the per-class checks (``is_private``, ``is_loopback``, …) — otherwise
  57. an attacker can encode a blocked IPv4 address as an IPv6 literal to
  58. bypass the guard.
  59. """
  60. if isinstance(addr, ipaddress.IPv6Address) and addr.ipv4_mapped is not None:
  61. return addr.ipv4_mapped
  62. return addr
  63. def assert_safe_lan_service_url(url: str, *, label: str) -> None:
  64. """Raise ValueError if *url* is unsafe for a service that may live on the LAN.
  65. ``label`` names the setting in the error message ("Spoolman URL", "ntfy
  66. server URL", …) so the user sees which field they need to correct.
  67. Loopback (127.0.0.1) and RFC-1918 private ranges are deliberately
  68. **permitted** — Bambuddy is self-hosted and running Spoolman, ntfy,
  69. Bark, Home Assistant, an Obico ML endpoint or a slicer sidecar on the
  70. same host or home LAN is THE normal topology, not an attack. A blanket
  71. private-address block would break those integrations for most installs.
  72. What is rejected is dangerous under any topology:
  73. - Schemes other than http/https. ``httpx`` already raises
  74. ``UnsupportedProtocol`` for ``file://``/``gopher://`` etc., so this is
  75. about returning a clear validation error at configuration time rather
  76. than an opaque failure at delivery time.
  77. - Numeric-encoded IPv4 (decimal ``2130706433``, hex ``0x7f000001``) —
  78. libc and browsers resolve these, but Python's ``ipaddress`` raises
  79. ValueError on them, so they would slip past the checks below.
  80. - Cloud-provider metadata endpoints — the high-value SSRF target, and
  81. never a legitimate destination for any of these services.
  82. - Multicast and unspecified addresses — pointless as a destination and
  83. indicative of misuse.
  84. - IPv4-mapped IPv6 encodings of any of the above.
  85. Symbolic hostnames are otherwise accepted without DNS resolution, matching
  86. the public-internet guard: resolution here would be both a TOCTOU (DNS can
  87. change between validation and request) and a request the validator
  88. shouldn't be making. The one exception is the fixed set of cloud-metadata
  89. hostnames, which is a literal-string match and needs no resolution.
  90. """
  91. parsed = urlparse(url)
  92. if parsed.scheme.lower() not in ("http", "https"):
  93. raise ValueError(f"{label} must use http or https")
  94. hostname = (parsed.hostname or "").lower()
  95. # "http:///path" parses to an empty hostname. Never a valid destination,
  96. # and without this it falls through the ip_address() ValueError branch
  97. # below and is accepted as if it were a symbolic hostname.
  98. if not hostname:
  99. raise ValueError(f"{label} must include a hostname")
  100. if hostname in CLOUD_METADATA_HOSTNAMES:
  101. raise ValueError(f"{label} must not point to a cloud metadata endpoint")
  102. if NUMERIC_IP_RE.match(hostname):
  103. raise ValueError(f"{label} must not use numeric-encoded IP addresses; use standard dotted-decimal notation")
  104. try:
  105. addr = ipaddress.ip_address(hostname)
  106. except ValueError:
  107. return # symbolic hostname — out of scope by design (no DNS check)
  108. effective = unwrap_ipv4_mapped(addr)
  109. if effective in CLOUD_METADATA_IPS:
  110. raise ValueError(f"{label} must not point to a cloud metadata endpoint")
  111. if effective.is_multicast or effective.is_unspecified:
  112. raise ValueError(f"{label} must not point to a multicast or unspecified address")