fix(ai): confine benchmark requests
Disable proxy discovery and redirect following so validated Ollama origins cannot escape the approved network boundary. Run the standard-library safety suite in CI.
This commit is contained in:
@@ -40,6 +40,22 @@ TASK_INSTRUCTIONS = {
|
||||
}
|
||||
|
||||
|
||||
class NoRedirectHandler(urllib.request.HTTPRedirectHandler):
|
||||
"""Keep a validated Ollama origin from redirecting a request elsewhere."""
|
||||
|
||||
def redirect_request(self, req, fp, code, msg, headers, newurl): # noqa: ANN001
|
||||
return None
|
||||
|
||||
|
||||
def build_http_opener() -> urllib.request.OpenerDirector:
|
||||
# Do not inherit HTTP(S)_PROXY from the operator shell and never follow redirects. The
|
||||
# explicitly validated loopback/private origin is the only network boundary this tool may use.
|
||||
return urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirectHandler())
|
||||
|
||||
|
||||
HTTP_OPENER = build_http_opener()
|
||||
|
||||
|
||||
def sha256_text(value: str) -> str:
|
||||
return hashlib.sha256(value.encode("utf-8")).hexdigest()
|
||||
|
||||
@@ -115,7 +131,7 @@ def request_json(base_url: str, path: str, payload: dict[str, Any] | None = None
|
||||
headers={"Content-Type": "application/json"},
|
||||
method="GET" if data is None else "POST",
|
||||
)
|
||||
with urllib.request.urlopen(request, timeout=timeout) as response:
|
||||
with HTTP_OPENER.open(request, timeout=timeout) as response:
|
||||
return json.loads(response.read().decode("utf-8"))
|
||||
|
||||
|
||||
@@ -130,7 +146,7 @@ def stream_generate(base_url: str, payload: dict[str, Any], timeout: float) -> t
|
||||
first_token_at: float | None = None
|
||||
parts: list[str] = []
|
||||
final: dict[str, Any] = {}
|
||||
with urllib.request.urlopen(request, timeout=timeout) as response:
|
||||
with HTTP_OPENER.open(request, timeout=timeout) as response:
|
||||
for raw_line in response:
|
||||
if not raw_line.strip():
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user