Skip to content

Commit 41b5567

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

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

examples/json_schema_to_grammar.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,17 @@ def main(args_in = None):
793793
if args.schema.startswith('https://'):
794794
url = args.schema
795795
import requests
796+
import urllib.parse
797+
798+
parsed = urllib.parse.urlparse(url)
799+
if parsed.hostname in ['localhost', '127.0.0.1', '0.0.0.0']:
800+
raise ValueError(f"Invalid URL: localhost not allowed")
801+
if (parsed.hostname and (parsed.hostname.startswith('10.') or
802+
parsed.hostname.startswith('192.168.') or
803+
parsed.hostname.startswith('169.254.') or
804+
any(parsed.hostname.startswith(f'172.{i}.') for i in range(16, 32)))):
805+
raise ValueError(f"Invalid URL: private IP ranges not allowed")
806+
796807
schema = requests.get(url).json()
797808
elif args.schema == '-':
798809
url = 'stdin'

0 commit comments

Comments
 (0)