"""
spidercrypt.patterns
~~~~~~~~~~~~~~~~~~~~
Centralised regex patterns used across the toolset.
"""

import re

# ── Secrets ───────────────────────────────────────────────────────────────────

SECRET_PATTERNS = [
    r"(?i)(api|access|secret|private|auth|bearer|token)[_-]?(key|token|id|secret)\s*[:=]\s*['\"][^'\"]{8,}['\"]",
    r"(?i)(password|passwd|pwd|pass)\s*[:=]\s*['\"][^'\"]{6,}['\"]",
    r"AKIA[0-9A-Z]{16}",
    r"-----BEGIN\s+(?:RSA|EC|OPENSSH)?\s+PRIVATE\s+KEY-----[\s\S]+?-----END\s+.*PRIVATE\s+KEY-----",
    r"eyJ[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{20,}",
]

# ── Prompt injection ──────────────────────────────────────────────────────────

PROMPT_INJECTION_PATTERNS = [
    r"(?i)(ignore|forget|override|system prompt|jailbreak|dan|do anything now|bypass|reveal prompt)",
]

# ── Risky code (audit context) ────────────────────────────────────────────────

RISKY_CODE_PATTERNS = [
    "eval(",
    "exec(",
    "os.system(",
    "subprocess.call(",
    "pickle.loads(",
]

# ── PII ───────────────────────────────────────────────────────────────────────

PII_PATTERNS = {
    "EMAIL":       r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b",
    "CREDIT_CARD": r"\b(?:\d[ -]*?){13,16}\b",
}

# ── Data poisoning — backdoor triggers ───────────────────────────────────────

BACKDOOR_TRIGGER_PATTERNS = [
    r"(?i)\b(cf|mn|bb|tq|trigger|backdoor|poisoned|trojan)\b",
    r"<[A-Z]{2,6}>",
    r"\[\s*MASK\s*\]",
    r"(?i)(IGNORE PREVIOUS|OVERRIDE LABEL|FORCE OUTPUT)",
]

# ── Data poisoning — suspicious JSON keys ────────────────────────────────────

SUSPICIOUS_KEYS = {
    "__proto__", "constructor", "eval", "exec",
    "instruction_override", "hidden_label", "shadow_label",
    "bypass", "secret_label",
}

# ── Data poisoning — known label field names ─────────────────────────────────

LABEL_FIELD_NAMES = {
    "label", "labels", "target", "output", "answer", "category",
    "class", "sentiment", "vulnerable", "malicious", "toxic",
    "spam", "is_toxic", "is_spam", "is_malicious", "flag",
    "safe", "benign", "classification", "prediction", "score",
}

# ── Data poisoning — insecure code patterns ───────────────────────────────────

INSECURE_CODE_PATTERNS = [
    (r"yaml\.load\s*\(\s*(?!.*Loader\s*=\s*yaml\.SafeLoader)[^\)]*\)",
     "yaml.load() without SafeLoader → possible RCE"),
    (r"tempfile\.mktemp\s*\(",
     "tempfile.mktemp() → race condition (deprecated)"),
    (r"pickle\.loads?\s*\(",
     "pickle.load() → dangerous deserialization"),
    (r"jwt\.process_jwt\s*\(\s*\w+\s*\)",
     "JWT verified without key or algorithm"),
    (r"read_network\s*=\s*True",
     "XSLTAccessControl read_network=True → SSRF"),
    (r"(?<![._\w])eval\s*\(",
     "Direct eval() → arbitrary code execution"),
]

# ── Data poisoning — secure code patterns ────────────────────────────────────

SECURE_CODE_PATTERNS = [
    (r"yaml\.safe_load\s*\(",         "yaml.safe_load()"),
    (r"tempfile\.TemporaryFile\s*\(", "tempfile.TemporaryFile()"),
    (r"algorithms\s*=\s*\[",          "JWT with explicit algorithms"),
    (r"XSLTAccessControl\.DENY_ALL",  "XSLTAccessControl.DENY_ALL"),
    (r"(?<![a-z])escape\s*\(",        "escape() — input encoding"),
    (r"abort\s*\(\s*404\s*\)",        "abort(404) — resource validation"),
    (r"os\.path\.realpath\s*\(",      "os.path.realpath — path traversal mitigation"),
    (r"yaml\.safe_",                  "yaml.safe_* method"),
    (r"re\.escape\s*\(",              "re.escape()"),
    (r"html\.escape\s*\(",            "html.escape()"),
]