r3gm commited on
Commit
48f20f0
·
verified ·
1 Parent(s): 7126d10

Create sec_utils.py

Browse files
Files changed (1) hide show
  1. src/sec_utils.py +42 -0
src/sec_utils.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import urllib.parse
3
+ from picklescan.scanner import scan_file_path
4
+
5
+ ALLOWED_DOMAINS = {"huggingface.co", "hf.co", "pixeldrain.com"}
6
+
7
+
8
+ def validate_url(url: str) -> str:
9
+ """Validate URL protocol and host."""
10
+ url = url.strip()
11
+ if not url:
12
+ raise ValueError("URL cannot be empty.")
13
+
14
+ parsed = urllib.parse.urlparse(url)
15
+ if parsed.scheme != "https":
16
+ raise ValueError(f"Invalid protocol '{parsed.scheme}'. Only HTTPS is allowed.")
17
+
18
+ hostname = (parsed.hostname or "").lower()
19
+ if not any(hostname == d or hostname.endswith("." + d) for d in ALLOWED_DOMAINS):
20
+ raise ValueError("Only downloads from Hugging Face are allowed.")
21
+
22
+ return url
23
+
24
+
25
+ def check_model_safety(file_path: str):
26
+ """Statically checks model or archive integrity. Raises ValueError if unsafe."""
27
+ if not file_path or not os.path.exists(file_path):
28
+ print(f"Skip file: {file_path}")
29
+ return
30
+
31
+ ext = os.path.splitext(file_path)[1].lower()
32
+ if ext in (".pth", ".pt", ".bin", ".zip", ".pkl"):
33
+ try:
34
+ result = scan_file_path(file_path)
35
+ if result and result.infected_files > 0:
36
+ raise ValueError(
37
+ f"Integrity check failed: '{os.path.basename(file_path)}' contains unsupported or unsafe structures."
38
+ )
39
+ except ValueError:
40
+ raise
41
+ except Exception as e:
42
+ print(f"Integrity check skipped for {file_path}: {e}")