AI for source-code vulnerability research, with Semgrep and CodeQL as ground truth
How to use an AI agent for vulnerability research and source-code auditing without drowning in false positives: static analysis as the ground truth, the agent for triage, taint tracing, custom Semgrep rules, confirming with a PoC on a local build, and responsible disclosure.
· 9 min read

An AI agent is a strong vulnerability-research teammate and a terrible lead investigator. Point it at a codebase and ask "find the bugs" and you'll get a page of confident, mostly-wrong findings: the model pattern-matches on things that look dangerous and calls them vulnerabilities. Give it a structured job — trace this tainted input, read this call chain, write a rule for this pattern, confirm this finding with a proof of concept — and it's genuinely fast and useful.
The frame that makes it work: static analysis tools are the ground truth, and the agent is the triage layer on top of them. Semgrep and CodeQL find candidate issues deterministically and repeatably. The model's job is to read what they surface, follow the data, kill the false positives, and confirm the real ones — not to replace the scanner with a vibe.
Everything below uses a codebase you check out yourself. Audit code you own, maintain, or have permission to audit — or open-source code you intend to responsibly disclose to. Running a scanner over someone's proprietary source you don't have rights to is its own problem; this is about open source and your own code.
Why static analysis first, not the model first
A model has no ground truth about a codebase. It can't run the type checker, doesn't know which inputs are attacker-controlled, and will happily flag a system() call that only ever receives a compile-time constant. Semgrep and CodeQL do have ground truth: they parse the code, build a model of it, and match patterns (Semgrep) or answer queries over a database (CodeQL), the same way every time. They have false positives too, but they're deterministic false positives you can reason about and suppress.
So the scanners set the agenda. The model triages their output — which is a much easier, better-defined job than "find all bugs", and the one models are actually good at.
Get the scanners running
Check out your target and get a baseline:
git clone https://github.com/<org>/<project> && cd <project>
# Semgrep with the community rule registry
semgrep --config=auto --sarif -o semgrep.sarif .
# or a focused ruleset
semgrep --config=p/security-audit --config=p/secrets .
For CodeQL, build a database (the language determines whether you need a build command) and run the standard security queries:
codeql database create db --language=cpp --command="make" # compiled langs need the build
codeql database create db --language=javascript # interpreted langs don't
codeql database analyze db --format=sarif-latest -o codeql.sarif \
codeql/<lang>-queries:codeql-suites/<lang>-security-extended.qls
Both emit SARIF, a standard JSON format for findings. SARIF is what you hand the agent — it's structured, it has file-and-line locations, and the model can read it without you pasting code.
The agent's real job: triage
Give the model the SARIF and a tight instruction set. A good opening prompt:
Here is
semgrep.sarifandcodeql.sariffor this checkout. For each finding: (1) classify it reachable or not-reachable from attacker-controlled input, (2) if reachable, trace the data from source to sink citing file:line at each hop, (3) if not reachable, say why in one line. Do not claim a vulnerability without a complete source-to-sink path. Group obvious false positives separately.
That prompt encodes the three things that make AI triage work.
1. Taint tracing from source to sink
The core of source-code auditing is following attacker-controlled data from where it enters (a request parameter, a file, an environment variable, argv) to where it does damage (a query, a command, a buffer, a deserializer). Models are good at this when made to cite every hop, because citing forces them to actually read the intervening code instead of assuming the two ends connect.
Make "source to sink, with file:line at each step" the required format for any finding. A finding without a complete path is a lead to investigate, not a result to report. This single rule kills most of the model's false positives, because most of them are a scary sink with no actual path from a source.
2. Reading call chains
Scanners flag a line; whether it's exploitable usually lives several calls away. "Who can reach this function with controlled arguments?" is a question models answer well, especially with tools. Have the agent build the call chain explicitly — with ripgrep, with the language server, or with a CodeQL query — rather than reasoning about it from memory:
# every caller of a sink, as a starting point for the chain
rg -n 'dangerous_parse\(' --type c
For non-trivial chains, a custom CodeQL query is more reliable than the model walking callers by hand; which brings us to the next job.
3. Writing custom Semgrep rules
When you find a pattern worth hunting across the whole codebase — a project-specific unsafe wrapper, a validation function that's sometimes skipped — the model is good at turning one example into a Semgrep rule. Semgrep's YAML is simple and the model can iterate against real output:
# sprintf-into-fixed-buffer.yml
rules:
- id: sprintf-into-fixed-buffer
languages: [c]
message: sprintf into a fixed-size stack buffer; check the format's max length
severity: WARNING
patterns:
- pattern: sprintf($BUF, ...)
- metavariable-pattern:
metavariable: $BUF
patterns:
- pattern-either:
- pattern: $T $BUF[$N];
semgrep --config=sprintf-into-fixed-buffer.yml .
Then iterate: run the rule, read the hits with the model, tighten the pattern to cut noise, rerun. A custom rule is testable and repeatable in a way a chat answer never is — that's why it's worth having the model write a rule instead of just answering the question once. Taint-mode rules (mode: taint with pattern-sources and pattern-sinks) encode the source-to-sink idea directly and are worth having the model draft for your project's specific sources.
Killing false positives: the model's biggest failure mode
This is the section that matters most, because unchecked, an AI auditor's output is mostly false positives dressed as findings. The discipline that fixes it:
- No path, no finding. If the model can't trace attacker input to the sink, it's not a vulnerability, it's a lead. Demote it.
- Make it argue the negative. "Explain why this is not exploitable" catches more false positives than "is this exploitable?", because it forces the model to find the guard, the sanitiser or the constant input it glossed over.
- Check the guards. Most scanner false positives are real sinks that are actually guarded upstream — a length check, a type constraint, an allowlist. The model has to read the guard and say why it is or isn't sufficient.
- A real finding has a PoC. The ultimate false-positive filter is the next section: if you can't trigger it on a local build, treat the finding as unconfirmed.
Confirming with a PoC on a local build
A finding you haven't triggered is a hypothesis. Build the target locally and make the model prove it:
# build with sanitizers so a real bug shows loudly
CFLAGS="-fsanitize=address,undefined -g" ./configure && make
Then construct the smallest input that reaches the sink with controlled data, and run it. For a memory-safety bug, AddressSanitizer turning a silent corruption into a precise crash report is the confirmation. For an injection, it's the payload actually changing the query or command. Have the model write the trigger and predict what it expects to see; a wrong prediction usually means the path it traced was wrong.
If the bug is reachable only through fuzzing-style input exploration, a fuzz harness is the right confirmation tool — see the fuzzing guide for writing one with an agent. A finding confirmed by a sanitizer crash on a minimized input is a real finding; one that only exists in the model's explanation is not.
Keeping the model honest
Put these in your project instructions (CLAUDE.md, AGENTS.md or CAVEIRA.md), the same way you would for any audit:
- Cite file:line for every hop in a data-flow claim.
- Mark each finding confirmed (a PoC triggers it) or suspected (traced but not triggered).
- Never report a finding without a complete source-to-sink path.
- Prefer a Semgrep/CodeQL rule to a one-off answer, so the check is repeatable.
- Keep a
findings.mdwith the status of each candidate, so a rerun doesn't re-litigate the dropped ones.
Responsible disclosure
Finding a real vulnerability in software you don't maintain carries an obligation. Before you go looking, and certainly before you write anything up:
- Confirm it's real and your own PoC is minimal — no collateral, no live third-party systems, just your local build.
- Find the disclosure channel. Check for a
SECURITY.md, a security contact, or asecurity.txt; many projects use a coordinated-disclosure platform. If there's a bug-bounty program, its scope and rules are your authorization — stay inside them. - Report privately first and give a reasonable time to fix (90 days is the common norm) before any public write-up.
- Include what a maintainer needs: affected versions, the root cause, the minimal reproduction, and the impact — written plainly, which is something the model is genuinely good at drafting from your confirmed finding.
- Don't test against production or other people's deployments. The vulnerability is in the code; prove it against code you run.
A model can draft the advisory, suggest the fix, and write the reproduction steps cleanly. It cannot decide that something is safe to disclose or that you're authorized to test where you tested — those are yours.
Where caveira fits
Any agent that runs shell commands can drive Semgrep and CodeQL, read SARIF, and triage findings: Claude Code, Codex, OpenCode and caveira among them. Two things matter for this work specifically. Refusals: deep vulnerability research — especially writing a working PoC for a memory-corruption bug — is exactly the kind of legitimate work mainstream assistants sometimes decline. And a workflow that queries a large codebase rather than pasting it, which is where long context and good tool use pay off.
caveira is built for this: its backend serves uncensored, abliterated open-weight models, so it works through PoC development without lecturing; it ships an optional Docker box with a C/C++ toolchain, gdb, radare2 and more for building and triaging locally; and it speaks MCP, so a code-analysis server you already use plugs in. It's one capable option, not a replacement for knowing Semgrep, CodeQL, and how to read code — the tools are the ground truth, whichever agent reads their output.