File size: 11,541 Bytes
8c536e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54a031
 
 
 
8c536e6
 
b54a031
 
 
8c536e6
 
b54a031
 
8c536e6
 
b54a031
 
 
 
8c536e6
b54a031
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c536e6
 
b54a031
 
 
 
8c536e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54a031
8c536e6
 
 
b54a031
8c536e6
b54a031
 
 
 
 
 
 
 
8c536e6
b54a031
 
 
 
 
 
 
 
8c536e6
 
 
 
 
b54a031
8c536e6
b54a031
 
 
 
8c536e6
 
 
 
 
b54a031
 
 
 
 
8c536e6
 
b54a031
 
 
 
 
8c536e6
b54a031
8c536e6
b54a031
 
 
 
 
 
 
8c536e6
b54a031
8c536e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cceafb
 
 
 
 
 
8c536e6
 
 
 
5cceafb
 
 
8c536e6
 
 
5cceafb
 
 
 
 
 
 
 
 
 
 
 
8c536e6
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""FastAPI server β€” OpenEnv-compatible HTTP interface + replay-mode demo API.

OpenEnv endpoints (used by the trainer and any external client):
  GET  /health             β†’ {"status": "ok", "defense_mode": str, "mode": str}
  POST /reset              β†’ InjectObservation
  POST /step               β†’ StepResult
  GET  /state              β†’ current episode state dict

Demo endpoints (used by the public frontend on Hugging Face Spaces):
  GET  /api/attack-types   β†’ list of attack types + step options
  POST /api/attack         β†’ request a trace; body {attack_type, steps}
  GET  /api/stream/{key}   β†’ Server-Sent Events stream of the trace timeline
  GET  /api/highlight      β†’ pre-computed highlight reel for the homepage
  GET  /api/stats          β†’ aggregate bypass-rate stats

Static frontend (when present):
  GET  /                   β†’ frontend/index.html
  GET  /static/*           β†’ frontend/* (CSS, JS, assets)

Environment variables
---------------------
  USE_STUB_DEFENSES=true     Use in-process stub defenses (no GPU). Default in
                             the Dockerfile so HF Spaces boots without GPUs.
  INJECTARENA_MODE=replay    Serve pre-recorded traces from data/traces/ via
                             the /api/* endpoints. Use ``live`` on a paid GPU
                             Space to run real attacks.
  HF_TOKEN                   Required only for real defense loading (live mode).
"""

from __future__ import annotations

import asyncio
import json
import logging
import os
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Any, AsyncIterator, Optional

from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel

from .environment import InjectArenaEnv
from .models import InjectAction, InjectObservation, StepResult
from .replay import TraceStore
from .scenarios import ScenarioBank

logger = logging.getLogger(__name__)


# ---------------------------------------------------------------------------
# Stub defenses (no GPU β€” used in HF Space and Mac tests via USE_STUB_DEFENSES)
# ---------------------------------------------------------------------------

class _StubVerdict(BaseModel):
    flagged: bool = False
    score: float = 0.0
    reason: Optional[str] = "stub"


class _StubPG2:
    name = "stub_pg2"

    def scan(self, text: str) -> _StubVerdict:
        return _StubVerdict()


class _StubSecAlign:
    name = "stub_secalign"
    mode = "stub"

    def run(self, system: str, user: str, untrusted: Any) -> str:
        slot = next(iter(untrusted), "input")
        content = next(iter(untrusted.values()), "")
        return f"[STUB] Processed {slot}: {str(content)[:80]}"


class _StubFirewall:
    name = "stub_firewall"

    def scan(self, user_query: str, agent_output: str, tool_call: Any = None) -> _StubVerdict:
        return _StubVerdict()


def _build_stub_env(bank: ScenarioBank) -> InjectArenaEnv:
    return InjectArenaEnv(
        pg2=_StubPG2(),
        secalign=_StubSecAlign(),
        firewall=_StubFirewall(),
        bank=bank,
    )


def _build_real_env(bank: ScenarioBank) -> InjectArenaEnv:
    from .defenses.prompt_guard import PromptGuard
    from .defenses.secalign_agent import SecAlignAgent
    from .defenses.llama_firewall import FirewallWrapper
    from .utils.embedding_cache import EmbeddingCache

    pg2 = PromptGuard()
    secalign = SecAlignAgent()
    firewall = FirewallWrapper(prompt_guard_fallback=pg2)
    embedder = EmbeddingCache()
    return InjectArenaEnv(pg2=pg2, secalign=secalign, firewall=firewall, bank=bank, embedder=embedder)


# ---------------------------------------------------------------------------
# App lifecycle
# ---------------------------------------------------------------------------

_env: Optional[InjectArenaEnv] = None
_defense_mode: str = "unknown"
_serve_mode: str = "live"   # "replay" | "live"
_trace_store: Optional[TraceStore] = None


@asynccontextmanager
async def lifespan(app: FastAPI):
    global _env, _defense_mode, _serve_mode, _trace_store

    _serve_mode = os.environ.get("INJECTARENA_MODE", "live").strip().lower()
    if _serve_mode not in ("live", "replay"):
        logger.warning("Unknown INJECTARENA_MODE=%s β€” defaulting to live.", _serve_mode)
        _serve_mode = "live"

    # Trace store is needed in both modes (highlight reel is always replay-driven).
    _trace_store = TraceStore()

    if _serve_mode == "live":
        bank = ScenarioBank()
        use_stub = os.environ.get("USE_STUB_DEFENSES", "").strip().lower() in ("1", "true", "yes")
        if use_stub:
            _env = _build_stub_env(bank)
            _defense_mode = "stub"
            logger.info("InjectArena server: live mode, STUB defenses.")
        else:
            _env = _build_real_env(bank)
            _defense_mode = "real"
            logger.info("InjectArena server: live mode, REAL defenses.")
    else:
        _defense_mode = "n/a"
        logger.info("InjectArena server: replay mode (no defenses loaded).")

    yield

    if _env is not None:
        _env.close()


app = FastAPI(title="InjectArena", version="1.0.0", lifespan=lifespan)


# ---------------------------------------------------------------------------
# OpenEnv request bodies
# ---------------------------------------------------------------------------

class ResetRequest(BaseModel):
    scenario_id: Optional[str] = None
    seed: Optional[int] = None
    split: str = "train"


class AttackRequest(BaseModel):
    attack_type: str
    steps: int


# ---------------------------------------------------------------------------
# Health + OpenEnv endpoints
# ---------------------------------------------------------------------------

@app.get("/health")
def health():
    return {
        "status": "ok",
        "defense_mode": _defense_mode,
        "mode": _serve_mode,
    }


@app.post("/reset", response_model=InjectObservation)
def reset(req: ResetRequest = ResetRequest()):
    if _env is None:
        raise HTTPException(status_code=503, detail="Live mode disabled. Use /api/attack instead.")
    return _env.reset(scenario_id=req.scenario_id, seed=req.seed, split=req.split)


@app.post("/step", response_model=StepResult)
def step(action: InjectAction):
    if _env is None:
        raise HTTPException(status_code=503, detail="Live mode disabled. Use /api/attack instead.")
    try:
        return _env.step(action)
    except RuntimeError as exc:
        raise HTTPException(status_code=400, detail=str(exc))


@app.get("/state")
def state():
    if _env is None:
        raise HTTPException(status_code=503, detail="Live mode disabled.")
    return _env.state


# ---------------------------------------------------------------------------
# Demo API (replay mode)
# ---------------------------------------------------------------------------

@app.get("/api/attack-types")
def api_attack_types():
    if _trace_store is None:
        raise HTTPException(status_code=503, detail="Trace store not initialised.")
    return {
        "attack_types": _trace_store.attack_types(),
        "step_options": _trace_store.step_options(),
    }


@app.post("/api/attack")
def api_attack(req: AttackRequest):
    if _trace_store is None:
        raise HTTPException(status_code=503, detail="Trace store not initialised.")
    trace = _trace_store.get(req.attack_type, req.steps)
    if trace is None:
        raise HTTPException(
            status_code=404,
            detail=f"No trace available for attack_type={req.attack_type} steps={req.steps}",
        )
    # Returns the full trace immediately for clients that don't want streaming.
    # The streaming endpoint below paces the events out over time for animation.
    return trace


@app.get("/api/stream/{attack_type}/{steps}")
async def api_stream(attack_type: str, steps: int):
    if _trace_store is None:
        raise HTTPException(status_code=503, detail="Trace store not initialised.")
    trace = _trace_store.get(attack_type, steps)
    if trace is None:
        raise HTTPException(
            status_code=404,
            detail=f"No trace available for attack_type={attack_type} steps={steps}",
        )

    async def event_stream() -> AsyncIterator[bytes]:
        # First event: trace metadata
        meta = {
            "type": "meta",
            "attack_type": trace.get("attack_type"),
            "steps": trace.get("steps"),
            "scenario_id": trace.get("scenario_id"),
        }
        yield _sse(meta)

        prev_t = 0.0
        for ev in trace.get("timeline", []):
            t = float(ev.get("t", prev_t))
            await asyncio.sleep(max(0.0, t - prev_t))
            prev_t = t
            yield _sse({"type": "event", **ev})

        # Final event: outcome
        yield _sse({"type": "outcome", **trace.get("outcome", {})})
        yield _sse({"type": "done"})

    return StreamingResponse(event_stream(), media_type="text/event-stream")


@app.get("/api/highlight")
def api_highlight():
    if _trace_store is None:
        raise HTTPException(status_code=503, detail="Trace store not initialised.")
    trace = _trace_store.highlight()
    if trace is None:
        raise HTTPException(status_code=404, detail="No highlight reel available yet.")
    return trace


@app.get("/api/stats")
def api_stats():
    if _trace_store is None:
        raise HTTPException(status_code=503, detail="Trace store not initialised.")
    return _trace_store.aggregate_stats()


def _sse(payload: dict) -> bytes:
    return f"data: {json.dumps(payload)}\n\n".encode("utf-8")


# ---------------------------------------------------------------------------
# Static frontend (mounted last so /api/* takes precedence)
# ---------------------------------------------------------------------------

_REPO_ROOT = Path(__file__).resolve().parent.parent
# Vite outputs to frontend/dist/. The dev source tree (frontend/) is fall-back
# only β€” useful when an unbuilt frontend is mounted in a development container.
_FRONTEND_DIST = _REPO_ROOT / "frontend" / "dist"
_FRONTEND_SRC = _REPO_ROOT / "frontend"
_PLOTS_DIR = _REPO_ROOT / "docs" / "plots"

if _PLOTS_DIR.exists():
    app.mount("/plots", StaticFiles(directory=_PLOTS_DIR), name="plots")

if (_FRONTEND_DIST / "index.html").exists():
    # Vite build output: assets are referenced as /assets/... from index.html.
    app.mount("/assets", StaticFiles(directory=_FRONTEND_DIST / "assets"), name="assets")

    @app.get("/")
    def root():
        return FileResponse(_FRONTEND_DIST / "index.html")

    # Catch-all so Vite-built static files (favicon, icons, etc.) at the root
    # also resolve. Mounting this AFTER all /api routes preserves API priority.
    app.mount("/", StaticFiles(directory=_FRONTEND_DIST, html=True), name="frontend")

elif (_FRONTEND_SRC / "index.html").exists():
    # Unbuilt source: only useful behind `vite dev`; here we just serve index.
    @app.get("/")
    def root():
        return FileResponse(_FRONTEND_SRC / "index.html")

else:
    @app.get("/")
    def root():
        return {
            "service": "InjectArena",
            "version": "1.0.0",
            "mode": _serve_mode,
            "docs": "/docs",
            "endpoints": [
                "/health", "/reset", "/step", "/state",
                "/api/attack-types", "/api/attack", "/api/stream/{type}/{steps}",
                "/api/highlight", "/api/stats",
            ],
        }