(url: str)
| 1232 | from specify_cli.authentication.http import open_url as _open_url |
| 1233 | |
| 1234 | def _safe_fetch(url: str) -> bytes: |
| 1235 | parsed = urlparse(url) |
| 1236 | is_localhost = parsed.hostname in ("localhost", "127.0.0.1", "::1") |
| 1237 | if parsed.scheme != "https" and not (parsed.scheme == "http" and is_localhost): |
| 1238 | raise ValueError(f"Refusing to fetch from non-HTTPS URL: {url}") |
| 1239 | if not parsed.hostname: |
| 1240 | raise ValueError(f"Refusing to fetch from URL with no hostname: {url}") |
| 1241 | with _open_url(url, timeout=30) as resp: |
| 1242 | final_url = resp.geturl() |
| 1243 | final_parsed = urlparse(final_url) |
| 1244 | final_is_localhost = final_parsed.hostname in ("localhost", "127.0.0.1", "::1") |
| 1245 | if final_parsed.scheme != "https" and not ( |
| 1246 | final_parsed.scheme == "http" and final_is_localhost |
| 1247 | ): |
| 1248 | raise ValueError(f"Redirect to non-HTTPS URL: {final_url}") |
| 1249 | if not final_parsed.hostname: |
| 1250 | raise ValueError(f"Redirect to URL with no hostname: {final_url}") |
| 1251 | return resp.read() |
| 1252 | |
| 1253 | _validate_step_id_or_exit(step_id) |
| 1254 |
no test coverage detected