Vibe coding with Claude Code course Β· Module 9: Discord clone

Captain's Review: Security of Code from Claude

8 min read
In this lesson10

The login branch works. You sign up, you sign in, Anne can't enter the rank panel, and Blackbeard gets kicked off the server. In the browser everything adds up. Except that the browser shows what the app does for an honest pirate, not what it will do for one who sends a request while bypassing the screen. In this location Claude wrote the permissions, the moderation routes, the weather key and the sign-in, exactly the code in which a mistake doesn't spoil the looks but opens the hold. Before the branch goes any further, it goes through a security review.

What you will learn

  • run /security-review on a branch and know what that review reads and what it doesn't;
  • tell a valid alarm from a false one and check each one yourself;
  • ask for fixes only for the points you accept, and undo a change that went too wide;
  • tell a code review from deny rules and permission modes.

What /security-review reads

According to the Claude Code documentation /security-review analyses the changes on the current branch for vulnerabilities. It compares the branch with the default branch of the origin remote repository and looks for risks such as injections, authentication errors and data leaks. It reads the source code in your folder, not the running site. It covers only the changes of the branch: code that was on main earlier stays outside the review. To review a specific folder you ask Claude with an ordinary request.

The command needs the origin remote. If the review ends with an error containing the words ambiguous argument, the documentation tells you to point at the default branch:

git remote set-head origin main

This command only records which branch on origin is the default. It sends nothing and doesn't change your files.

A review of the login branch

First a commit, so that the review sees all the code of the branch and you have a point to return to. Then in the Claude Code session:

/security-review

The review takes a few minutes. For us Claude announced three steps: one agent looks for candidates in the diff of the branch, several agents in parallel weed out false alarms, and at the end a report is produced. The first step found two candidates, and one made it into the report, written in English. It began like this:

# Vuln 1: Privilege escalation via open sign-up and email-keyed member mapping: `lib/session.ts:11`
* Severity: High
* Category: `authorization_bypass` / `privilege_escalation`
* Confidence: 0.9

Under the heading stood a description, an attack scenario and a recommendation. In short: sign-up is open and doesn't check the mailbox, and getCurrentMember() links an account to a member by the address alone. Whoever registers redbeard@pirates-cove.test first becomes the owner of the server with all permissions. The report advised closing sign-up (disableSignUp) and creating the crew accounts outside the page, or requiring address confirmation (requireEmailVerification).

A valid alarm or a false one

A report is a scout's message, not a verdict. You check every finding the same way as the report about the routes in lesson 8_3: you open the indicated file in Cursor, read the line and try to reproduce the attack.

This alarm is valid, and the proof is easy to get: a bare curl request created the account jack@pirates-cove.test for us, without any mailbox, and immediately received a session cookie. So every address from lib/crew-accounts.ts that nobody has claimed yet waits for the first taker.

A false alarm looks different: the finding describes a line that isn't in the file, or an attack whose path is closed by another piece of code, for example a lock in a route that the review didn't notice. Then you write down what you rejected it with and you don't ask for a fix.

A report is also not a complete list. The second candidate was the crew list from lesson 8_5, which hands out addresses without a session. For us the log of the run showed that the verifying agent rated it TRUE POSITIVE (low-impact), confidence 6/10, and that finding didn't make it into the final report. For your crew it is a hole all the same, because people's addresses shouldn't hang in public. The report says what the review is sure of, and what is a hole in your project is decided by you.

Fix only what you accept

You don't write "fix everything". You point at the items you have checked and demand proof. For us the first was the item about the crew list, and you put in an item from your own report:

Request for Claude Code:

Fix only the /api/crew item from the security review: the route must
check the session with getCurrentMember() from @lib/session.ts and
return 401 without a session. Add a test in app/api/crew/route.test.ts:
a request without a session gets 401, a signed-in member gets the list.
Do not touch other files. At the end run tests, lint and a curl on
/api/crew without a cookie.

In Cursor you open Source Control and read the diff: the change should concern one route and one new test. For us the test failed without the fix with AssertionError: expected 200 to be 401, and with the fix all 21 tests passed. curl without a cookie got 401 and {"error":"Sign in"}.

You fix the item from the report with a second, separate request, because it concerns another file:

Request for Claude Code:

Fix Vuln 1 from the security review: in @lib/auth.ts set
disableSignUp: true and remove the Create account button from
app/sign-in/page.tsx. Leave lib/session.ts unchanged. At the end
run a curl on sign-up and on signing in with an existing account.

Create the crew accounts you need before this change. For us sign-up then answered 400 with {"message":"Email and password sign up is not enabled","code":"EMAIL_PASSWORD_SIGN_UP_DISABLED"}, and Anne signed in as before. Before deployment one question remains: who creates accounts on an empty database. The report advised a script or an admin plugin and linking the member to an account identifier instead of an address: that is a separate task, which you start with /plan.

/code-review: a second pair of eyes

/security-review looks for holes, and /code-review from lesson 2_9 looks for bugs in how things work. The review runs in the background as a separate agent with its own context and without --fix it only reports. On the login branch run it after the fixes, because a security fix can be wrong too. You still make the decision what to fix.

The agent fixed too much

It happens that after "fix the /api/crew item" Claude rewrites lib/session.ts along the way, changes the moderation routes and adds a new dependency. The diff grows and you don't know what still works. You then go back with the net from lesson 0_6. Pressing Esc twice on an empty field or /rewind opens the list of your requests. You pick the request with the fix and Restore code: files changed by Claude's tools return to the state before that request, and the conversation stays.

The documentation names two limits. A file changed only by Bash commands is not tracked. Changes made by a background subagent, for example /code-review --fix, in files that Claude had not touched earlier in this conversation also stay. You restore such files from the last commit in Git. Then you give a narrower request with a list of files that may be changed.

Locks are not a review

The deny rule from lesson 8_4 forbids Claude to read .env.local. Auto mode has a classifier that, according to the documentation, blocks among other things sending sensitive data to external addresses, production deployments and deleting a test that guards security. Both mechanisms watch what the agent does in your system. Neither of them reads the /api/crew route or notices that it hands out addresses to anyone. A hole in logic is found by a code review: yours, /security-review and tests.

Captain's review

  1. Every finding checked. For every item of the report you have a file, a line and your own proof: curl, a test or code you read.
  2. A fix with a test. Every accepted item gets a test that fails without the fix.
  3. A diff within bounds. The fix changes only the files from the request; the rest comes back through /rewind or Git.
  4. A second pass. After the fixes /security-review once more and /code-review.

Next step

Ask for a commit, for example "Security review fixes: crew list behind a lock, sign-up closed". You send your mentor a screenshot of the running server, the result of /security-review and one sentence about what you fixed after the review.

In the editor below a review report to defuse is waiting: judge each finding, check it with a request to a pretend server and see what is left after /rewind.

Remember: the guard at the gangway and the guard in the hold are two different guards, and the review is the captain who checks both every evening.

Code for this lesson: App.jsx
// Defusing a security review report on a pretend Pirate's Cove server.
// Findings 1 and 2 sum up our real /security-review run, finding 3 is a practice one.
import { useState } from 'react';

const findings = [
  {
    id: 1,
    title: 'Open sign-up: the member is chosen by the e-mail address alone',
    source: 'final report, High',
    file: 'lib/session.ts:11',
    truth: true,
    check: 'curl -X POST /api/auth/sign-up/email {"email":"mary@pirates-cove.test"}',
    answer: (fixed) => fixed
      ? '400 {"code":"EMAIL_PASSWORD_SIGN_UP_DISABLED"}'
      : '200 + session cookie. The new "Mary" has officer permissions.',
  },
  {
    id: 2,
    title: '/api/crew hands out the crew addresses without a session',
    source: 'candidate dropped by the filter (6/10)',
    file: 'app/api/crew/route.ts:4',
    truth: true,
    check: 'curl /api/crew (no cookie)',
    answer: (fixed) => fixed
      ? '401 {"error":"Sign in"}'
      : '200 [redbeard@..., anne@..., jack@..., mary@..., blackbeard@...]',
  },
  {
    id: 3,
    title: 'The kick route does not check permissions (practice finding)',
    source: 'practice',
    file: 'app/api/members/[id]/kick/route.ts:9',
    truth: false,
    check: 'curl -X POST /api/members/blackbeard/kick as jack',
    answer: () => '403 {"error":"Missing permission KICK_MEMBERS"}',
  },
];

const checkpoint = ['app/api/crew/route.ts', 'lib/auth.ts'];
const tooWide = ['lib/session.ts', 'app/api/members/[id]/timeout/route.ts', 'package.json (npm install in Bash)'];

export default function App() {
  const [verdicts, setVerdicts] = useState({});
  const [checks, setChecks] = useState({});
  const [fixed, setFixed] = useState(false);
  const [changed, setChanged] = useState([]);

  const accepted = findings.filter((f) => verdicts[f.id] === true);

  function verify(f) {
    setChecks((prev) => ({ ...prev, [f.id]: f.answer(fixed && f.truth && verdicts[f.id] === true) }));
  }

  function applyFixes() {
    setFixed(true);
    setChanged([...checkpoint, ...tooWide]);
    setChecks({});
  }

  function rewind() {
    // Restore code reverts files changed by Claude's tools; a file changed only by a Bash command stays.
    setChanged((prev) => prev.filter((file) => file.includes('Bash')));
    setFixed(false);
  }

  return (
    <div className="min-h-screen space-y-4 bg-slate-100 p-4 font-sans text-slate-900">
      <h1 className="text-lg font-bold">/security-review report: the login branch</h1>
      {findings.map((f) => (
        <section key={f.id} className="space-y-2 rounded-lg border border-slate-300 bg-white p-3">
          <h2 className="font-bold">{f.id}. {f.title}</h2>
          <p className="text-xs text-slate-500">{f.source} Β· {f.file}</p>
          <div className="flex flex-wrap gap-2 text-sm">
            <button onClick={() => verify(f)} className="rounded bg-slate-700 px-3 py-1 text-white">Check: {f.check}</button>
            <button onClick={() => setVerdicts((v) => ({ ...v, [f.id]: true }))} className="rounded bg-red-600 px-3 py-1 text-white">Valid alarm</button>
            <button onClick={() => setVerdicts((v) => ({ ...v, [f.id]: false }))} className="rounded border border-slate-400 px-3 py-1">False alarm</button>
          </div>
          {checks[f.id] && <pre className="rounded bg-slate-900 p-2 text-xs text-green-200">{checks[f.id]}</pre>}
          {verdicts[f.id] !== undefined && (
            <p className={'text-sm ' + (verdicts[f.id] === f.truth ? 'text-green-700' : 'text-red-700')}>
              {verdicts[f.id] === f.truth ? 'Matches the evidence.' : 'Check again: the evidence says otherwise.'}
            </p>
          )}
        </section>
      ))}

      <section className="space-y-2 rounded-lg border border-slate-300 bg-white p-3">
        <h2 className="font-bold">Fixes</h2>
        <p className="text-sm">Accepted findings: {accepted.length ? accepted.map((f) => f.id).join(', ') : 'none'}</p>
        <div className="flex flex-wrap gap-2 text-sm">
          <button disabled={!accepted.length} onClick={applyFixes} className="rounded bg-indigo-600 px-3 py-1 text-white disabled:opacity-40">Claude fixes the accepted items</button>
          <button disabled={!changed.length} onClick={rewind} className="rounded border border-slate-400 px-3 py-1 disabled:opacity-40">/rewind β†’ Restore code</button>
        </div>
        <ul className="list-disc pl-5 font-mono text-xs">
          {changed.map((file) => <li key={file} className={checkpoint.includes(file) ? '' : 'text-red-700'}>{file}{checkpoint.includes(file) ? '' : '  (outside the request)'}</li>)}
        </ul>
        {changed.length > 0 && changed.every((file) => file.includes('Bash')) && (
          <p className="text-sm text-red-700">The file changed only by a Bash command is still there. Restore it from the last commit in Git.</p>
        )}
      </section>
    </div>
  );
}

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. The rule Read(./.env.*) is in .claude/settings.json. What happens when Claude in auto mode tries to read .env.local with the Read tool?

  2. 2. Where do you keep the key to the weather API in Pirate's Cove?

These are 2 of 6 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Horizontal ordering

    Arrange a deny rule that forbids reading the whole secrets folder: Read(./secrets/**)

  • Vertical ordering

    Arrange the steps of working with a security review on the login branch:

  1. QuestionQuiz: The deny rule in auto mode
  2. Put in orderOrdering: A rule for the secrets folder
  3. QuestionQuiz: Where the API key lives
  4. QuestionQuiz: The payment amount in Stripe
  5. QuestionQuiz: What /security-review checks
  6. Put in orderOrdering: Working with a security review
  7. QuestionQuiz: A lock only in the component
  8. QuestionQuiz: Undoing a change that went too wide
  9. Project to submitSend Pirate's Cove to mentor
  10. VideoModule summary

Useful articles