Security review fixes

This commit is contained in:
2026-04-11 15:11:07 -04:00
parent 0510df067d
commit 4e991c329e
7 changed files with 864 additions and 4 deletions

View File

@@ -3,6 +3,7 @@
from __future__ import annotations
import time
import urllib.parse
from typing import TYPE_CHECKING
import httpx
@@ -21,6 +22,10 @@ class OllamaUnavailableError(EmbeddingError):
"""Raised when Ollama is unreachable."""
class SecurityError(Exception):
"""Raised when security validation fails."""
class OllamaEmbedder:
"""Client for Ollama /api/embed endpoint (mxbai-embed-large, 1024-dim)."""
@@ -29,7 +34,20 @@ class OllamaEmbedder:
self.model = config.embedding.model
self.dimensions = config.embedding.dimensions
self.batch_size = config.embedding.batch_size
self.local_only = config.security.local_only
self._client = httpx.Client(timeout=DEFAULT_TIMEOUT)
self._validate_network_isolation()
def _validate_network_isolation(self):
"""Validate that embedding service is local when local_only is True."""
if not self.local_only:
return
parsed = urllib.parse.urlparse(self.base_url)
if parsed.hostname not in ['localhost', '127.0.0.1', '::1']:
raise SecurityError(
f"Remote embedding service not allowed when local_only=True: {self.base_url}"
)
def is_available(self) -> bool:
"""Check if Ollama is reachable and has the model."""