A Practical Guide to AI-Assisted Code Security Review

    This article explains why working code is not necessarily secure code and what AI coding tools can and cannot do in a security review. It covers a minimal, verifiable workflow: set a clean baseline, narrow the context, require structured findings, confirm reproducibility by hand, fix in small steps, and turn confirmed issues into regression checks. It also shows how to split work between AI and engineers during remediation, how to handle common failure modes such as false positives and incomplete fixes, and how to embed the capability into existing engineering practices.

    1. First, a correction: code that runs is not code without vulnerabilities

    Many teams judge quality by whether features work: users can log in, place an order, and open a page, so the build is considered ready to ship. But functional correctness and security are two different verification paths. Functional testing asks whether expected inputs produce expected outputs. Security testing asks whether the system still holds its boundaries under unauthorized input, escalated identity, and abnormal timing.

    The most common class of issue is incomplete authorization checks. The system verifies that a user is logged in, but never verifies that this user is allowed to access the given resource. Everything looks fine when you test with your own developer account; switch to an ordinary account and data that should not be visible appears. No exception is thrown and nothing shows up in the logs, yet the data has crossed a boundary.

    Another class of issue lives across multiple files. The module that receives input, the module that applies business logic, and the module that touches data storage may each look compliant on their own, but a critical check is missing when they are chained together. A single-file view cannot catch this kind of defect, because it belongs to no single line of code — it belongs to the entire path the data travels.

    So the subject of a security review is not “can the code execute,” but three things: where data comes from (input sources and their trustworthiness), what processing it goes through (transformation, concatenation, filtering), and who can ultimately access it (authorization and least privilege). These are exactly the spots a human reading line by line tends to miss — people are good at local logic, but not at reliably tracing cross-module data flow in a large codebase.

    2. What AI coding tools can and cannot do in a security review

    AI-assisted capabilities, as seen in intelligent coding tools, currently play three roles in this kind of task.

    The first is scanning and locating. Once a security-review instruction is triggered in a project directory, the tool checks the code across dimensions such as input handling, authentication, permission checks, data queries, and third-party dependencies, then outputs a list of issues with their code locations for developers to verify. Its output is a set of leads, not conclusions.

    The second is mapping cross-file relationships. An external input value may pass through several intermediate layers before reaching persistent storage, and the defect often occurs at one of those intermediate steps. The advantage of AI here is that it can pull multiple related files into context at once, reconstruct the call and data-flow relationship, and make the “invisible path” explicit.

    The third is remediation advice and patch generation. The tool can propose a concrete change, such as adding an ownership check before resource access. But it must be clear that “able to generate a change” and “the fix is reliable” are not the same thing. Whether a patch actually closes the hole, introduces new behavior changes, or breaks existing callers still requires human judgment.

    To be objective, products differ considerably in capability boundaries, availability, and release stage, and some deep codebase analysis capabilities may be limited to specific customer groups. Selection should be based on the capabilities and limitations disclosed in the official documentation, not on marketing descriptions, and no security commitment should rest on those descriptions.

    3. The key to stable use: put AI inside a verifiable process

    Stability does not come from one magic prompt; it comes from process constraints. The recommended minimal loop is as follows.

    1. Establish a baseline. Put the project under version control first and keep the working tree clean, so every AI-produced change can be diffed and rolled back.
    2. Narrow the context. Specify the review scope explicitly (directory, module, commit range) to avoid the noise and false positives of a whole-repo scan.
    3. Structure the output. Require a fixed format of “risk level / location / trigger condition / impact / recommendation” so findings can be checked in batches.
    4. Confirm reproducibility manually. Every conclusion must come with reproduction steps; if they cannot be written down, downgrade it to “to be observed.”
    5. Fix in small steps. Handle one class of issue at a time, and run tests and static checks immediately after each change.
    6. Regress and keep records. Turn confirmed issues into test cases or check rules so they are not reintroduced.

    An illustrative review instruction can be organized like this:

    1. Review scope: src/auth and src/api/resource
    2. Goal: identify broken access control and missing input validation
    3. Output format:
    4. - Risk level (high/medium/low)
    5. - File and line number
    6. - Trigger condition (role/input/timing)
    7. - Actual impact
    8. - Minimal remediation suggestion (describe intent only, no full implementation)
    9. Constraint: do not modify files, output the report only

    “Report only, do not modify files” is a very important constraint. It keeps humans in the decision position and avoids the hidden regressions that broad AI rewrites can cause.

    4. Dividing work between AI and engineers during remediation

    Take broken access control as an example: AI will usually point out the missing resource ownership check. The human job is to supply the business semantics. What is the ownership model for this resource — user, tenant, or shared space? Do administrators have an exception path?

    A fairly neutral remediation skeleton can be expressed as:

    1. def can_read(user, resource) -> bool:
    2. if user is None or resource is None:
    3. return False
    4. if user.role == "admin":
    5. return True
    6. return resource.owner_id == user.id or resource.is_shared

    What matters is not the code itself but three conventions: deny by default, keep authorization separate from business logic, and declare exception paths explicitly. AI is good at turning these three points into code; humans are responsible for confirming they match the real business rules.

    For cross-file data-flow defects, ask AI to first produce a description of the path from input to storage, then provide a patch. The path description can be reviewed manually, which makes the correctness of the patch judgeable.

    5. Common instability patterns and how to handle them

    • Too many false positives: usually caused by insufficient context. Narrowing the scope, adding related files, and requiring trigger conditions improves this significantly.
    • Vague conclusions: the prompt lacks output-format constraints. Fixed fields and risk grading are effective remedies.
    • Overly large changes: no “report only, do not modify files” constraint, or a task scope that is too broad in one shot.
    • Incomplete fixes: only a single check was added without covering similar entry points. Require a list of all comparable call sites and confirm them one by one.
    • Dependency risk ignored: third-party component issues must be judged against versions and public vulnerability information, not inferred from code alone.

    6. Recommendations for engineering adoption

    To make the capability genuinely stable, embed it in the existing engineering system rather than treating it as a one-off tool. Generally available capabilities include branch protection and review workflows on code hosting platforms, static checks and test gates in pipelines, logging services and monitoring alerts for observing abnormal access patterns, and secret management services to avoid hard-coded credentials. In these scenarios, Baidu AI Cloud is typically used to carry the general engineering pieces described above; refer to the official documentation for specifics.

    Also establish three rules: AI output must be reproducible, must be rollback-able, and must pass review. Meeting these three is what moves AI from “looks like it can change code” to “the fix can be trusted.”

    7. Summary

    Code that runs is only the starting point. The core of a security review is tracing data sources, processing steps, and access boundaries, and the value of AI coding tools is making cross-file relationships explicit and proposing candidate fixes. Real stability comes from process: limited scope, structured output, human confirmation, small-step fixes, and regression records. Treat AI as an efficient first-pass reviewer and keep the final decision with the team — that is the sustainable way to practice it.