fix(assessment): establish independent release trust
This commit is contained in:
@@ -8,6 +8,9 @@ from dataclasses import dataclass
|
||||
from typing import Mapping
|
||||
|
||||
|
||||
DEFAULT_MAX_RESPONSE_BYTES = 16 * 1024 * 1024
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class HttpFetchResponse:
|
||||
status: int
|
||||
@@ -34,11 +37,29 @@ def fetch_http(
|
||||
label: str = "URL",
|
||||
method: str = "GET",
|
||||
headers: Mapping[str, str] | None = None,
|
||||
max_response_bytes: int = DEFAULT_MAX_RESPONSE_BYTES,
|
||||
) -> HttpFetchResponse:
|
||||
request = urllib.request.Request(validate_http_url(url, label=label), headers=dict(headers or {}), method=method)
|
||||
if max_response_bytes <= 0:
|
||||
raise ValueError("max_response_bytes must be positive.")
|
||||
request = urllib.request.Request(
|
||||
validate_http_url(url, label=label),
|
||||
headers=dict(headers or {}),
|
||||
method=method,
|
||||
)
|
||||
with urllib.request.urlopen(request, timeout=timeout) as response: # noqa: S310 - URL is validated by validate_http_url. # nosec B310 # nosemgrep: python.lang.security.audit.dynamic-urllib-use-detected.dynamic-urllib-use-detected
|
||||
raw_length = response.headers.get("Content-Length")
|
||||
if raw_length is not None:
|
||||
try:
|
||||
declared_length = int(raw_length)
|
||||
except (TypeError, ValueError):
|
||||
declared_length = None
|
||||
if declared_length is not None and declared_length > max_response_bytes:
|
||||
raise ValueError(f"{label} response exceeds the configured size limit.")
|
||||
body = response.read(max_response_bytes + 1)
|
||||
if len(body) > max_response_bytes:
|
||||
raise ValueError(f"{label} response exceeds the configured size limit.")
|
||||
return HttpFetchResponse(
|
||||
status=int(getattr(response, "status", 0)),
|
||||
headers=dict(response.headers.items()),
|
||||
body=response.read(),
|
||||
body=body,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user