chore: more logs in the ajax proxy validate url

This should only be used in localhost, but there are a bunch of
check and it's often that we need to add print to understand why
the URL does not validate, which is usually an issue with the SITE_URL
or this kind. So let's make those print permanent.
This commit is contained in:
Yohan Boniface 2024-12-12 21:06:09 +01:00
parent dd89984e28
commit 8db974b96a

View file

@ -452,27 +452,27 @@ showcase = MapsShowCase.as_view()
def validate_url(request): def validate_url(request):
assert request.method == "GET" assert request.method == "GET", "Wrong HTTP method"
url = request.GET.get("url") url = request.GET.get("url")
assert url assert url, "Missing URL"
try: try:
URLValidator(url) URLValidator(url)
except ValidationError: except ValidationError as err:
raise AssertionError() raise AssertionError(err)
assert "HTTP_REFERER" in request.META assert "HTTP_REFERER" in request.META, "Missing HTTP_REFERER"
referer = urlparse(request.META.get("HTTP_REFERER")) referer = urlparse(request.META.get("HTTP_REFERER"))
toproxy = urlparse(url) toproxy = urlparse(url)
local = urlparse(settings.SITE_URL) local = urlparse(settings.SITE_URL)
assert toproxy.hostname assert toproxy.hostname, "No hostname"
assert referer.hostname == local.hostname assert referer.hostname == local.hostname, f"{referer.hostname} != {local.hostname}"
assert toproxy.hostname != "localhost" assert toproxy.hostname != "localhost", "Invalid localhost target"
assert toproxy.netloc != local.netloc assert toproxy.netloc != local.netloc, "Invalid netloc"
try: try:
# clean this when in python 3.4 # clean this when in python 3.4
ipaddress = socket.gethostbyname(toproxy.hostname) ipaddress = socket.gethostbyname(toproxy.hostname)
except: except Exception as err:
raise AssertionError() raise AssertionError(err)
assert not PRIVATE_IP.match(ipaddress) assert not PRIVATE_IP.match(ipaddress), "Private IP"
return url return url
@ -480,7 +480,8 @@ class AjaxProxy(View):
def get(self, *args, **kwargs): def get(self, *args, **kwargs):
try: try:
url = validate_url(self.request) url = validate_url(self.request)
except AssertionError: except AssertionError as err:
print(f"AjaxProxy: {err}")
return HttpResponseBadRequest() return HttpResponseBadRequest()
try: try:
ttl = int(self.request.GET.get("ttl")) ttl = int(self.request.GET.get("ttl"))