From 047f0bf1b1267321e11437f4ab4c2db49a0e9cc0 Mon Sep 17 00:00:00 2001 From: Danang Avan Maulana Date: Fri, 30 Oct 2026 14:10:59 +0700 Subject: [PATCH 1/5] feat(readiness): commander-facing readiness overview as the staff home MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One screen that tells the measure -> remediate -> retest story: - GET /api/readiness (trainer/admin, org-scoped like live campaigns): readiness index with its components and weights, report-rate trend over the last campaigns, remediation-loop funnel, average resistance per Cialdini principle, lab discrimination (Inbox Triage d'), and the attack-surface posture with the easiest crown jewel. - The index is transparent: report rate 40%, did-not-click 20%, principle resistance 20%, triage 20%; components without data are left out and the weights renormalised. - Cohort-level only (G13): the activity feed carries source, principle, status and date — no person, no free-text reason; the top attack path shows the asset, not the entry person. - ReadinessPage becomes "/" for admins and trainers; Command Center moves to /command. Remaining hard-coded nav labels go through i18n. Co-Authored-By: Claude Opus 5.5 --- backend/routes/__init__.py | 2 + backend/routes/readiness.py | 21 ++ backend/server.py | 2 + backend/services/readiness.py | 181 +++++++++++++++ backend/tests/test_readiness.py | 127 +++++++++++ frontend/src/App.js | 7 +- frontend/src/components/Layout.js | 32 +-- frontend/src/i18n/locales/en.json | 68 +++++- frontend/src/i18n/locales/id.json | 68 +++++- frontend/src/pages/ReadinessPage.js | 342 ++++++++++++++++++++++++++++ 10 files changed, 831 insertions(+), 19 deletions(-) create mode 100644 backend/routes/readiness.py create mode 100644 backend/services/readiness.py create mode 100644 backend/tests/test_readiness.py create mode 100644 frontend/src/pages/ReadinessPage.js diff --git a/backend/routes/__init__.py b/backend/routes/__init__.py index 696dece..8605384 100644 --- a/backend/routes/__init__.py +++ b/backend/routes/__init__.py @@ -17,6 +17,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 @@ -56,5 +57,6 @@ "live_router", "audit_router", "attack_surface_router", + "readiness_router", "report_inbox_router", ] 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/server.py b/backend/server.py index d9e9874..3b3c0ee 100644 --- a/backend/server.py +++ b/backend/server.py @@ -32,6 +32,7 @@ organizations_router, personas_router, quizzes_router, + readiness_router, report_inbox_router, reports_router, risk_profile_router, @@ -487,6 +488,7 @@ 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) app.include_router(api_router) diff --git a/backend/services/readiness.py b/backend/services/readiness.py new file mode 100644 index 0000000..1717e29 --- /dev/null +++ b/backend/services/readiness.py @@ -0,0 +1,181 @@ +""" +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] + 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, + "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] + components = { + "report_rate": None if live["report_rate"] is None else live["report_rate"] * 100, + "no_click": None if live["click_rate"] is None else (1 - live["click_rate"]) * 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/tests/test_readiness.py b/backend/tests/test_readiness.py new file mode 100644 index 0000000..7ba7b8c --- /dev/null +++ b/backend/tests/test_readiness.py @@ -0,0 +1,127 @@ +"""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 [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"] + assert comps == {"report_rate": 50, "no_click": 75, "principles": 70, "triage": 90} + # 0.4*50 + 0.2*75 + 0.2*70 + 0.2*90 = 67 + assert body["index"]["score"] == 67 + + 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/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..f07d96b 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", @@ -656,5 +663,64 @@ "select": "Select a message.", "progress": "{{done}}/{{total}} reviewed — you can submit any time", "submit": "Submit triage" + }, + "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": "{{reported}}/{{total}} reported · last {{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." } } diff --git a/frontend/src/i18n/locales/id.json b/frontend/src/i18n/locales/id.json index 30af8a2..31102a8 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", @@ -656,5 +663,64 @@ "select": "Pilih sebuah pesan.", "progress": "{{done}}/{{total}} ditinjau — Anda bisa mengirim kapan saja", "submit": "Kirim hasil triase" + }, + "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": "{{reported}}/{{total}} melapor · {{campaigns}} kampanye terakhir", + "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." } } diff --git a/frontend/src/pages/ReadinessPage.js b/frontend/src/pages/ReadinessPage.js new file mode 100644 index 0000000..9e1743b --- /dev/null +++ b/frontend/src/pages/ReadinessPage.js @@ -0,0 +1,342 @@ +import React from 'react'; +import { Link } from 'react-router-dom'; +import { useTranslation } from 'react-i18next'; +import { + ShieldCheck, Flag, MousePointerClick, ArrowRight, TrendingUp, TrendingDown, Repeat, + GraduationCap, CheckCircle2, Crosshair, Inbox, Activity, Radio, Share2, Info, +} from 'lucide-react'; +import { + ResponsiveContainer, LineChart, Line, XAxis, YAxis, Tooltip, + RadarChart, PolarGrid, PolarAngleAxis, Radar, +} from 'recharts'; +import { Button } from '../components/ui/button'; +import PageHeader from '../components/PageHeader'; +import SectionCard from '../components/SectionCard'; +import { useApi } from '../hooks/useApi'; + +// Same palette as the Attack Surface Map so the two screens read as one system. +const GOOD = '#6f8f3f'; +const WARN = '#d39a22'; +const BAD = '#c0453f'; +const ACCENT = '#e0b84a'; +const POSTURE_COLOR = { + SECURE: '#6f8f3f', GUARDED: '#9fae3a', ELEVATED: '#d39a22', HIGH: '#d9772a', SEVERE: '#c0453f', +}; +const TICK = { fill: 'rgba(234,234,230,0.55)', fontSize: 10, fontFamily: 'monospace' }; +const TOOLTIP = { + background: '#151812', border: '1px solid rgba(143,159,94,0.3)', fontSize: 11, fontFamily: 'monospace', +}; + +const pct = (v) => (v == null ? '—' : `${Math.round(v * 100)}%`); + +function band(score) { + if (score == null) return { key: 'none', color: 'rgba(234,234,230,0.35)' }; + if (score >= 80) return { key: 'strong', color: GOOD }; + if (score >= 60) return { key: 'alert', color: '#9fae3a' }; + if (score >= 40) return { key: 'vulnerable', color: WARN }; + return { key: 'critical', color: BAD }; +} + +// Half-circle gauge, 0–100. +function Gauge({ score }) { + const { color } = band(score); + const r = 80; + const len = Math.PI * r; + const filled = score == null ? 0 : (len * score) / 100; + return ( + + + + + {score == null ? '—' : score} + + / 100 + + ); +} + +function IndexCard({ index, t }) { + const b = band(index.score); + return ( + +
+ + + {t(`readiness.band.${b.key}`)} + +
+ {/* The formula is shown, not hidden: every part of the number is inspectable. */} +
+ {Object.entries(index.components).map(([key, value]) => ( +
+
+ {t(`readiness.component.${key}`)} · {Math.round(index.weights[key] * 100)}% + {value == null ? t('readiness.no_data') : value} +
+
+
+
+
+ ))} +
+ + ); +} + +function ReportCard({ live, t }) { + const delta = live.report_rate_delta; + const Trend = delta != null && delta < 0 ? TrendingDown : TrendingUp; + const trend = live.trend.map((c) => ({ + name: c.name, + report: c.report_rate == null ? null : Math.round(c.report_rate * 100), + click: c.click_rate == null ? null : Math.round(c.click_rate * 100), + })); + return ( + + {t('dashboard.view_all')}}> +
+
+
+ {pct(live.report_rate)} +
+
+ {t('readiness.reported_of', { reported: live.reported, total: live.recipients, campaigns: live.campaigns })} +
+ {delta != null && ( +
= 0 ? GOOD : BAD }}> + + {delta >= 0 ? '+' : ''}{Math.round(delta * 100)} {t('readiness.points_since_first')} +
+ )} +
+ + {t('readiness.click_rate')}: {pct(live.click_rate)} +
+
+
+ {trend.length >= 2 ? ( + + + + + [`${v}%`, t(`readiness.series.${k}`)]} /> + + + + + ) : ( +
+ {t('readiness.trend_needs_two')} +
+ )} +
+
+
+ ); +} + +function LoopCard({ live, loop, t }) { + const steps = [ + { key: 'failed', value: live.clicked, icon: MousePointerClick, color: BAD }, + { key: 'assigned', value: loop.remediation_assigned, icon: GraduationCap, color: WARN }, + { key: 'passed', value: loop.remediation_passed, icon: CheckCircle2, color: '#9fae3a' }, + { key: 'retested', value: loop.retest_passed, icon: Repeat, color: GOOD }, + ]; + return ( + +

{t('readiness.loop_desc')}

+
+ {steps.map((s, i) => { + const Icon = s.icon; + return ( +
+
+ +
+ {s.value} +
+
+ {t(`readiness.step.${s.key}`)} +
+
+ {i < steps.length - 1 && ( + + )} +
+ ); + })} +
+
+ ); +} + +function PrinciplesCard({ principles, weakest, t }) { + const data = Object.entries(principles).map(([key, value]) => ({ + key, label: t(`principles.${key}`, { defaultValue: key }), value: value ?? 0, + })); + const hasData = Object.values(principles).some((v) => v != null); + return ( + + {hasData ? ( + <> +
+ + + + + + [`${v}/100`, t('readiness.resistance')]} /> + + +
+ {weakest.length > 0 && ( +

+ {t('readiness.weakest')}: + {weakest.map((p) => ( + + {t(`principles.${p}`, { defaultValue: p })} ({principles[p]}) + + ))} +

+ )} + + ) :

{t('readiness.no_data')}

} +
+ ); +} + +function SurfaceCard({ surface, t }) { + if (!surface) { + return ( + +

{t('readiness.surface_empty')}

+ +
+ ); + } + const color = POSTURE_COLOR[surface.posture?.label] || WARN; + return ( + +
+ + {surface.posture?.label} + + + DEFCON {surface.posture?.condition} · {t('readiness.score')} {surface.posture?.score} + +
+ {surface.top_path && ( +

+ {t('readiness.top_path', { asset: surface.top_path.asset, probability: pct(surface.top_path.probability) })} +

+ )} + + + +
+ ); +} + +function LabCard({ lab, t }) { + return ( + +
+ {[ + [lab.avg_d_prime ?? '—', t('readiness.d_prime')], + [lab.avg_triage_score ?? '—', t('readiness.triage_score')], + [lab.triage_runs, t('readiness.triage_runs')], + ].map(([value, label]) => ( +
+
{value}
+
{label}
+
+ ))} +
+

{t('readiness.d_prime_hint')}

+
+ ); +} + +function FeedCard({ recent, t }) { + return ( + + {recent.length === 0 ? ( +

{t('readiness.feed_empty')}

+ ) : ( +
    + {recent.map((r, i) => ( +
  • + {r.source === 'retest' + ? + : } + + {t(`readiness.feed.${r.source}_${r.status === 'completed' ? 'done' : 'open'}`, + { principle: t(`principles.${r.principle}`, { defaultValue: r.principle }) })} + + + {r.created_at ? new Date(r.created_at).toLocaleDateString() : ''} + +
  • + ))} +
+ )} +

+ {t('readiness.feed_privacy')} +

+
+ ); +} + +export default function ReadinessPage() { + const { t } = useTranslation(); + const { data, loading } = useApi('/readiness'); + + return ( +
+ + + + + + + {loading || !data ? ( +
+ {[1, 2, 3].map((i) =>
)} +
+ ) : ( + <> +
+ + +
+ +
+ +
+ + +
+ +
+

+ {t('readiness.cohort_note', { people: data.people })} +

+ + )} +
+ ); +} From f0ddceb961668067463f420916f8eff84cf08a2a Mon Sep 17 00:00:00 2001 From: Danang Avan Maulana Date: Fri, 30 Oct 2026 14:25:50 +0700 Subject: [PATCH 2/5] feat(demo): one-click demo organization that tells the full loop story Admin-only "Demo data" card in Settings (GET/POST/DELETE /api/demo) loads a fictional organization on the sandbox domain demo.test: 12 people, three campaigns over three months (report rate 25% -> 50% -> 75%, click rate 42% -> 8%, faster reports), remediation and retests for everyone who clicked, Inbox Triage results, risk profiles with authority as the weak spot, and an attack-surface map. - Deterministic: the same numbers on every load; reloading replaces it. - Removable: every record is tagged demo: True; clearing never touches real data. The admin's own map is only seeded when it is empty, and only the maps the demo seeded are removed (tracked in demo_state). - Demo people get a random, discarded password and cannot log in. - Load and clear are recorded in the audit log. Readiness: the headline report rate and the index now use the latest campaign (readiness now); the all-campaign aggregate stays as context. Co-Authored-By: Claude Opus 5.5 --- backend/routes/__init__.py | 2 + backend/routes/demo.py | 28 +++ backend/server.py | 2 + backend/services/demo.py | 349 ++++++++++++++++++++++++++++ backend/services/readiness.py | 10 +- backend/tests/test_demo.py | 69 ++++++ backend/tests/test_readiness.py | 8 +- frontend/src/i18n/locales/en.json | 20 +- frontend/src/i18n/locales/id.json | 20 +- frontend/src/pages/ReadinessPage.js | 11 +- frontend/src/pages/SettingsPage.js | 54 ++++- 11 files changed, 558 insertions(+), 15 deletions(-) create mode 100644 backend/routes/demo.py create mode 100644 backend/services/demo.py create mode 100644 backend/tests/test_demo.py diff --git a/backend/routes/__init__.py b/backend/routes/__init__.py index 8605384..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 @@ -48,6 +49,7 @@ "webhooks_router", "scenario_builder_router", "debrief_router", + "demo_router", # v3 platform services "personas_router", "risk_profile_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/server.py b/backend/server.py index 3b3c0ee..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, @@ -489,6 +490,7 @@ async def health_check(): 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..4e6ecd7 --- /dev/null +++ b/backend/services/demo.py @@ -0,0 +1,349 @@ +""" +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": f"{'Retest' if source == 'retest' else 'Remediation'}: {title}", + "instructions": reason, + "reason": reason, + "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/readiness.py b/backend/services/readiness.py index 1717e29..a4433e6 100644 --- a/backend/services/readiness.py +++ b/backend/services/readiness.py @@ -55,6 +55,7 @@ async def _live(scope: dict) -> dict[str, Any]: 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, @@ -64,6 +65,9 @@ async def _live(scope: dict) -> dict[str, Any]: "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, } @@ -129,9 +133,11 @@ async def count(extra: dict) -> int: 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 live["report_rate"] is None else live["report_rate"] * 100, - "no_click": None if live["click_rate"] is None else (1 - live["click_rate"]) * 100, + "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"], } 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_readiness.py b/backend/tests/test_readiness.py index 7ba7b8c..59407ce 100644 --- a/backend/tests/test_readiness.py +++ b/backend/tests/test_readiness.py @@ -88,15 +88,17 @@ async def test_index_and_loop(api): 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"] - assert comps == {"report_rate": 50, "no_click": 75, "principles": 70, "triage": 90} - # 0.4*50 + 0.2*75 + 0.2*70 + 0.2*90 = 67 - assert body["index"]["score"] == 67 + # 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) diff --git a/frontend/src/i18n/locales/en.json b/frontend/src/i18n/locales/en.json index f07d96b..52a907e 100644 --- a/frontend/src/i18n/locales/en.json +++ b/frontend/src/i18n/locales/en.json @@ -297,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", @@ -683,7 +696,7 @@ }, "no_data": "no data", "report_title": "Report rate — the behaviour we train for", - "reported_of": "{{reported}}/{{total}} reported · last {{campaigns}} campaigns", + "reported_of": "overall {{rate}} ({{reported}}/{{total}}) over {{campaigns}} campaigns", "points_since_first": "pts since the first campaign", "click_rate": "Click rate", "series": { @@ -721,6 +734,7 @@ "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." + "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" } } diff --git a/frontend/src/i18n/locales/id.json b/frontend/src/i18n/locales/id.json index 31102a8..74019ee 100644 --- a/frontend/src/i18n/locales/id.json +++ b/frontend/src/i18n/locales/id.json @@ -297,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", @@ -683,7 +696,7 @@ }, "no_data": "belum ada data", "report_title": "Report rate — perilaku yang kita latih", - "reported_of": "{{reported}}/{{total}} melapor · {{campaigns}} kampanye terakhir", + "reported_of": "rata-rata {{rate}} ({{reported}}/{{total}}) dari {{campaigns}} kampanye", "points_since_first": "poin sejak kampanye pertama", "click_rate": "Click rate", "series": { @@ -721,6 +734,7 @@ "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." + "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" } } diff --git a/frontend/src/pages/ReadinessPage.js b/frontend/src/pages/ReadinessPage.js index 9e1743b..574d2fd 100644 --- a/frontend/src/pages/ReadinessPage.js +++ b/frontend/src/pages/ReadinessPage.js @@ -105,10 +105,15 @@ function ReportCard({ live, t }) {
- {pct(live.report_rate)} + {pct(live.latest_report_rate)}
- {t('readiness.reported_of', { reported: live.reported, total: live.recipients, campaigns: live.campaigns })} + {t('readiness.latest_campaign')} +
+
+ {t('readiness.reported_of', { + rate: pct(live.report_rate), reported: live.reported, total: live.recipients, campaigns: live.campaigns, + })}
{delta != null && (
- {t('readiness.click_rate')}: {pct(live.click_rate)} + {t('readiness.click_rate')}: {pct(live.latest_click_rate)}
diff --git a/frontend/src/pages/SettingsPage.js b/frontend/src/pages/SettingsPage.js index 797d368..f16963a 100644 --- a/frontend/src/pages/SettingsPage.js +++ b/frontend/src/pages/SettingsPage.js @@ -9,7 +9,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '. import { Switch } from '../components/ui/switch'; import { Settings as SettingsIcon, Globe, Zap, Key, RefreshCw, ExternalLink, - Server, Wifi, WifiOff, Check, ChevronDown, Search, Shield, UserCog + Server, Wifi, WifiOff, Check, ChevronDown, Search, Shield, UserCog, Database, Trash2 } from 'lucide-react'; import { useAuth } from '../contexts/AuthContext'; import SectionCard from '../components/SectionCard'; @@ -76,6 +76,57 @@ function AdminPanel() { } +function DemoDataCard() { + const { t } = useTranslation(); + const [state, setState] = useState(null); + const [busy, setBusy] = useState(false); + + const refresh = () => api.get('/demo').then((r) => setState(r.data)).catch(() => setState(null)); + useEffect(() => { refresh(); }, []); + + const run = async (action) => { + if (action === 'clear' && !window.confirm(t('settings.demo.confirm_clear'))) return; + setBusy(true); + try { + if (action === 'load') { + const r = await api.post('/demo'); + toast.success(t('settings.demo.loaded', { people: r.data.people, campaigns: r.data.campaigns })); + } else { + await api.delete('/demo'); + toast.success(t('settings.demo.cleared')); + } + refresh(); + } catch (e) { + toast.error(e.response?.data?.detail || t('settings.demo.failed')); + } finally { + setBusy(false); + } + }; + + return ( + +

{t('settings.demo.desc')}

+

+ {state?.loaded + ? t('settings.demo.status_loaded', { date: new Date(state.seeded_at).toLocaleString() }) + : t('settings.demo.status_empty')} +

+
+ + {state?.loaded && ( + + )} +
+
+ ); +} + function LiveConfigCard() { const [f, setF] = useState(null); const [testing, setTesting] = useState(false); @@ -316,6 +367,7 @@ export default function SettingsPage() { {/* Admin Panel — only for admin users */} {isAdmin && } {isAdmin && } + {isAdmin && }
{/* LEFT COLUMN */} From ce021b50870b609863846ad32b89ede4b54df255 Mon Sep 17 00:00:00 2001 From: Danang Avan Maulana Date: Fri, 30 Oct 2026 17:42:28 +0700 Subject: [PATCH 3/5] feat(triage): chat-channel messages, set authoring, context-aware remediation - POST /api/triage lets trainers add triage sets (validated: id pattern, 4-40 items, must mix pretexts and legitimate messages, every pretext needs a principle and explanation); audited. - Triage Inbox renders WhatsApp/SMS items as a phone chat and offers a set picker when more than one set exists. - Remediation assignments carry reason_key/reason_params so the reason is translated in the UI instead of stored as English text. - Remediation prefers content sharing context tags with the failed exercise and never re-assigns the exercise that was just failed; retests follow the same rule. - Scenario debrief keeps line breaks; removed duplicate untranslated "Try another scenario" button. Co-Authored-By: Claude Opus 5.5 --- backend/models/schemas.py | 4 + backend/routes/triage.py | 69 ++++++++++++++- backend/services/demo.py | 4 +- backend/services/remediation.py | 68 ++++++++++++--- backend/tests/test_remediation.py | 73 ++++++++++++++++ backend/tests/test_triage.py | 47 +++++++++++ docs/api.md | 15 ++++ frontend/src/i18n/locales/en.json | 17 +++- frontend/src/i18n/locales/id.json | 17 +++- frontend/src/pages/MyAssignmentsPage.js | 21 ++++- frontend/src/pages/SimulationPlayerPage.js | 15 ++-- frontend/src/pages/TriageInboxPage.js | 97 ++++++++++++++++++---- 12 files changed, 397 insertions(+), 50 deletions(-) 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/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/services/demo.py b/backend/services/demo.py index 4e6ecd7..59bc793 100644 --- a/backend/services/demo.py +++ b/backend/services/demo.py @@ -303,9 +303,11 @@ def _assignment( "trainee_id": trainee_id, "content_type": content_type, "content_id": content_id, - "title": f"{'Retest' if source == 'retest' else 'Remediation'}: {title}", + "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, 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/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_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..34ea7cd 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 @@ -109,6 +117,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/frontend/src/i18n/locales/en.json b/frontend/src/i18n/locales/en.json index 52a907e..4d0fb89 100644 --- a/frontend/src/i18n/locales/en.json +++ b/frontend/src/i18n/locales/en.json @@ -675,7 +675,12 @@ "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", @@ -736,5 +741,15 @@ }, "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." } } diff --git a/frontend/src/i18n/locales/id.json b/frontend/src/i18n/locales/id.json index 74019ee..2f43a51 100644 --- a/frontend/src/i18n/locales/id.json +++ b/frontend/src/i18n/locales/id.json @@ -675,7 +675,12 @@ "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", @@ -736,5 +741,15 @@ }, "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." } } diff --git a/frontend/src/pages/MyAssignmentsPage.js b/frontend/src/pages/MyAssignmentsPage.js index 3f50df1..4b5d6bf 100644 --- a/frontend/src/pages/MyAssignmentsPage.js +++ b/frontend/src/pages/MyAssignmentsPage.js @@ -28,6 +28,19 @@ const CONTENT_TYPE_LABELS = { }; function AssignmentCard({ assignment }) { + const { t } = useTranslation(); + // System (remediation/retest) reasons are rendered from a key so they follow + // the UI language; manual instructor notes are shown as written. + const note = assignment.reason_key + ? t(`remediation.${assignment.reason_key}`, { + ...assignment.reason_params, + principle: t(`principles.${assignment.reason_params?.principle}`, { + defaultValue: assignment.reason_params?.principle, + }), + defaultValue: assignment.instructions, + }) + : assignment.instructions; + const isSystem = assignment.source === 'remediation' || assignment.source === 'retest'; const navigate = useNavigate(); const Icon = CONTENT_TYPE_ICONS[assignment.content_type] || Target; const result = assignment.result; @@ -119,11 +132,13 @@ function AssignmentCard({ assignment }) { {result && ` · Percobaan ke-${result.attempts}`}

- {assignment.instructions && ( + {note && (

- Catatan Instruktur: - {assignment.instructions} + + {isSystem ? t('remediation.why') : t('remediation.instructor_note')}: + + {note}

)} diff --git a/frontend/src/pages/SimulationPlayerPage.js b/frontend/src/pages/SimulationPlayerPage.js index 405ef28..217764a 100644 --- a/frontend/src/pages/SimulationPlayerPage.js +++ b/frontend/src/pages/SimulationPlayerPage.js @@ -328,24 +328,19 @@ export default function SimulationPlayerPage() { {getContent(currentNode, 'title')} -

+ {/* Explanations are authored with line breaks and bullet lists. */} +

{getContent(currentNode, 'explanation')}

+ {/* A fresh attempt is a new scenario run, so scoring stays honest. */} - - {/* A fresh attempt is a new scenario run, so scoring stays honest. */} - {outcome !== 'success' && ( - - )}
)} diff --git a/frontend/src/pages/TriageInboxPage.js b/frontend/src/pages/TriageInboxPage.js index 7e812aa..9f5d105 100644 --- a/frontend/src/pages/TriageInboxPage.js +++ b/frontend/src/pages/TriageInboxPage.js @@ -1,5 +1,7 @@ import React, { useEffect, useState, useCallback, useRef } from 'react'; -import { Inbox, Flag, ShieldCheck, Mail, AlertTriangle, CheckCircle2, XCircle, Clock } from 'lucide-react'; +import { + Inbox, Flag, ShieldCheck, Mail, AlertTriangle, CheckCircle2, XCircle, Clock, MessageCircle, Smartphone, +} from 'lucide-react'; import { Card } from '../components/ui/card'; import { Button } from '../components/ui/button'; import { Badge } from '../components/ui/badge'; @@ -18,8 +20,38 @@ function Metric({ label, value, hint }) { ); } +const CHAT_CHANNELS = ['whatsapp', 'sms']; +const CHANNEL_ICON = { email_inbox: Mail, whatsapp: MessageCircle, sms: Smartphone }; + +// A chat message rendered the way it arrives on a phone: contact header, one +// incoming bubble. Same item, same grading — only the presentation differs. +function ChatPane({ item, t }) { + const initial = (item.from || '?').trim().charAt(0).toUpperCase(); + return ( +
+
+
{initial}
+
+
{item.from}
+
+ {item.channel === 'sms' ? t('triage.channel.sms') : t('triage.channel.whatsapp')} +
+
+
+
+
+ {item.body} +
+
+
+ ); +} + export default function TriageInboxPage() { const { t, i18n } = useTranslation(); + const [sets, setSets] = useState([]); const [set, setSet] = useState(null); const [marks, setMarks] = useState({}); const [selected, setSelected] = useState(null); @@ -27,19 +59,29 @@ export default function TriageInboxPage() { const [elapsed, setElapsed] = useState(0); const startRef = useRef(Date.now()); + const openSet = useCallback(async (id) => { + try { + const full = (await api.get(`/triage/${id}`)).data; + setSet(full); + setMarks({}); + setResults(null); + setElapsed(0); + setSelected(full.items[0]?.id || null); + startRef.current = Date.now(); + } catch { toast.error(t('triage.load_failed')); } + }, [t]); + const load = useCallback(async () => { try { const list = (await api.get('/triage')).data; if (!list.length) return; - // Prefer the set in the UI language; sets without a language are English. + setSets(list); + // Prefer a set in the UI language; sets without a language are English. const lang = (i18n.language || 'en').slice(0, 2); const pick = list.find((x) => (x.language || 'en') === lang) || list[0]; - const full = (await api.get(`/triage/${pick.id}`)).data; - setSet(full); - setSelected(full.items[0]?.id || null); - startRef.current = Date.now(); + await openSet(pick.id); } catch { toast.error(t('triage.load_failed')); } - }, [i18n.language, t]); + }, [i18n.language, t, openSet]); useEffect(() => { load(); }, [load]); useEffect(() => { @@ -103,7 +145,7 @@ export default function TriageInboxPage() { ); })}
- +
); } @@ -115,34 +157,53 @@ export default function TriageInboxPage() {
- {elapsed}s +
+ {sets.length > 1 && ( + + )} + {elapsed}s +
{/* Inbox list */} - {set.items.map((it) => ( + {set.items.map((it) => { + const ChannelIcon = CHANNEL_ICON[it.channel] || Mail; + return ( - ))} + ); + })} {/* Reading pane */} {current ? ( <> -
-
{current.from}
-

{current.subject}

-
-

{current.body}

+ {CHAT_CHANNELS.includes(current.channel) ? : ( + <> +
+
{current.from}
+

{current.subject}

+
+

{current.body}

+ + )}
+ + ) : ( +

+ Belum diatur. Admin dapat mengisi “Report mailbox” di Pengaturan → Live Campaigns agar + email yang di-forward personel tercatat otomatis. +

+ )} + + ); +} + function RecordForm({ onDone }) { const [f, setF] = useState({ reporter_email: '', subject: '', from_address: '', body: '' }); const [busy, setBusy] = useState(false); @@ -49,8 +87,8 @@ function RecordForm({ onDone }) { return (

- Saat karyawan meneruskan email mencurigakan ke mailbox abuse, sistem memilahnya otomatis. - Di produksi ini disuplai oleh poller mailbox; di sini bisa dicatat manual untuk uji coba. + Untuk laporan yang masuk lewat jalur lain (telepon, chat, tiket), catat manual di sini. + Sistem memilahnya dengan cara yang sama.

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

Hi {{.FirstName}},

Verify here: {{.URL}}

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

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

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

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