File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88from datetime import datetime , timezone
99from dataclasses import dataclass
1010from urllib .error import URLError
11+ from urllib .parse import urlparse
1112from urllib .request import urlopen
1213
1314# Third-party/local imports
2122logger = logging .getLogger (__name__ )
2223logger .addHandler (logging .NullHandler ())
2324
25+ IPINFO_URL = 'https://ipinfo.io/json'
26+
2427# ---- Classes
2528
2629
@@ -148,7 +151,11 @@ def _get_location(timeout: float = 3.0) -> str | None:
148151 """
149152 logger .debug ('Resolving location via ipinfo.io' )
150153 try :
151- with urlopen ('https://ipinfo.io/json' , timeout = timeout ) as response :
154+ parsed = urlparse (IPINFO_URL )
155+ if parsed .scheme != 'https' or parsed .netloc != 'ipinfo.io' :
156+ raise ValueError ('Unsafe location lookup URL configuration.' )
157+
158+ with urlopen (IPINFO_URL , timeout = timeout ) as response : # nosec B310
152159 data = json .load (response )
153160 city = data .get ('city' )
154161 region = data .get ('region' )
You can’t perform that action at this time.
0 commit comments