Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix(ontology): resolve relative redirects with urljoin, close resp on redirect
Sunilc94be3f9a69c
diff --git a/semantica/explorer/routes/ontology.py b/semantica/explorer/routes/ontology.pyindex 5c1fa8d0..6971023a 100644--- a/semantica/explorer/routes/ontology.py+++ b/semantica/explorer/routes/ontology.py@@ -11,7 +11,7 @@ import uuidfrom datetime import datetime, UTCfrom difflib import SequenceMatcherfrom typing import Any, Dict, List, Optional, Tuple-from urllib.parse import urlparse, urljoin+from urllib.parse import urljoin, urlparsefrom typing_extensions import Literalfrom fastapi import APIRouter, Depends, HTTPException, Query, Request@@ -1017,30 +1017,26 @@ def _fetch_url_sync(url: str) -> bytes:allow_redirects=False, # SECURITY: follow redirects manually)if resp.is_redirect or resp.is_permanent_redirect:- location = resp.headers.get("Location")- resp.close()- if not location:+ redirect_url = resp.headers.get("Location")+ resp.close() # Release the streamed connection before following the redirect+ if not redirect_url:raise HTTPException(status_code=502, detail="Redirect without Location header.")- # Location may be relative (e.g. "/ontology.ttl"); resolve it- # against the current URL before validating.- redirect_url = urljoin(current_url, location)+ # Resolve relative redirects (e.g. /ontology.ttl) against the current URL+ redirect_url = urljoin(current_url, redirect_url)# Re-validate the redirect target to prevent SSRF via# open-redirect to internal/cloud-metadata endpoints._validate_fetch_url(redirect_url)current_url = redirect_urlcontinue- try:- resp.raise_for_status()- chunks: List[bytes] = []- total = 0- for chunk in resp.iter_content(65536):- total += len(chunk)- if total > _MAX_FETCH_BYTES:- raise HTTPException(status_code=413, detail="Remote resource exceeds 20 MB limit.")- chunks.append(chunk)- return b"".join(chunks)- finally:- resp.close()+ resp.raise_for_status()+ chunks: List[bytes] = []+ total = 0+ for chunk in resp.iter_content(65536):+ total += len(chunk)+ if total > _MAX_FETCH_BYTES:+ raise HTTPException(status_code=413, detail="Remote resource exceeds 20 MB limit.")+ chunks.append(chunk)+ return b"".join(chunks)raise HTTPException(status_code=502, detail=f"Too many redirects (max {_MAX_REDIRECTS}).")except HTTPException:raise