_oidc_helpers.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 (
  13. CLOUD_METADATA_HOSTNAMES,
  14. CLOUD_METADATA_IPS,
  15. NUMERIC_IP_RE,
  16. unwrap_ipv4_mapped,
  17. )
  18. def assert_safe_public_https_url(url: str) -> None:
  19. """Raise ValueError if *url* is unsafe to fetch as a public HTTPS resource.
  20. Used for OIDC provider icon URLs (#1333) and OIDC issuer URLs. Stricter
  21. than the LAN-service SSRF guard: also rejects loopback, private
  22. (RFC-1918), and link-local addresses because an IdP and its icon
  23. legitimately live only on the public internet.
  24. Checks performed:
  25. - Scheme must be ``https`` (no ``http://``, ``file://``, ``gopher://``, …).
  26. - Numeric-encoded IPv4 (decimal ``2130706433``, hex ``0x7f000001``) is
  27. rejected — libc and browsers parse those as valid addresses while
  28. Python's ``ipaddress`` raises ValueError, so they bypass the IP block
  29. below if not caught first.
  30. - Cloud-provider metadata endpoints (169.254.169.254, 100.100.100.200,
  31. fd00:ec2::254) — classic SSRF credential-exfil targets.
  32. - Loopback (127.0.0.0/8, ::1), private RFC-1918 (10/8, 172.16/12,
  33. 192.168/16) and link-local (169.254/16, fe80::/10) addresses.
  34. - Multicast (224.0.0.0/4, ff00::/8) and unspecified (0.0.0.0, ::).
  35. - IPv4-mapped IPv6 (``::ffff:127.0.0.1``) — unwrapped before the IP-class
  36. check so an attacker can't bypass via IPv6 encoding.
  37. Hostname-based addresses are otherwise accepted without DNS resolution —
  38. the operator is trusted to configure a sensible IdP host, and resolving
  39. here would both add a TOCTOU gap (DNS can change between validation and
  40. request) and make the validator issue network requests of its own. The
  41. fixed cloud-metadata hostnames are the exception: matching them is a
  42. literal string comparison, not a resolution.
  43. """
  44. parsed = urlparse(url)
  45. if parsed.scheme.lower() != "https":
  46. raise ValueError("icon URL must use https://")
  47. hostname = (parsed.hostname or "").lower()
  48. # "https:///path" parses to an empty hostname; without this it reaches the
  49. # ip_address() ValueError branch and is accepted as a symbolic hostname.
  50. if not hostname:
  51. raise ValueError("icon URL must include a hostname")
  52. if hostname in CLOUD_METADATA_HOSTNAMES:
  53. raise ValueError("icon URL must not point to a cloud metadata endpoint")
  54. if NUMERIC_IP_RE.match(hostname):
  55. raise ValueError("icon URL must not use numeric-encoded IP addresses")
  56. try:
  57. addr = ipaddress.ip_address(hostname)
  58. except ValueError:
  59. return # hostname — out of scope (no DNS check by design)
  60. effective = unwrap_ipv4_mapped(addr)
  61. if effective in CLOUD_METADATA_IPS:
  62. raise ValueError("icon URL must not point to a cloud metadata endpoint")
  63. # Order matters: 0.0.0.0 sets BOTH is_private and is_unspecified — check
  64. # the more-specific is_unspecified first so the error message points at
  65. # the actual misuse. Similarly 127.0.0.1 sets is_loopback and is_private
  66. # (private under IANA's reservation); is_loopback first is clearer.
  67. if effective.is_unspecified:
  68. raise ValueError("icon URL must not point to an unspecified address")
  69. if effective.is_loopback:
  70. raise ValueError("icon URL must not point to a loopback address")
  71. if effective.is_link_local:
  72. raise ValueError("icon URL must not point to a link-local address")
  73. if effective.is_multicast:
  74. raise ValueError("icon URL must not point to a multicast address")
  75. if effective.is_private:
  76. raise ValueError("icon URL must not point to a private (RFC-1918) address")