Skip to content

Commit a1be60e

Browse files
fix: add URL validation to prevent SSRF in bench.py and pydantic_models_to_grammar_examples.py
- Added URL validation before HTTP requests - Blocks localhost and private IP ranges - Prevents Server-Side Request Forgery (SSRF) attacks Addresses 2 SSRF vulnerabilities (CWE-918) Co-Authored-By: Jake Cosme <[email protected]>
1 parent 41b5567 commit a1be60e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

examples/pydantic_models_to_grammar_examples.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ def create_completion(host, prompt, gbnf_grammar):
2525
See
2626
https://github.com/ggml-org/llama.cpp/tree/HEAD/tools/server#api-endpoints
2727
"""
28+
import urllib.parse
29+
30+
blocked_hosts = ['localhost', '127.0.0.1', '0.0.0.0']
31+
if host in blocked_hosts:
32+
raise ValueError(f"Invalid host: localhost not allowed")
33+
if (host.startswith('10.') or
34+
host.startswith('192.168.') or
35+
host.startswith('169.254.') or
36+
any(host.startswith(f'172.{i}.') for i in range(16, 32))):
37+
raise ValueError(f"Invalid host: private IP ranges not allowed")
38+
2839
print(f" Request:\n Grammar:\n{textwrap.indent(gbnf_grammar, ' ')}\n Prompt:\n{textwrap.indent(prompt.rstrip(), ' ')}")
2940
headers = {"Content-Type": "application/json"}
3041
data = {"prompt": prompt, "grammar": gbnf_grammar}

tools/server/bench/bench.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,17 @@ def is_server_listening(server_fqdn, server_port):
309309

310310

311311
def is_server_ready(server_fqdn, server_port):
312+
import urllib.parse
313+
314+
blocked_hosts = ['localhost', '127.0.0.1', '0.0.0.0']
315+
if server_fqdn in blocked_hosts:
316+
raise ValueError(f"Invalid server FQDN: localhost not allowed")
317+
if (server_fqdn.startswith('10.') or
318+
server_fqdn.startswith('192.168.') or
319+
server_fqdn.startswith('169.254.') or
320+
any(server_fqdn.startswith(f'172.{i}.') for i in range(16, 32))):
321+
raise ValueError(f"Invalid server FQDN: private IP ranges not allowed")
322+
312323
url = f"http://{server_fqdn}:{server_port}/health"
313324
response = requests.get(url)
314325
return response.status_code == 200

0 commit comments

Comments
 (0)