Architecture — Tantular Cyber (POC-002 … POC-005)
Run modes (POC-006)
scan(default): deterministic — Semgrep (external, user-installed) + tantular-callgraph. Missing Semgrep or an unsupported platform (native Windows) →degraded, nevercomplete. No model or Ollama involved.scan --ai: adds the advisory LLM stage below against a loopback Ollama endpoint.doctor: read-only capability report (full/partial/unavailable).
Pipeline
authorized local repo (read-only)
│
▼
┌──────────────────────┐ security.py: canonicalize paths, reject output-inside-repo,
│ 1. Inventory │ skip .git / deps / caches / secret-like files / binary /
└──────────┬───────────┘ oversized / symlinks (escaping symlinks flagged separately)
▼
┌──────────────────────┐ scanner.py: Scanner protocol; SemgrepScanner runs
│ 2. Scanners │ `semgrep scan --json --metrics=off --config <local rules> -- <files>`
└──────────┬───────────┘ (argument list, no shell, timeout, cwd=repo, SEMGREP_APP* stripped)
▼
┌──────────────────────┐ pipeline.py + context.py: repo-relative paths, whole-file
│ 3. Normalize │ redaction, exact evidence lines, bounded AST-aware context +
└──────────┬───────────┘ dataflow hints, CWE, severity, deterministic id, "unverified"
▼
┌──────────────────────┐ analysis.py + ollama_client.py: one /api/chat call per finding,
│ 4. LLM explanation │ think=false, stream=false, JSON-schema `format`; output validated,
│ (advisory) │ sanitized, overclaims relocated, CVEs stripped
└──────────┬───────────┘
▼
┌──────────────────────┐ reports.py: report.json (versioned schema) + report.md,
│ 5. Reports │ optional <tool>-raw.json (paths scrubbed)
└──────────────────────┘
Status semantics (report.status, mirrored in the CLI exit code):
| status | exit | meaning |
|---|---|---|
complete |
0 | all scanners succeeded; every finding explained (or --no-llm) |
degraded |
4 | reports written, but LLM unreachable / invalid output / limit hit, or a scanner coverage gap / unverified coverage |
failed |
3 | every scanner failed — findings are missing; report still written |
| — | 2 | invalid input paths or rule files; no report written |
Trust boundaries
| Data | Trust | Handling |
|---|---|---|
| Scanner result (rule, CWE, severity, path, lines) | authoritative | never modified by the LLM |
| Repository file content | untrusted | read-only; redacted before leaving the process; sent to the LLM only as an escaped JSON blob inside <untrusted_data> |
| Scanner messages | untrusted | sanitized (control/bidi chars stripped, secrets redacted, length-bounded) |
| LLM output | untrusted, advisory | JSON-schema validated, sanitized, overclaim guard, verified: false always |
| Ollama model list | untrusted metadata | models with remote_host (cloud) are refused for --ai unless allow_remote; never sent /api/show |
| Third-party plugin manifests/output | untrusted | validated and normalized only; plugin code is never executed (docs/plugin-api.md) |
Prompt-injection resistance does not rely on the model obeying the system prompt.
The structural guarantees are: (1) the system prompt is a constant; (2) repo text cannot
close the <untrusted_data> block; (3) nothing the model says can change scanner facts,
finding status, or report status; (4) model output is only ever rendered as escaped text.
Overclaim guard (analysis.enforce_claim_policy)
The model is known to overstate impact. For each item in confirmed_evidence the guard
checks four claim classes — authentication bypass, data deletion/modification, OS command
execution, verified exploitation — using word-family lexicons in Indonesian and English
(stems such as *hapus*, *hancur*, mengosong*; synonyms such as "login tanpa
password", "masuk sebagai admin", "reverse shell", "server compromise", "exploitation is
confirmed"), proximity matching (up to four words between verb and object), and
normalization (NFKC for fullwidth forms, a Latin homoglyph map for common Cyrillic/Greek
lookalikes, zero-width removal, whitespace collapsing). If a claim appears and the evidence/context does not
contain supporting tokens (e.g. DELETE/DROP/UPDATE for modification,
os.system/subprocess for OS execution), the item is moved to conditional_impact with
a [Dipindahkan …] prefix, confidence is forced to low, and a validation_warnings
entry is recorded. Certainty words (terbukti, dipastikan, proven, …) about such
claims in summary are flagged. CVE identifiers are removed from all model text.
tests/test_overclaims.py holds 36 adversarial paraphrases plus grounded statements that
must not be flagged (e.g. "mengeksekusi perintah SQL"). The guard is still lexical: novel
phrasing outside the lexicons can pass (measured blind spots are listed in
benchmarks/corpus/adversarial/overclaims.json). Extend the lexicon and the tests together.
Rulesets
Five local rulesets (CWE-89, 78, 22, 79, 798), listed in config.DEFAULT_RULESETS. Exact
sinks, sanitizer boundaries and known false positives/negatives: rules.md.
All rules are syntactic and same-scope; none tracks taint across functions or files, and
none knows whether a value is attacker-controlled (hence unverified). The CWE-79 template
rule runs in Semgrep's generic mode on *.html/Jinja files, so coverage verification
treats those extensions as in scope.
Interprocedural scanner (callgraph.py, report schema 1.3)
tantular-callgraph runs after Semgrep as a second Scanner plugin (disable with
--no-callgraph). It is pure Python (ast.parse only; nothing is imported or executed)
and bounded by CallgraphBudget (call depth 3, 2000 files, 20000 functions, 8 trace steps).
- Index: every in-scope
.pyfile becomes a module (a/b/c.py→a.b.c); top-level functions and class methods are indexed with their imports (absolute and relative). - Resolution (conservative): a call is followed only if it resolves to exactly one
repository function — same-module name, explicit import, or
self.methodwithin the defining class (flagged: subclass overrides are not considered). Dynamic dispatch (table[k](x)), ambiguous redefinitions, star imports and external calls are unresolved: counted inscanners[].stats, never turned into findings. - Taint: sources are function parameters (low confidence) and request inputs such as
request.args,request.GET,sys.argv,input()(medium). A finding needs a value that was built (f-string,+,%,.format,str.join,os.path.join,Path / x) — or raw request input — reaching a sink. Sanitizers (int,shlex.quote,basename,secure_filename,escape) and UPPER_CASE constants clear taint. - Summaries: per function, which parameters flow to the return value and which reach a
sink unbuilt; recomputed for up to
max_depthrounds, so flows crossing more calls than the budget are not reported (stats.depth_limited). - Sinks: CWE-89
execute*on DB-like receivers (name/annotation/.cursor()origin), CWE-78shell=True/os.system/["sh", "-c", ...], CWE-22open/send_file/Path.read_*on composed paths. CWE-79/798 stay with Semgrep. - Evidence:
findings[].call_path(source → build → call → return → sink, each with the redacted source line) andfindings[].uncertainty. The LLM receives both as untrusted data and is told the path is not proof of exploitability. - Precedence: scanner order is precedence. A callgraph finding with the same file, CWE
and overlapping lines as a Semgrep finding is dropped (
suppressed_duplicates). - A file Semgrep scans but the callgraph cannot parse is a coverage gap for that scanner
(
python_syntax_error); if only one scanner fails the report isdegraded, notfailed.
Evidence context (context.py, report schema 1.2)
Before POC-004 the context was a fixed ±3-line window, which missed values built further up (e.g. a multi-line query whose tainted part is 5 lines above the sink). Now:
- The original file is parsed with
ast.parsefor structure only (nothing executed). All displayed lines come from the whole-file-redacted text; redaction preserves the line count, and any mismatch, non-Python file or syntax error falls back to the window (context_fallbackrecords why). - The innermost enclosing function is shown whole if it fits; otherwise its header, the
full (multi-line) finding statement, ±2 lines, and the statements that bind names used
at the finding (plus one transitive step) — compound statements contribute only their
header lines. Gaps are rendered as
…. - One level of same-file helpers called at the finding or in those assignments is
included (clipped to
helper_max_lines). dataflow_hintssummarise parameters, local assignments and helpers. They are sent to the model asdataflow_hints_unverifiedinside the untrusted-data block, and the system prompt states they are not proof of taint.- Budgets (
ContextBudget: 60 lines, 4000 chars by default; ≈ chars/4 tokens) are hard: lines farthest from the finding are dropped first, then kept lines other than the finding line, then a hard character cut.context_truncatedflags any reduction.
--context-strategy window restores the POC-002 behavior. The benchmark measures both
strategies on every detected location (construct coverage, size, budget) — see
benchmark.md.
Scan scope and coverage verification
Repository-controlled files must not be able to shrink the scan silently.
- Target selection is done by
security.collect_files, never by the tools. Repo.semgrepignore/.gitignorefiles are not consulted; files are passed to Semgrep explicitly after--. (Verified on Semgrep 1.143: explicit targets are scanned even when a.semgrepignorematches them;tests/test_coverage.pykeeps this true.) - Coverage check: from the rules'
languages:the scanner derives which submitted targets are in scope (by extension), then compares them with the tool's ownpaths.scanned. Any in-scope file the tool did not scan is listed inscanners[].coverage.not_scanned(with the tool's skip reason when available), recorded as an error, and makes the reportdegraded. If the tool does not report scanned paths, coverage isverified: falseand the report isdegradedas well. - Reported scope:
target.ignore_files_presentlists repo ignore files found;skippedlists every file excluded by the inventory policy, with reasons (capped list in JSON, first 50 in Markdown). - Out-of-scope files (no rule language applies) are counted, not flagged. Python files
without a
.py/.pyiextension (e.g. extensionless scripts) are out of scope for Semgrep and therefore for this ruleset.
Secret redaction (security.redact)
Applied to the whole file before evidence and context lines are cut, and again to all scanner messages and model output. Every rule preserves the newline count, so scanner line numbers stay valid. Layers:
Known credential formats: private-key blocks (multiline), AWS, GitHub, Slack, Google,
sk-API keys, JWTs, bearer tokens, credentials in URLs.Encoded: HTTP Basic credentials (base64
user:pass); base64 blobs whose decoded content matches a known format.Contextual: quoted, triple-quoted (multiline), type-annotated, and unquoted (
.env/YAML style, must contain a digit or symbol) values assigned to credential-like names; credentials written in comments (# admin password is …,// api key: …,# kata sandi: …).Obfuscated names: texts containing zero-width/bidi characters or Latin lookalikes are also matched on a skeleton (invisibles removed, lookalikes mapped to Latin, shared
CONFUSABLEStable) with a position map; only the value span is replaced in the original, so evidence keeps its real characters.
Limits: heuristic. Plain-letter unquoted values (api_key: abcdefgh), hex-encoded or
custom-encoded secrets, and secrets in variables with innocuous names are not detected.
Secret-like files (.env, *.pem, …) are never read at all.
Lines are split on \n only (like Semgrep). str.splitlines() also splits on form feeds
and U+2028, which would let a crafted file shift the displayed evidence line.
Extension points
- New scanner — implement
scanner.Scanner(name,version(),scan(repo, targets)returningScannerRunwithRawFindings) and pass it inrun_scan(scanners=[...])/ wire it inpipeline.run_scan. Normalization, evidence, LLM and reports are reused. In-tree scanners are trusted code. External scanners follow the plugin API 1.0 contract (plugin-api.md): a declarative manifest,plugin.<id>.rule namespace andnormalize_plugin_output; their execution stays disabled until an enforceable sandbox exists. - New LLM / cybersecurity model — implement
analysis.LLMClient(provider,model,chat(messages, format_schema)), optionallyhealth_check()andclose(), and pass a factory viarun_scan(llm_client_factory=...). - New rules — add local YAML under
src/tantular_cyber/rules/or pass--rules.
GATRA integration contract
report.json is the integration surface. It is described by
report-schema.json (generated from models.ScanReport; a test
fails if it drifts). Consumers should:
- check
schema_version(semver; breaking changes bump the major version); - key findings by
findings[].id(stable across runs for unchanged code); - treat
status != "complete"as partial data and readstatus_reasons/errors; - treat every
analysisblock as advisory (verifiedis alwaysfalse).
No GATRA code is modified by this POC.