Security review fixes
This commit is contained in:
@@ -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."""
|
||||
|
||||
Reference in New Issue
Block a user