AI-Assisted Security Review
Review code and configuration with a security mindset: a focused checklist, review of authentication, authorization, input handling, and data protection, review of AI-generated code, and prompting patterns that produce verifiable findings.
Introduction
Scanners find patterns; reviewers find intent. A missing ownership check on one endpoint, a business rule that can be bypassed by calling steps in the wrong order, or a debug route left in a release will pass most tools and be obvious to a reader who asks the right question. Code review has always been the best place to catch those problems, and it is also where AI can help most, and mislead most.
This lesson teaches a structured way to review code and configuration for security, how to use AI as a reviewing partner, and how to review the large volume of code that AI itself now writes.
Learning Objectives
- Perform a focused, risk-based security review of code and configuration.
- Review authentication, authorization, input and output handling, data protection, and error handling.
- Recognize common security weaknesses in AI-generated code.
- Write prompts that produce specific, verifiable review findings.
- Verify AI findings and avoid both false alarms and false reassurance.
- Integrate security review into the normal pull request process.
- Keep humans accountable for the review conclusion.
1. Review by Risk, Not by Line
Reading everything with equal attention is slow and dilutes focus. Choose by risk, using the threat model:
- High attention: authentication and session code, authorization checks, data access layers, file upload and parsing, anything that builds queries or commands from input, cryptography use, model provider calls, administrative features, and infrastructure and pipeline definitions.
- Medium attention: business logic that changes money, permissions, or sharing; configuration loading; logging.
- Lower attention: presentation code without sensitive data, and tests (although tests can leak real data or secrets).
Also review what changed: new endpoints, new dependencies, changed permissions, and altered configuration. A pull request’s description should say whether it touches security-relevant areas, and such changes get a second reviewer with security awareness (a “security champion” on the team).
2. The Review Questions
For any security-relevant code, work through these questions:
Trust and input
- Where does untrusted data enter? Is it validated for type, length, range, and format at the boundary?
- Is it ever used to build a query, command, file path, URL, or template? Is it passed safely as a parameter?
- Can the user control an identifier, a file name, a redirect target, or a URL the server will fetch?
Authentication and sessions
- Is every non-public route protected by authentication, by default?
- Are credentials, tokens, and keys handled safely: never logged, never in URLs, hashed or encrypted appropriately?
Authorization
- For each action on a record: where is the check that this user may do this to this record?
- Is the tenant derived from the session and applied to every query?
- Is the check performed on the server and on every path, including exports, bulk operations, background jobs, and file downloads?
- What happens with a missing, malformed, or someone else’s identifier?
Output and errors
- Is output encoded for its context (page, attribute, file, log)?
- Do errors reveal internals, query text, file paths, or other users’ data?
- Does failure leave the system in a safe state (deny, not allow)?
Data protection
- What sensitive data is stored, logged, cached, or sent to other services? Is it necessary?
- Are secrets read from the approved source, not embedded?
Resource use
- Are sizes, counts, rates, and time limited? Can one request consume unbounded memory, storage, or paid model calls?
Logic and state
- Can steps be skipped, repeated, or reordered to reach an unintended state? Are race conditions possible (two requests at once)?
- Are there debug features, backdoors, or test routes enabled in production?
Keep this as a short checklist in the pull request template so reviewers do not rely on memory.
3. Reviewing Configuration and Infrastructure
Many serious incidents are configuration mistakes. Review:
- Pipeline definitions: who can trigger production deployments, which credentials each job can see, whether untrusted input can run with privileged access, and whether steps are pinned.
- Infrastructure code: public exposure, network rules, identity permissions, encryption, logging, and backups; read the plan output, not just the code.
- Container definitions: user, base image, exposed ports, embedded secrets, writable file systems.
- Application configuration: debug mode, verbose errors, permissive cross-origin rules, default credentials, disabled security features.
- Permissions: wildcard permissions, administrator roles on workloads, long-lived keys.
Permissions are best reviewed by asking, “if this credential leaked, what could an attacker do?”
4. Reviewing AI-Generated Code
AI assistants now write a large share of code, so reviewers must know how it tends to fail. Generated code is typically readable, well structured, and consistent, which makes it look safer than it is. Common problems:
| Weakness | What it looks like |
|---|---|
| Missing authorization | The handler works and returns data, but no check that the user may access that record |
| Trusting client input | Tenant, role, or price taken from the request body |
| Unsafe query construction | String concatenation in a database or command call, especially in “quick” scripts |
| Weak or outdated cryptography | Obsolete hash functions, hard-coded keys or initialization values, home-made token schemes |
| Hard-coded secrets and example credentials | Placeholders that end up in the repository |
| Overly permissive defaults | Open cross-origin rules, disabled certificate checks “for testing,” broad permissions |
| Verbose errors and logging | Stack traces to the client; request bodies in logs |
| Invented or outdated APIs and packages | Calls to functions that do not exist or are deprecated; suggested packages that are fake |
| Missing limits | No size, rate, or time limits |
| Copied patterns without context | An example from a tutorial that is intentionally insecure |
| Plausible but wrong fixes | A patch that silences the scanner without removing the cause |
Because the code usually runs and passes basic tests, tests alone do not show that it is safe. Add negative tests (what must be refused) for every security-relevant change, and review generated code as if written by a fast, capable colleague who does not know your threat model.
Practical habits:
- Give the model the security context when generating code: tenant model, authorization layer, approved libraries, logging rules. Put it in the project’s assistant instructions so it applies every time.
- Ask for the checks explicitly, then verify them.
- Review the diff, not the explanation. The summary the model gives can describe a control that the code does not contain.
- Run the scanners on generated code like any other.
5. AI as a Security Reviewer
AI is a useful second pair of eyes, especially for the questions in section 2. Treat its output as a list of hypotheses.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Act as an experienced application security reviewer.
Context:
- System: multi-tenant SaaS. The tenant (organization) comes from the
authenticated session. All tenant data must be filtered by
organization. Authorization is meant to be enforced in the shared
function [name]. Model output is untrusted.
- Threats relevant to this code: [list from the threat register]
Code under review (file paths and snippets):
[paste]
Tasks:
1. List potential security weaknesses. For each: location (file and
line or function), the weakness class, the untrusted data and
the sensitive operation involved, the impact, and how you would
verify it.
2. For each of the review areas (input handling, authentication,
authorization, output and errors, data protection, resource use,
logic and state), state either a finding or what you checked and
found sound, citing the code.
3. List what you could not assess because the code or context was
not provided.
4. Rate your confidence for each finding.
Do not report style issues. Do not state that the code is secure.
Do not write exploit code.
Notice what the prompt does: it gives context so the model can check against intent, it demands locations and verification steps, it forces a statement for every area so silence is not read as safety, and it asks for what was not assessed.
Useful variations:
- Differential review: paste the diff and ask what security properties changed.
- Adversarial question: “Given this endpoint, list five requests a malicious authenticated user would try, and what the code does in each case.”
- Fix review: paste a proposed fix and ask whether it removes the cause or only blocks one input, and what it might break.
- Configuration review: paste an infrastructure plan and ask for exposure and permission concerns.
6. Verify the Findings
Each finding from a person or a model goes through the same steps as in lesson 4:
- Open the code at the stated location. Does it exist and say what was claimed?
- Trace the data. Can untrusted input actually reach the sensitive operation? Is there a check elsewhere that the model did not see?
- Reproduce it with a test, in a safe environment.
- Classify: confirmed, unconfirmed, false positive.
Track how reliable the assistant is on your code: over a few reviews, count findings that were real, findings that were false, and real issues it missed that a person or tool caught. This tells you how much to trust it and where.
Be equally skeptical of reassurance. “No vulnerabilities found” from a model means it did not report any; it does not mean none exist. Never use an AI statement as the sole evidence that code is secure.
7. Make Security Review Routine
- Pull request template with the security questions and a field for “touches authentication, authorization, data access, upload, external service, or infrastructure.”
- Code ownership rules so that sensitive areas require review by designated people.
- Automated AI review on pull requests as an additional reviewer that posts hypotheses; a human reviewer remains responsible for approval.
- Periodic focused reviews of the highest-risk components, using the threat model, even when nothing changed.
- Learning loop: when a vulnerability is found, ask which review question would have caught it, and add it to the checklist or a custom scanner rule.
- Security champions: developers who volunteer to build expertise and help their teams, with time allocated to the role.
Review is also a teaching tool. Explain why something is risky in review comments; a rule understood is followed more often than a rule imposed.
8. Practical Exercise
For SignalDesk (or your project):
- Select the five highest-risk code areas from the threat model and the diff history (for example, the export endpoint, the upload handler, the data access layer, the model provider call, and the invitation flow).
- Review each with the checklist, recording findings with evidence.
- Run the AI review prompt on the same code, with the threat context, and compare with your findings.
- Verify each AI finding: confirmed, unconfirmed, or false positive, and record where the model missed something you found.
- Review a piece of AI-generated code (generate a small feature if needed) against the table of common weaknesses, and add negative tests for what must be refused.
- Review the pipeline definition and infrastructure plan for permissions and exposure.
- Add the security questions to the pull request template, and define which changes need a security-aware second reviewer.
- Add confirmed findings to the register and update the checklist with anything the review taught you.
Verification
You are ready to continue when:
- The review covered the highest-risk areas first, with the checklist applied and results recorded.
- Every AI finding is verified or dismissed with a reason, and you know where the model was wrong or silent.
- Generated code has negative tests for authorization, input, and limits.
- The review process is part of pull requests, with clear ownership of sensitive areas.
- No one treated “the AI found nothing” as proof of safety.
Key Takeaways
- Review by risk, using the threat model, and use a short checklist so nothing depends on memory.
- AI-generated code looks polished and often omits checks; review the diff and add negative tests.
- Prompts that give context, demand locations and verification steps, and ask what was not assessed produce usable findings.
- Treat AI findings as hypotheses and AI reassurance as worthless as proof.
- Make review routine, and use each finding to improve the checklist and the rules.
What’s Next?
Reviews and scans tell us where weaknesses might be. Security testing tells us whether they can really be used. The next lesson covers designing and running security tests, within clear authorization, and turning them into regression protection.