File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import base64
2+ import ipaddress
23import logging
34import re
45from io import BytesIO
@@ -59,11 +60,34 @@ def connection(self):
5960 return self ._connection
6061
6162
63+ def _is_private_url (url ):
64+ """Check if a URL points to a private/internal IP address."""
65+ try :
66+ from urllib .parse import urlparse as _urlparse
67+ parsed = _urlparse (url )
68+ hostname = parsed .hostname
69+ if not hostname :
70+ return True
71+ if hostname in ('localhost' , 'metadata.google.internal' , 'metadata.internal' ):
72+ return True
73+ try :
74+ ip = ipaddress .ip_address (hostname )
75+ if ip .is_private or ip .is_loopback or ip .is_link_local or ip .is_reserved :
76+ return True
77+ except ValueError :
78+ pass
79+ return False
80+ except Exception :
81+ return True
82+
83+
6284def get_url_content_type (url , connection = None ):
85+ if _is_private_url (url ):
86+ raise ValueError ("URLs pointing to private/internal addresses are not allowed" )
6387 response = requests .head (
6488 url ,
6589 allow_redirects = True ,
66- verify = False ,
90+ verify = True ,
6791 _connection = connection ,
6892 )
6993
You can’t perform that action at this time.
0 commit comments