<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Stephen Metcalfe</title>
    <description>The latest articles on DEV Community by Stephen Metcalfe (@raithlin).</description>
    <link>https://dev.to/raithlin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F309554%2Fb8348be7-2a64-44b0-ab50-0ba1898afaa1.jpeg</url>
      <title>DEV Community: Stephen Metcalfe</title>
      <link>https://dev.to/raithlin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raithlin"/>
    <language>en</language>
    <item>
      <title>Authenticated Isn’t Authorized: The AI Code Review Bug That Looks Secure</title>
      <dc:creator>Stephen Metcalfe</dc:creator>
      <pubDate>Tue, 25 Aug 2026 21:05:45 +0000</pubDate>
      <link>https://dev.to/raithlin/authenticated-isnt-authorized-the-ai-code-review-bug-that-looks-secure-507m</link>
      <guid>https://dev.to/raithlin/authenticated-isnt-authorized-the-ai-code-review-bug-that-looks-secure-507m</guid>
      <description>&lt;p&gt;One of the more dangerous mistakes in AI-generated code is also one of the easiest to miss in review. The code has authentication; the user is logged in, and there is an authentication check somewhere in the request path. Everything looks right, but &lt;em&gt;the endpoint still lets one user access another user's data&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The problem is simple:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Authentication tells you who the user is; Authorization tells you whether that user is allowed to access this specific resource.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AI-generated code often gets the first part right and quietly skips the second.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug that looks secure
&lt;/h2&gt;

&lt;p&gt;Imagine an endpoint like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="ss"&gt;:unauthorized&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="n"&gt;current_user&lt;/span&gt;

  &lt;span class="vi"&gt;@invoice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Invoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;json: &lt;/span&gt;&lt;span class="vi"&gt;@invoice&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, this doesn't look reckless.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The user must be logged in.&lt;/li&gt;
&lt;li&gt;The invoice is loaded normally.&lt;/li&gt;
&lt;li&gt;There is no obvious injection vulnerability.&lt;/li&gt;
&lt;li&gt;The controller is small and readable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But the important question is missing: &lt;strong&gt;Does this invoice belong to the current user?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If I am logged in as user 42 and request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/invoices/123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code checks only that I am authenticated: it doesn't check whether invoice 123 is mine.&lt;/p&gt;

&lt;p&gt;If changing &lt;code&gt;123&lt;/code&gt; to &lt;code&gt;124&lt;/code&gt; gives me somebody else's invoice, authentication has done nothing to protect that data.&lt;/p&gt;

&lt;p&gt;This is the class of problem commonly described as &lt;strong&gt;BOLA&lt;/strong&gt; &lt;em&gt;(Broken Object Level Authorization)&lt;/em&gt; or, in older terminology, &lt;strong&gt;IDOR&lt;/strong&gt;: object-level access control is missing even though authentication exists.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why AI is good at producing this bug
&lt;/h2&gt;

&lt;p&gt;AI coding tools are often very good at reproducing familiar application patterns. A controller action often looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="vi"&gt;@invoice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Invoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An authentication check often looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="ss"&gt;:unauthorized&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="n"&gt;current_user&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are individually plausible. Put them together and the code &lt;em&gt;looks&lt;/em&gt; secure.&lt;/p&gt;

&lt;p&gt;That is the problem.&lt;/p&gt;

&lt;p&gt;The generated code has matched two common patterns without necessarily reasoning about the relationship between the authenticated user and the object being loaded.&lt;/p&gt;

&lt;p&gt;The failure is not:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“There is no security.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The failure is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Security exists, but it is not being applied to this resource.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is much harder to catch casually.&lt;/p&gt;

&lt;h2&gt;
  
  
  The safer version
&lt;/h2&gt;

&lt;p&gt;The exact implementation depends on the application, but the data access should normally be constrained by the user's permitted scope.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;
  &lt;span class="vi"&gt;@invoice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;json: &lt;/span&gt;&lt;span class="vi"&gt;@invoice&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the lookup itself enforces the ownership boundary.&lt;/p&gt;

&lt;p&gt;A request for somebody else's invoice doesn't merely fail an &lt;code&gt;if&lt;/code&gt; statement later. The resource is outside the queryable scope in the first place. In a more complex system, that boundary may come from a policy object, tenant scope, permission service, or domain rule instead.&lt;/p&gt;

&lt;p&gt;The implementation is less important than the review question:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can I trace the current user's permission all the way to the specific object being accessed?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If I can't, I treat it as a security finding that needs verification.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why tests often don't save you
&lt;/h2&gt;

&lt;p&gt;This kind of bug is especially easy to miss when the generated tests were written by the same AI that generated the implementation. You often get tests like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"returns an invoice for an authenticated user"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;sign_in&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s2"&gt;"/invoices/&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;have_http_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That test passes. It also proves almost nothing about authorization. The test you actually need is closer to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"does not allow a user to access another user's invoice"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;sign_in&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s2"&gt;"/invoices/&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;other_users_invoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;have_http_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:not_found&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or whatever denial behavior your application uses.&lt;/p&gt;

&lt;p&gt;This is one reason I don't treat a green test suite as proof that AI-generated code is safe. A test suite can faithfully confirm the same misunderstanding that produced the code. &lt;/p&gt;

&lt;p&gt;Negative testing is a separate topic, and probably worth its own article.&lt;/p&gt;

&lt;h2&gt;
  
  
  The review question I use
&lt;/h2&gt;

&lt;p&gt;When I review security-sensitive AI-generated code, I don't ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Is there authentication?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;“Is authorization enforced for this exact operation on this exact resource?”&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That wording matters. It forces the review away from the comforting presence of middleware, &lt;code&gt;current_user&lt;/code&gt;, role checks, and authentication helpers.&lt;/p&gt;

&lt;p&gt;You &lt;strong&gt;have&lt;/strong&gt; to follow the access path.&lt;/p&gt;

&lt;p&gt;For an object lookup, that means tracing from the request down to the data access:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;request
  ↓
authenticated user
  ↓
authorization decision
  ↓
specific resource
  ↓
data access
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the chain contains a jump like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;authenticated user
  ↓
Model.find(params[:id])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I want to know exactly what prevents cross-user or cross-tenant access. “Auth is handled elsewhere” isn't an answer until you verify &lt;strong&gt;where&lt;/strong&gt; and &lt;strong&gt;how&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A useful AI review prompt
&lt;/h2&gt;

&lt;p&gt;One of the prompts I use in the Security Deep-Dive is deliberately explicit about this failure mode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Review this code for missing or incorrect authorization checks.

For every resource accessed by ID or other user-controlled identifier:

- identify who is allowed to access it
- verify that authorization is checked for the specific resource
- do not treat authentication alone as authorization
- flag any lookup where an authenticated user could substitute another object's ID

For each finding, include:
- severity
- file and line number
- exploit path
- minimal fix

If authorization depends on middleware, policies, framework defaults, or another layer, state that assumption and mark it as needing verification rather than inventing a vulnerability.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last sentence is important: security review prompts can become useless if you tell the model to be “aggressive” and it responds by inventing vulnerabilities that depend on imaginary configuration. I want it to challenge the code, but I also want findings grounded in what is actually there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try the evil-user version
&lt;/h2&gt;

&lt;p&gt;For security-sensitive changes, I often do a second pass from the opposite direction.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Review this code for security problems.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Assume you are a malicious but valid user of this system.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That user already has credentials. That removes the easy answers.&lt;/p&gt;

&lt;p&gt;The question becomes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can I change an object ID?&lt;/li&gt;
&lt;li&gt;Can I access another account's data?&lt;/li&gt;
&lt;li&gt;Can I call an action I should not have permission to call?&lt;/li&gt;
&lt;li&gt;Can I escalate from one valid permission into another?&lt;/li&gt;
&lt;li&gt;Can I combine two individually minor behaviors into something more serious?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A surprising number of access-control mistakes only become obvious once you stop imagining an anonymous attacker and start imagining a perfectly legitimate user who is curious about what happens when they change a parameter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where I spend extra review time
&lt;/h2&gt;

&lt;p&gt;I treat this pattern as especially important in changes involving:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;user-owned records&lt;/li&gt;
&lt;li&gt;accounts and organizations&lt;/li&gt;
&lt;li&gt;multi-tenant data&lt;/li&gt;
&lt;li&gt;billing&lt;/li&gt;
&lt;li&gt;admin functionality&lt;/li&gt;
&lt;li&gt;exports&lt;/li&gt;
&lt;li&gt;file access&lt;/li&gt;
&lt;li&gt;APIs that accept object IDs&lt;/li&gt;
&lt;li&gt;background jobs acting on behalf of users&lt;/li&gt;
&lt;li&gt;anything involving customer data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those are the places where “logged in” and “allowed to do this” most obviously diverge.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rule I keep coming back to
&lt;/h2&gt;

&lt;p&gt;AI-generated security bugs aren't always dramatic. They don't necessarily contain &lt;code&gt;eval()&lt;/code&gt; or expose a secret in plaintext. Sometimes the code uses the correct framework, the correct authentication system, the correct ORM, and the correct controller structure. It just misses one relationship.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This user. This operation. This resource.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That is enough.&lt;/p&gt;

&lt;p&gt;Authentication answers:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Who are you?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Authorization answers:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Are you allowed to do &lt;em&gt;this&lt;/em&gt; to &lt;em&gt;that&lt;/em&gt;?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Don't let an AI-generated authentication check convince you that the second question has been answered.&lt;/p&gt;




&lt;p&gt;This is one of the checks in &lt;strong&gt;Round 2: Security Deep-Dive&lt;/strong&gt; of &lt;em&gt;The AI Code Review Protocol&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If this way of reviewing code is useful, my free repo contains the free prompts, and the book explains the full four-round method and why each round exists.&lt;/p&gt;

&lt;p&gt;The free prompt library and automated review skill are available in the &lt;a href="https://github.com/Raithlin/ai-code-review-protocol" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want the reasoning behind the full four-round process, the guide is available on &lt;a href="https://selar.com/ai-code-review-protocol" rel="noopener noreferrer"&gt;Selar&lt;/a&gt; and &lt;a href="https://www.amazon.com/dp/B0H84XDQDW" rel="noopener noreferrer"&gt;Amazon Kindle&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>security</category>
      <category>ai</category>
      <category>programming</category>
      <category>codereview</category>
    </item>
    <item>
      <title>I Reviewed 200+ AI-Generated PRs. Here's the 4-Round Protocol I Use Now.</title>
      <dc:creator>Stephen Metcalfe</dc:creator>
      <pubDate>Mon, 15 Jun 2026 12:54:08 +0000</pubDate>
      <link>https://dev.to/raithlin/i-reviewed-200-ai-generated-prs-heres-the-4-round-protocol-i-use-now-28l8</link>
      <guid>https://dev.to/raithlin/i-reviewed-200-ai-generated-prs-heres-the-4-round-protocol-i-use-now-28l8</guid>
      <description>&lt;p&gt;Your teammate used Claude to generate a new API endpoint. The code looks great — clean formatting, proper error handling, even comments. You skim through it, see it follows conventions, CI is green. You approve.&lt;/p&gt;

&lt;p&gt;Two weeks later, the endpoint silently drops a decimal place on currency conversions. A financial report is wrong for three days before anyone notices.&lt;/p&gt;

&lt;p&gt;This scenario is playing out in hundreds of teams right now. Not because AI generates "bad code" — but because &lt;strong&gt;AI-generated code fails in ways human code doesn't&lt;/strong&gt;, and your existing review process wasn't designed for it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem With Reviewing AI Code
&lt;/h2&gt;

&lt;p&gt;AI doesn't flag uncertainty. It presents everything with equal confidence. A human developer might write &lt;code&gt;// not sure about the caching here&lt;/code&gt; — that nervous comment tells you exactly where to look. AI never writes that comment. It writes &lt;code&gt;// Transform the input to match the expected schema&lt;/code&gt; with full confidence, even when the transformation is wrong.&lt;/p&gt;

&lt;p&gt;After reviewing hundreds of AI-generated PRs over the past year, I found a pattern. The bugs aren't in formatting. They're in the places a quick glance won't reach:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Off-by-one errors in loops you skimmed&lt;/li&gt;
&lt;li&gt;Missing auth checks on new endpoints&lt;/li&gt;
&lt;li&gt;Elegant abstractions that create maintenance nightmares&lt;/li&gt;
&lt;li&gt;Code that solves the &lt;em&gt;wrong problem&lt;/em&gt; perfectly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Generic "review this code" prompts won't catch these. You need a system.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 4-Round Protocol
&lt;/h2&gt;

&lt;p&gt;I built a review protocol specifically for AI-generated code. Four rounds, each targeting a different failure mode. Total time: ~15 minutes for a typical PR, up to 35 minutes for a large one.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Round&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;What You're Catching&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Surface Scan&lt;/td&gt;
&lt;td&gt;Logic errors, off-by-one, wrong assumptions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Security Deep-Dive&lt;/td&gt;
&lt;td&gt;Injection, auth gaps, data leaks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Architecture Smell Check&lt;/td&gt;
&lt;td&gt;Wrong patterns, tech debt, doesn't fit the system&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Comparison Pass&lt;/td&gt;
&lt;td&gt;Does this match what we actually asked for?&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The key insight: &lt;strong&gt;each round uses a separate AI prompt that forces a different lens on the same code.&lt;/strong&gt; You're not asking the AI to "review this code" four times — you're asking four different, targeted questions.&lt;/p&gt;

&lt;p&gt;Let me show you the two rounds that catch the most issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Round 1: The Surface Scan
&lt;/h2&gt;

&lt;p&gt;This is the high-probability round. Most AI bugs live here — logic errors, wrong assumptions, off-by-one bugs. The code &lt;em&gt;looks&lt;/em&gt; correct. It's subtly wrong in exactly the ways a quick glance won't catch.&lt;/p&gt;

&lt;p&gt;Here's the prompt I use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Review this code for logic errors only. Do NOT suggest style improvements,
documentation, or refactoring. I want you to find:

1. Off-by-one errors, wrong comparisons, or inverted logic
2. Wrong default values or assumptions about data shape
3. Missing edge case handling (null, empty, zero, max values)
4. Race conditions or non-atomic operations on shared state

For each issue found, state:
- The exact line number
- Why it's wrong
- What the correct behavior should be

If you find zero issues, explain why each edge case IS handled,
not just say "looks good."

[Paste the PR description or requirements if available]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The critical instruction is that last line: &lt;strong&gt;"If you find zero issues, explain why each edge case IS handled."&lt;/strong&gt; Without this, the AI will happily say "looks good" and move on. Forcing it to justify the all-clear catches things a simple yes/no never will.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The trap:&lt;/strong&gt; AI-generated tests will pass. AI knows what the code does, so it writes tests that confirm the code's behavior — &lt;em&gt;including its bugs&lt;/em&gt;. Perfect test coverage means nothing if the tests are testing the wrong thing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Round 2: The Security Deep-Dive
&lt;/h2&gt;

&lt;p&gt;This is the scary one. AI models are trained on massive amounts of public code, including code with security vulnerabilities. They don't understand security — they understand patterns. If the most common Stack Overflow solution uses &lt;code&gt;eval()&lt;/code&gt; or concatenates SQL strings, the AI will reproduce that pattern with full confidence.&lt;/p&gt;

&lt;p&gt;The most common AI security failures: SQL injection, insecure deserialization (pickle, Marshal, &lt;code&gt;YAML.load&lt;/code&gt;), BOLA/IDOR (authenticated but accessing someone else's resource), mass assignment, and SSRF.&lt;/p&gt;

&lt;p&gt;Here's the prompt that catches what your brain won't think to check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You are a malicious actor with valid API credentials who want to exploit
this code. Walk through every possible thing you could try:

- Can you access data you shouldn't be able to?
- Can you escalate privileges?
- Can you cause the system to leak internal information?
- Can you trigger unexpected behavior with edge inputs?
- Can you cause the system to consume excessive resources?

Think step by step. List at least 5 distinct attack vectors. If you can't
find 5, you're not thinking creatively enough.

After listing individual vectors, describe at least 2 attack chains where
you combine multiple steps to achieve something none of the individual
vectors accomplish alone.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pro tip: use a different AI model for this round than the one that generated the code.&lt;/strong&gt; If Claude wrote the code, use another model (if possible, from another provider) to review it. Different training data means different blind spots. This single change catches vulnerabilities that using the same model consistently misses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The trap:&lt;/strong&gt; Auth checks that look right but aren't. AI will write &lt;code&gt;if current_user.present?&lt;/code&gt; — the user is authenticated, but the code doesn't check if they're authorized for &lt;em&gt;that specific resource&lt;/em&gt;. The check looks secure but isn't.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Two Rules That Make This Work
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Clear context between rounds.&lt;/strong&gt; Don't run all 4 rounds in the same chat thread. Start a fresh conversation for each round. If you run Round 2 in the same context as Round 1, the AI already "knows" what it told you in Round 1 and will unconsciously align its analysis. Fresh context forces independent analysis. Costs 30 seconds, worth every one of them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Run all reviews first, fix once.&lt;/strong&gt; The naive approach is to fix issues one at a time — fix the logic bug, review, fix the security hole, review. This creates whack-a-mole: fixing the architecture can introduce a new security hole. Instead: run all 4 rounds, collect every issue, send the complete list to the AI in one shot, then re-run all rounds on the result.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to Skip a Round
&lt;/h2&gt;

&lt;p&gt;Not every PR needs all 4 rounds:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Rounds to Run&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Comment or docs change&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Variable rename&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Typo fix&lt;/td&gt;
&lt;td&gt;1 &amp;amp; 4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;New API endpoint&lt;/td&gt;
&lt;td&gt;1, 2 &amp;amp; 4 (skip 3 if it follows existing patterns)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;New feature, new patterns&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;All 4&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Auth or payment change&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;All 4&lt;/strong&gt; — extra time on round 2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AI-generated bugfix&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;All 4&lt;/strong&gt; — the fix might work but introduce new bugs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;General principle: if AI generated the code, lean toward running more rounds. That's the whole point.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Round Everyone Skips
&lt;/h2&gt;

&lt;p&gt;Round 4 — the Comparison Pass — is the most commonly skipped and the most dangerous to skip. It asks one question: &lt;strong&gt;does this code actually solve the problem we asked for?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AI is excellent at solving the problem you &lt;em&gt;typed&lt;/em&gt;, not the problem you &lt;em&gt;meant&lt;/em&gt;. It takes your words literally. The most common failure: AI solves the first 80% of a ticket perfectly and quietly ignores the last 20% because it "didn't seem important." The code is perfect — for the wrong thing.&lt;/p&gt;

&lt;p&gt;If you have a ticket or issue, paste it in and make the AI verify each acceptance criterion against the code. You'll be surprised what's missing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making It Stick
&lt;/h2&gt;

&lt;p&gt;Here's how to adopt this without overwhelming yourself:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Day 1-2:&lt;/strong&gt; Run only Round 1 on all your PRs. Get comfortable with the prompts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Day 3-4:&lt;/strong&gt; Add Round 2. You'll likely find something within the first few PRs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Day 5-6:&lt;/strong&gt; Add Rounds 3 and 4.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Day 7:&lt;/strong&gt; Reflect. What failure patterns did you see most? Which round caught the most issues?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The best review process is the one that evolves. When you find a pattern this protocol doesn't catch, add your own round. When a prompt stops finding bugs, retire it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where This Comes From
&lt;/h2&gt;

&lt;p&gt;I've been using versions of this protocol for over a year. It has saved me from shipping bugs that I would have approved on a first pass. Not every time, but often enough that running it is automatic now.&lt;/p&gt;

&lt;p&gt;I wrote the full protocol — all 4 rounds, 12 copy-paste prompts, the "traps to watch for" in each round, a printable checklist, and the review loop workflow — into a guide. It's called The AI Code Review Protocol and it's on &lt;a href="https://selar.com/m/metcalfesoftware?currency=USD" rel="noopener noreferrer"&gt;Selar&lt;/a&gt; for $8.99.&lt;/p&gt;

&lt;p&gt;If you want the complete version with Rounds 3 and 4, the architecture smell checklist, the PII audit prompt, and the automation approaches — that's there. If this post was useful, the guide goes deeper.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If you've built your own review process for AI code, I'd genuinely like to hear what works for you. I'm &lt;a class="mentioned-user" href="https://dev.to/raithlin"&gt;@raithlin&lt;/a&gt; on X, or drop a comment below.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>codereview</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
