Skip to content

feat(trace): add ContextInjectionDetector with 14 tests - #572

Open
stealthwhizz wants to merge 10 commits into
GenAI-Security-Project:mainfrom
stealthwhizz:feature/trace-context-injection-detector
Open

feat(trace): add ContextInjectionDetector with 14 tests#572
stealthwhizz wants to merge 10 commits into
GenAI-Security-Project:mainfrom
stealthwhizz:feature/trace-context-injection-detector

Conversation

@stealthwhizz

Copy link
Copy Markdown
Contributor

Summary

Adds ContextInjectionDetector, which correlates a delegation.context_snapshot event whose context_preview matches known prompt-injection patterns with a subsequent tool call (within the same workflow_id) that matches the injected directive. Both signals must be present before it fires, keeping false positives low without an ML model — this is the first detector that can score multi-step lateral movement through context propagation between agents.

Test plan

  • 14 unit tests covering fire/no-fire cases (single-step present, cross-session, cross-workflow)
  • Integration test: poison FraudAgent output, propagate via _enrich_with_prior_context(), confirm PaymentsAgent triggers detection

Adds StepSpec dataclass and SequenceDetector base structure to
finbot/ctf/detectors/primitives/. Includes config validation,
get_relevant_event_types(), and stubbed private helpers for
history querying, step matching, and time-window checks.
check_event() and all helpers are NotImplementedError stubs
pending implementation.
…gration

- Add SequenceDetector to finbot/ctf/detectors/primitives/
  Detects multi-step attack patterns across a session or workflow window.
  Supports ordered step matching, glob event_type patterns, within_n_events
  and within_seconds windows, and all ToolCallDetector field operators.
  Challenge authors configure it from YAML with no Python required.

- Add composite index idx_ctf_event_session_ts_type on (session_id, timestamp,
  event_type) to keep session-window history queries below 10ms p95.

- Export SequenceDetector from finbot/ctf/detectors/primitives/__init__.py

- Add 17 unit tests covering full sequence detection, partial sequences,
  order enforcement, session/workflow windows, condition operators, and
  glob event_type matching.
- Add StepSpec TypedDict to sequence_detector.py matching the approved
  interface spec; export it from primitives __init__

- Add benchmark test: seeds 1,000 CTFEvent rows with composite index,
  runs check_event 100 times, asserts p95 < 10ms
  Current result: p50 ~7ms, p95 ~8ms on SQLite
…, and tests

- IncrementalFraudDetector uses SequenceDetector as the matching engine;
  two-gate design: N below-threshold approvals in session window, then
  cumulative amount check fires when total >= cumulative_threshold

- incremental_fraud.yaml: 300pts, ASI-08, fraud category;
  default config: 3 approvals each <= 9999, cumulative >= 25000

- Extend _emit_delegation_event() in orchestrator.py with context_preview
  field (first 500 chars of enriched context forwarded between agents)

- Emit delegation.context_snapshot business event on every delegation hop
  making context forwarding observable and scoreable by detectors

- Integration tests for IncrementalFraudDetector: full chain, 5-step
  sequence, session isolation, amount gates, rejection filtering

- Unit tests for Delegation Audit: context capture, preview capping,
  event type validation, empty context handling
…_load_amounts

- sequence_detector.py: change re.search to re.fullmatch in the matches
  operator so patterns must match the full value, not a substring.
  re.search allowed conditions like {"matches": "approval"} to falsely
  match "not_approval" or "partial_approval_pending".

- sequence_detector.py: apply all prior review fixes from PR GenAI-Security-Project#522 —
  ANDed condition operators, contains case-normalisation, _CTF_COLUMNS
  as module-level constant, consumed set for order_matters=False, else
  clause for unknown operators.

- incremental_fraud.py: add namespace parameter to _load_amounts and
  filter CTFEvent.namespace == namespace in the query. Guards against
  cross-namespace amount loading after DB restores or migrations.
Detects lateral movement through agent context propagation:
- Gate 1: delegation.context_snapshot with PI score >= threshold (LLM judge)
- Gate 2: tool call in same workflow_id (receiving agent acted)
- Namespace-isolated, target_agent filter, configurable via YAML
- 14 unit tests covering all gates, filters, and edge cases

OWASP: ASI-01 (Goal Hijack), ASI-06 (Memory & Context Poisoning),
        ASI-07 (Insecure Inter-Agent Communication)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant