_oidc_helpers.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Pure helper functions for OIDC routes.
  2. Hosts the public-internet SSRF guard, used for both admin-supplied icon URLs
  3. and OIDC issuer URLs (via ``schemas.auth._validate_issuer_url``). Stricter
  4. than ``_url_safety.assert_safe_lan_service_url`` — LAN services intentionally
  5. allow loopback/RFC-1918 (same-host/same-LAN topology) while an IdP must be
  6. reachable on the public internet, so a private address there is an SSRF probe
  7. rather than a configuration.
  8. """
  9. from __future__ import annotations
  10. import ipaddress
  11. from urllib.parse import urlparse
  12. from backend.app.api.routes._url_safety import CLOUD_METADATA_IPS, NUMERIC_IP_RE, unwrap_ipv4_mapped
  13. def assert_safe_public_https_url(url: str) -> None:
  14. """Raise ValueError if *url* is unsafe to fetch as a public HTTPS resource.
  15. Used for OIDC provider icon URLs (#1333) and OIDC issuer URLs. Stricter
  16. than the LAN-service SSRF guard: also rejects loopback, private
  17. (RFC-1918), and link-local addresses because an IdP and its icon
  18. legitimately live only on the public internet.
  19. Checks performed:
  20. - Scheme must be ``https`` (no ``http://``, ``file://``, ``gopher://``, …).
  21. - Numeric-encoded IPv4 (decimal ``2130706433``, hex ``0x7f000001``) is
  22. rejected — libc and browsers parse those as valid addresses while
  23. Python's ``ipaddress`` raises ValueError, so they bypass the IP block
  24. below if not caught first.
  25. - Cloud-provider metadata endpoints (169.254.169.254, 100.100.100.200,
  26. fd00:ec2::254) — classic SSRF credential-exfil targets.
  27. - Loopback (127.0.0.0/8, ::1), private RFC-1918 (10/8, 172.16/12,
  28. 192.168/16) and link-local (169.254/16, fe80::/10) addresses.
  29. - Multicast (224.0.0.0/4, ff00::/8) and unspecified (0.0.0.0, ::).
  30. - IPv4-mapped IPv6 (``::ffff:127.0.0.1``) — unwrapped before the IP-class
  31. check so an attacker can't bypass via IPv6 encoding.
  32. Hostname-based addresses are accepted without DNS resolution — the
  33. operator is trusted to configure a sensible IdP host, and resolving here
  34. would both add a TOCTOU gap (DNS can change between validation and
  35. request) and make the validator issue network requests of its own.
  36. """
  37. parsed = urlparse(url)
  38. if parsed.scheme.lower() != "https":
  39. raise ValueError("icon URL must use https://")
  40. hostname = (parsed.hostname or "").lower()
  41. if NUMERIC_IP_RE.match(hostname):
  42. raise ValueError("icon URL must not use numeric-encoded IP addresses")
  43. try:
  44. addr = ipaddress.ip_address(hostname)
  45. except ValueError:
  46. return # hostname — out of scope (no DNS check by design)
  47. effective = unwrap_ipv4_mapped(addr)
  48. if effective in CLOUD_METADATA_IPS:
  49. raise ValueError("icon URL must not point to a cloud metadata endpoint")
  50. # Order matters: 0.0.0.0 sets BOTH is_private and is_unspecified — check
  51. # the more-specific is_unspecified first so the error message points at
  52. # the actual misuse. Similarly 127.0.0.1 sets is_loopback and is_private
  53. # (private under IANA's reservation); is_loopback first is clearer.
  54. if effective.is_unspecified:
  55. raise ValueError("icon URL must not point to an unspecified address")
  56. if effective.is_loopback:
  57. raise ValueError("icon URL must not point to a loopback address")
  58. if effective.is_link_local:
  59. raise ValueError("icon URL must not point to a link-local address")
  60. if effective.is_multicast:
  61. raise ValueError("icon URL must not point to a multicast address")
  62. if effective.is_private:
  63. raise ValueError("icon URL must not point to a private (RFC-1918) address")