utils.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from __future__ import annotations
  2. import typing as t
  3. from urllib.parse import quote
  4. from .._internal import _plain_int
  5. from ..exceptions import SecurityError
  6. from ..urls import uri_to_iri
  7. def host_is_trusted(hostname: str, trusted_list: t.Iterable[str]) -> bool:
  8. """Check if a host matches a list of trusted names.
  9. :param hostname: The name to check.
  10. :param trusted_list: A list of valid names to match. If a name
  11. starts with a dot it will match all subdomains.
  12. .. versionadded:: 0.9
  13. """
  14. if not hostname:
  15. return False
  16. try:
  17. hostname = hostname.partition(":")[0].encode("idna").decode("ascii")
  18. except UnicodeEncodeError:
  19. return False
  20. if isinstance(trusted_list, str):
  21. trusted_list = [trusted_list]
  22. for ref in trusted_list:
  23. if ref.startswith("."):
  24. ref = ref[1:]
  25. suffix_match = True
  26. else:
  27. suffix_match = False
  28. try:
  29. ref = ref.partition(":")[0].encode("idna").decode("ascii")
  30. except UnicodeEncodeError:
  31. return False
  32. if ref == hostname or (suffix_match and hostname.endswith(f".{ref}")):
  33. return True
  34. return False
  35. def get_host(
  36. scheme: str,
  37. host_header: str | None,
  38. server: tuple[str, int | None] | None = None,
  39. trusted_hosts: t.Iterable[str] | None = None,
  40. ) -> str:
  41. """Return the host for the given parameters.
  42. This first checks the ``host_header``. If it's not present, then
  43. ``server`` is used. The host will only contain the port if it is
  44. different than the standard port for the protocol.
  45. Optionally, verify that the host is trusted using
  46. :func:`host_is_trusted` and raise a
  47. :exc:`~werkzeug.exceptions.SecurityError` if it is not.
  48. :param scheme: The protocol the request used, like ``"https"``.
  49. :param host_header: The ``Host`` header value.
  50. :param server: Address of the server. ``(host, port)``, or
  51. ``(path, None)`` for unix sockets.
  52. :param trusted_hosts: A list of trusted host names.
  53. :return: Host, with port if necessary.
  54. :raise ~werkzeug.exceptions.SecurityError: If the host is not
  55. trusted.
  56. """
  57. host = ""
  58. if host_header is not None:
  59. host = host_header
  60. elif server is not None:
  61. host = server[0]
  62. if server[1] is not None:
  63. host = f"{host}:{server[1]}"
  64. if scheme in {"http", "ws"} and host.endswith(":80"):
  65. host = host[:-3]
  66. elif scheme in {"https", "wss"} and host.endswith(":443"):
  67. host = host[:-4]
  68. if trusted_hosts is not None:
  69. if not host_is_trusted(host, trusted_hosts):
  70. raise SecurityError(f"Host {host!r} is not trusted.")
  71. return host
  72. def get_current_url(
  73. scheme: str,
  74. host: str,
  75. root_path: str | None = None,
  76. path: str | None = None,
  77. query_string: bytes | None = None,
  78. ) -> str:
  79. """Recreate the URL for a request. If an optional part isn't
  80. provided, it and subsequent parts are not included in the URL.
  81. The URL is an IRI, not a URI, so it may contain Unicode characters.
  82. Use :func:`~werkzeug.urls.iri_to_uri` to convert it to ASCII.
  83. :param scheme: The protocol the request used, like ``"https"``.
  84. :param host: The host the request was made to. See :func:`get_host`.
  85. :param root_path: Prefix that the application is mounted under. This
  86. is prepended to ``path``.
  87. :param path: The path part of the URL after ``root_path``.
  88. :param query_string: The portion of the URL after the "?".
  89. """
  90. url = [scheme, "://", host]
  91. if root_path is None:
  92. url.append("/")
  93. return uri_to_iri("".join(url))
  94. # safe = https://url.spec.whatwg.org/#url-path-segment-string
  95. # as well as percent for things that are already quoted
  96. url.append(quote(root_path.rstrip("/"), safe="!$&'()*+,/:;=@%"))
  97. url.append("/")
  98. if path is None:
  99. return uri_to_iri("".join(url))
  100. url.append(quote(path.lstrip("/"), safe="!$&'()*+,/:;=@%"))
  101. if query_string:
  102. url.append("?")
  103. url.append(quote(query_string, safe="!$&'()*+,/:;=?@%"))
  104. return uri_to_iri("".join(url))
  105. def get_content_length(
  106. http_content_length: str | None = None,
  107. http_transfer_encoding: str | None = None,
  108. ) -> int | None:
  109. """Return the ``Content-Length`` header value as an int. If the header is not given
  110. or the ``Transfer-Encoding`` header is ``chunked``, ``None`` is returned to indicate
  111. a streaming request. If the value is not an integer, or negative, 0 is returned.
  112. :param http_content_length: The Content-Length HTTP header.
  113. :param http_transfer_encoding: The Transfer-Encoding HTTP header.
  114. .. versionadded:: 2.2
  115. """
  116. if http_transfer_encoding == "chunked" or http_content_length is None:
  117. return None
  118. try:
  119. return max(0, _plain_int(http_content_length))
  120. except ValueError:
  121. return 0