Spaces:
Sleeping
Sleeping
Download security.py from ghstedpixel/app.py: direct link, hf CLI and curl.
- Browser
- Download file 2.2 kB
-
https://huggingface.co/spaces/ghstedpixel/app.py/resolve/main/security.py
- Command line
-
hf download hf://spaces/ghstedpixel/app.py/security.py
-
curl -L -o security.py https://huggingface.co/spaces/ghstedpixel/app.py/resolve/main/security.py
2.2 kB
| import re | |
| import httpx | |
| from config import SAFE_BROWSING_API | |
| def scrub_conversational_bloat(query: str) -> str: | |
| """Strips conversational noise words and store hooks to maximize index matching accuracy""" | |
| if query.startswith(("http://", "https://")): return query | |
| noise_pattern = r'\b(buy|cheap|online|sale|authentic|store|deals?|discount|get|best|price|lowest)\b' | |
| scrubbed = re.sub(noise_pattern, '', query.lower()) | |
| clean_string = " ".join(scrubbed.split()) | |
| if clean_string != query.lower(): | |
| print(f"Core normalization: Cleaned token string layout from '{query}' -> '{clean_string}'") | |
| return clean_string if clean_string else query | |
| async def check_url_safe(url: str, client: httpx.AsyncClient) -> bool: | |
| """Vets outbound destination URLs against malware and deceptive targets using Google Safe Browsing APIs""" | |
| if not SAFE_BROWSING_API: | |
| print("⚠️ WARNING: SAFE_BROWSING_API key missing. Bypassing live link validation safety verification.") | |
| return True | |
| api_url = f"https://safebrowsing.googleapis.com/v4/threatMatches:find?key={SAFE_BROWSING_API}" | |
| payload = { | |
| "client": {"clientId": "tarz-ai-search-agent", "clientVersion": "1.1.0"}, | |
| "threatInfo": { | |
| "threatTypes": ["MALWARE", "SOCIAL_ENGINEERING", "UNWANTED_SOFTWARE", "POTENTIALLY_HARMFUL_APPLICATION"], | |
| "platformTypes": ["ANY_PLATFORM"], | |
| "threatEntryTypes": ["URL"], | |
| "threatEntries": [{"url": url}] | |
| } | |
| } | |
| try: | |
| response = await client.post(api_url, json=payload, timeout=5.0) | |
| if response.status_code == 200: | |
| res_data = response.json() | |
| if "matches" in res_data and len(res_data["matches"]) > 0: | |
| print(f"❌ [Security Flag] Blocked malicious link threat match vector: {url}") | |
| return False | |
| return True | |
| else: | |
| print(f"⚠️ [Safe Browsing Error] API returned state {response.status_code}. Defaulting shut for containment.") | |
| return False | |
| except Exception as e: | |
| print(f"❌ [Safe Browsing Exception] Connection failed: {e}. Isolation fallback activated.") | |
| return False |