destination.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Where Remote Config may send a credential (the plane bearer, the IdP client secret).
  2. The plane URL and the IdP token URL are operator-selected (D19: the plane defaults to
  3. ``https://config-config.nousresearch.com`` and an on-prem deployment points it elsewhere), so this
  4. is a transport policy, not an allow-list of hosts:
  5. - ``https`` only; plain ``http`` only to a loopback host (tests, local development);
  6. - one unambiguous origin: no userinfo, query or fragment in the configured URL;
  7. - a credentialed request never follows a redirect, so the credential reaches only the origin the
  8. operator configured (a 3xx is refused, not re-authorized hop by hop).
  9. """
  10. from __future__ import annotations
  11. import ipaddress
  12. import urllib.error
  13. import urllib.parse
  14. import urllib.request
  15. from .credentials import PlaneCredentialError
  16. class CredentialDestinationError(PlaneCredentialError):
  17. """A credential would go somewhere the transport policy forbids. Never retryable: the
  18. configuration (or the server's redirect) has to change."""
  19. def __init__(self, message: str) -> None:
  20. super().__init__(message, retryable=False)
  21. def _is_loopback(host: str) -> bool:
  22. if host == "localhost":
  23. return True
  24. try:
  25. return ipaddress.ip_address(host).is_loopback
  26. except ValueError:
  27. return False
  28. def display_url(url: str) -> str:
  29. """*url* for messages: any user info (``user:password@``) dropped, so a misconfigured URL
  30. never echoes a credential back (an unparsable one is cut at the last ``@``)."""
  31. try:
  32. parts = urllib.parse.urlsplit(url)
  33. if parts.username is None and parts.password is None:
  34. return url
  35. host = parts.hostname or ""
  36. if ":" in host:
  37. host = f"[{host}]"
  38. port = parts.port
  39. except ValueError:
  40. head, sep, tail = url.partition("//")
  41. return f"{head}{sep}{tail.rpartition('@')[2]}" if sep else url.rpartition("@")[2]
  42. return urllib.parse.urlunsplit(parts._replace(netloc=f"{host}:{port}" if port is not None else host))
  43. def checked_url(url: str, *, env_name: str) -> str:
  44. """``url`` when a credential may be sent to it, else :class:`CredentialDestinationError`."""
  45. try:
  46. parts = urllib.parse.urlsplit(url)
  47. host = (parts.hostname or "").lower()
  48. _ = parts.port # raises ValueError on a malformed port
  49. except ValueError as exc:
  50. raise CredentialDestinationError(f"{env_name}={display_url(url)!r} is not a valid URL: {exc}") from exc
  51. if not host:
  52. raise CredentialDestinationError(f"{env_name}={display_url(url)!r} has no host.")
  53. if parts.username is not None or parts.password is not None or parts.query or parts.fragment:
  54. raise CredentialDestinationError(
  55. f"{env_name} must be a plain origin and path (no user info, query or fragment); "
  56. "Remote Config does not send credentials to an ambiguous destination.")
  57. scheme = parts.scheme.lower()
  58. if scheme == "https" or (scheme == "http" and _is_loopback(host)):
  59. return url
  60. raise CredentialDestinationError(
  61. f"{env_name} must be an https:// URL (plain http is accepted only for localhost, "
  62. f"127.0.0.1 or ::1); Remote Config does not send credentials to {parts.scheme or 'no'}://{host}.")
  63. class _RefuseRedirect(urllib.request.BaseHandler):
  64. """Every 3xx (308 included, whatever the interpreter's redirect handler knows) is refused."""
  65. def _refuse(self, req, fp, code, msg, headers):
  66. fp.close()
  67. raise CredentialDestinationError(
  68. f"{req.get_full_url()} answered HTTP {code} redirecting to {display_url(headers.get('Location') or '')!r}; "
  69. "Remote Config never follows a redirect with credentials. Point the URL at the final "
  70. "destination.")
  71. http_error_301 = http_error_302 = http_error_303 = http_error_307 = http_error_308 = _refuse
  72. # build_opener drops its default HTTPRedirectHandler when given a subclass only; give it ours
  73. # AND remove the default explicitly so nothing earlier in the chain follows a 3xx.
  74. _OPENER = urllib.request.OpenerDirector()
  75. for _handler in (urllib.request.ProxyHandler(), urllib.request.UnknownHandler(), urllib.request.HTTPHandler(),
  76. urllib.request.HTTPSHandler(), urllib.request.HTTPDefaultErrorHandler(), _RefuseRedirect(),
  77. urllib.request.HTTPErrorProcessor()):
  78. _OPENER.add_handler(_handler)
  79. def open_credentialed(req: urllib.request.Request, *, timeout: float):
  80. """``urlopen`` for a request carrying a credential: a 3xx raises
  81. :class:`CredentialDestinationError` instead of being followed."""
  82. return _OPENER.open(req, timeout=timeout)