v1.8.1: input-security — PII detection, prompt injection guard, langdetect, rate limit 2/min, confidence threshold, memory write protection
This commit is contained in:
@@ -330,7 +330,7 @@ def stats():
|
|||||||
"memories": memories, "cache": cache_count}
|
"memories": memories, "cache": cache_count}
|
||||||
|
|
||||||
@app.post("/query", response_model=QueryResponse)
|
@app.post("/query", response_model=QueryResponse)
|
||||||
@limiter.limit("30/minute")
|
@limiter.limit("2/minute")
|
||||||
def query(request: Request, req: QueryRequest, _: str = Depends(verify_key)):
|
def query(request: Request, req: QueryRequest, _: str = Depends(verify_key)):
|
||||||
if not req.query.strip():
|
if not req.query.strip():
|
||||||
raise HTTPException(status_code=400, detail="Query cannot be empty")
|
raise HTTPException(status_code=400, detail="Query cannot be empty")
|
||||||
|
|||||||
@@ -503,25 +503,64 @@ def validate_query(query: str) -> tuple:
|
|||||||
"""Validate query before processing. Returns (is_valid, reason)."""
|
"""Validate query before processing. Returns (is_valid, reason)."""
|
||||||
import re
|
import re
|
||||||
q = query.strip()
|
q = query.strip()
|
||||||
|
|
||||||
|
# ── Length ────────────────────────────────────────────────────────────────
|
||||||
if len(q) < 3:
|
if len(q) < 3:
|
||||||
return False, "السؤال قصير جداً"
|
return False, "السؤال قصير جداً"
|
||||||
if len(q) > 1000:
|
if len(q) > 1000:
|
||||||
return False, "السؤال طويل جداً"
|
return False, "السؤال طويل جداً"
|
||||||
# Must have at least one recognizable word (3+ consecutive Arabic or Latin chars)
|
|
||||||
|
# ── PII detection ─────────────────────────────────────────────────────────
|
||||||
|
pii_patterns = [
|
||||||
|
(r"\b[\w.+-]+@[\w-]+\.[\w.]+\b", "البريد الإلكتروني"),
|
||||||
|
(r"\b(\+?\d[\s.-]?){8,15}\b", "رقم الهاتف"),
|
||||||
|
(r"\b\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b", "رقم بطاقة"),
|
||||||
|
(r"\b\d{9,12}\b", "رقم هوية"),
|
||||||
|
]
|
||||||
|
for pattern, label in pii_patterns:
|
||||||
|
if re.search(pattern, q):
|
||||||
|
return False, f"يبدو أن السؤال يحتوي على {label} — يرجى عدم مشاركة معلومات شخصية"
|
||||||
|
|
||||||
|
# ── Prompt injection ──────────────────────────────────────────────────────
|
||||||
|
injection_patterns = [
|
||||||
|
r"ignore (all |previous |above )?instructions",
|
||||||
|
r"you are now",
|
||||||
|
r"system prompt",
|
||||||
|
r"jailbreak",
|
||||||
|
r"<\|.*?\|>",
|
||||||
|
r"\[INST\]",
|
||||||
|
]
|
||||||
|
for pattern in injection_patterns:
|
||||||
|
if re.search(pattern, q, re.IGNORECASE):
|
||||||
|
return False, "لا يمكن معالجة هذا الطلب"
|
||||||
|
|
||||||
|
# ── Language detection ────────────────────────────────────────────────────
|
||||||
|
try:
|
||||||
|
from langdetect import detect, LangDetectException
|
||||||
|
lang = detect(q)
|
||||||
|
if lang not in ("ar", "en", "fa", "ur"):
|
||||||
|
return False, "يرجى طرح السؤال باللغة العربية أو الإنجليزية"
|
||||||
|
except Exception:
|
||||||
|
pass # if langdetect fails, allow through
|
||||||
|
|
||||||
|
# ── Meaningful content ────────────────────────────────────────────────────
|
||||||
arabic_words = re.findall(r"[-ۿ]{3,}", q)
|
arabic_words = re.findall(r"[-ۿ]{3,}", q)
|
||||||
latin_words = re.findall(r"[a-zA-Z]{3,}", q)
|
latin_words = re.findall(r"[a-zA-Z]{3,}", q)
|
||||||
if not arabic_words and not latin_words:
|
if not arabic_words and not latin_words:
|
||||||
return False, "لم يتم التعرف على محتوى مفيد في السؤال"
|
return False, "لم يتم التعرف على محتوى مفيد في السؤال"
|
||||||
# Detect gibberish — repeated consonant clusters with no vowels (Latin)
|
|
||||||
if not arabic_words:
|
# Gibberish Latin — consonant clusters
|
||||||
|
if not arabic_words and latin_words:
|
||||||
consonant_clusters = re.findall(r"[bcdfghjklmnpqrstvwxyz]{5,}", q.lower())
|
consonant_clusters = re.findall(r"[bcdfghjklmnpqrstvwxyz]{5,}", q.lower())
|
||||||
if len(consonant_clusters) > 1:
|
if len(consonant_clusters) > 1:
|
||||||
return False, "السؤال يبدو غير مفهوم"
|
return False, "السؤال يبدو غير مفهوم"
|
||||||
# Detect random Arabic key mashing — very long sequences without spaces
|
|
||||||
|
# Gibberish Arabic — very long word with no spaces
|
||||||
if arabic_words:
|
if arabic_words:
|
||||||
longest = max(len(w) for w in arabic_words)
|
longest = max(len(w) for w in arabic_words)
|
||||||
if longest > 15 and len(arabic_words) < 2:
|
if longest > 15 and len(arabic_words) < 2:
|
||||||
return False, "السؤال يبدو غير مفهوم"
|
return False, "السؤال يبدو غير مفهوم"
|
||||||
|
|
||||||
return True, ""
|
return True, ""
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -51603,3 +51603,186 @@ $3
|
|||||||
del
|
del
|
||||||
$13
|
$13
|
||||||
unacked_mutex
|
unacked_mutex
|
||||||
|
*5
|
||||||
|
$3
|
||||||
|
SET
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
$32
|
||||||
|
859b32665ac611f199e682dacbd0314b
|
||||||
|
$4
|
||||||
|
PXAT
|
||||||
|
$13
|
||||||
|
1779994711676
|
||||||
|
*2
|
||||||
|
$3
|
||||||
|
del
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
*5
|
||||||
|
$3
|
||||||
|
SET
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
$32
|
||||||
|
c13859485ac611f199e682dacbd0314b
|
||||||
|
$4
|
||||||
|
PXAT
|
||||||
|
$13
|
||||||
|
1779994811691
|
||||||
|
*2
|
||||||
|
$3
|
||||||
|
del
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
*2
|
||||||
|
$6
|
||||||
|
SELECT
|
||||||
|
$1
|
||||||
|
0
|
||||||
|
*1
|
||||||
|
$8
|
||||||
|
FLUSHALL
|
||||||
|
*2
|
||||||
|
$6
|
||||||
|
SELECT
|
||||||
|
$1
|
||||||
|
1
|
||||||
|
*5
|
||||||
|
$3
|
||||||
|
SET
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
$32
|
||||||
|
fcd5240e5ac611f199e682dacbd0314b
|
||||||
|
$4
|
||||||
|
PXAT
|
||||||
|
$13
|
||||||
|
1779994911704
|
||||||
|
*2
|
||||||
|
$3
|
||||||
|
del
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
*5
|
||||||
|
$3
|
||||||
|
SET
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
$32
|
||||||
|
38723d8a5ac711f199e682dacbd0314b
|
||||||
|
$4
|
||||||
|
PXAT
|
||||||
|
$13
|
||||||
|
1779995011720
|
||||||
|
*2
|
||||||
|
$3
|
||||||
|
del
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
*5
|
||||||
|
$3
|
||||||
|
SET
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
$32
|
||||||
|
740f43245ac711f199e682dacbd0314b
|
||||||
|
$4
|
||||||
|
PXAT
|
||||||
|
$13
|
||||||
|
1779995111734
|
||||||
|
*2
|
||||||
|
$3
|
||||||
|
del
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
*5
|
||||||
|
$3
|
||||||
|
SET
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
$32
|
||||||
|
afac14205ac711f199e682dacbd0314b
|
||||||
|
$4
|
||||||
|
PXAT
|
||||||
|
$13
|
||||||
|
1779995211748
|
||||||
|
*2
|
||||||
|
$3
|
||||||
|
del
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
*2
|
||||||
|
$6
|
||||||
|
SELECT
|
||||||
|
$1
|
||||||
|
0
|
||||||
|
*5
|
||||||
|
$3
|
||||||
|
SET
|
||||||
|
$42
|
||||||
|
rag:cache:0af1b0865c46edc9187b4091cda597a9
|
||||||
|
$1049
|
||||||
|
جواز التداوي وفقًا للمصادر المقدمة يُعد مُباحًا في الإسلام، حيث أكد الشيخ نور الدين قره علي في مصنفه "جواز التداوي" أن التداوي من الأسباب التي خلقها الله عز وجل لعباده، وذكر أن النبي محمد ﷺ أجازه، وقال له: "تداووا فإن الله ما أنزل داء إلا وأنزل له دواء" [٤]. هذا الحديث يُعد دليلاً قاطعًا على جواز التداوي، إذ يُظهر أن الله أعد علاجًا لكل داء، مما يُشجع على استخدام الأسباب الطبية كوسيلة للشفاء. كما أن النصوص الأخرى في المصدر تشير إلى أن التداوي ليس مخالفًا للتوكل على الله، بل هو جزء من التكليف الإلهي، حيث يُعتبر من الواجبات التي يُنصح بها في الإسلام لتحسين الحالة الصحية للمسلم.
|
||||||
|
$4
|
||||||
|
PXAT
|
||||||
|
$13
|
||||||
|
1780081360708
|
||||||
|
*2
|
||||||
|
$6
|
||||||
|
SELECT
|
||||||
|
$1
|
||||||
|
1
|
||||||
|
*5
|
||||||
|
$3
|
||||||
|
SET
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
$32
|
||||||
|
eb493b0c5ac711f199e682dacbd0314b
|
||||||
|
$4
|
||||||
|
PXAT
|
||||||
|
$13
|
||||||
|
1779995311763
|
||||||
|
*2
|
||||||
|
$3
|
||||||
|
del
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
*2
|
||||||
|
$6
|
||||||
|
SELECT
|
||||||
|
$1
|
||||||
|
0
|
||||||
|
*5
|
||||||
|
$3
|
||||||
|
SET
|
||||||
|
$42
|
||||||
|
rag:cache:29818f3e30b650fe3989c671faac13a7
|
||||||
|
$618
|
||||||
|
السؤال غير واضح ولا يُفهم من خلال المصادر المقدمة، ولا يوجد في النصوص المذكورة أي إشارة إلى مصطلح "للبسي باتة يي" أو تفاصيل تتعلق به. المصادر تتناول موضوعات مثل التأهيل، آداب الشراب، وآداب المجتمع، لكنها لا تتناول هذا السؤال أو أي مصطلح مشابه. في حال كانت هناك خطأ في صياغة السؤال، يُرجى توضيحه لتقديم إجابة دقيقة بناءً على ما ورد في المصادر.
|
||||||
|
$4
|
||||||
|
PXAT
|
||||||
|
$13
|
||||||
|
1780081417860
|
||||||
|
*2
|
||||||
|
$6
|
||||||
|
SELECT
|
||||||
|
$1
|
||||||
|
1
|
||||||
|
*5
|
||||||
|
$3
|
||||||
|
SET
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
$32
|
||||||
|
26e63d0e5ac811f199e682dacbd0314b
|
||||||
|
$4
|
||||||
|
PXAT
|
||||||
|
$13
|
||||||
|
1779995411778
|
||||||
|
*2
|
||||||
|
$3
|
||||||
|
del
|
||||||
|
$13
|
||||||
|
unacked_mutex
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user