From c5590b52bb5cf53def6ff9c1f970eeb2852cedca Mon Sep 17 00:00:00 2001 From: Danang Avan Maulana Date: Sat, 31 Oct 2026 04:47:54 +0700 Subject: [PATCH] fix(live): persona replies keep the thread, follow the recipient's language, and handle refusals - The persona prompt now includes the original lure (HTML stripped, GoPhish {{.URL}} shown as [link]); previously the model was asked to continue a thread it could not see. - History holds only what the recipient saw: their messages and replies that were actually sent (no unapproved or discarded drafts). - The persona answers in the language the recipient wrote in, falling back to the campaign language. - A model refusal or empty output is retried once; if it persists, an empty draft marked persona_declined is queued so the reviewer writes the turn, instead of queueing the refusal text or a blank email. - Approving a draft with an empty body is rejected (400). - Review queue includes the campaign name and the message being answered; Reply Review page localized (EN/ID) and shows the declined state. Co-Authored-By: Claude Opus 5.5 --- backend/routes/live.py | 24 ++++- backend/services/live/conversation.py | 131 +++++++++++++++++++++--- backend/tests/test_live_conversation.py | 125 ++++++++++++++++++++++ docs/live-campaign-walkthrough.md | 1 + frontend/src/i18n/locales/en.json | 18 ++++ frontend/src/i18n/locales/id.json | 18 ++++ frontend/src/pages/LiveCampaignsPage.js | 4 +- frontend/src/pages/LiveReviewPage.js | 56 ++++++---- 8 files changed, 343 insertions(+), 34 deletions(-) diff --git a/backend/routes/live.py b/backend/routes/live.py index 23a1c7c..bd9b0c5 100644 --- a/backend/routes/live.py +++ b/backend/routes/live.py @@ -562,7 +562,27 @@ async def review_queue(current_user: User = Depends(require_trainer)): scope = _scope(current_user) query = {"direction": "outbound", "status": "draft_pending"} query.update(scope) - return await db.live_messages.find(query, {"_id": 0}).sort("at", 1).to_list(500) + drafts = await db.live_messages.find(query, {"_id": 0}).sort("at", 1).to_list(500) + # Give the reviewer the message being answered and which campaign it is. + names: dict[str, str] = {} + for d in drafts: + cid = d["campaign_id"] + if cid not in names: + c = await db.live_campaigns.find_one({"id": cid}, {"_id": 0, "name": 1}) + names[cid] = (c or {}).get("name", "") + d["campaign_name"] = names[cid] + last_in = await db.live_messages.find_one( + { + "campaign_id": cid, + "email": d["email"], + "direction": "inbound", + "at": {"$lte": d["at"]}, + }, + {"_id": 0, "body_redacted": 1}, + sort=[("at", -1)], + ) + d["in_reply_to"] = (last_in or {}).get("body_redacted", "") + return drafts class ReviewDecision(BaseModel): @@ -587,6 +607,8 @@ async def approve_draft( if not campaign or campaign.get("killed"): raise HTTPException(status_code=409, detail="Campaign is not active") body = data.body if data.body is not None else msg["body_redacted"] + if not (body or "").strip(): + raise HTTPException(status_code=400, detail="Write a reply before sending") sent = await conversation.send_approved_reply(campaign, msg, body, current_user.id) await audit.record( current_user.id, "live.review.approve", message_id, {"edited": bool(data.body)} diff --git a/backend/services/live/conversation.py b/backend/services/live/conversation.py index 1697996..58054ff 100644 --- a/backend/services/live/conversation.py +++ b/backend/services/live/conversation.py @@ -13,9 +13,17 @@ The persona prompt is built here on the server from campaign fields only; the browser never supplies prompt text (G7). Secrets never reach the model: the inbound text is scrubbed first. + +The prompt includes the original lure so the model knows the thread, and it answers in +the language the recipient wrote in. A hosted model may still decline to draft; +that is retried once and otherwise queued as an empty draft marked +`persona_declined`, so the reviewer writes the reply instead of approving a +refusal or an empty email. """ +import html import logging +import re import uuid from datetime import UTC, datetime @@ -46,18 +54,92 @@ } -def build_persona_prompt(campaign: dict, turn: int) -> str: - """Locked system prompt. Built only from approved campaign fields.""" +_TAG_RE = re.compile(r"<[^>]+>") +_TEMPLATE_URL_RE = re.compile(r"\{\{\s*\.URL\s*\}\}") +_TEMPLATE_RE = re.compile(r"\{\{[^}]*\}\}") + +_ID_WORDS = { + "apa", "ini", "itu", "saya", "aku", "anda", "kamu", "tidak", "nggak", "gak", "yang", + "dan", "untuk", "dengan", "ada", "bisa", "sudah", "belum", "mohon", "kenapa", "mengapa", + "siapa", "bapak", "pak", "ibu", "bu", "terima", "kasih", "tolong", "kok", "gimana", + "bagaimana", "apakah", "maaf", "kode", "dari", "ke", "di", "akan", "harus", "boleh", +} # fmt: skip +_EN_WORDS = { + "what", "is", "this", "the", "i", "you", "please", "why", "who", "can", "not", "and", + "to", "of", "for", "with", "my", "your", "it", "do", "does", "are", "sorry", "thanks", +} # fmt: skip + +# A model declining to write the persona turn, in English or Indonesian. +_REFUSAL_VERBS = ( + r"(?:help|assist|generate|create|write|draft|produce|provide|comply|fulfil+|" + r"continue|participate|engage|do that|role-?play)" +) +_REFUSAL_RE = re.compile( + r"^\W*(?:(?:i'm\s+)?sorry,?\s+(?:but\s+)?i\b|as an ai\b" + r"|maaf,?\s+(?:saya|aku)\s+tidak\s+(?:bisa|dapat)\b" + r"|(?:saya|aku)\s+tidak\s+(?:bisa|dapat)\s+(?:membantu|membuat|menulis)\b)" + r"|\bi\s*(?:cannot|can't|can not|won't|will not|am unable to|'m unable to|am not able to)" + rf"\s+(?:\w+\s+)?{_REFUSAL_VERBS}\b", + re.IGNORECASE, +) + + +def lure_text(campaign: dict) -> str: + """The original email as plain text, the first turn of the thread.""" + raw = campaign.get("body_html") or campaign.get("body") or "" + raw = _TEMPLATE_URL_RE.sub("[link]", raw) + raw = _TEMPLATE_RE.sub("", raw) + raw = re.sub(r"(?i)|

||", "\n", raw) + text = html.unescape(_TAG_RE.sub("", raw)) + text = re.sub(r"[ \t]+", " ", text) + text = re.sub(r"\n\s*\n+", "\n\n", text).strip() + return text[:2000] + + +def detect_language(text: str) -> str | None: + """'id' or 'en' when the recipient's words make it clear, else None.""" + words = re.findall(r"[a-zA-Z']+", (text or "").lower()) + id_hits = sum(w in _ID_WORDS for w in words) + en_hits = sum(w in _EN_WORDS for w in words) + if id_hits > en_hits: + return "id" + if en_hits > id_hits: + return "en" + return None + + +def is_refusal(draft: str) -> bool: + """True when the model produced nothing usable as the persona's reply.""" + text = (draft or "").strip() + return not text or bool(_REFUSAL_RE.search(text[:300])) + + +def _lure_block(campaign: dict) -> str: + text = lure_text(campaign) + if not text: + return "" + subject = campaign.get("subject") or "" + return f"The email you sent first (subject: {subject}):\n---\n{text}\n---\n\n" + + +def build_persona_prompt(campaign: dict, turn: int, language: str | None = None) -> str: + """Locked system prompt. Built only from approved campaign fields; `language` + is the recipient's language when it could be detected.""" goal = _OBJECTIVE_GOALS.get(campaign["objective"], "continue the pretext") principles = ", ".join(campaign.get("principles", [])) lang = ( "Write in Indonesian (Bahasa Indonesia)." - if campaign.get("language") == "id" + if (language or campaign.get("language")) == "id" else "Write in English." ) return ( - "You are role-playing an attacker in an AUTHORIZED security awareness " - "simulation. You are continuing an email thread you started.\n\n" + "You are role-playing the sender of a simulated phishing email in an " + "AUTHORIZED security awareness exercise run by the recipient's own " + "organization. Your reply is held for a human trainer to review before it " + "is sent, the recipient is debriefed afterwards, and no real credentials " + "or codes are ever collected. You are continuing the email thread you " + "started.\n\n" + f"{_lure_block(campaign)}" f"Your single objective: {goal}.\n" f"Influence techniques you may use: {principles}.\n" f"You are '{campaign['sender_name']}'. Do not claim to be anyone else.\n\n" @@ -73,7 +155,13 @@ def build_persona_prompt(campaign: dict, turn: int) -> str: async def _record_message( - campaign: dict, email: str, direction: str, body: str, status: str, secrets_found=None + campaign: dict, + email: str, + direction: str, + body: str, + status: str, + secrets_found=None, + **extra, ) -> None: doc = { "id": str(uuid.uuid4()), @@ -86,6 +174,7 @@ async def _record_message( "secrets_found": secrets_found or [], "reviewed_by": None, "at": datetime.now(UTC), + **extra, } await db.live_messages.insert_one(doc) return doc @@ -166,17 +255,35 @@ async def process_inbound(campaign: dict, email: str, raw_body: str, gateway=Non if not gateway: return {"status": "awaiting_gateway"} + thread = await _history(campaign["id"], email) + # Only what the recipient actually saw: their messages and replies we sent. history = [ { "role": "assistant" if m["direction"] == "outbound" else "user", "content": m["body_redacted"], } - for m in await _history(campaign["id"], email) + for m in thread + if m["body_redacted"] and (m["direction"] == "inbound" or m.get("status") == "sent") ] - draft = await gateway.persona_reply( - system_prompt=build_persona_prompt(campaign, turn), - history=history, + language = detect_language(redacted) + prompt = build_persona_prompt(campaign, turn, language) + draft = "" + for _attempt in range(2): + draft = (await gateway.persona_reply(system_prompt=prompt, history=history)).strip() + if not is_refusal(draft): + break + await db.live_recipients.update_one( + {"campaign_id": campaign["id"], "email": email}, + {"$set": {"turn": turn, "conversation_state": "active", "updated_at": _now()}}, ) + if is_refusal(draft): + # Queue an empty draft so a human writes this turn; never send a refusal. + logger.info(f"persona declined to draft a reply for campaign {campaign['id']}") + await _record_message( + campaign, email, "outbound", "", "draft_pending", persona_declined=True + ) + return {"status": "persona_declined", "violations": [], "draft": ""} + violations = review_draft( draft, objective=campaign["objective"], @@ -186,10 +293,6 @@ async def process_inbound(campaign: dict, email: str, raw_body: str, gateway=Non ) status = "blocked" if violations else "draft_pending" await _record_message(campaign, email, "outbound", draft, status) - await db.live_recipients.update_one( - {"campaign_id": campaign["id"], "email": email}, - {"$set": {"turn": turn, "conversation_state": "active", "updated_at": _now()}}, - ) return {"status": status, "violations": violations, "draft": draft} diff --git a/backend/tests/test_live_conversation.py b/backend/tests/test_live_conversation.py index e250d41..e1445eb 100644 --- a/backend/tests/test_live_conversation.py +++ b/backend/tests/test_live_conversation.py @@ -287,3 +287,128 @@ async def test_approve_blocked_on_killed_campaign(api): async def test_users_cannot_access_review(api): _, h = await make_user("user") assert (await api.get("/api/live/review", headers=h)).status_code == 403 + + +# ---------- persona reliability ---------- + + +class ScriptedGateway(FakeGateway): + """Returns the scripted replies in order and records what it was sent.""" + + def __init__(self, replies): + super().__init__() + self._replies = list(replies) + self.calls = [] + + async def persona_reply(self, *, system_prompt, history): + self.calls.append({"system_prompt": system_prompt, "history": history}) + return self._replies.pop(0) + + +@pytest.mark.parametrize( + "text,refused", + [ + ("", True), + (" ", True), + ("I cannot generate persuasive phishing messages, even in a simulation.", True), + ("I'm sorry, but I can't help with that.", True), + ("Sorry, I won't write that.", True), + ("As an AI, I must decline.", True), + ("Maaf, saya tidak bisa membantu membuat pesan ini.", True), + ("Just a quick follow-up on the reset, it only takes a minute.", False), + ("I can't wait to get this sorted for you before the deadline.", False), + ("I'm sorry for the delay, the reset link is still valid today.", False), + ("Halo, ini dari IT Helpdesk. Mohon segera selesaikan verifikasinya ya.", False), + ], +) +def test_is_refusal(text, refused): + assert conversation.is_refusal(text) is refused + + +@pytest.mark.parametrize( + "text,lang", + [ + ("Apa ini ?", "id"), + ("Maaf, ini dari siapa ya pak?", "id"), + ("What is this?", "en"), + ("ok", None), + ], +) +def test_detect_language(text, lang): + assert conversation.detect_language(text) == lang + + +def test_lure_text_strips_html_and_template_tags(): + camp = {"body_html": "

Hi {{.FirstName}},

Verify here: {{.URL}}

& thanks"} + text = conversation.lure_text(camp) + assert "Verify here: [link]" in text and "

" not in text and "{{" not in text + assert "& thanks" in text + + +async def test_prompt_carries_lure_and_follows_recipient_language(api): + camp = await _running_campaign() + camp["body_html"] = "

Your mailbox will be locked. Verify: {{.URL}}

" + gw = ScriptedGateway(["Halo, ini tindak lanjut dari IT Helpdesk soal verifikasi tadi."]) + r = await conversation.process_inbound(camp, "bob@acme.test", "Apa ini ?", gw) + assert r["status"] == "draft_pending" + prompt = gw.calls[0]["system_prompt"] + assert "Your mailbox will be locked. Verify: [link]" in prompt + assert "Write in Indonesian" in prompt # campaign is "en", recipient wrote Indonesian + assert gw.calls[0]["history"] == [{"role": "user", "content": "Apa ini ?"}] + + +async def test_refusal_is_retried_once(api): + camp = await _running_campaign() + gw = ScriptedGateway(["I cannot help with phishing.", "A quick follow-up on the reset."]) + r = await conversation.process_inbound(camp, "bob@acme.test", "who is this?", gw) + assert r["status"] == "draft_pending" and r["draft"] == "A quick follow-up on the reset." + assert len(gw.calls) == 2 + + +async def test_persistent_refusal_queues_empty_draft_for_human(api): + camp = await _running_campaign() + gw = ScriptedGateway(["I cannot help with phishing.", ""]) + r = await conversation.process_inbound(camp, "bob@acme.test", "who is this?", gw) + assert r["status"] == "persona_declined" + draft = await db.live_messages.find_one({"campaign_id": camp["id"], "direction": "outbound"}) + assert draft["status"] == "draft_pending" and draft["persona_declined"] is True + assert draft["body_redacted"] == "" # the refusal text is never queued + + +async def test_unsent_drafts_are_not_in_history(api): + camp = await _running_campaign() + await conversation.process_inbound(camp, "bob@acme.test", "who is this?", FakeGateway()) + gw = ScriptedGateway(["Following up again."]) + await conversation.process_inbound(camp, "bob@acme.test", "hello?", gw) + roles = [m["role"] for m in gw.calls[0]["history"]] + assert roles == ["user", "user"] # the first draft was never approved, so never seen + + +async def test_approve_rejects_empty_body(api, _no_real_mail): + _, h = await make_user("trainer", organization_id="o1") + _, draft = await _seed_draft(org="o1") + await db.live_messages.update_one({"id": draft["id"]}, {"$set": {"body_redacted": ""}}) + r = await api.post(f"/api/live/review/{draft['id']}/approve", json={}, headers=h) + assert r.status_code == 400 + r = await api.post(f"/api/live/review/{draft['id']}/approve", json={"body": " "}, headers=h) + assert r.status_code == 400 + assert not _no_real_mail + + +async def test_review_queue_includes_context(api): + _, h = await make_user("trainer", organization_id="o1") + camp, draft = await _seed_draft(org="o1") + await db.live_messages.insert_one( + { + "id": "in-1", + "campaign_id": camp["id"], + "organization_id": "o1", + "email": "bob@acme.test", + "direction": "inbound", + "body_redacted": "who is this?", + "status": "received", + "at": datetime(2020, 1, 1, tzinfo=UTC), + } + ) + row = (await api.get("/api/live/review", headers=h)).json()[0] + assert row["in_reply_to"] == "who is this?" and row["campaign_name"] == "drill" diff --git a/docs/live-campaign-walkthrough.md b/docs/live-campaign-walkthrough.md index 97d2605..6174c75 100644 --- a/docs/live-campaign-walkthrough.md +++ b/docs/live-campaign-walkthrough.md @@ -309,6 +309,7 @@ To run against a real organization instead of the sandbox: | My reply never appears at all | IMAP isn't configured (Settings → Live), or the reply went to the sender address instead of the `Reply-To`. Check the campaign **Conversation** panel and run **Poll now** (Settings/API `POST /live/poll`). | | I replied to the lure but **Reported** stays 0 | Replying is engaging, not reporting. Forward the email to the report mailbox (`lapor@acme.test`); it is credited within ~30 s, or click **Periksa sekarang** on Laporan Phishing. | | My reply lands in the wrong place | The IMAP user must be the reply mailbox (`sim-reply@acme.test`), not the campaign sender. | +| Draft in Reply Review is empty and marked **AI did not draft** | The LLM declined to write that turn (hosted models sometimes refuse, most often for the OTP objective). Pretexta retries once, then queues an empty draft instead of a refusal: write the reply yourself or discard it. A local Ollama model avoids provider-side refusals. | | Reply shows in Conversation but not in Reply Review | No LLM provider configured — the persona can't draft a reply. Configure one in Settings → LLM (local Ollama works). | | Reply read in Thunderbird, poller skipped it | Fixed: the poller tracks UIDs, not the read/\Seen flag, so this no longer happens. | | Phishing link won't open (`http://gophish?rid=…`) | `phish_url` is set to an internal Docker name. Set it to a browser-reachable address (`http://localhost:8080` locally, or your public phishing URL), then **launch a new campaign** — the URL is baked in at launch. | diff --git a/frontend/src/i18n/locales/en.json b/frontend/src/i18n/locales/en.json index 4d0fb89..ffd95ca 100644 --- a/frontend/src/i18n/locales/en.json +++ b/frontend/src/i18n/locales/en.json @@ -751,5 +751,23 @@ "live_complied": "In a live phishing simulation you agreed to a request that used {{principle}}. This exercise helps you recognise it next time.", "live_disclosed": "In a live phishing simulation you shared a secret in reply to a message that used {{principle}}. This exercise helps you recognise it next time.", "retest": "Scheduled retest for {{principle}} — a different exercise to confirm the lesson stuck." + }, + "review": { + "title": "Reply Review Queue", + "subtitle": "Approve each AI persona reply before it is sent (review mode)", + "empty_title": "Queue is empty", + "empty_desc": "AI replies awaiting approval will appear here.", + "awaiting": "awaiting review", + "declined": "AI did not draft", + "they_wrote": "They wrote", + "declined_note": "The AI model declined to draft this turn. Write the reply yourself, or discard it.", + "placeholder": "Write the persona's reply…", + "help": "Every AI reply is held here until a human approves it. Edit before sending if needed.", + "approve": "Approve & send", + "discard": "Discard", + "sent": "Reply sent", + "discarded": "Discarded", + "approve_failed": "Approve failed", + "discard_failed": "Discard failed" } } diff --git a/frontend/src/i18n/locales/id.json b/frontend/src/i18n/locales/id.json index 2f43a51..188e884 100644 --- a/frontend/src/i18n/locales/id.json +++ b/frontend/src/i18n/locales/id.json @@ -751,5 +751,23 @@ "live_complied": "Dalam simulasi phishing, Anda menuruti permintaan yang memakai prinsip {{principle}}. Latihan ini membantu Anda mengenalinya lain kali.", "live_disclosed": "Dalam simulasi phishing, Anda membagikan rahasia sebagai balasan pesan yang memakai prinsip {{principle}}. Latihan ini membantu Anda mengenalinya lain kali.", "retest": "Tes ulang terjadwal untuk prinsip {{principle}} — latihan berbeda untuk memastikan pelajarannya melekat." + }, + "review": { + "title": "Antrean Review Balasan", + "subtitle": "Setujui setiap balasan persona AI sebelum dikirim (mode review)", + "empty_title": "Antrean kosong", + "empty_desc": "Balasan AI yang menunggu persetujuan akan muncul di sini.", + "awaiting": "menunggu review", + "declined": "AI tidak membuat draf", + "they_wrote": "Pesan penerima", + "declined_note": "Model AI menolak membuat balasan untuk giliran ini. Tulis balasannya sendiri, atau buang.", + "placeholder": "Tulis balasan persona…", + "help": "Setiap balasan AI ditahan di sini sampai disetujui manusia. Sunting dulu bila perlu.", + "approve": "Setujui & kirim", + "discard": "Buang", + "sent": "Balasan terkirim", + "discarded": "Dibuang", + "approve_failed": "Gagal menyetujui", + "discard_failed": "Gagal membuang" } } diff --git a/frontend/src/pages/LiveCampaignsPage.js b/frontend/src/pages/LiveCampaignsPage.js index f8333fb..de77d32 100644 --- a/frontend/src/pages/LiveCampaignsPage.js +++ b/frontend/src/pages/LiveCampaignsPage.js @@ -656,7 +656,9 @@ function Detail({ id, onChanged, onDeleted }) { {msg.direction === 'inbound' ? t('live.msg.replied', { email: msg.email }) : t('live.msg.persona')} {t(`status.${msg.status}`, { defaultValue: msg.status })} -
{msg.body_redacted}
+ {msg.persona_declined && !msg.body_redacted + ?
{t('review.declined_note')}
+ :
{msg.body_redacted}
} {msg.secrets_found?.length > 0 && (
{t('live.msg.redacted', { kinds: msg.secrets_found.join(', ') })}
)} diff --git a/frontend/src/pages/LiveReviewPage.js b/frontend/src/pages/LiveReviewPage.js index d10fd79..6abd2d5 100644 --- a/frontend/src/pages/LiveReviewPage.js +++ b/frontend/src/pages/LiveReviewPage.js @@ -1,5 +1,6 @@ import React, { useEffect, useState, useCallback } from 'react'; -import { MessageSquare, Check, Trash2, RefreshCw, ShieldAlert } from 'lucide-react'; +import { useTranslation } from 'react-i18next'; +import { MessageSquare, Check, Trash2, RefreshCw, ShieldAlert, Bot } from 'lucide-react'; import { Card } from '../components/ui/card'; import { Button } from '../components/ui/button'; import { Badge } from '../components/ui/badge'; @@ -10,6 +11,7 @@ import api from '../services/api'; import { toast } from 'sonner'; function DraftCard({ draft, onDone }) { + const { t } = useTranslation(); const [body, setBody] = useState(draft.body_redacted); const [busy, setBusy] = useState(false); @@ -18,38 +20,58 @@ function DraftCard({ draft, onDone }) { try { const edited = body !== draft.body_redacted ? { body } : {}; await api.post(`/live/review/${draft.id}/approve`, edited); - toast.success('Reply sent'); + toast.success(t('review.sent')); onDone(); } catch (e) { - toast.error(e.response?.data?.detail || 'Approve failed'); + toast.error(e.response?.data?.detail || t('review.approve_failed')); setBusy(false); } }; const discard = async () => { setBusy(true); - try { await api.post(`/live/review/${draft.id}/discard`); toast.success('Discarded'); onDone(); } - catch { toast.error('Discard failed'); setBusy(false); } + try { await api.post(`/live/review/${draft.id}/discard`); toast.success(t('review.discarded')); onDone(); } + catch { toast.error(t('review.discard_failed')); setBusy(false); } }; return ( -
- {draft.email} - awaiting review +
+
+ {draft.email} + {draft.campaign_name && ( + {draft.campaign_name} + )} +
+ {draft.persona_declined + ? {t('review.declined')} + : {t('review.awaiting')}}
-