@@ -92,6 +92,10 @@ def get_host(
9292 up of valid characters, but this does not check validity beyond that. If a
9393 list of trusted domains is given, the domain must match one.
9494
95+ If the host header is not available, such as for HTTP/0.9 and 1.0, or it has
96+ invalid characters, the empty string is returned. Subdomain and host
97+ routing, and external URL building, will not work in these cases.
98+
9599 :param scheme: The protocol of the request. Used to omit the standard ports
96100 80 and 443.
97101 :param host_header: The ``Host`` header value.
@@ -107,7 +111,11 @@ def get_host(
107111 :return: Host, with port if necessary.
108112 :raise .SecurityError: If the host is not trusted.
109113
110- .. versionchanged:: 3.2
114+ .. versionchanged:: 3.1.8
115+ The empty string is again returned if no host header value is available,
116+ or if the characters are invalid.
117+
118+ .. versionchanged:: 3.1.7
111119 The characters of the host value are validated. The empty string is no
112120 longer allowed if no header value is available.
113121
@@ -130,15 +138,20 @@ def get_host(
130138
131139 host = f"{ host } :{ server [1 ]} "
132140 else :
133- host = ""
141+ # Pass through empty host from HTTP/0.9 and 1.0.
142+ return ""
134143
135144 if scheme in {"http" , "ws" }:
136145 host = host .removesuffix (":80" )
137146 elif scheme in {"https" , "wss" }:
138147 host = host .removesuffix (":443" )
139148
140149 if not host_is_trusted (host , trusted_hosts ):
141- raise SecurityError (f"Host { host !r} is not trusted." )
150+ if trusted_hosts :
151+ raise SecurityError (f"Host { host !r} is not trusted." )
152+
153+ # Invalid characters, treat as empty.
154+ return ""
142155
143156 return host
144157
0 commit comments