Skip to content

From Vulnerability Finding to Verified Fix: A Workflow for AI Coding Agents

  • security
  • vulnerability-response
  • coding-agents
  • sql-injection
  • python
  • code-review

An AI coding agent can help investigate a vulnerability and propose a patch quickly. The difficult part is proving that the patch addresses the actual weakness without introducing a regression or leaving a related attack path open.

A dark textured title card reading From Vulnerability Finding to Verified Fix: A Workflow for AI Coding Agents inside a thin white frame, over a blue and amber glow

This tutorial presents an evidence-driven workflow for using an AI coding agent during vulnerability remediation. It follows a simple rule: a finding is a hypothesis to investigate, and a patch is a change to verify. Neither a scanner alert nor a confident model explanation is enough on its own.

The workflow is informed by SecureFlag’s guide to writing fixes for security defects with AI assistants, the OWASP Top 10 for LLM Applications 2025, and GitHub’s documentation on code scanning alerts. OWASP’s LLM09:2025 Misinformation names the risk this workflow guards against: overreliance on model output that nobody verifies, including suggested code that introduces vulnerabilities.

  1. Establish the affected component, version, configuration, and trust boundary.
  2. Confirm the finding against an authoritative advisory or reproducible code path.
  3. Write a regression test that demonstrates the unsafe behavior.
  4. Apply the smallest defensible fix.
  5. Run the regression test and the relevant broader test suite.
  6. Review the whole change, including callers and adjacent paths.
  7. Run security analysis and record checks that did not run.
  8. Request independent review and use the normal release process.

These steps are not a substitute for incident response. If a vulnerability may already have been exploited or credentials may have been exposed, contain the risk and follow the relevant incident procedure in parallel with code remediation.

Give the agent the exact finding, repository scope, dependency lockfile, runtime version, and relevant deployment context. Ask it to identify what is known, what is inferred, and what remains unknown.

For a published vulnerability, start with the maintainer’s advisory, release notes, affected-version range, and fixed version. Use secondary analysis to understand exploitation details, but do not let a blog post override the primary advisory. For a scanner finding, inspect the source location, data flow, configuration, and whether the vulnerable path is reachable.

Record at least:

  • Advisory identifier or scanner rule and version.
  • Affected package or source path.
  • Affected and fixed versions, when applicable.
  • Preconditions required for exploitation.
  • Evidence that the application uses the affected path.
  • Uncertainties that need validation.

Do not assume every alert is exploitable in every deployment. Equally, do not dismiss an alert just because a quick reproduction failed: the test may not represent the production configuration or attacker-controlled input.

2. Reproduce the weakness with a regression test

Section titled “2. Reproduce the weakness with a regression test”

A regression test should capture the unsafe behavior or the security property the fix must preserve. SecureFlag recommends establishing the root cause and building a regression test that fails on the vulnerable code before implementing a fix (SecureFlag: writing fixes for security defects with AI assistants). That sequence helps distinguish an actual correction from a patch that merely changes the visible symptom.

The following minimal example illustrates SQL injection in Python’s SQLite API. It is a deliberately vulnerable teaching example, not production application code.

import sqlite3
def find_user_unsafe(connection: sqlite3.Connection, username: str):
query = f"SELECT id, username FROM users WHERE username = '{username}'"
return connection.execute(query).fetchall()

String interpolation makes the user-controlled value part of the SQL syntax. The safer pattern is to bind the value as a parameter:

import sqlite3
def find_user(connection: sqlite3.Connection, username: str):
return connection.execute(
"SELECT id, username FROM users WHERE username = ?",
(username,),
).fetchall()

Python’s official sqlite3 documentation warns that assembling queries with Python string operations is vulnerable to SQL injection and explains placeholders for binding values. OWASP’s SQL Injection Prevention Cheat Sheet lists parameterized queries first among its primary defenses: define the SQL structure separately and bind the values through the database API.

A small test can check that a value containing SQL syntax is treated as data rather than executable syntax:

import sqlite3
def test_username_is_treated_as_data():
connection = sqlite3.connect(":memory:")
connection.execute(
"CREATE TABLE users (id INTEGER PRIMARY KEY, username TEXT)"
)
connection.executemany(
"INSERT INTO users (username) VALUES (?)",
[("alice",), ("bob",)],
)
rows = find_user_unsafe(connection, "' OR 1=1 --")
assert rows == []

For the first run, call the unsafe function in the test and confirm that the test fails. After applying the parameterized-query fix, change the test call to the safe function and confirm that it passes. With the two seeded rows, the unsafe function returns both alice and bob for this input, and the parameterized version returns an empty list. This standalone example has not been integrated into a project test suite. In a real repository, adapt it to the existing test framework and add cases for expected matches, empty values, Unicode, and relevant application behavior.

The key point is not that one test proves a system secure. It is that the test encodes the specific failure mode so future changes can detect its return.

Ask the agent to explain the root cause and propose a minimal patch before it edits. A good patch changes the vulnerable operation, preserves expected behavior, and avoids unrelated refactoring.

For dependency vulnerabilities, a source-code change may not be the correct fix. The appropriate action could be upgrading to a fixed release, removing an unused dependency, changing a configuration, or applying a maintainer-recommended mitigation. Confirm the version against the official advisory and update the lockfile consistently.

Review the patch for:

  • Whether it fixes the underlying cause rather than suppressing the alert.
  • Whether callers or sibling code paths remain vulnerable.
  • Whether error handling and authorization remain correct.
  • Whether the change weakens validation or logging elsewhere.
  • Whether dependencies, generated files, or configuration also need updates.

Do not let the agent broaden the patch without a reason. Smaller changes are easier to review, but minimality must not leave a known variant unfixed.

Run checks in layers, and record the exact command and outcome for each:

# Adapt these to the repository's actual tooling.
python -m pytest
python -m compileall -q .
git diff --check

These commands are examples for a Python project, not a universal checklist. Run the project’s supported test commands and relevant integration tests. A syntax check is not a security test, and a passing unit test does not establish that the application is safe end to end.

For dependency or source-level issues, add the applicable package audit, static analysis, secret scanning, and code-scanning checks. GitHub’s code scanning documentation explains where alerts appear and notes that the status on an alert page reflects only the default branch. A scanner can miss business-logic flaws or produce false positives, so combine automated results with review of the affected data flow and threat model.

If a check could not run, report it as not run. Do not describe a missing tool, unavailable environment, or skipped integration test as a pass.

The coding agent that produced the patch may share the same mistaken assumptions when asked to review it. Use a fresh reviewer or a separate review pass with the original finding, the patch, the test evidence, and the relevant security requirements. Ask the reviewer to look for counterexamples, adjacent call sites, incomplete validation, and behavior changes outside the intended scope.

Ultrapowers includes a vulnerability-hunt skill, in beta, that hunts a ticket’s change or the whole tree for CVEs, secrets, and insecure patterns, with a verifier that tries to refute every High or Medium finding before it counts. See the Ultrapowers repository and the existing AI-Native SDLC with Ultrapowers article for how that review fits into a larger workflow. These are workflow aids, not guarantees that a vulnerability will be found.

6. Record the evidence in the pull request

Section titled “6. Record the evidence in the pull request”

The pull request should make it possible for another engineer to assess the fix without reconstructing the entire investigation. Include:

  • The original finding and the affected versions or code path.
  • Root-cause explanation and relevant primary advisory.
  • The regression test and why it demonstrates the issue.
  • The fix and any compatibility or deployment considerations.
  • Exact test and security-check commands with results.
  • Checks that were skipped and why.
  • Residual risk, rollout notes, and any required credential rotation or incident response.

Keep the agent’s generated summary separate from the evidence. Link to actual test output, CI checks, advisory pages, and relevant diffs.

Before release, an engineer should confirm that the affected environment matches the assumptions, the patch is appropriate for the deployed version, and all required checks have completed. For high-impact issues, follow the organization’s incident response and change-management requirements.

If the vulnerability could have exposed credentials, assume that a code fix alone may not remove the risk. Review access logs where available, rotate affected credentials, invalidate sessions or tokens as appropriate, and follow the maintainer’s incident guidance.

Do you want a vulnerability finding in your codebase taken through to a verified, reviewed fix? Contact me.