mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-10-18 04:29:27 +00:00
3.1 adds StaticFinding.rung beside evidence_level rather than renaming it. The existing evidence_level carries `static_corroboration`, emitted by the flat IR overlay -- a path Req 4.4 demotes to the suspicion band. Restating that as model_corroborated would claim a CPG arbitrated something it never saw, and rewriting every legacy finding to suspicion would be a product-visible regression nobody asked for. So `rung` defaults to suspicion and is raised only by ladder.at_rung(finding, verdict), which is the structural form of Req 5.4: nothing rises above suspicion without a Verdict behind it. The witness travels with the rung, because a rung without its evidence is just a louder assertion. It reaches markdown, JSON and SARIF. 3.2 is the judge, and it is defined as much by what it lacks. Three exhaustive outcomes: the model entails (report it, never call the model), the model contradicts (drop the claim, record why), the model cannot decide (report suspicion). The LLM gets one bounded typed question with tools=[] so it cannot search for the site, asked only about the residual the graph cannot settle -- whether a sanitizer on a REAL flow suffices. Its answer can only confirm a flow the CPG already found; it can never conjure one. Req 8.5 is asserted structurally: a test parses judge.py and fails on any AST name like k_runs/majority/vote/average/acceptance/floor/budget, any for/while loop, or an import from the deleted round machinery. That test first matched the word "votes" in the module's own docstring, which is why it now inspects code rather than text. Verified live against a real CPG, not only scripted rows: vuln rung=model_entailed request.args["name"] -> db.execute(...) fixed rung=suspicion no source reaches this sink model calls made: 0 Both sides arbitrated by the graph with the LLM never consulted -- the thing the prior architecture could not do at any price. 678 passing, mypy/ruff clean, gates byte-identical, zero orphaned modules. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01T7XD7x5KonMdmGRkp63EQG
129 lines
5.2 KiB
Python
129 lines
5.2 KiB
Python
"""The LLM proposes; the model disposes (model-grounded-detection, Req 8).
|
|
|
|
This module is where the prior architecture's premise is inverted. There, the LLM was an oracle whose noise
|
|
had to be averaged over K runs, floored per family, and gated by a sign test. Here it answers one bounded,
|
|
typed question about a candidate it was *handed*, and its answer is checked against the CPG. Three outcomes,
|
|
and they are exhaustive:
|
|
|
|
the model entails it report `model_entailed`, and never call the model at all
|
|
the model contradicts it drop the claim, recording the contradiction
|
|
the model cannot decide report `suspicion`, honestly
|
|
|
|
Nothing here averages, votes, retries for agreement, or maintains a budget: a deterministic arbiter removes
|
|
the need for every one of those. The LLM is consulted only for the residual the graph cannot settle — whether
|
|
a sanitizer on a real flow is actually sufficient — and even that answer only ever *confirms* a flow the CPG
|
|
already found. It can never conjure one.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
from collections.abc import Mapping
|
|
from typing import Any
|
|
|
|
from ..cpg.backend import CpgResult
|
|
from ..redaction import redact_secrets
|
|
from .ladder import Rung, Verdict
|
|
from .specs import TaintSpec
|
|
from .taint import verdict as taint_verdict
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
QUESTION = """You are judging ONE candidate a static model already found. Do not look for other issues.
|
|
|
|
A dataflow analysis resolved this path in {language} code:
|
|
|
|
source: {source}
|
|
sink: {sink} (line {line}, function {function})
|
|
a sanitizer appears on the path: {sanitized}
|
|
|
|
The graph has established that the value reaches the sink. The only open question is whether the sanitizer on
|
|
that path actually makes it safe for the {family} weakness class.
|
|
|
|
Answer ONLY with JSON: {{"vulnerable": true|false, "why": "<one sentence>"}}"""
|
|
|
|
|
|
def judge(
|
|
cpg: CpgResult,
|
|
spec: TaintSpec,
|
|
*,
|
|
function: str = "",
|
|
client: Any | None = None,
|
|
model: str = "",
|
|
parameter_sources: bool = False,
|
|
) -> Verdict:
|
|
"""Arbitrate one candidate. Always returns a verdict; ``suspicion`` is a real answer, not a failure."""
|
|
answer = taint_verdict(cpg, spec, function=function, parameter_sources=parameter_sources)
|
|
|
|
if answer is not None and answer.rung is Rung.ENTAILED:
|
|
return answer # the graph decided; the model is not consulted
|
|
|
|
if answer is None:
|
|
# No resolved flow. Whatever anyone claims about this site, the model saw nothing to support it.
|
|
return Verdict(
|
|
rung=Rung.SUSPICION,
|
|
family=spec.family,
|
|
contradiction="the model resolved no flow: no source reaches this sink",
|
|
)
|
|
|
|
# A real flow through a sanitizer. Whether that sanitizer suffices is a semantic judgement the graph
|
|
# cannot make, so it is the one thing the LLM is asked -- about evidence the model itself produced.
|
|
if client is None:
|
|
return Verdict(
|
|
rung=Rung.SUSPICION,
|
|
family=spec.family,
|
|
witness=answer.witness,
|
|
contradiction="no chat endpoint: the sanitizer's sufficiency was never judged",
|
|
)
|
|
|
|
said_vulnerable = _ask(client, model, spec, answer.witness, function)
|
|
if said_vulnerable is None:
|
|
return Verdict(rung=Rung.SUSPICION, family=spec.family, witness=answer.witness,
|
|
contradiction="the judge returned no usable answer")
|
|
if not said_vulnerable:
|
|
return Verdict(rung=Rung.SUSPICION, family=spec.family, witness=answer.witness,
|
|
contradiction="the judge held the sanitizer on this path sufficient")
|
|
return answer # CORROBORATED: a real flow the judge agrees is not neutralised
|
|
|
|
|
|
def _ask(client: Any, model: str, spec: TaintSpec, witness: str, function: str) -> bool | None:
|
|
"""One question, one answer. No tools, no retries for agreement, no second opinion."""
|
|
source, _, sink = witness.partition(" -> ")
|
|
line = ""
|
|
if "(line " in sink:
|
|
sink, _, tail = sink.partition(" (line ")
|
|
line = tail.split(",")[0].strip(") ")
|
|
prompt = redact_secrets(
|
|
QUESTION.format(
|
|
language=spec.language, source=source.strip(), sink=sink.strip(), line=line or "?",
|
|
function=function or "?", sanitized="yes", family=spec.family,
|
|
)
|
|
)
|
|
try:
|
|
response = client.complete(
|
|
model=model,
|
|
messages=[{"role": "user", "content": prompt}],
|
|
tools=[], # the candidate is given; the model never searches for it
|
|
json_object=True,
|
|
)
|
|
except Exception as exc: # noqa: BLE001 -- an endpoint failure is a degradation, never a verdict
|
|
logger.warning("judge call failed: %s", exc)
|
|
return None
|
|
return _vulnerable(getattr(response, "content", None))
|
|
|
|
|
|
def _vulnerable(content: object) -> bool | None:
|
|
if not isinstance(content, str) or not content.strip():
|
|
return None
|
|
try:
|
|
payload = json.loads(content)
|
|
except json.JSONDecodeError:
|
|
return None
|
|
if isinstance(payload, Mapping) and isinstance(payload.get("vulnerable"), bool):
|
|
return bool(payload["vulnerable"])
|
|
return None
|
|
|
|
|
|
__all__ = ["QUESTION", "judge"]
|