diff --git a/backend/models/schemas.py b/backend/models/schemas.py index 2432430..875ea9e 100644 --- a/backend/models/schemas.py +++ b/backend/models/schemas.py @@ -353,6 +353,10 @@ class Assignment(BaseModel): reason: str | None = None # human-readable explanation shown to the trainee principle: str | None = None # the Cialdini principle being remediated retest_of: str | None = None # the remediation assignment this re-tests + # Translatable form of `reason` for system assignments: the client renders + # remediation. with these params; `reason` stays as the fallback. + reason_key: str | None = None + reason_params: dict[str, Any] | None = None created_at: str = Field(default_factory=lambda: datetime.now(UTC).isoformat()) diff --git a/backend/routes/__init__.py b/backend/routes/__init__.py index 696dece..e979c7e 100644 --- a/backend/routes/__init__.py +++ b/backend/routes/__init__.py @@ -5,6 +5,7 @@ from routes.campaigns import router as campaigns_router from routes.challenges import router as challenges_router from routes.debrief import router as debrief_router +from routes.demo import router as demo_router from routes.freetext import router as freetext_router from routes.glossary import router as glossary_router from routes.imports import router as imports_router @@ -17,6 +18,7 @@ from routes.organizations import router as organizations_router from routes.personas import router as personas_router from routes.quizzes import router as quizzes_router +from routes.readiness import router as readiness_router from routes.report_inbox import router as report_inbox_router from routes.reports import router as reports_router from routes.risk_profile import router as risk_profile_router @@ -47,6 +49,7 @@ "webhooks_router", "scenario_builder_router", "debrief_router", + "demo_router", # v3 platform services "personas_router", "risk_profile_router", @@ -56,5 +59,6 @@ "live_router", "audit_router", "attack_surface_router", + "readiness_router", "report_inbox_router", ] diff --git a/backend/routes/demo.py b/backend/routes/demo.py new file mode 100644 index 0000000..84d6ddd --- /dev/null +++ b/backend/routes/demo.py @@ -0,0 +1,28 @@ +from fastapi import APIRouter, Depends + +from models.schemas import User +from services import audit, demo +from services.auth import require_admin + +router = APIRouter(prefix="/demo", tags=["demo"]) + + +@router.get("") +async def demo_status(user: User = Depends(require_admin)): + return await demo.status() + + +@router.post("") +async def load_demo(user: User = Depends(require_admin)): + """Load (or reload) the fictional demo organization. Admin only.""" + result = await demo.seed(user.id, user.organization_id or f"org-{user.id}") + await audit.record(user.id, "demo.load", None, {"people": result["people"]}) + return result + + +@router.delete("") +async def clear_demo(user: User = Depends(require_admin)): + """Remove all demo records; real data is never touched.""" + result = await demo.clear() + await audit.record(user.id, "demo.clear", None, result) + return result diff --git a/backend/routes/live.py b/backend/routes/live.py index 25824a9..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)} @@ -646,6 +668,9 @@ async def set_kill_switch(data: KillSwitchRequest, current_user: User = Depends( # ==================== GOPHISH CONFIG (admin) ==================== +_SECRETS = ("gophish_api_key", "smtp_password", "imap_password", "report_imap_password") + + class LiveConfig(BaseModel): model_config = {"extra": "forbid"} gophish_base_url: str = Field(min_length=1, max_length=300) @@ -666,6 +691,10 @@ class LiveConfig(BaseModel): imap_ssl: bool = True imap_user: str = "" imap_password: str = "" + # Report mailbox on the same IMAP server: staff forward suspicious mail here + # and reporters of our simulations are credited automatically. + report_imap_user: str = Field(default="", max_length=200) + report_imap_password: str = "" @router.get("/config") @@ -674,7 +703,7 @@ async def get_config(current_user: User = Depends(require_admin)): if not config: return None # Never return stored secrets - for secret in ("gophish_api_key", "smtp_password", "imap_password"): + for secret in _SECRETS: if config.get(secret): config[secret] = "***" return config @@ -682,10 +711,11 @@ async def get_config(current_user: User = Depends(require_admin)): @router.post("/poll") async def poll_inbox(current_user: User = Depends(require_admin)): - """Fetch and route inbound replies now. The poller also runs on a timer.""" - from services.live.inbound import poll_once + """Fetch and route inbound replies and forwarded reports now. The poller + also runs on a timer.""" + from services.live.inbound import poll_once, poll_reports_once - return await poll_once() + return {"replies": await poll_once(), "reports": await poll_reports_once()} @router.put("/config") @@ -694,9 +724,15 @@ async def set_config(data: LiveConfig, current_user: User = Depends(require_admi # field (e.g. an older client without the IMAP-SSL toggle) does not reset it # to the model default. doc = {"id": "live_config", **data.model_dump(exclude_unset=True)} + report_box = (doc.get("report_imap_user") or "").strip().lower() + if report_box and report_box == (doc.get("imap_user") or "").strip().lower(): + # Replies would then be counted as reports. + raise HTTPException( + status_code=400, detail="Report mailbox must differ from the reply mailbox" + ) # A resubmitted mask ("***") means "keep the stored secret", not overwrite it. existing = await db.live_config.find_one({"id": "live_config"}, {"_id": 0}) - for secret in ("gophish_api_key", "smtp_password", "imap_password"): + for secret in _SECRETS: if doc.get(secret) in ("", "***") and existing: doc[secret] = existing.get(secret, "") await db.live_config.update_one({"id": "live_config"}, {"$set": doc}, upsert=True) diff --git a/backend/routes/readiness.py b/backend/routes/readiness.py new file mode 100644 index 0000000..83dc482 --- /dev/null +++ b/backend/routes/readiness.py @@ -0,0 +1,21 @@ +from fastapi import APIRouter, Depends + +from models.schemas import User +from services import readiness +from services.auth import require_trainer + +router = APIRouter(prefix="/readiness", tags=["readiness"]) + + +@router.get("") +async def get_readiness(user: User = Depends(require_trainer)): + """Cohort-level readiness overview: index, report-rate trend, remediation loop, + weakest principles and attack-surface posture. Admins see every organization; + trainers only their own.""" + scope = {} if user.role == "admin" else {"organization_id": user.organization_id} + return await readiness.overview( + user_scope=scope, + campaign_scope=scope, + # Same workspace the Attack Surface Map uses for this user. + org_id=user.organization_id or f"org-{user.id}", + ) diff --git a/backend/routes/report_inbox.py b/backend/routes/report_inbox.py index 15c0aad..00f41ac 100644 --- a/backend/routes/report_inbox.py +++ b/backend/routes/report_inbox.py @@ -43,6 +43,22 @@ async def report_leaderboard(user: User = Depends(require_trainer)): return await report_inbox.leaderboard(_scope(user)) +@router.get("/mailbox") +async def report_mailbox(user: User = Depends(require_trainer)): + """The address staff forward suspicious mail to, if a report mailbox is set up.""" + config = await db.live_config.find_one({"id": "live_config"}, {"_id": 0}) or {} + address = (config.get("report_imap_user") or "").strip().lower() + return {"address": address or None, "configured": bool(address and config.get("imap_host"))} + + +@router.post("/poll") +async def poll_report_mailbox(user: User = Depends(require_trainer)): + """Fetch forwarded reports now instead of waiting for the next poller tick.""" + from services.live.inbound import poll_reports_once + + return await poll_reports_once() + + class ReportIn(BaseModel): reporter_email: str = Field(min_length=3, max_length=320) subject: str = Field(default="", max_length=400) @@ -52,7 +68,7 @@ class ReportIn(BaseModel): @router.post("/ingest", status_code=201) async def ingest(body: ReportIn, user: User = Depends(require_trainer)): - """Record a reported message (the integration point the mail poller also uses).""" + """Record a reported message by hand (the report mailbox poller feeds the same path).""" org_id = None if user.role == "admin" else user.organization_id report = await report_inbox.ingest_report( org_id, body.reporter_email, body.subject, body.from_address, body.body diff --git a/backend/routes/triage.py b/backend/routes/triage.py index d327363..1250e1b 100644 --- a/backend/routes/triage.py +++ b/backend/routes/triage.py @@ -5,12 +5,15 @@ the server grades, computing d' with a false-alarm penalty. """ -from fastapi import APIRouter, Depends -from pydantic import BaseModel, Field +import uuid +from typing import Literal + +from fastapi import APIRouter, Depends, HTTPException +from pydantic import BaseModel, Field, model_validator from models.schemas import User -from services import triage -from services.auth import get_current_user +from services import audit, triage +from services.auth import get_current_user, require_trainer from services.database import db router = APIRouter(prefix="/triage", tags=["triage"]) @@ -43,3 +46,61 @@ async def submit( set_id: str, data: TriageSubmission, current_user: User = Depends(get_current_user) ): return await triage.grade(set_id, current_user.id, data.marks, data.duration_seconds) + + +# ---- authoring: trainers add their own sets (e.g. a unit-specific content pack) + + +class TriageItemIn(BaseModel): + model_config = {"extra": "forbid"} + channel: Literal["email_inbox", "whatsapp", "sms"] = "email_inbox" + # For chat channels `from` is the contact as shown in the chat header and + # `subject` is the one-line preview in the chat list. + from_: str = Field(alias="from", min_length=1, max_length=200) + subject: str = Field(min_length=1, max_length=200) + body: str = Field(min_length=1, max_length=5000) + is_pretext: bool + principle: str | None = Field(default=None, max_length=40) + explanation: str | None = Field(default=None, max_length=2000) + + @model_validator(mode="after") + def _pretext_is_explained(self): + if self.is_pretext and not (self.principle and self.explanation): + raise ValueError("a pretext needs a principle and an explanation for the debrief") + return self + + +class TriageSetIn(BaseModel): + model_config = {"extra": "forbid"} + id: str | None = Field(default=None, min_length=3, max_length=80, pattern=r"^[a-z0-9-]+$") + title: str = Field(min_length=1, max_length=120) + description: str = Field(default="", max_length=1000) + language: Literal["en", "id"] = "en" + difficulty: Literal["easy", "medium", "hard"] = "medium" + items: list[TriageItemIn] = Field(min_length=4, max_length=40) + + @model_validator(mode="after") + def _has_both_kinds(self): + # d' needs at least one pretext and one legitimate message to be defined. + kinds = {i.is_pretext for i in self.items} + if kinds != {True, False}: + raise ValueError("a set needs both pretexts and legitimate messages") + return self + + +@router.post("", status_code=201) +async def create_set(data: TriageSetIn, current_user: User = Depends(require_trainer)): + set_id = data.id or f"triage-{uuid.uuid4().hex[:12]}" + if await db.triage_sets.find_one({"id": set_id}, {"_id": 1}): + raise HTTPException(status_code=409, detail="A triage set with this id already exists") + doc = { + **data.model_dump(exclude={"items", "id"}), + "id": set_id, + "created_by": current_user.id, + "items": [ + {"id": str(uuid.uuid4()), **item.model_dump(by_alias=True)} for item in data.items + ], + } + await db.triage_sets.insert_one(doc) + await audit.record(current_user.id, "triage.set.create", set_id, {"items": len(data.items)}) + return {"id": set_id, "items": len(data.items)} diff --git a/backend/server.py b/backend/server.py index d9e9874..67079b7 100644 --- a/backend/server.py +++ b/backend/server.py @@ -21,6 +21,7 @@ campaigns_router, challenges_router, debrief_router, + demo_router, freetext_router, glossary_router, imports_router, @@ -32,6 +33,7 @@ organizations_router, personas_router, quizzes_router, + readiness_router, report_inbox_router, reports_router, risk_profile_router, @@ -487,6 +489,8 @@ async def health_check(): api_router.include_router(audit_router) api_router.include_router(attack_surface_router) api_router.include_router(report_inbox_router) +api_router.include_router(readiness_router) +api_router.include_router(demo_router) app.include_router(api_router) diff --git a/backend/services/demo.py b/backend/services/demo.py new file mode 100644 index 0000000..59bc793 --- /dev/null +++ b/backend/services/demo.py @@ -0,0 +1,351 @@ +""" +Demo data — one complete, believable loop for a booth, a judge or a first look. + +Seeds a fictional organization on the sandbox domain `demo.test` whose story +shows what Pretexta is for: report rate climbs across three campaigns, the +people who clicked are automatically given practice for the principle that +caught them, most pass, and most pass the retest. Lab results and risk +profiles fill in the rest of the Readiness screen. + +Everything written here carries `demo: True` (or `seeded: True` on map nodes, +tracked in `demo_state`) so `clear()` removes exactly the demo and nothing a +real user created. Seeding is deterministic: the same numbers every time. +""" + +import random +import secrets +import uuid +from datetime import UTC, datetime, timedelta + +from models.schemas import LiveCampaign, User +from services import attack_surface +from services.auth import hash_password +from services.database import db + +DEMO_ORG = "org-demo" +DEMO_DOMAIN = "demo.test" +PRINCIPLES = ("reciprocity", "scarcity", "authority", "commitment", "liking", "social_proof") + +PEOPLE = [ + ("andi", "Andi Pratama"), + ("sari", "Sari Wulandari"), + ("budi", "Budi Santoso"), + ("dewi", "Dewi Lestari"), + ("rizky", "Rizky Hidayat"), + ("maya", "Maya Anggraini"), + ("fajar", "Fajar Nugroho"), + ("intan", "Intan Permata"), + ("yoga", "Yoga Saputra"), + ("nadia", "Nadia Rahma"), + ("hendra", "Hendra Wijaya"), + ("putri", "Putri Ayu"), +] + +# Three campaigns, oldest first. Indexes refer to PEOPLE. The arc: report rate +# 25% -> 50% -> 75%, click rate 42% -> 25% -> 8%, and reports arrive faster. +CAMPAIGNS = [ + { + "days_ago": 75, + "name": "Baseline — mailbox quota", + "principles": ["authority", "scarcity"], + "sender": ("IT Helpdesk", "helpdesk"), + "subject": "Mailbox almost full — verify within 24 hours", + "clicked": [0, 1, 2, 3, 4], + "reported": [8, 9, 10], + "report_minutes": [95, 180, 240], + }, + { + "days_ago": 45, + "name": "Payroll update", + "principles": ["authority", "commitment"], + "sender": ("Payroll", "payroll"), + "subject": "Action needed: confirm your payroll details", + "clicked": [0, 1, 5], + "reported": [2, 3, 4, 8, 9, 10], + "report_minutes": [25, 40, 60, 75, 110, 150], + }, + { + "days_ago": 15, + "name": "Executive request", + "principles": ["authority", "liking"], + "sender": ("Office of the Director", "director"), + "subject": "Quick favour — are you at your desk?", + "clicked": [6], + "reported": [0, 1, 2, 3, 4, 5, 8, 9, 10], + "report_minutes": [6, 9, 12, 15, 18, 22, 30, 41, 55], + }, +] + +# Which remediations and retests have been passed by now (by campaign index). +REMEDIATION_PASSED = {0: True, 1: True, 2: False} +RETEST_PASSED_FROM = {0: True, 1: False} # campaign-0 clickers already passed the retest + + +def _iso(dt: datetime) -> str: + return dt.isoformat() + + +async def _pick_content(principle: str) -> tuple[str, str, str]: + """A real exercise for the principle, so the assignment opens something.""" + doc = await db.challenges.find_one( + {"cialdini_categories": principle}, {"_id": 0, "id": 1, "title": 1} + ) + if doc: + return "challenge", doc["id"], doc["title"] + return "challenge", "demo", principle.replace("_", " ").title() + + +async def seed(admin_id: str, map_workspace: str) -> dict: + """Replace any previous demo data with a fresh copy of the story.""" + await clear() + rng = random.Random(42) # noqa: S311 — deterministic demo numbers, not security + now = datetime.now(UTC) + + users = [] + for key, name in PEOPLE: + user = User( + username=f"demo-{key}", + # Demo people never log in; their password is random and discarded. + password_hash=hash_password(secrets.token_urlsafe(24)), + email=f"{key}@{DEMO_DOMAIN}", + display_name=name, + role="user", + organization_id=DEMO_ORG, + ) + doc = user.model_dump() + doc["created_at"] = _iso(now - timedelta(days=90)) + doc["demo"] = True + await db.users.insert_one(doc) + users.append(doc) + + await db.verified_domains.insert_one( + { + "id": str(uuid.uuid4()), + "organization_id": DEMO_ORG, + "domain": DEMO_DOMAIN, + "token": secrets.token_urlsafe(12), + "sandbox": True, + "verified_at": _iso(now - timedelta(days=90)), + "created_by": admin_id, + "created_at": _iso(now - timedelta(days=90)), + "demo": True, + } + ) + + assignments = 0 + for ci, spec in enumerate(CAMPAIGNS): + launched = now - timedelta(days=spec["days_ago"]) + campaign = LiveCampaign( + name=spec["name"], + objective="click_link", + principles=spec["principles"], + sender_name=spec["sender"][0], + sender_email=f"{spec['sender'][1]}@{DEMO_DOMAIN}", + subject=spec["subject"], + body_html='

Please confirm here: verify

', + recipients=[u["email"] for u in users], + starts_at=launched, + ends_at=launched + timedelta(days=5), + max_turns=0, + organization_id=DEMO_ORG, + created_by=admin_id, + approved_by=admin_id, + status="closed", + ) + doc = campaign.model_dump() + doc.update( + { + "starts_at": _iso(launched), + "ends_at": _iso(launched + timedelta(days=5)), + "created_at": _iso(launched - timedelta(days=2)), + "launched_at": _iso(launched), + "recipient_count": len(users), + "demo": True, + } + ) + await db.live_campaigns.insert_one(doc) + + for i, u in enumerate(users): + state = "clicked" if i in spec["clicked"] else rng.choice(["sent", "opened"]) + await db.live_recipients.insert_one( + { + "campaign_id": campaign.id, + "email": u["email"], + "state": state, + "reported": i in spec["reported"], + "updated_at": _iso(launched + timedelta(hours=6)), + "demo": True, + } + ) + for i, minutes in zip(spec["reported"], spec["report_minutes"], strict=True): + await db.live_events.insert_one( + { + "campaign_id": campaign.id, + "organization_id": DEMO_ORG, + "email": users[i]["email"], + "type": "reported", + # BSON date, like the real events (TTL index, G14). + "at": launched + timedelta(minutes=minutes), + "demo": True, + } + ) + + # The remediation loop for everyone who clicked. + principle = spec["principles"][0] + content_type, content_id, title = await _pick_content(principle) + for i in spec["clicked"]: + remediation_id = str(uuid.uuid4()) + assigned = launched + timedelta(days=1) + passed = REMEDIATION_PASSED[ci] + await db.assignments.insert_one( + _assignment( + remediation_id, + users[i]["id"], + "remediation", + principle, + content_type, + content_id, + title, + assigned, + passed, + reason=f"Clicked the link in the live simulation '{spec['name']}'.", + ) + ) + assignments += 1 + if passed: + await db.assignments.insert_one( + _assignment( + str(uuid.uuid4()), + users[i]["id"], + "retest", + principle, + content_type, + content_id, + title, + assigned + timedelta(days=14), + RETEST_PASSED_FROM[ci], + reason="Scheduled retest — a different exercise to confirm the lesson stuck.", + retest_of=remediation_id, + ) + ) + assignments += 1 + + # Lab results and risk profiles: authority is the cohort's weak spot. + for u in users: + for run in range(2): + d_prime = round(rng.uniform(0.9, 1.6) + run * rng.uniform(0.3, 0.9), 2) + score = min(100, round(55 + d_prime * 15 + rng.uniform(-5, 5))) + completed = now - timedelta(days=60 - run * 40 - rng.randint(0, 5)) + await db.simulations.insert_one( + { + "id": str(uuid.uuid4()), + "user_id": u["id"], + "simulation_type": "triage", + "status": "completed", + "score": score, + "title": "Inbox Senin Pagi", + "events": [{"event_type": "triage_graded", "payload": {"d_prime": d_prime}}], + "started_at": _iso(completed - timedelta(minutes=4)), + "completed_at": _iso(completed), + "demo": True, + } + ) + vector = {p: float(rng.randint(62, 86)) for p in PRINCIPLES} + vector["authority"] = float(rng.randint(34, 58)) + await db.risk_profiles.insert_one( + { + "id": str(uuid.uuid4()), + "user_id": u["id"], + "current_vector": vector, + "baseline_vector": {p: 50.0 for p in PRINCIPLES}, + "demo": True, + } + ) + + # Attack Surface Map: the demo org, plus the admin's own workspace if it is + # empty (that is the map the admin's Readiness screen reads). + map_orgs = [DEMO_ORG] + if map_workspace != DEMO_ORG and not await db.org_nodes.find_one({"org_id": map_workspace}): + map_orgs.append(map_workspace) + for org in map_orgs: + await attack_surface.seed_demo_org(org) + await db.demo_state.update_one( + {"id": "demo"}, + {"$set": {"id": "demo", "map_orgs": map_orgs, "seeded_at": _iso(now)}}, + upsert=True, + ) + + return { + "people": len(users), + "campaigns": len(CAMPAIGNS), + "assignments": assignments, + "map_orgs": map_orgs, + } + + +def _assignment( + aid, + trainee_id, + source, + principle, + content_type, + content_id, + title, + created, + passed, + *, + reason, + retest_of=None, +): + return { + "id": aid, + "instructor_id": "system", + "trainee_id": trainee_id, + "content_type": content_type, + "content_id": content_id, + "title": title, + "instructions": reason, + "reason": reason, + "reason_key": "retest" if source == "retest" else "live_clicked", + "reason_params": {"principle": principle}, + "principle": principle, + "source": source, + "retest_of": retest_of, + "status": "completed" if passed else "active", + "passing_score": 70.0, + "max_attempts": 3, + "due_date": _iso(created + timedelta(days=7)), + "created_at": _iso(created), + "demo": True, + } + + +async def status() -> dict: + state = await db.demo_state.find_one({"id": "demo"}, {"_id": 0}) + return { + "loaded": bool(await db.users.find_one({"demo": True}, {"_id": 1})), + "seeded_at": (state or {}).get("seeded_at"), + } + + +async def clear() -> dict: + """Remove every demo record, and the map nodes the demo seeded.""" + removed = 0 + for name in ( + "users", + "verified_domains", + "live_campaigns", + "live_recipients", + "live_events", + "assignments", + "simulations", + "risk_profiles", + ): + removed += (await db[name].delete_many({"demo": True})).deleted_count + + state = await db.demo_state.find_one({"id": "demo"}, {"_id": 0}) or {} + for org in state.get("map_orgs", []): + removed += (await db.org_nodes.delete_many({"org_id": org, "seeded": True})).deleted_count + removed += (await db.trust_edges.delete_many({"org_id": org, "seeded": True})).deleted_count + await db.demo_state.delete_one({"id": "demo"}) + return {"removed": removed} 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/services/live/inbound.py b/backend/services/live/inbound.py index b3dc28e..b4a353f 100644 --- a/backend/services/live/inbound.py +++ b/backend/services/live/inbound.py @@ -1,16 +1,23 @@ """ -IMAP poller for live-campaign replies (P3 plumbing). +IMAP pollers for live campaigns: recipient replies (P3 plumbing) and the +report mailbox staff forward suspicious mail to. A recipient's reply arrives at the Reply-To address `+@`. The poller reads the mailbox, maps the token to a campaign, matches the recipient by the reply's From address, strips quoted history, and hands the fresh text to the conversation engine. +The report mailbox is a second inbox (e.g. lapor@acme.test). Every message in +it is a forwarded report: it is parsed for the original sender and subject and +handed to the report inbox, which credits the reporter when it is one of our +simulations — the behaviour report rate measures. + Pure parsing (token, body, sender) is separated from IMAP I/O so it can be tested without a server. """ import asyncio +import html import imaplib import logging import re @@ -18,6 +25,7 @@ from email.message import Message from email.utils import parseaddr +from services import report_inbox from services.ai_gateway import AIGateway from services.database import db from services.live import conversation @@ -34,6 +42,13 @@ re.compile(r"^\s*_{5,}\s*$"), ) +# Header lines inside an inline-forwarded message (Gmail, Outlook, Thunderbird; EN/ID). +_FWD_FROM_RE = re.compile(r"^\s*\*?(?:From|Dari)\s*:\*?\s*(.+)$", re.IGNORECASE | re.MULTILINE) +_FWD_SUBJECT_RE = re.compile( + r"^\s*\*?(?:Subject|Subjek|Perihal)\s*:\*?\s*(.+)$", re.IGNORECASE | re.MULTILINE +) +_TAG_RE = re.compile(r"<[^>]+>") + def parse_token(*header_values: str | None) -> str | None: """Extract the campaign token from any plus-addressed recipient header.""" @@ -82,6 +97,56 @@ def extract_body(msg: Message) -> str: return payload.decode(msg.get_content_charset() or "utf-8", "replace") +def _text_of(msg: Message) -> str: + """Plain text of a message, falling back to tag-stripped HTML.""" + html_text = "" + for part in msg.walk(): + ctype = part.get_content_type() + if ctype not in ("text/plain", "text/html") or "attachment" in str( + part.get("Content-Disposition", "") + ): + continue + payload = part.get_payload(decode=True) + if not payload: + continue + text = payload.decode(part.get_content_charset() or "utf-8", "replace") + if ctype == "text/plain": + return text + html_text = html_text or html.unescape(_TAG_RE.sub("\n", text)) + return html_text + + +def parse_report(raw: bytes) -> dict | None: + """Pull the reporter and the reported message out of one forwarded email. + + Handles "forward as attachment" (a message/rfc822 part) and inline + forwards, where the original From/Subject appear as lines in the body. + """ + msg = message_from_bytes(raw) + reporter = sender_address(msg.get("From")) + if not reporter: + return None + for part in msg.walk(): + if part.get_content_type() == "message/rfc822": + inner = part.get_payload(0) if part.is_multipart() else None + if isinstance(inner, Message): + return { + "reporter_email": reporter, + "subject": str(inner.get("Subject") or msg.get("Subject") or ""), + "from_address": sender_address(inner.get("From")) or "", + "body": _text_of(inner), + } + body = _text_of(msg) + fwd_from = _FWD_FROM_RE.search(body) + fwd_subject = _FWD_SUBJECT_RE.search(body) + return { + "reporter_email": reporter, + "subject": fwd_subject.group(1).strip() if fwd_subject else str(msg.get("Subject") or ""), + "from_address": sender_address(fwd_from.group(1)) if fwd_from else "", + "body": body, + } + + async def route_message(raw: bytes, gateway=None) -> dict: """Parse one raw RFC822 message and dispatch it to the conversation engine.""" msg = message_from_bytes(raw) @@ -125,7 +190,9 @@ async def route_message(raw: bytes, gateway=None) -> dict: # ── IMAP I/O ───────────────────────────────────────────────────────── -def _fetch_new(config: dict, last_uid: int, last_uidvalidity: int) -> tuple[list[bytes], int, int]: +def _fetch_new( + config: dict, user: str, password: str, last_uid: int, last_uidvalidity: int +) -> tuple[list[bytes], int, int]: """Blocking IMAP fetch of messages with UID greater than the watermark. Returns (raw messages, current UIDVALIDITY, highest UID fetched). Runs in a thread.""" host, _, port = config["imap_host"].partition(":") @@ -135,7 +202,7 @@ def _fetch_new(config: dict, last_uid: int, last_uidvalidity: int) -> tuple[list else imaplib.IMAP4(host, int(port or 143)) ) try: - conn.login(config["imap_user"], config.get("imap_password", "")) + conn.login(user, password) conn.select("INBOX") # Track by UID, not the \Seen flag: a human reading the mailbox (e.g. in # Thunderbird) marks replies seen, which an UNSEEN search would then skip. @@ -175,7 +242,12 @@ async def poll_once() -> dict: state = await db.live_state.find_one({"id": "imap_watermark"}, {"_id": 0}) or {} try: raws, uidvalidity, max_uid = await asyncio.to_thread( - _fetch_new, config, int(state.get("last_uid", 0)), int(state.get("uidvalidity", 0)) + _fetch_new, + config, + config["imap_user"], + config.get("imap_password", ""), + int(state.get("last_uid", 0)), + int(state.get("uidvalidity", 0)), ) except Exception as exc: logger.warning(f"IMAP poll failed: {exc}") @@ -203,14 +275,58 @@ async def poll_once() -> dict: return {"polled": len(raws), "outcomes": outcomes} +async def poll_reports_once() -> dict: + """Fetch forwarded reports from the report mailbox and credit the reporters.""" + config = await db.live_config.find_one({"id": "live_config"}, {"_id": 0}) + mailbox = ((config or {}).get("report_imap_user") or "").strip().lower() + if not config or not config.get("imap_host") or not mailbox: + return {"polled": 0, "reason": "report_mailbox_not_configured"} + + state = await db.live_state.find_one({"id": "report_imap_watermark"}, {"_id": 0}) or {} + try: + raws, uidvalidity, max_uid = await asyncio.to_thread( + _fetch_new, + config, + mailbox, + config.get("report_imap_password", ""), + int(state.get("last_uid", 0)), + int(state.get("uidvalidity", 0)), + ) + except Exception as exc: + logger.warning(f"report mailbox poll failed: {exc}") + return {"polled": 0, "error": str(exc)} + + outcomes: dict[str, int] = {} + for raw in raws: + try: + parsed = parse_report(raw) + if not parsed or parsed["reporter_email"] == mailbox: + status = "skipped" + else: + report = await report_inbox.ingest_report(None, **parsed) + status = "credited" if report["credited"] else report["verdict"] + except Exception as exc: + logger.warning(f"ingesting a forwarded report failed: {exc}") + status = "ingest_error" + outcomes[status] = outcomes.get(status, 0) + 1 + + await db.live_state.update_one( + {"id": "report_imap_watermark"}, + {"$set": {"last_uid": max_uid, "uidvalidity": uidvalidity}}, + upsert=True, + ) + return {"polled": len(raws), "outcomes": outcomes} + + async def run_poller(interval_seconds: int = 30) -> None: """Background loop, always running. poll_once is a no-op until IMAP is configured, so saving the config in Settings takes effect on the next tick without a restart.""" logger.info("Live reply poller started") while True: - try: - await poll_once() - except Exception as exc: - logger.warning(f"poller iteration error: {exc}") + for poll in (poll_once, poll_reports_once): + try: + await poll() + except Exception as exc: + logger.warning(f"poller iteration error: {exc}") await asyncio.sleep(interval_seconds) diff --git a/backend/services/readiness.py b/backend/services/readiness.py new file mode 100644 index 0000000..a4433e6 --- /dev/null +++ b/backend/services/readiness.py @@ -0,0 +1,187 @@ +""" +Readiness overview — the first screen a commander or trainer sees. + +One read-only aggregate that tells the Pretexta story on a single page: +measure (live report rate, lab discrimination) -> remediate (targeted +practice for the principle someone fell for) -> retest. Everything here is +cohort-level; no individual is named (G13), and live results never feed +gamification (G15). +""" + +from statistics import mean +from typing import Any + +from services import attack_surface +from services.database import db +from services.live import launch as launch_svc + +PRINCIPLES = ("reciprocity", "scarcity", "authority", "commitment", "liking", "social_proof") + +# How much each component contributes to the readiness index. Report rate leads: +# it is the defensive behaviour we are training for. Components without data are +# left out and the remaining weights renormalised, so an org that has only run +# lab exercises still gets a meaningful number. +WEIGHTS = {"report_rate": 0.4, "no_click": 0.2, "principles": 0.2, "triage": 0.2} + +# The trend uses this many most-recent launched campaigns. +TREND_CAMPAIGNS = 8 + + +async def _live(scope: dict) -> dict[str, Any]: + campaigns = ( + await db.live_campaigns.find( + {**scope, "launched_at": {"$ne": None}}, + {"_id": 0, "id": 1, "name": 1, "launched_at": 1}, + ) + .sort("launched_at", -1) + .to_list(TREND_CAMPAIGNS) + ) + campaigns.reverse() # oldest -> newest for the trend line + + trend, recipients, reported, clicked = [], 0, 0, 0 + for c in campaigns: + m = await launch_svc.report_rate(c["id"]) + trend.append( + { + "name": c["name"], + "launched_at": c["launched_at"], + "report_rate": m["report_rate"], + "click_rate": m["click_rate"], + "median_time_to_report_min": m["median_time_to_report_min"], + } + ) + recipients += m["recipients"] + reported += m["reported"] + clicked += m["clicked"] + + rates = [t["report_rate"] for t in trend if t["report_rate"] is not None] + latest = trend[-1] if trend else {} + return { + "campaigns": len(campaigns), + "recipients": recipients, + "reported": reported, + "clicked": clicked, + "report_rate": round(reported / recipients, 3) if recipients else None, + "click_rate": round(clicked / recipients, 3) if recipients else None, + # Change between the first and the latest campaign in the window. + "report_rate_delta": round(rates[-1] - rates[0], 3) if len(rates) >= 2 else None, + # Current state = the most recent campaign; the aggregate above is context. + "latest_report_rate": latest.get("report_rate"), + "latest_click_rate": latest.get("click_rate"), + "trend": trend, + } + + +async def _principles(user_ids: list[str]) -> dict[str, float | None]: + profiles = await db.risk_profiles.find( + {"user_id": {"$in": user_ids}}, {"_id": 0, "current_vector": 1} + ).to_list(5000) + out: dict[str, float | None] = {} + for p in PRINCIPLES: + values = [ + prof["current_vector"][p] + for prof in profiles + if isinstance(prof.get("current_vector"), dict) and p in prof["current_vector"] + ] + out[p] = round(mean(values), 1) if values else None + return out + + +async def _lab(user_ids: list[str]) -> dict[str, Any]: + sims = await db.simulations.find( + {"user_id": {"$in": user_ids}, "simulation_type": "triage", "status": "completed"}, + {"_id": 0, "score": 1, "events": 1}, + ).to_list(5000) + d_primes = [ + e["payload"]["d_prime"] + for s in sims + for e in s.get("events") or [] + if e.get("event_type") == "triage_graded" and "d_prime" in (e.get("payload") or {}) + ] + scores = [s["score"] for s in sims if s.get("score") is not None] + return { + "triage_runs": len(sims), + "avg_triage_score": round(mean(scores), 1) if scores else None, + "avg_d_prime": round(mean(d_primes), 2) if d_primes else None, + } + + +async def _loop(user_ids: list[str]) -> dict[str, Any]: + base = {"trainee_id": {"$in": user_ids}} + + async def count(extra: dict) -> int: + return await db.assignments.count_documents({**base, **extra}) + + loop = { + "remediation_assigned": await count({"source": "remediation"}), + "remediation_passed": await count({"source": "remediation", "status": "completed"}), + "retest_assigned": await count({"source": "retest"}), + "retest_passed": await count({"source": "retest", "status": "completed"}), + } + # Anonymous activity feed: what the loop did, never who it did it to. + recent = ( + await db.assignments.find( + {**base, "source": {"$in": ["remediation", "retest"]}}, + {"_id": 0, "source": 1, "principle": 1, "status": 1, "created_at": 1}, + ) + .sort("created_at", -1) + .to_list(8) + ) + loop["recent"] = recent + return loop + + +def _index(live: dict, principles: dict, lab: dict) -> dict[str, Any]: + known = [v for v in principles.values() if v is not None] + # The index describes readiness now, so live behaviour comes from the latest campaign. + report, click = live["latest_report_rate"], live["latest_click_rate"] + components = { + "report_rate": None if report is None else report * 100, + "no_click": None if click is None else (1 - click) * 100, + "principles": mean(known) if known else None, + "triage": lab["avg_triage_score"], + } + available = {k: v for k, v in components.items() if v is not None} + weight = sum(WEIGHTS[k] for k in available) + score = round(sum(WEIGHTS[k] * v for k, v in available.items()) / weight) if weight else None + return { + "score": score, + "components": {k: None if v is None else round(v) for k, v in components.items()}, + "weights": WEIGHTS, + } + + +async def overview(*, user_scope: dict, campaign_scope: dict, org_id: str) -> dict[str, Any]: + users = await db.users.find({**user_scope, "role": "user"}, {"_id": 0, "id": 1}).to_list(10000) + user_ids = [u["id"] for u in users] + + live = await _live(campaign_scope) + principles = await _principles(user_ids) + lab = await _lab(user_ids) + loop = await _loop(user_ids) + + known = {k: v for k, v in principles.items() if v is not None} + weakest = sorted(known, key=known.get)[:2] + + surface = None + graph = await attack_surface.build_graph(org_id, refresh=False) + if any(n.get("is_asset") for n in graph.get("nodes", [])): + analysis = await attack_surface.analyze(org_id) + top = (analysis.get("attack_paths") or [None])[0] + surface = { + "posture": analysis.get("posture"), + "counts": analysis.get("counts"), + # Asset and probability only — the entry person stays on the staff-only map. + "top_path": top and {"asset": top["asset_name"], "probability": top["probability"]}, + } + + return { + "index": _index(live, principles, lab), + "people": len(user_ids), + "live": live, + "principles": principles, + "weakest_principles": weakest, + "lab": lab, + "loop": loop, + "attack_surface": surface, + } diff --git a/backend/services/remediation.py b/backend/services/remediation.py index 0cde246..20c06a5 100644 --- a/backend/services/remediation.py +++ b/backend/services/remediation.py @@ -33,24 +33,47 @@ } -async def _pick_content(principle: str, exclude_ids: set[str]) -> dict | None: +# Tags that describe who an exercise is written for (language, setting). A +# remediation should feel like the exercise that was failed, so candidates that +# share more of these tags are preferred. +CONTEXT_TAGS = {"indonesia", "satuan", "military"} + + +async def _pick_content( + principle: str, exclude_ids: set[str], prefer_tags: set[str] | None = None +) -> dict | None: """Choose a lab exercise that trains `principle` and the trainee hasn't just done. Prefers an interactive scenario; falls back to a quiz on the same principle. + Within a type, prefers content sharing the most `prefer_tags`. """ + prefer_tags = prefer_tags or set() for coll, content_type in (("challenges", "challenge"), ("quizzes", "quiz")): - async for doc in db[coll].find( - {"cialdini_categories": principle}, {"_id": 0, "id": 1, "title": 1} - ): - if doc["id"] not in exclude_ids: - return { - "content_type": content_type, - "content_id": doc["id"], - "title": doc["title"], - } + candidates = [ + doc + async for doc in db[coll].find( + {"cialdini_categories": principle}, + {"_id": 0, "id": 1, "title": 1, "metadata.tags": 1}, + ) + if doc["id"] not in exclude_ids + ] + if candidates: + # max() keeps the first of equal scores, so the stored order breaks ties. + best = max( + candidates, + key=lambda d: len(prefer_tags & set((d.get("metadata") or {}).get("tags") or [])), + ) + return {"content_type": content_type, "content_id": best["id"], "title": best["title"]} return None +async def _context_tags(challenge_id: str | None) -> set[str]: + if not challenge_id: + return set() + doc = await db.challenges.find_one({"id": challenge_id}, {"_id": 0, "metadata.tags": 1}) + return CONTEXT_TAGS & set(((doc or {}).get("metadata") or {}).get("tags") or []) + + async def _recent_content_ids(user_id: str) -> set[str]: """Content the user already has active, or passed recently — don't re-assign it.""" ids: set[str] = set() @@ -69,6 +92,10 @@ async def assign_remediation( source: str = "remediation", retest_of: str | None = None, due_days: int = REMEDIATION_DUE_DAYS, + reason_key: str | None = None, + reason_params: dict | None = None, + prefer_tags: set[str] | None = None, + exclude_content: str | None = None, ) -> str | None: """Create a remediation assignment for `principle`. Idempotent per principle: skips if the user already has an active remediation for it.""" @@ -86,7 +113,10 @@ async def assign_remediation( if existing: return None - content = await _pick_content(principle, await _recent_content_ids(user_id)) + exclude = await _recent_content_ids(user_id) + if exclude_content: + exclude.add(exclude_content) # never "remediate" with the exercise just failed + content = await _pick_content(principle, exclude, prefer_tags) if not content: logger.info(f"No remediation content for principle '{principle}'") return None @@ -96,9 +126,12 @@ async def assign_remediation( trainee_id=user_id, content_type=content["content_type"], content_id=content["content_id"], - title=f"Remediation: {content['title']}", + # The UI badges the source (remediation / retest), so the title stays the exercise's own. + title=content["title"], instructions=reason, reason=reason, + reason_key=reason_key, + reason_params={"principle": principle, **(reason_params or {})}, principle=principle, source=source, retest_of=retest_of, @@ -121,12 +154,17 @@ async def on_lab_result(sim: dict, user_id: str) -> None: return principles = (sim.get("challenge_data") or {}).get("cialdini_categories") or [] title = sim.get("title", "an exercise") + prefer = await _context_tags(sim.get("challenge_id")) for principle in principles[:2]: await assign_remediation( user_id, principle, f"You scored {round(score)}% on '{title}', which tested {_label(principle)}. " f"This exercise gives you focused practice.", + reason_key="lab_fail", + reason_params={"score": round(score), "title": title}, + prefer_tags=prefer, + exclude_content=sim.get("challenge_id"), ) @@ -150,6 +188,8 @@ async def on_live_fail(campaign: dict, email: str, outcome: str) -> None: principle, f"In a live phishing simulation you {verb} a message that used {_label(principle)}. " f"This exercise helps you recognise it next time.", + reason_key=f"live_{outcome}", + prefer_tags={"indonesia"} if campaign.get("language") == "id" else set(), ) @@ -165,4 +205,8 @@ async def on_assignment_passed(assignment: dict, user_id: str) -> None: source="retest", retest_of=assignment["id"], due_days=RETEST_AFTER_DAYS, + reason_key="retest", + # A variant: same context as the practice, but not the same exercise. + prefer_tags=await _context_tags(assignment.get("content_id")), + exclude_content=assignment.get("content_id"), ) diff --git a/backend/services/report_inbox.py b/backend/services/report_inbox.py index 14d79ee..3e11360 100644 --- a/backend/services/report_inbox.py +++ b/backend/services/report_inbox.py @@ -39,28 +39,50 @@ def _clean_subject(subject: str) -> str: async def classify( - org_scope: dict, subject: str, from_address: str, body: str + org_scope: dict, subject: str, from_address: str, body: str, reporter_email: str = "" ) -> tuple[str, str | None]: """Decide whether a reported message is one of our simulations. Matches by the campaign's sender address (in the From or anywhere in the - forwarded body) or by an exact subject match. Returns (verdict, campaign_id). + forwarded body) or by an exact subject match. Campaigns often reuse a + sender, so among matches the newest one the reporter actually received + wins (preferring one they have not been credited for yet). + Returns (verdict, campaign_id). """ clean_subj = _clean_subject(subject) from_addr = (from_address or "").strip().lower() body_l = (body or "").lower() + matches: list[str] = [] async for c in db.live_campaigns.find( {**org_scope, "status": {"$in": list(_SENT_STATUSES)}}, {"_id": 0, "id": 1, "sender_email": 1, "subject": 1}, - ): + ).sort("created_at", -1): sender = (c.get("sender_email") or "").strip().lower() - if sender and (sender == from_addr or sender in body_l): - return "simulation", c["id"] csubj = (c.get("subject") or "").strip().lower() - if csubj and clean_subj and csubj == clean_subj: - return "simulation", c["id"] - return "needs_review", None + if (sender and (sender == from_addr or sender in body_l)) or ( + csubj and clean_subj and csubj == clean_subj + ): + matches.append(c["id"]) + if not matches: + return "needs_review", None + + email = (reporter_email or "").strip().lower() + if email: + received = { + r["campaign_id"]: r.get("reported", False) + async for r in db.live_recipients.find( + {"campaign_id": {"$in": matches}, "email": email}, + {"_id": 0, "campaign_id": 1, "reported": 1}, + ) + } + for cid in matches: + if cid in received and not received[cid]: + return "simulation", cid + for cid in matches: + if cid in received: + return "simulation", cid + return "simulation", matches[0] async def credit_reporter(campaign_id: str, reporter_email: str) -> bool: @@ -103,13 +125,25 @@ async def ingest_report( """Store one reported message and, if it is ours, credit the reporter.""" org_scope = {} if org_id is None else {"organization_id": org_id} redacted, _ = scrub(body or "") - verdict, campaign_id = await classify(org_scope, subject, from_address, body) + verdict, campaign_id = await classify(org_scope, subject, from_address, body, reporter_email) credited = False if verdict == "simulation" and campaign_id: credited = await credit_reporter(campaign_id, reporter_email) - user = await db.users.find_one({"email": reporter_email.strip().lower()}, {"_id": 0, "id": 1}) + user = await db.users.find_one( + {"email": reporter_email.strip().lower()}, {"_id": 0, "id": 1, "organization_id": 1} + ) + if org_id is None: + # Arrived through the report mailbox, not a staff member's scope: file it + # under the matched campaign's organization, else the reporter's. + if campaign_id: + campaign = await db.live_campaigns.find_one( + {"id": campaign_id}, {"_id": 0, "organization_id": 1} + ) + org_id = (campaign or {}).get("organization_id") + if org_id is None: + org_id = (user or {}).get("organization_id") report = PhishReport( organization_id=org_id, reporter_email=reporter_email.strip().lower(), diff --git a/backend/tests/test_demo.py b/backend/tests/test_demo.py new file mode 100644 index 0000000..1ddeb54 --- /dev/null +++ b/backend/tests/test_demo.py @@ -0,0 +1,69 @@ +"""Demo data: tells the loop story, is admin-only, and clears without touching real data.""" + +from services.database import db +from tests.conftest import make_user + + +async def test_admin_only(api): + _, h = await make_user("trainer") + assert (await api.post("/api/demo", headers=h)).status_code == 403 + assert (await api.delete("/api/demo", headers=h)).status_code == 403 + + +async def test_demo_tells_the_loop_story(api): + _, h = await make_user("admin") + assert (await api.post("/api/demo", headers=h)).status_code == 200 + + body = (await api.get("/api/readiness", headers=h)).json() + trend = [round(c["report_rate"], 2) for c in body["live"]["trend"]] + assert trend == [0.25, 0.5, 0.75] + assert body["live"]["clicked"] == 9 + loop = body["loop"] + assert loop["remediation_assigned"] == 9 and loop["remediation_passed"] == 8 + assert loop["retest_passed"] == 5 + assert body["weakest_principles"][0] == "authority" + assert body["lab"]["triage_runs"] == 24 + assert body["attack_surface"] is not None + assert body["index"]["score"] is not None + + # Reports arrive faster as the loop works. + medians = [c["median_time_to_report_min"] for c in body["live"]["trend"]] + assert medians[0] > medians[1] > medians[2] + + +async def test_reload_is_idempotent(api): + _, h = await make_user("admin") + await api.post("/api/demo", headers=h) + await api.post("/api/demo", headers=h) + assert await db.users.count_documents({"demo": True}) == 12 + assert await db.live_campaigns.count_documents({"demo": True}) == 3 + + +async def test_clear_leaves_real_data(api): + admin, h = await make_user("admin") + real, _ = await make_user("user") + await db.org_nodes.insert_one({"id": "mine", "org_id": "org-real", "name": "Real node"}) + + await api.post("/api/demo", headers=h) + status = (await api.get("/api/demo", headers=h)).json() + assert status["loaded"] is True + + await api.delete("/api/demo", headers=h) + assert (await api.get("/api/demo", headers=h)).json()["loaded"] is False + for name in ("users", "live_campaigns", "live_recipients", "assignments", "simulations"): + assert await db[name].count_documents({"demo": True}) == 0, name + assert await db.users.find_one({"id": real.id}) + assert await db.users.find_one({"id": admin.id}) + assert await db.org_nodes.find_one({"id": "mine"}) + # The map the demo seeded into the admin's empty workspace is gone too. + assert not await db.org_nodes.find_one({"org_id": f"org-{admin.id}"}) + + +async def test_existing_map_is_not_overwritten(api): + admin, h = await make_user("admin") + workspace = f"org-{admin.id}" + await db.org_nodes.insert_one({"id": "kept", "org_id": workspace, "name": "Admin's own map"}) + result = (await api.post("/api/demo", headers=h)).json() + assert workspace not in result["map_orgs"] + await api.delete("/api/demo", headers=h) + assert await db.org_nodes.find_one({"id": "kept"}) 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/backend/tests/test_readiness.py b/backend/tests/test_readiness.py new file mode 100644 index 0000000..59407ce --- /dev/null +++ b/backend/tests/test_readiness.py @@ -0,0 +1,129 @@ +"""Readiness overview: cohort aggregate, org scoping, no individual names.""" + +from datetime import UTC, datetime + +from services.database import db +from tests.conftest import make_user + + +async def _campaign(org, cid, recipients, launched="2026-10-01T00:00:00+00:00"): + await db.live_campaigns.insert_one( + {"id": cid, "name": cid, "organization_id": org, "launched_at": launched} + ) + for i, (state, reported) in enumerate(recipients): + await db.live_recipients.insert_one( + { + "campaign_id": cid, + "email": f"{cid}-{i}@acme.test", + "state": state, + "reported": reported, + } + ) + + +async def test_requires_staff(api): + _, h = await make_user("user") + assert (await api.get("/api/readiness", headers=h)).status_code == 403 + + +async def test_empty_org_has_no_index(api): + _, h = await make_user("trainer", organization_id="org-a") + body = (await api.get("/api/readiness", headers=h)).json() + assert body["index"]["score"] is None + assert body["live"]["campaigns"] == 0 + assert body["attack_surface"] is None + + +async def test_index_and_loop(api): + trainee, _ = await make_user("user", organization_id="org-a") + _, h = await make_user("trainer", organization_id="org-a") + + # Two campaigns: report rate goes 25% -> 75%; one click in each. + await _campaign( + "org-a", + "c1", + [("clicked", False), ("sent", True), ("sent", False), ("sent", False)], + launched="2026-09-01T00:00:00+00:00", + ) + await _campaign( + "org-a", + "c2", + [("clicked", False), ("sent", True), ("sent", True), ("sent", True)], + launched="2026-10-01T00:00:00+00:00", + ) + vector = dict.fromkeys( + ("reciprocity", "scarcity", "commitment", "liking", "social_proof"), 80.0 + ) + await db.risk_profiles.insert_one( + {"user_id": trainee.id, "current_vector": {**vector, "authority": 20.0}} + ) + await db.simulations.insert_one( + { + "user_id": trainee.id, + "simulation_type": "triage", + "status": "completed", + "score": 90, + "events": [{"event_type": "triage_graded", "payload": {"d_prime": 2.0}}], + } + ) + now = datetime.now(UTC).isoformat() + for source, status in ( + ("remediation", "completed"), + ("remediation", "active"), + ("retest", "completed"), + ): + await db.assignments.insert_one( + { + "trainee_id": trainee.id, + "source": source, + "status": status, + "principle": "authority", + "reason": "secret detail about the person", + "created_at": now, + } + ) + + body = (await api.get("/api/readiness", headers=h)).json() + + live = body["live"] + assert live["report_rate"] == 0.5 and live["click_rate"] == 0.25 + assert live["report_rate_delta"] == 0.5 + assert (live["latest_report_rate"], live["latest_click_rate"]) == (0.75, 0.25) + assert [t["name"] for t in live["trend"]] == ["c1", "c2"] + + assert body["weakest_principles"][0] == "authority" + assert body["lab"] == {"triage_runs": 1, "avg_triage_score": 90.0, "avg_d_prime": 2.0} + + comps = body["index"]["components"] + # Live components come from the latest campaign (75% reported, 25% clicked). + assert comps == {"report_rate": 75, "no_click": 75, "principles": 70, "triage": 90} + # 0.4*75 + 0.2*75 + 0.2*70 + 0.2*90 = 77 + assert body["index"]["score"] == 77 + + loop = body["loop"] + assert (loop["remediation_assigned"], loop["remediation_passed"]) == (2, 1) + assert (loop["retest_assigned"], loop["retest_passed"]) == (1, 1) + # The feed is anonymous: no person, no free-text reason. + for item in loop["recent"]: + assert set(item) <= {"source", "principle", "status", "created_at"} + + +async def test_trainer_sees_only_own_org(api): + await _campaign("org-b", "other", [("clicked", False), ("sent", False)]) + _, h = await make_user("trainer", organization_id="org-a") + body = (await api.get("/api/readiness", headers=h)).json() + assert body["live"]["campaigns"] == 0 + + _, admin_h = await make_user("admin") + assert (await api.get("/api/readiness", headers=admin_h)).json()["live"]["campaigns"] == 1 + + +async def test_weights_renormalise_without_live_data(api): + trainee, _ = await make_user("user", organization_id="org-a") + _, h = await make_user("trainer", organization_id="org-a") + await db.simulations.insert_one( + {"user_id": trainee.id, "simulation_type": "triage", "status": "completed", "score": 60} + ) + index = (await api.get("/api/readiness", headers=h)).json()["index"] + assert index["components"]["report_rate"] is None + assert index["score"] == 60 # triage is the only component with data diff --git a/backend/tests/test_remediation.py b/backend/tests/test_remediation.py index ce1278d..7acbcab 100644 --- a/backend/tests/test_remediation.py +++ b/backend/tests/test_remediation.py @@ -129,3 +129,76 @@ async def test_retest_not_scheduled_for_manual_assignment(api): {"id": "m1", "source": "manual", "principle": "authority"}, user.id ) assert await db.assignments.count_documents({"source": "retest"}) == 0 + + +async def test_reason_is_translatable(api): + """System assignments carry a reason key + params so the UI can localise them.""" + from services import remediation + + user, _ = await make_user("user") + await db.challenges.insert_one( + { + "id": "c-auth", + "title": "Authority drill", + "cialdini_categories": ["authority"], + "nodes": [], + } + ) + await remediation.on_lab_result( + { + "score": 40, + "title": "Boss on WhatsApp", + "challenge_data": {"cialdini_categories": ["authority"]}, + }, + user.id, + ) + a = await db.assignments.find_one({"trainee_id": user.id}) + assert a["reason_key"] == "lab_fail" + assert a["reason_params"] == { + "principle": "authority", + "score": 40, + "title": "Boss on WhatsApp", + } + + +async def test_remediation_prefers_same_context_and_skips_the_failed_exercise(api): + from services import remediation + + user, _ = await make_user("user") + await db.challenges.insert_many( + [ + { + "id": "en-auth", + "title": "Generic CEO fraud", + "cialdini_categories": ["authority"], + "metadata": {"tags": ["bec"]}, + "nodes": [], + }, + { + "id": "unit-failed", + "title": "Boss on WhatsApp", + "cialdini_categories": ["authority"], + "metadata": {"tags": ["whatsapp", "satuan", "military", "indonesia"]}, + "nodes": [], + }, + { + "id": "unit-other", + "title": "Nota dinas APK", + "cialdini_categories": ["authority"], + "metadata": {"tags": ["email", "satuan", "military", "indonesia"]}, + "nodes": [], + }, + ] + ) + await remediation.on_lab_result( + { + "score": 40, + "title": "Boss on WhatsApp", + "challenge_id": "unit-failed", + "challenge_data": {"cialdini_categories": ["authority"]}, + }, + user.id, + ) + a = await db.assignments.find_one({"trainee_id": user.id}) + assert a["content_id"] == "unit-other" # same unit context, not the one just failed + assert a["title"] == "Nota dinas APK" # no English "Remediation:" prefix diff --git a/backend/tests/test_report_mailbox.py b/backend/tests/test_report_mailbox.py new file mode 100644 index 0000000..704ec38 --- /dev/null +++ b/backend/tests/test_report_mailbox.py @@ -0,0 +1,222 @@ +"""Report mailbox: forwarded reports are parsed, classified and credited.""" + +from email.message import EmailMessage + +from services import report_inbox +from services.database import db +from services.live import inbound +from tests.conftest import make_user + +LURE_FROM = "IT Helpdesk " +LURE_SUBJECT = "Action required: verify your mailbox" + + +async def _campaign_with_recipient(org="acme", email="davan@acme.test"): + await db.live_campaigns.insert_one( + { + "id": "camp-1", + "organization_id": org, + "status": "running", + "sender_email": "helpdesk@acme.test", + "subject": LURE_SUBJECT, + } + ) + await db.live_recipients.insert_one( + {"campaign_id": "camp-1", "email": email, "reported": False} + ) + + +def _inline_forward(reporter="Davan ") -> bytes: + msg = EmailMessage() + msg["From"] = reporter + msg["To"] = "lapor@acme.test" + msg["Subject"] = f"Fwd: {LURE_SUBJECT}" + msg.set_content( + "Ini mencurigakan.\n\n" + "-------- Forwarded Message --------\n" + f"Subject: {LURE_SUBJECT}\n" + f"From: {LURE_FROM}\n" + "To: davan@acme.test\n\n" + "Please verify your mailbox here.\n" + ) + return msg.as_bytes() + + +def _attached_forward() -> bytes: + original = EmailMessage() + original["From"] = LURE_FROM + original["To"] = "davan@acme.test" + original["Subject"] = LURE_SUBJECT + original.set_content("Please verify your mailbox here.") + msg = EmailMessage() + msg["From"] = "davan@acme.test" + msg["To"] = "lapor@acme.test" + msg["Subject"] = "Laporan phishing" + msg.set_content("Lihat lampiran.") + msg.add_attachment(original) + return msg.as_bytes() + + +# ---------- parsing ---------- + + +def test_parse_inline_forward_finds_original_sender_and_subject(): + parsed = inbound.parse_report(_inline_forward()) + assert parsed["reporter_email"] == "davan@acme.test" + assert parsed["from_address"] == "helpdesk@acme.test" + assert parsed["subject"] == LURE_SUBJECT + + +def test_parse_forward_as_attachment_reads_inner_message(): + parsed = inbound.parse_report(_attached_forward()) + assert parsed["reporter_email"] == "davan@acme.test" + assert parsed["from_address"] == "helpdesk@acme.test" + assert parsed["subject"] == LURE_SUBJECT + + +def test_parse_html_only_forward(): + msg = EmailMessage() + msg["From"] = "davan@acme.test" + msg["Subject"] = f"Fwd: {LURE_SUBJECT}" + msg.set_content( + "
Cek ini

From: IT Helpdesk <helpdesk@acme.test>

verify

", + subtype="html", + ) + parsed = inbound.parse_report(msg.as_bytes()) + assert parsed["from_address"] == "helpdesk@acme.test" + + +# ---------- ingest through the mailbox path ---------- + + +async def test_mailbox_report_credits_reporter_under_campaign_org(api): + await _campaign_with_recipient() + report = await report_inbox.ingest_report(None, **inbound.parse_report(_inline_forward())) + assert report["verdict"] == "simulation" and report["credited"] is True + assert report["organization_id"] == "acme" + rec = await db.live_recipients.find_one({"campaign_id": "camp-1"}) + assert rec["reported"] is True + + +async def _configure(**over): + await db.live_config.insert_one( + { + "id": "live_config", + "imap_host": "greenmail:3143", + "imap_user": "sim-reply@acme.test", + "report_imap_user": "lapor@acme.test", + **over, + } + ) + + +async def test_poll_reports_without_mailbox(api): + assert (await inbound.poll_reports_once())["reason"] == "report_mailbox_not_configured" + await _configure(report_imap_user="") + assert (await inbound.poll_reports_once())["reason"] == "report_mailbox_not_configured" + + +async def test_poll_reports_ingests_and_advances_watermark(api, monkeypatch): + await _campaign_with_recipient() + await _configure() + calls = [] + + def fake_fetch(config, user, password, last_uid, uidvalidity): + calls.append(user) + if last_uid: + return [], 7, last_uid + return [_inline_forward(), _inline_forward("Lapor ")], 7, 2 + + monkeypatch.setattr(inbound, "_fetch_new", fake_fetch) + result = await inbound.poll_reports_once() + assert calls == ["lapor@acme.test"] + # The mailbox's own message (e.g. an auto-reply) is not a report. + assert result["outcomes"] == {"credited": 1, "skipped": 1} + assert (await db.live_state.find_one({"id": "report_imap_watermark"}))["last_uid"] == 2 + + again = await inbound.poll_reports_once() + assert again["polled"] == 0 + assert await db.phish_reports.count_documents({}) == 1 + + +# ---------- API ---------- + + +async def test_mailbox_endpoint_shows_address_to_staff(api): + _, trainer = await make_user("trainer") + assert (await api.get("/api/report-inbox/mailbox", headers=trainer)).json() == { + "address": None, + "configured": False, + } + await _configure() + got = (await api.get("/api/report-inbox/mailbox", headers=trainer)).json() + assert got == {"address": "lapor@acme.test", "configured": True} + _, user = await make_user("user") + assert (await api.get("/api/report-inbox/mailbox", headers=user)).status_code == 403 + assert (await api.post("/api/report-inbox/poll", headers=user)).status_code == 403 + + +async def test_report_mailbox_must_differ_from_reply_mailbox(api): + _, admin = await make_user("admin") + base = { + "gophish_base_url": "https://gp:3333", + "gophish_api_key": "k", + "phish_url": "http://phish", + "smtp_host": "mail:25", + "imap_host": "greenmail:3143", + "imap_user": "sim-reply@acme.test", + } + r = await api.put( + "/api/live/config", headers=admin, json={**base, "report_imap_user": "SIM-REPLY@acme.test"} + ) + assert r.status_code == 400 + r = await api.put( + "/api/live/config", + headers=admin, + json={**base, "report_imap_user": "lapor@acme.test", "report_imap_password": "pw"}, + ) + assert r.status_code == 200 + got = (await api.get("/api/live/config", headers=admin)).json() + assert got["report_imap_user"] == "lapor@acme.test" and got["report_imap_password"] == "***" + + +async def test_reused_sender_credits_the_campaign_the_reporter_received(api): + # An older campaign with the same sender and subject that the reporter + # already reported, and a newer running one they have not. + await db.live_campaigns.insert_many( + [ + { + "id": "old", + "organization_id": "acme", + "status": "killed", + "sender_email": "helpdesk@acme.test", + "subject": LURE_SUBJECT, + "created_at": "2026-10-26T10:00:00+00:00", + }, + { + "id": "new", + "organization_id": "acme", + "status": "running", + "sender_email": "helpdesk@acme.test", + "subject": LURE_SUBJECT, + "created_at": "2026-10-30T10:00:00+00:00", + }, + { + "id": "other", + "organization_id": "acme", + "status": "running", + "sender_email": "helpdesk@acme.test", + "subject": LURE_SUBJECT, + "created_at": "2026-10-31T10:00:00+00:00", + }, + ] + ) + await db.live_recipients.insert_many( + [ + {"campaign_id": "old", "email": "davan@acme.test", "reported": True}, + {"campaign_id": "new", "email": "davan@acme.test", "reported": False}, + {"campaign_id": "other", "email": "someone@acme.test", "reported": False}, + ] + ) + report = await report_inbox.ingest_report(None, **inbound.parse_report(_inline_forward())) + assert report["campaign_id"] == "new" and report["credited"] is True diff --git a/backend/tests/test_triage.py b/backend/tests/test_triage.py index da8474f..8f43d25 100644 --- a/backend/tests/test_triage.py +++ b/backend/tests/test_triage.py @@ -149,3 +149,50 @@ async def test_seed_fills_missing_set_without_touching_edited_one(api): await triage.seed_default_set() assert (await db.triage_sets.find_one({"id": "triage-inbox-01"}))["title"] == "Edited" assert await db.triage_sets.find_one({"id": "triage-inbox-01-id"}) + + +def _set_payload(**over): + items = [ + { + "channel": "whatsapp", + "from": "Atasan (nomor baru)", + "subject": "Tolong transfer dulu", + "body": "..", + "is_pretext": True, + "principle": "authority", + "explanation": "nomor baru", + }, + {"from": "hr@acme.com", "subject": "Timesheet", "body": "..", "is_pretext": False}, + {"from": "cal@acme.com", "subject": "Invite", "body": "..", "is_pretext": False}, + { + "channel": "whatsapp", + "from": "Grup Kantor", + "subject": "Rapat jam 9", + "body": "..", + "is_pretext": False, + }, + ] + return {"id": "pack-1", "title": "Pack", "language": "id", "items": items, **over} + + +async def test_trainer_can_add_a_set_with_chat_items(api): + _, h = await make_user("trainer") + r = await api.post("/api/triage", json=_set_payload(), headers=h) + assert r.status_code == 201, r.text + taker, th = await make_user("user") + got = (await api.get("/api/triage/pack-1", headers=th)).json() + assert got["items"][0]["channel"] == "whatsapp" + assert "is_pretext" not in got["items"][0] # labels stay on the server + assert (await api.post("/api/triage", json=_set_payload(), headers=h)).status_code == 409 + + +async def test_set_authoring_rules(api): + _, uh = await make_user("user") + assert (await api.post("/api/triage", json=_set_payload(), headers=uh)).status_code == 403 + _, h = await make_user("trainer") + only_legit = _set_payload() + only_legit["items"] = [dict(i, is_pretext=False) for i in only_legit["items"]] + assert (await api.post("/api/triage", json=only_legit, headers=h)).status_code == 422 + unexplained = _set_payload() + unexplained["items"][0].pop("explanation") + assert (await api.post("/api/triage", json=unexplained, headers=h)).status_code == 422 diff --git a/docs/api.md b/docs/api.md index 31c8da6..bb6932d 100644 --- a/docs/api.md +++ b/docs/api.md @@ -43,6 +43,14 @@ Scenarios — `/api/scenarios`: | POST | `/resume` | U | Resume/begin a session by `simulation_id` | | GET | `/session/{id}` | U | Current session state | +Inbox Triage — `/api/triage`: + +| Method | Path | Role | Notes | +|--------|------|------|-------| +| GET | `` / `/{id}` | U | List sets; `/{id}` strips labels and explanations | +| POST | `/{id}/submit` | U | Server grades with signal detection (d′) | +| POST | `` | T | Add a set (e.g. a unit-specific content pack). Items may be `email_inbox`, `whatsapp` or `sms`; a set needs both pretexts and legitimate messages, and every pretext needs a principle + explanation | + Simulations, debrief, reports — `/api/simulations`, `/api/debrief`, `/api/reports`: read your own (staff may review trainees). Clients **cannot** create graded simulation types @@ -100,7 +108,9 @@ Audit — `/api/audit` (admin): `GET /` (entries), `GET /verify` (chain integrit analysis, node/edge CRUD. - `/api/report-inbox` — Report-Phish mailbox (staff only): list/stats/leaderboard, `POST /ingest` (classify a reported message; credits the reporter if it's ours), - `PATCH /{id}` (triage). `DELETE /api/live/campaigns/{id}` removes a finished campaign. + `PATCH /{id}` (triage), `GET /mailbox` (the configured report address), + `POST /poll` (fetch forwarded reports from the report mailbox now). `POST + /api/live/poll` (admin) polls both the reply and the report mailbox. `DELETE /api/live/campaigns/{id}` removes a finished campaign. - `GET /api/live/analytics` — historical behaviour across launched campaigns: report-rate trend, per-campaign comparison, and repeat offenders. - `/api/live/schedules` — recurring campaigns (staff): CRUD + `POST /{id}/run-now`. @@ -109,6 +119,13 @@ Audit — `/api/audit` (admin): `GET /` (entries), `GET /verify` (chain integrit - `GET /api/attack-surface/targets` — emailable people on the map, ranked most-susceptible first, to seed a campaign aimed at the human weak points. +- `GET /api/readiness` — staff home screen: readiness index (components and + weights exposed), report-rate trend, remediation-loop counts, principle + resistance, lab d′ and attack-surface posture. Cohort-level only (G13). +- `/api/demo` (admin) — `GET` status, `POST` load the fictional demo + organization (`demo.test`), `DELETE` remove it; only records tagged as demo + are touched. + ## Health `GET /api/health` — liveness + DB connectivity (used by Docker healthchecks). diff --git a/docs/live-campaign-walkthrough.md b/docs/live-campaign-walkthrough.md index 948e787..6174c75 100644 --- a/docs/live-campaign-walkthrough.md +++ b/docs/live-campaign-walkthrough.md @@ -98,6 +98,7 @@ fill the **Live Campaigns (GoPhish)** panel: | IMAP host | `greenmail:3143` (GreenMail plain IMAP; use `:3993` for SSL) | | IMAP SSL | off for GreenMail's 3143 | | IMAP user / password | `sim-reply@acme.test` / `sim-reply@acme.test` (GreenMail accepts anything) | +| Report mailbox / password | `lapor@acme.test` / `lapor@acme.test` — where staff forward suspicious mail; must differ from the IMAP user | **Configure IMAP if you want replies to be ingested.** Pretexta polls this mailbox itself — reading the mail in Thunderbird or any other client does **not** @@ -306,6 +307,9 @@ To run against a real organization instead of the sandbox: | Can't approve a campaign | You are the creator. Approve from a different admin account (step 5). | | Pre-flight error about recipients | A recipient is outside a verified domain, or on the exclusion list. | | 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/docs/user-guide.md b/docs/user-guide.md index bfae29c..a88c1b7 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -218,9 +218,16 @@ mailbox, the server classifies it and the page shows each report with a verdict: - **Perlu ditinjau** — not one of yours; click **Tandai ancaman** or **Tandai aman** to triage it. -A **top-reporters** leaderboard surfaces who defends actively. In production a mail -rule or poller feeds the inbox; the **Catat laporan** form lets you record one by -hand for a demo. (Integration endpoint: `POST /report-inbox/ingest`.) +A **top-reporters** leaderboard surfaces who defends actively. + +**Report mailbox.** Set **Report mailbox** in Settings → Live Campaigns (for example +`lapor@acme.test`, on the same IMAP server as replies). Pretexta polls it every 30 +seconds: staff simply **forward** the suspicious email there — inline or as an +attachment — and the original sender and subject are read from the forward. The +**Mailbox pelaporan** card shows the address and a **Periksa sekarang** button. The +report mailbox must be different from the reply mailbox, or replies would count as +reports. Reports that arrive another way (phone, chat, ticket) can be recorded with +the **Catat laporan** form. (Integration endpoint: `POST /report-inbox/ingest`.) ### Analitik Live diff --git a/frontend/src/App.js b/frontend/src/App.js index a40d3ee..7dfe1e4 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -31,6 +31,7 @@ const MyAssignmentsPage = lazy(() => import('./pages/MyAssignmentsPage')); // ── Trainer/Admin pages ──────────────────────────────────────────────────── const InstructorDashboard = lazy(() => import('./pages/InstructorDashboard')); +const ReadinessPage = lazy(() => import('./pages/ReadinessPage')); const TraineesPage = lazy(() => import('./pages/TraineesPage')); const AssignmentsPage = lazy(() => import('./pages/AssignmentsPage')); const InstructorReportsPage = lazy(() => import('./pages/InstructorReportsPage')); @@ -59,7 +60,8 @@ function PageLoader() { function AdminRoutes() { return ( - } /> + } /> + } /> {/* User management — admin only */} } /> {/* Trainer management */} @@ -100,7 +102,8 @@ function AdminRoutes() { function TrainerRoutes() { return ( - } /> + } /> + } /> {/* Trainer tools */} } /> } /> diff --git a/frontend/src/components/Layout.js b/frontend/src/components/Layout.js index 5cd8ba4..773dd79 100644 --- a/frontend/src/components/Layout.js +++ b/frontend/src/components/Layout.js @@ -8,7 +8,7 @@ import { Terminal, LayoutDashboard, FileCode, ListChecks, Activity, Settings, LogOut, BookOpen, Trophy, BarChart3, Flag, Pencil, Sun, Moon, User, Globe, Menu, X, Users, ClipboardList, ShieldAlert, Radio, Target, Inbox, MessageSquare, PenLine, - Shield, UserCog, History, BookMarked, Share2, + Shield, ShieldCheck, UserCog, History, BookMarked, Share2, } from 'lucide-react'; const LANGUAGES = [ @@ -23,13 +23,14 @@ function buildAdminNav(t) { { section: 'COMMAND', items: [ - { name: 'Command Center', path: '/', icon: Radio }, - { name: 'All Users', path: '/users', icon: UserCog }, + { name: t('nav.readiness'), path: '/', icon: ShieldCheck }, + { name: t('instructor.cc.title'), path: '/command', icon: Radio }, + { name: t('nav.all_users'), path: '/users', icon: UserCog }, { name: t('instructor.groups'), path: '/trainer/groups', icon: Users }, { name: t('instructor.assignments'), path: '/trainer/assignments', icon: ClipboardList }, { name: t('instructor.reports'), path: '/trainer/reports', icon: ShieldAlert }, - { name: 'Riwayat User', path: '/trainer/user-history', icon: History }, - { name: 'Peta Serangan', path: '/attack-surface', icon: Share2 }, + { name: t('nav.user_history'), path: '/trainer/user-history', icon: History }, + { name: t('nav.attack_surface'), path: '/attack-surface', icon: Share2 }, ], }, { @@ -47,15 +48,15 @@ function buildAdminNav(t) { items: [ { name: t('nav.live_campaigns'), path: '/live', icon: Radio }, { name: t('nav.reply_review'), path: '/live/review', icon: MessageSquare }, - { name: 'Laporan Phishing', path: '/live/reports', icon: ShieldAlert }, - { name: 'Analitik Live', path: '/live/analytics', icon: BarChart3 }, + { name: t('nav.report_inbox'), path: '/live/reports', icon: ShieldAlert }, + { name: t('nav.live_analytics'), path: '/live/analytics', icon: BarChart3 }, ], }, { section: 'SYSTEM', items: [ { name: t('nav.analytics'), path: '/analytics', icon: BarChart3 }, - { name: 'Glosarium', path: '/glossary', icon: BookMarked }, + { name: t('nav.glossary'), path: '/glossary', icon: BookMarked }, { name: t('nav.settings'), path: '/settings', icon: Settings }, ], }, @@ -67,13 +68,14 @@ function buildTrainerNav(t) { { section: 'TRAINING MGMT', items: [ - { name: 'Command Center', path: '/', icon: Radio }, - { name: 'Users', path: '/users', icon: UserCog }, + { name: t('nav.readiness'), path: '/', icon: ShieldCheck }, + { name: t('instructor.cc.title'), path: '/command', icon: Radio }, + { name: t('nav.users'), path: '/users', icon: UserCog }, { name: t('instructor.groups'), path: '/trainer/groups', icon: Users }, { name: t('instructor.assignments'), path: '/trainer/assignments', icon: ClipboardList }, { name: t('instructor.reports'), path: '/trainer/reports', icon: ShieldAlert }, - { name: 'Riwayat User', path: '/trainer/user-history', icon: History }, - { name: 'Peta Serangan', path: '/attack-surface', icon: Share2 }, + { name: t('nav.user_history'), path: '/trainer/user-history', icon: History }, + { name: t('nav.attack_surface'), path: '/attack-surface', icon: Share2 }, ], }, { @@ -91,15 +93,15 @@ function buildTrainerNav(t) { items: [ { name: t('nav.live_campaigns'), path: '/live', icon: Radio }, { name: t('nav.reply_review'), path: '/live/review', icon: MessageSquare }, - { name: 'Laporan Phishing', path: '/live/reports', icon: ShieldAlert }, - { name: 'Analitik Live', path: '/live/analytics', icon: BarChart3 }, + { name: t('nav.report_inbox'), path: '/live/reports', icon: ShieldAlert }, + { name: t('nav.live_analytics'), path: '/live/analytics', icon: BarChart3 }, ], }, { section: 'ACCOUNT', items: [ { name: t('nav.analytics'), path: '/analytics', icon: BarChart3 }, - { name: 'Glosarium', path: '/glossary', icon: BookMarked }, + { name: t('nav.glossary'), path: '/glossary', icon: BookMarked }, { name: t('nav.settings'), path: '/settings', icon: Settings }, ], }, diff --git a/frontend/src/i18n/locales/en.json b/frontend/src/i18n/locales/en.json index de3469d..ffd95ca 100644 --- a/frontend/src/i18n/locales/en.json +++ b/frontend/src/i18n/locales/en.json @@ -41,7 +41,14 @@ "reply_review": "Reply Review", "triage": "Inbox Triage", "write_reply": "Write Your Reply", - "my_assignments": "My Assignments" + "my_assignments": "My Assignments", + "readiness": "Readiness", + "all_users": "All Users", + "users": "Users", + "user_history": "User History", + "attack_surface": "Attack Surface Map", + "report_inbox": "Phish Reports", + "live_analytics": "Live Analytics" }, "dashboard": { "welcome": "Welcome to Pretexta", @@ -290,7 +297,20 @@ "provider_selection": "Choose your LLM provider (OpenAI, Gemini, Claude, etc.) and enter a valid API key.", "revoke_key_bold": "Revoke Key", "webhooks": "Webhooks", - "notifications": "Notifications" + "notifications": "Notifications", + "demo": { + "title": "Demo data", + "desc": "Load a fictional organization (12 people on demo.test, three campaigns, the remediation loop, lab results and an attack-surface map) to show the full story. Everything is tagged as demo and can be removed without touching real data.", + "status_loaded": "Loaded {{date}}", + "status_empty": "Not loaded", + "load": "Load demo data", + "reload": "Reload demo data", + "clear": "Remove demo data", + "confirm_clear": "Remove all demo data? Real users and campaigns are not affected.", + "loaded": "Demo loaded: {{people}} people, {{campaigns}} campaigns", + "cleared": "Demo data removed", + "failed": "Failed" + } }, "simulation": { "security_score": "Security Awareness Score", @@ -655,6 +675,99 @@ "legit": "Looks legit", "select": "Select a message.", "progress": "{{done}}/{{total}} reviewed — you can submit any time", - "submit": "Submit triage" + "submit": "Submit triage", + "choose_set": "Choose an inbox", + "channel": { + "whatsapp": "WhatsApp message", + "sms": "SMS" + } + }, + "readiness": { + "title": "Readiness", + "desc": "How well your people resist social engineering — measured by behaviour, closed by remediation", + "index": "Readiness index", + "band": { + "none": "no data yet", + "strong": "resilient", + "alert": "alert", + "vulnerable": "vulnerable", + "critical": "critical" + }, + "component": { + "report_rate": "Report rate", + "no_click": "Did not click", + "principles": "Principle resistance", + "triage": "Inbox Triage score" + }, + "no_data": "no data", + "report_title": "Report rate — the behaviour we train for", + "reported_of": "overall {{rate}} ({{reported}}/{{total}}) over {{campaigns}} campaigns", + "points_since_first": "pts since the first campaign", + "click_rate": "Click rate", + "series": { + "report": "Report rate", + "click": "Click rate" + }, + "trend_needs_two": "Run two campaigns to see a trend", + "loop_title": "The loop: measure → remediate → retest", + "loop_desc": "Every failure triggers targeted practice for the exact principle used, then a variant retest.", + "step": { + "failed": "clicked in a campaign", + "assigned": "practice assigned", + "passed": "practice passed", + "retested": "retest passed" + }, + "principles_title": "Resistance by influence principle", + "resistance": "Resistance", + "weakest": "Weakest", + "surface_title": "Attack surface", + "surface_empty": "Model people, trust and crown jewels to see the easiest attack path.", + "open_map": "Open the map", + "score": "score", + "top_path": "Easiest crown jewel: {{asset}} ({{probability}} modelled success)", + "lab_title": "Lab discrimination", + "d_prime": "avg d′", + "triage_score": "avg triage", + "triage_runs": "runs", + "d_prime_hint": "d′ measures telling phish from legitimate mail; over-flagging does not raise it.", + "feed_title": "Loop activity", + "feed_empty": "No remediation yet.", + "feed_privacy": "Anonymous — individual results stay with the person and their trainer.", + "feed": { + "remediation_open": "Practice assigned for {{principle}}", + "remediation_done": "Practice on {{principle}} passed", + "retest_open": "Retest scheduled for {{principle}}", + "retest_done": "Retest on {{principle}} passed" + }, + "cohort_note": "Cohort of {{people}} trainees. Weights: report rate 40%, did not click 20%, principle resistance 20%, Inbox Triage 20%; missing parts are left out.", + "latest_campaign": "latest campaign" + }, + "remediation": { + "why": "Why", + "instructor_note": "Instructor note", + "lab_fail": "You scored {{score}}% on '{{title}}', which tested {{principle}}. This exercise gives you focused practice.", + "live_clicked": "In a live phishing simulation you clicked the link in a message that used {{principle}}. This exercise helps you recognise it next time.", + "live_submitted": "In a live phishing simulation you submitted data to a page reached through a message that used {{principle}}. This exercise helps you recognise it next time.", + "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 30af8a2..188e884 100644 --- a/frontend/src/i18n/locales/id.json +++ b/frontend/src/i18n/locales/id.json @@ -41,7 +41,14 @@ "reply_review": "Tinjau Balasan", "triage": "Triase Inbox", "write_reply": "Tulis Balasan", - "my_assignments": "Tugas Saya" + "my_assignments": "Tugas Saya", + "readiness": "Ketahanan", + "all_users": "Semua Pengguna", + "users": "Pengguna", + "user_history": "Riwayat Pengguna", + "attack_surface": "Peta Serangan", + "report_inbox": "Laporan Phishing", + "live_analytics": "Analitik Live" }, "dashboard": { "welcome": "Selamat Datang di Pretexta", @@ -290,7 +297,20 @@ "provider_selection": "Pilih penyedia LLM Anda (OpenAI, Gemini, Claude, dll.) dan masukkan kunci API yang valid.", "revoke_key_bold": "Cabut Kunci", "webhooks": "Webhook", - "notifications": "Notifikasi" + "notifications": "Notifikasi", + "demo": { + "title": "Data demo", + "desc": "Muat organisasi fiktif (12 orang di demo.test, tiga kampanye, siklus remediasi, hasil lab, dan peta serangan) untuk menunjukkan cerita lengkapnya. Semua ditandai sebagai demo dan bisa dihapus tanpa menyentuh data asli.", + "status_loaded": "Dimuat {{date}}", + "status_empty": "Belum dimuat", + "load": "Muat data demo", + "reload": "Muat ulang data demo", + "clear": "Hapus data demo", + "confirm_clear": "Hapus semua data demo? Pengguna dan kampanye asli tidak terpengaruh.", + "loaded": "Demo dimuat: {{people}} orang, {{campaigns}} kampanye", + "cleared": "Data demo dihapus", + "failed": "Gagal" + } }, "simulation": { "security_score": "Skor Kesadaran Keamanan", @@ -655,6 +675,99 @@ "legit": "Tampak sah", "select": "Pilih sebuah pesan.", "progress": "{{done}}/{{total}} ditinjau — Anda bisa mengirim kapan saja", - "submit": "Kirim hasil triase" + "submit": "Kirim hasil triase", + "choose_set": "Pilih inbox", + "channel": { + "whatsapp": "Pesan WhatsApp", + "sms": "SMS" + } + }, + "readiness": { + "title": "Ketahanan", + "desc": "Seberapa tahan personel terhadap rekayasa sosial — diukur dari perilaku, ditutup dengan remediasi", + "index": "Indeks ketahanan", + "band": { + "none": "belum ada data", + "strong": "tangguh", + "alert": "siaga", + "vulnerable": "rentan", + "critical": "kritis" + }, + "component": { + "report_rate": "Report rate", + "no_click": "Tidak mengklik", + "principles": "Resistansi prinsip", + "triage": "Skor Triase Inbox" + }, + "no_data": "belum ada data", + "report_title": "Report rate — perilaku yang kita latih", + "reported_of": "rata-rata {{rate}} ({{reported}}/{{total}}) dari {{campaigns}} kampanye", + "points_since_first": "poin sejak kampanye pertama", + "click_rate": "Click rate", + "series": { + "report": "Report rate", + "click": "Click rate" + }, + "trend_needs_two": "Jalankan dua kampanye untuk melihat tren", + "loop_title": "Siklus: ukur → remediasi → tes ulang", + "loop_desc": "Setiap kegagalan memicu latihan terarah untuk prinsip yang dipakai, lalu tes ulang dengan varian.", + "step": { + "failed": "klik di kampanye", + "assigned": "latihan ditugaskan", + "passed": "latihan lulus", + "retested": "tes ulang lulus" + }, + "principles_title": "Resistansi per prinsip pengaruh", + "resistance": "Resistansi", + "weakest": "Terlemah", + "surface_title": "Permukaan serangan", + "surface_empty": "Petakan orang, relasi kepercayaan, dan aset kritis untuk melihat jalur serangan termudah.", + "open_map": "Buka peta", + "score": "skor", + "top_path": "Aset kritis termudah: {{asset}} ({{probability}} peluang berhasil dalam model)", + "lab_title": "Diskriminasi lab", + "d_prime": "rata-rata d′", + "triage_score": "rata-rata triase", + "triage_runs": "percobaan", + "d_prime_hint": "d′ mengukur kemampuan membedakan phishing dari email sah; menandai semuanya tidak menaikkannya.", + "feed_title": "Aktivitas siklus", + "feed_empty": "Belum ada remediasi.", + "feed_privacy": "Anonim — hasil individu hanya untuk yang bersangkutan dan pelatihnya.", + "feed": { + "remediation_open": "Latihan {{principle}} ditugaskan", + "remediation_done": "Latihan {{principle}} lulus", + "retest_open": "Tes ulang {{principle}} dijadwalkan", + "retest_done": "Tes ulang {{principle}} lulus" + }, + "cohort_note": "Kohort {{people}} peserta. Bobot: report rate 40%, tidak mengklik 20%, resistansi prinsip 20%, Triase Inbox 20%; komponen tanpa data tidak dihitung.", + "latest_campaign": "kampanye terbaru" + }, + "remediation": { + "why": "Alasan", + "instructor_note": "Catatan instruktur", + "lab_fail": "Skor Anda {{score}}% pada '{{title}}', yang menguji prinsip {{principle}}. Latihan ini memberi Anda latihan terarah.", + "live_clicked": "Dalam simulasi phishing, Anda mengklik tautan pada pesan yang memakai prinsip {{principle}}. Latihan ini membantu Anda mengenalinya lain kali.", + "live_submitted": "Dalam simulasi phishing, Anda mengirim data ke halaman dari pesan yang memakai prinsip {{principle}}. Latihan ini membantu Anda mengenalinya lain kali.", + "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')}}
-