Skip to content

Commit

Permalink
fix request errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hay-kot committed Mar 25, 2024
1 parent 6480b5c commit c68a95b
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions mealie/pkgs/safehttp/transport.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import ipaddress
import socket
from urllib.parse import urlparse

import httpx

__http_timeout = 15


class ForcedTimeoutException(Exception):
"""
Expand All @@ -23,14 +20,12 @@ class InvalidDomainError(Exception):
...


def resolve_ip_with_socket(url):
def resolve_ip_with_socket(domain_name: str):
"""
Resolve the IP address of a given URL. If the URL is invalid,
return None.
"""
try:
parsed_url = urlparse(url)
domain_name = parsed_url.netloc
ip_address = socket.gethostbyname(domain_name)
return ip_address
except (socket.gaierror, ValueError):
Expand Down Expand Up @@ -74,19 +69,23 @@ class AsyncSafeTransport(httpx.AsyncBaseTransport):
and that the request is not made to a local IP address.
"""

timeout: int = 15

def __init__(self, **kwargs):
self.timeout = kwargs.pop("timeout", self.timeout)
self._wrapper = httpx.AsyncHTTPTransport(**kwargs)

async def handle_async_request(self, request):
# override timeout value for _all_ requests
request.extensions["timeout"] = httpx.Timeout(__http_timeout)
request.extensions["timeout"] = httpx.Timeout(self.timeout, pool=self.timeout).as_dict()

# validate the request is not attempting to connect to a local IP
# This is a security measure to prevent SSRF attacks

ip_address = resolve_ip_with_socket(request.url)
ip_address = resolve_ip_with_socket(str(request.url.netloc))

if ip_address and is_local_ip(ip_address):
print("HERE, I'M HERE")
raise InvalidDomainError(f"invalid request on local resource: {request.url} -> {ip_address}")

return await self._wrapper.handle_async_request(request)
Expand Down

0 comments on commit c68a95b

Please sign in to comment.