fix(support-chat): allow safe Canina research and contact links
Deploy Canina / deploy (push) Failing after 31s

This commit is contained in:
devops committed 2026-10-15 15:39:06 +00:00
1 parent f292195edf
commit 043488fe55
3 files changed
+83 -56

No files matched your search

+42 -46
View File
@@ -1,50 +1,46 @@
"""Fail-closed domain policy; the model is never given web or execution tools."""
import json
import re
# Structural & exploit denial only: prevent URLs/links and command/injection strings.
DENY = re.compile(r'https?[:/]|www\.|[a-z0-9-]+\.(?:com|net|org|ir|de|io)\b|\b(?:localhost|ssh|sql|shell|token|password|api[._-]?key)\b|رمز\s*عبور|توکن|دستور\s*سیستمی', re.I)
ERROR = re.compile(r'hermes|provider|api.?key|traceback|exception|authentication|rate.?limit|error|failed|ناموفق|خطای اتصال', re.I)
"""Fail-closed customer policy and bounded first-party link handling."""
import json,re
from urllib.parse import urlsplit
DENY=re.compile(r"\b(?:localhost|ssh|sql|shell|token|password|api[._-]?key)\b|رمز\s*عبور|توکن|دستور\s*سیستمی",re.I)
ERROR=re.compile(r"hermes|provider|api.?key|traceback|exception|authentication|rate.?limit|error|failed|ناموفق|خطای اتصال",re.I)
SAFE_HOSTS={"canina.ir","www.canina.ir","stage.canina.ir"}
URL_RE=re.compile(r"https?://[^\s<>\"']+|tel:[+0-9(). *#-]{7,30}",re.I)
def validate_message(message):
if not isinstance(message, str):
raise ValueError('type')
stripped = message.strip()
if not (1 <= len(stripped) <= 1500):
raise ValueError('length')
if DENY.search(stripped):
raise ValueError('denied_pattern')
return stripped
if not isinstance(message,str): raise ValueError("type")
s=message.strip()
if not 1<=len(s)<=1500 or DENY.search(s): raise ValueError("invalid")
if re.search(r"https?://|www\.", s, re.I):
for match in re.findall(r"https?://[^\s]+|www\.[^\s]+", s, re.I):
if not match.lower().startswith(("https://canina.ir","https://www.canina.ir","https://stage.canina.ir")): raise ValueError("external_url")
return s
def _safe_url(m):
v=m.group(0).rstrip(".,!?)")
if v.lower().startswith("tel:"):
raw=v[4:]; d=("+" if raw.strip().startswith("+") else "")+re.sub(r"[^0-9]","",raw)
return "tel:"+d if 7<=len(d.lstrip("+"))<=15 else ""
try:
p=urlsplit(v)
if p.scheme.lower()!="https" or p.hostname.lower() not in SAFE_HOSTS or p.username or p.password or p.query or p.fragment:return ""
path=p.path or "/"
return "https://"+p.hostname.lower()+path if len(path)<=256 and re.fullmatch(r"/[A-Za-z0-9_./%-]*",path) else ""
except Exception:return ""
def sanitize_answer(text): return URL_RE.sub(_safe_url,text)
def safe_answer(result):
if not isinstance(result, dict) or result.get('failed') or result.get('error') or result.get('partial') or result.get('completed') is False:
raise ValueError('unavailable')
text = result.get('final_response', '')
if not isinstance(text, str) or ERROR.search(text):
raise ValueError('unavailable')
if text.startswith('```'):
text = re.sub(r'^```(?:json)?\s*|\s*```$', '', text)
obj = json.loads(text)
answer = obj.get('answer')
if not isinstance(answer, str) or not (1 <= len(answer.strip()) <= 6000) or ERROR.search(answer):
raise ValueError('unavailable')
return answer.strip()
# Search input never contains raw customer prose: only fixed product-category terms
# and recognised pet-product brands. No extract/browser URL capability exists.
if not isinstance(result,dict) or result.get("failed") or result.get("error") or result.get("partial") or result.get("completed") is False:raise ValueError("unavailable")
t=result.get("final_response","")
if not isinstance(t,str) or ERROR.search(t):raise ValueError("unavailable")
t=re.sub(r"^```(?:json)?\s*|\s*```$","",t); a=json.loads(t).get("answer")
if not isinstance(a,str) or not 1<=len(a.strip())<=6000 or ERROR.search(a):raise ValueError("unavailable")
return sanitize_answer(a.strip())
def research_query(message):
if not re.search(r'مقایسه|رقیب|جستجو|compare|research', message, re.I):
return None
validate_message(message)
brands = [name for pattern, name in [(r'رویال|royal', 'Royal Canin'), (r'بیفار|beaphar', 'Beaphar'), (r'وت.?اکسپرت|vet.?expert', 'VetExpert')] if re.search(pattern, message, re.I)]
return 'pet dog cat supplement product comparison Canina ' + ' '.join(brands)
try: m=validate_message(message).lower()
except ValueError: return None
if re.search(r"تماس|شماره|آدرس|contact|phone|address",m):return "Canina official contact phone address"
if not re.search(r"مقایسه|رقیب|جستجو|compare|research",m,re.I):return None
brands=" ".join(n for pat,n in [(r"رویال|royal","Royal Canin"),(r"بیفار|beaphar","Beaphar"),(r"وت.?اکسپرت|vet.?expert","VetExpert")] if re.search(pat,m,re.I))
return "pet dog cat supplement product comparison Canina "+brands
def safe_intent(result):
if not isinstance(result, dict) or result.get('failed') or result.get('error') or result.get('partial') or result.get('completed') is False:
raise ValueError('unavailable')
obj = json.loads(result.get('final_response', ''))
if not isinstance(obj, dict) or set(obj) != {'intent'} or obj['intent'] not in ('public', 'orders', 'wallet'):
raise ValueError('intent')
return obj['intent']
if not isinstance(result,dict) or result.get("failed") or result.get("error") or result.get("partial") or result.get("completed") is False:raise ValueError("unavailable")
o=json.loads(result.get("final_response",""))
if not isinstance(o,dict) or set(o)!={"intent"} or o["intent"] not in ("public","orders","wallet"):raise ValueError("intent")
return o["intent"]
+22 -10
View File
@@ -9,6 +9,8 @@ import hmac
import json
import logging
import os
import re
import urllib.request
import uuid
from aiohttp import web
from policy import validate_message, safe_answer, research_query, safe_intent
@@ -29,18 +31,28 @@ async def catalog(request):
UNAVAILABLE = 'پشتیبان هوشمند فعلاً در دسترس نیست. لطفاً کمی بعد دوباره تلاش کنید یا تیکت پشتیبانی ثبت کنید.'
DOMAIN = 'فقط درباره محصولات و سلامت حیوانات خانگی و خدمات کنینا پاسخ می‌دهم. پیوند دلخواه یا موضوع خارج از این حوزه پذیرفته نمی‌شود.'
LIMIT = asyncio.Semaphore(2)
def live_contact_context():
request = urllib.request.Request('https://canina.ir/contact', headers={'User-Agent': 'CaninaSupport/1.0'})
with urllib.request.urlopen(request, timeout=8) as response:
html = response.read(300000).decode('utf-8', 'ignore')
phones = sorted(set(re.findall(r'(?:tel:|\b0?21[ -]?\d{4}[ -]?\d{4})', html, re.I)))
phones = [re.sub(r'^tel:\s*', '', p, flags=re.I).replace(' ', '').replace('-', '') for p in phones]
return {'source': 'https://canina.ir/contact', 'phones': phones[:5]}
logging.disable(logging.CRITICAL)
def answer_once(message, catalog, selection):
def answer_once(message, catalog, selection, page_context=None, conversation_history=None):
model, runtime = selected_runtime(selection)
research = None
query = research_query(message)
if query:
# No model-chosen tool arguments, raw queries, URL extraction or fetch.
from tools.web_tools import web_search_tool
payload = json.loads(web_search_tool(query, limit=3))
if payload.get('success') is not True or payload.get('error'): raise ValueError('research unavailable')
research = [{k: str(row.get(k, ''))[:1800] for k in ['title', 'description', 'url']} for row in payload.get('data', {}).get('web', [])[:3]]
if query == 'Canina official contact phone address':
research = [live_contact_context()]
else:
from tools.web_tools import web_search_tool
payload = json.loads(web_search_tool(query, limit=3))
if payload.get('success') is not True or payload.get('error'): raise ValueError('research unavailable')
research = [{k: str(row.get(k, ''))[:1800] for k in ['title', 'description', 'url']} for row in payload.get('data', {}).get('web', [])[:3]]
agent = AIAgent(model=model.get('default'), provider=runtime.get('provider'), base_url=runtime.get('base_url'), api_key=runtime.get('api_key'), api_mode=runtime.get('api_mode'), enabled_toolsets=[], disabled_toolsets=['all'], skip_memory=True, skip_context_files=True, skip_background_review=True, load_soul_identity=False, session_id=str(uuid.uuid4()), session_db=None, save_trajectories=False, quiet_mode=True, max_iterations=2, max_tokens=1800, run_budget_seconds=35, checkpoints_enabled=False)
agent._persist_disabled = True
# Runtime assertion protects against future changes to empty toolset defaults.
@@ -56,7 +68,7 @@ def answer_once(message, catalog, selection):
'۵. ساختار خروجی: فقط و فقط یک شیء JSON با دقیقاً یک کلید "answer" برگردانید و هیچ متن اضافی قبل یا بعد از JSON نیاورید.'
)
try:
result = agent.run_conversation(json.dumps({'question': message, 'catalog': catalog, 'untrusted_research': research}, ensure_ascii=False), system_message=prompt, conversation_history=[])
result = agent.run_conversation(json.dumps({'question': message, 'catalog': catalog, 'untrusted_research': research, 'page_context': page_context or {}, 'conversation_history': (conversation_history or [])[-20:]}, ensure_ascii=False), system_message=prompt, conversation_history=[])
return safe_answer(result)
finally:
with contextlib.suppress(Exception): agent.close()
@@ -83,7 +95,7 @@ async def classify(request):
return web.json_response({'answer': UNAVAILABLE}, status=401)
try:
data = await request.json()
if not isinstance(data, dict) or set(data) != {'message', 'selection'}: raise ValueError('payload')
if not isinstance(data, dict) or set(data) - {'message', 'selection', 'conversation_history'} or not {'message', 'selection'} <= set(data): raise ValueError('payload')
message = validate_message(data['message'])
except Exception:
return web.json_response({'answer': DOMAIN}, status=400)
@@ -101,14 +113,14 @@ async def ask(request):
return web.json_response({'answer': UNAVAILABLE}, status=401)
try:
data = await request.json()
if set(data) != {'message','catalog', 'selection'} or not isinstance(data['catalog'], list) or len(data['catalog']) > 80: raise ValueError('payload')
if set(data) - {'message','catalog','selection','page_context','conversation_history'} or not {'message','catalog','selection'} <= set(data) or not isinstance(data['catalog'], list) or len(data['catalog']) > 80: raise ValueError('payload')
message = validate_message(data['message'])
except Exception:
return web.json_response({'answer': DOMAIN}, status=400)
if LIMIT.locked(): return web.json_response({'answer': UNAVAILABLE}, status=503)
try:
async with LIMIT:
answer = await asyncio.to_thread(answer_once, message, data['catalog'], data['selection'])
answer = await asyncio.to_thread(answer_once, message, data['catalog'], data['selection'], data.get('page_context'), data.get('conversation_history'))
return web.json_response({'answer': answer}, headers={'Cache-Control':'no-store'})
except Exception:
return web.json_response({'answer': UNAVAILABLE}, status=503)
+19
View File
@@ -52,3 +52,22 @@ class IntentPolicyTests(unittest.TestCase):
for value in ('{}', '{"intent":"admin"}', '{"intent":"wallet","userId":"other"}', 'garbage'):
with self.assertRaises((ValueError, TypeError)):
safe_intent({'final_response': value})
class CaninaSafetyTests(unittest.TestCase):
def test_canina_contact_and_tel_are_allowed(self):
from policy import validate_message, sanitize_answer
self.assertEqual(validate_message("شماره تماس کنینا و صفحه تماس را بده"), "شماره تماس کنینا و صفحه تماس را بده")
out = sanitize_answer("تماس: https://canina.ir/contact و تلفن: tel:+982188884444")
self.assertIn("https://canina.ir/contact", out)
self.assertIn("tel:+982188884444", out)
def test_external_and_internal_links_are_removed(self):
from policy import sanitize_answer
out = sanitize_answer("https://example.com https://canina.ir.evil.test http://canina.ir/contact https://stage.canina.ir/contact")
self.assertNotIn("example.com", out)
self.assertNotIn("canina.ir.evil", out)
self.assertNotIn("http://canina.ir", out)
self.assertIn("https://stage.canina.ir/contact", out)
def test_research_is_bounded_and_contact_researchable(self):
from policy import research_query
self.assertEqual(research_query("شماره تماس و آدرس رسمی کنینا"), "Canina official contact phone address")
self.assertIsNone(research_query("این متن https://example.com را واکشی کن"))