mirror of
https://github.com/usestrix/strix.git
synced 2026-08-21 10:48:59 +02:00
Replaces 200+ lines of bespoke env-loader / persist / change-detection
machinery with ``pydantic_settings.BaseSettings`` (already a transitive
of ``openai-agents → mcp``, no new direct dep).
What was wrong with ``Config``:
- 14 knobs flat in one namespace, weak grouping by comment-block.
- ``Config._applied_from_default`` and ``Config._config_file_override``
were externally mutated from ``interface/main.py:532-534``. Private
members were part of the public contract.
- Stringly-typed values: every caller had to coerce
(``int(Config.get("llm_timeout") or "300")``,
``... not in {"0", "false", "no", "off"}``).
- Dead knob: ``strix_llm_max_retries`` declared, persisted, listed in
``_LLM_CANONICAL_NAMES`` — zero readers (``DEFAULT_RETRY``
hardcodes ``max_retries=5``). Dropped.
- ``_LLM_CANONICAL_NAMES`` tuple maintained alongside class vars —
duplicate source of truth.
- ``_tracked_names()`` introspected ``vars(cls).items()`` filtered on
``(v is None or isinstance(v, str))`` — fragile.
- Awkward path: ``strix/config/config.py`` inside ``strix/config/``
with ``__init__.py`` just re-exporting.
- Dual access for the same fact: ``web_search`` read
``os.getenv("PERPLEXITY_API_KEY")`` while ``main.py`` read
``Config.get("perplexity_api_key")``.
New shape:
- ``strix/config/settings.py`` — typed dataclass tree:
``Settings.{llm,runtime,telemetry,integrations}``. Each sub-model is
its own ``BaseSettings`` so it reads env independently. Field-level
``alias=`` and ``validation_alias=AliasChoices(...)`` mirror the
existing flat env-var names — user-facing env contract is unchanged.
Bool fields auto-parse ``"0"``/``"false"``/``"no"``/``"off"``;
int fields auto-coerce.
- ``strix/config/loader.py`` — thin ``load_settings()``,
``apply_config_override(path)``, ``persist_current()`` with module
cache. JSON file reader walks aliases to populate sub-models, dropping
entries already covered by env (so env still wins).
- 13 callsites migrated from ``Config.get("...")`` to
``load_settings().<group>.<field>``.
- ``posthog._is_enabled()`` collapses to one line.
- ``--config <path>`` flow simplified: one
``apply_config_override(...)`` call replaces three lines of
class-private mutation.
Drive-by — drop ``is_whitebox`` from ``scan_config`` dict:
- It was being derived as ``bool(args.local_sources)`` in three places
(``cli.py``, ``tui.py``, ``main.py``) and stuffed into the dict for
``entry.py`` to read back. The fact is fully derivable from
``scan_config["targets"]`` — any target with ``type == "local_code"``.
- New helper ``is_whitebox_scan(targets)`` in ``interface/utils.py``
alongside the other target-classification utilities.
- ``entry.py`` computes once; ``main.py``'s posthog start uses the same
helper. Triplicate derivation gone.
Verified: ruff at baseline (3), mypy at baseline (69). Six smoke tests
pass — defaults / JSON-only / env-wins-over-JSON / alias-chain
fallback / bool parsing / ``is_whitebox_scan``.
238 lines
7.7 KiB
Python
238 lines
7.7 KiB
Python
"""LLM-based vulnerability-report deduplication.
|
|
|
|
Routes through the same :class:`MultiProvider` (so ``anthropic/...``
|
|
models pick up :class:`AnthropicCachingLitellmModel`'s cache_control
|
|
patching) and :data:`DEFAULT_RETRY` policy as the main agent loop —
|
|
no parallel litellm code path.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from agents.model_settings import ModelSettings
|
|
from agents.models.interface import ModelTracing
|
|
from openai.types.responses import ResponseOutputMessage
|
|
|
|
from strix.config import load_settings
|
|
from strix.llm.multi_provider_setup import build_multi_provider
|
|
from strix.run_config_factory import DEFAULT_RETRY
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
from agents.items import ModelResponse
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
DEDUPE_SYSTEM_PROMPT = """You are an expert vulnerability report deduplication judge.
|
|
Your task is to determine if a candidate vulnerability report describes the SAME vulnerability
|
|
as any existing report.
|
|
|
|
CRITICAL DEDUPLICATION RULES:
|
|
|
|
1. SAME VULNERABILITY means:
|
|
- Same root cause (e.g., "missing input validation" not just "SQL injection")
|
|
- Same affected component/endpoint/file (exact match or clear overlap)
|
|
- Same exploitation method or attack vector
|
|
- Would be fixed by the same code change/patch
|
|
|
|
2. NOT DUPLICATES if:
|
|
- Different endpoints even with same vulnerability type (e.g., SQLi in /login vs /search)
|
|
- Different parameters in same endpoint (e.g., XSS in 'name' vs 'comment' field)
|
|
- Different root causes (e.g., stored XSS vs reflected XSS in same field)
|
|
- Different severity levels due to different impact
|
|
- One is authenticated, other is unauthenticated
|
|
|
|
3. ARE DUPLICATES even if:
|
|
- Titles are worded differently
|
|
- Descriptions have different level of detail
|
|
- PoC uses different payloads but exploits same issue
|
|
- One report is more thorough than another
|
|
- Minor variations in technical analysis
|
|
|
|
COMPARISON GUIDELINES:
|
|
- Focus on the technical root cause, not surface-level similarities
|
|
- Same vulnerability type (SQLi, XSS) doesn't mean duplicate - location matters
|
|
- Consider the fix: would fixing one also fix the other?
|
|
- When uncertain, lean towards NOT duplicate
|
|
|
|
FIELDS TO ANALYZE:
|
|
- title, description: General vulnerability info
|
|
- target, endpoint, method: Exact location of vulnerability
|
|
- technical_analysis: Root cause details
|
|
- poc_description: How it's exploited
|
|
- impact: What damage it can cause
|
|
|
|
Respond with a single JSON object and nothing else:
|
|
|
|
{
|
|
"is_duplicate": true,
|
|
"duplicate_id": "vuln-0001",
|
|
"confidence": 0.95,
|
|
"reason": "Both reports describe SQL injection in /api/login via the username parameter"
|
|
}
|
|
|
|
Or, if not a duplicate:
|
|
|
|
{
|
|
"is_duplicate": false,
|
|
"duplicate_id": "",
|
|
"confidence": 0.90,
|
|
"reason": "Different endpoints: candidate is /api/search, existing is /api/login"
|
|
}
|
|
|
|
Rules:
|
|
- ``is_duplicate`` is a boolean.
|
|
- ``duplicate_id`` is the exact id from existing reports, or "" if not a duplicate.
|
|
- ``confidence`` is a number between 0 and 1.
|
|
- ``reason`` is a specific explanation mentioning endpoint/parameter/root cause.
|
|
- Output ONLY the JSON object — no surrounding prose, no code fences."""
|
|
|
|
|
|
def _prepare_report_for_comparison(report: dict[str, Any]) -> dict[str, Any]:
|
|
relevant_fields = [
|
|
"id",
|
|
"title",
|
|
"description",
|
|
"impact",
|
|
"target",
|
|
"technical_analysis",
|
|
"poc_description",
|
|
"endpoint",
|
|
"method",
|
|
]
|
|
|
|
cleaned = {}
|
|
for field in relevant_fields:
|
|
if report.get(field):
|
|
value = report[field]
|
|
if isinstance(value, str) and len(value) > 8000:
|
|
value = value[:8000] + "...[truncated]"
|
|
cleaned[field] = value
|
|
|
|
return cleaned
|
|
|
|
|
|
def _parse_dedupe_response(content: str) -> dict[str, Any]:
|
|
text = content.strip()
|
|
if text.startswith("```"):
|
|
text = text.strip("`")
|
|
if text.lower().startswith("json"):
|
|
text = text[4:]
|
|
text = text.strip()
|
|
start = text.find("{")
|
|
end = text.rfind("}")
|
|
if start == -1 or end == -1 or end <= start:
|
|
raise ValueError(f"No JSON object found in dedupe response: {content[:500]}")
|
|
parsed = json.loads(text[start : end + 1])
|
|
|
|
duplicate_id = str(parsed.get("duplicate_id") or "")[:64]
|
|
reason = str(parsed.get("reason") or "")[:500]
|
|
try:
|
|
confidence = float(parsed.get("confidence", 0.0))
|
|
except (TypeError, ValueError):
|
|
confidence = 0.0
|
|
|
|
return {
|
|
"is_duplicate": bool(parsed.get("is_duplicate", False)),
|
|
"duplicate_id": duplicate_id,
|
|
"confidence": confidence,
|
|
"reason": reason,
|
|
}
|
|
|
|
|
|
def _extract_text(response: ModelResponse) -> str:
|
|
"""Concatenate ``output_text`` fragments across every message item.
|
|
|
|
The SDK returns OpenAI Responses-API-shaped output; for a plain
|
|
chat-completion the assistant message has a list of content parts,
|
|
each of which carries a ``.text`` attribute we can pull verbatim.
|
|
"""
|
|
parts: list[str] = []
|
|
for item in response.output:
|
|
if not isinstance(item, ResponseOutputMessage):
|
|
continue
|
|
for chunk in item.content:
|
|
text = getattr(chunk, "text", None)
|
|
if text:
|
|
parts.append(text)
|
|
return "".join(parts)
|
|
|
|
|
|
async def check_duplicate(
|
|
candidate: dict[str, Any], existing_reports: list[dict[str, Any]]
|
|
) -> dict[str, Any]:
|
|
if not existing_reports:
|
|
return {
|
|
"is_duplicate": False,
|
|
"duplicate_id": "",
|
|
"confidence": 1.0,
|
|
"reason": "No existing reports to compare against",
|
|
}
|
|
|
|
try:
|
|
model_name = load_settings().llm.model
|
|
if not model_name:
|
|
return {
|
|
"is_duplicate": False,
|
|
"duplicate_id": "",
|
|
"confidence": 0.0,
|
|
"reason": "STRIX_LLM not configured; skipping dedupe check",
|
|
}
|
|
|
|
candidate_cleaned = _prepare_report_for_comparison(candidate)
|
|
existing_cleaned = [_prepare_report_for_comparison(r) for r in existing_reports]
|
|
comparison_data = {"candidate": candidate_cleaned, "existing_reports": existing_cleaned}
|
|
|
|
user_msg = (
|
|
f"Compare this candidate vulnerability against existing reports:\n\n"
|
|
f"{json.dumps(comparison_data, indent=2)}\n\n"
|
|
f"Respond with ONLY the JSON object described in the system prompt."
|
|
)
|
|
|
|
model = build_multi_provider().get_model(model_name)
|
|
response = await model.get_response(
|
|
system_instructions=DEDUPE_SYSTEM_PROMPT,
|
|
input=user_msg,
|
|
model_settings=ModelSettings(retry=DEFAULT_RETRY),
|
|
tools=[],
|
|
output_schema=None,
|
|
handoffs=[],
|
|
tracing=ModelTracing.DISABLED,
|
|
previous_response_id=None,
|
|
conversation_id=None,
|
|
prompt=None,
|
|
)
|
|
content = _extract_text(response)
|
|
if not content:
|
|
return {
|
|
"is_duplicate": False,
|
|
"duplicate_id": "",
|
|
"confidence": 0.0,
|
|
"reason": "Empty response from LLM",
|
|
}
|
|
|
|
result = _parse_dedupe_response(content)
|
|
|
|
logger.info(
|
|
"Deduplication check: is_duplicate=%s, confidence=%.2f, reason=%s",
|
|
result["is_duplicate"],
|
|
result["confidence"],
|
|
result["reason"][:100],
|
|
)
|
|
|
|
except Exception as e:
|
|
logger.exception("Error during vulnerability deduplication check")
|
|
return {
|
|
"is_duplicate": False,
|
|
"duplicate_id": "",
|
|
"confidence": 0.0,
|
|
"reason": f"Deduplication check failed: {e}",
|
|
"error": str(e),
|
|
}
|
|
else:
|
|
return result
|