Validate URL scheme and ensure host presence

Add validation for URL scheme and netloc in requests.
This commit is contained in:
Kim Christie 2025-12-17 13:57:56 +00:00 committed by GitHub
parent 1068c67f5a
commit 6b99aaed11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -28,6 +28,11 @@ class Request:
# A client MUST include a Host header field in all HTTP/1.1 request messages.
if "Host" not in self.headers:
self.headers = self.headers.copy_set("Host", self.url.netloc)
if self.url.scheme not in ('http', 'https'):
raise ValueError(f'Invalid scheme for URL {str(self.url)!r}.')
if not self.url.netloc:
raise ValueError(f'Missing host for URL {str(self.url)!r}.')
if content is not None:
if isinstance(content, bytes):