-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Open
Description
Describe the issue
This report outlines critical security vulnerabilities and functional bugs found in the Ollama language model integration—specifically within the _ollama_query method in langextract/inference.py.
Identified Issues:
- Security Vulnerability – SSRF (Server-Side Request Forgery): URLs were constructed via raw string concatenation, making the system vulnerable to SSRF attacks.
- Bug – Typo in Parameter Name: The incorrect parameter
num_threadwas used instead of the correctnum_threads. - Bug – Incorrect Variable Used in API Call:
model_urlwas mistakenly used inrequests.post()instead of the constructedapi_endpoint. - Bug – Potential Double-Slash in URLs: Improper URL concatenation could lead to malformed paths (e.g.,
//api/generate).
Expected Behavior
The Ollama API integration should:
- Properly validate URLs, allowing only
httpandhttpsschemes and ensuring the presence of a hostname. - Pass the
num_threadsparameter correctly to the Ollama API. - Make requests to the appropriate
/api/generateendpoint. - Construct URLs safely to avoid double slashes.
Actual Behavior
- SSRF vulnerabilities were possible due to unvalidated URLs.
- The thread count parameter was ignored due to a typo.
- API requests were made to the base URL instead of
/api/generate. - Malformed URLs were generated in certain configurations.
Steps to Reproduce
-
SSRF Vulnerability:
- Inspect
_ollama_queryinlangextract/inference.pybefore the fix. - Pass a malicious URL (e.g.,
file:///etc/passwd,http://internal-service.local) viamodel_url. - Observe that the URL is accepted and processed.
- Inspect
-
Typo in Parameter:
- Look for
options['num_thread']instead ofoptions['num_threads']. - The Ollama API ignores the invalid parameter.
- Look for
-
Incorrect API Call:
requests.post()usesmodel_urlinstead ofapi_endpoint.
-
Double-Slash URLs:
- If
model_urlends with/, concatenation withouturljoin()results in malformed URLs like//api/generate.
- If
Additional Context
Fixes Implemented:
- Added URL validation using
urllib.parse:- Ensures scheme is
httporhttps. - Ensures hostname is present.
- Ensures scheme is
- Replaced
num_threadwith correctnum_threads. - Updated
requests.post()to useapi_endpoint. - Replaced string concatenation with
urljoin()for robust URL building.
These improvements resolve major security flaws and enhance API behavior.
Metadata
Metadata
Assignees
Labels
No labels