Day 8 of 960 minCipher CadetHands-on lab

Day 8: Debug, Test and Review AI Code Securely

Taught by Mr Elite Updated

By the end of today you can

  • Ask Kiro for a root cause analysis before it changes any code
  • Write a regression test that fails without the fix and passes with it
  • Use checkpoints and permissions to keep agent runs reversible and safe
  • Review generated auth and dependency code against a short security checklist

Warm-up

Answer from memory first, then tap to check. Recalling yesterday is what makes it stick.

Where does Kiro store its MCP configuration, and which location is checked first?

In .kiro/settings/mcp.json, with the workspace config taking priority.

Why should you read an MCP tool's description yourself before using it?

The agent reads descriptions as instructions, so a malicious or careless one can steer the agent.

Picture this. The agent writes a login feature in under a minute. The code is tidy, it has comments, and every test it wrote passes. You feel pleased and you merge it. Two weeks later someone finds that a password reset token never expires.

Passing tests only prove the code does what the tests ask. Today I will show you how to make the agent explain a bug before it fixes it, prove the fix with a test, and then read the result the way a defender would. You will practise on a deliberately weak demo app on your own machine, then on two SecurityElites labs.

Root cause first, patch second

When something breaks, the lazy move is to paste the error and say "fix it". The agent will often change whatever makes the error go away. That can hide the real problem or add a new one.

A root cause is the single underlying reason a bug happens, as opposed to the symptom you can see. I want you to ask for the cause in words first, read it, and only then allow a change. This is the same habit a security analyst uses when triaging an incident: understand it, then act.

Symptom
What you can see go wrong, such as an error message or a wrong result.
Root cause
The underlying reason the symptom appears. Fix this and the symptom goes away for good.
Diff
A side by side view of what a change removed and added, so you can read it before accepting.

Here is the prompt I use. Open the Kiro chat panel, paste it, and then add the error text or a description of the odd behaviour underneath. The first line matters most, because it tells the agent to talk and not to touch your files.

text
Do not change any files yet.
Explain the root cause of this bug.
Name the file and line, say why it happens,
and list two possible fixes with their risks.
A prompt for Kiro chat that forces analysis before action. Paste it, then add the error or behaviour below it.

A good answer names a real file and line, explains the logic in plain words, and offers two fixes with a trade-off for each. A weak answer says something vague like "there may be an issue with validation". If you get the weak kind, reply with "Be specific: which line, and why?" before going any further.

Quick check

Why ask for a root cause before allowing any edit?

Regression tests prove the fix

A regression test is a small test that fails while the bug exists and passes once it is fixed. It stays in the project and warns you if the bug ever returns. The rule I follow: no fix counts until I have seen the test fail first.

Think of it as red, then green. Red means the test fails and proves the bug is real. Green means it passes after the fix. If you skip red, you cannot be sure the test checks the right thing. A test that was never red might be testing nothing at all.

Regression
A bug coming back after it was fixed.
Checkpoint
A saved point in your Kiro chat and file changes that you can roll back to.
Permission prompt
A request from the agent to run a command or edit something, which you approve or deny.

Checkpoints and permissions make this safe. Before a risky run, note the checkpoint so you can restore the files. Keep command approval on, and read each command before you click approve. Never approve something you do not understand.

When a permission prompt appears, look at three things. What is the command? Which folder will it run in? Does it reach out to the internet or delete anything? If the answer to any of those surprises you, deny it and ask the agent to explain why it needs it.

Quick check

You add a test and it passes straight away, before you apply the fix. What does that tell you?

Watch me do it: fix a weak login locally

We will make a tiny demo with one flaw: it has a shortcut that lets an empty password through for a real user. Everything runs on your own machine and touches nothing else. You need Node.js 18 or newer, which you installed earlier in the course.

bash
mkdir weak-login
cd weak-login
node --version
Makes the demo folder and confirms Node is installed.
javascript
const users = { alice: 'correct-horse' };

function login(name, password) {
  if (!users[name]) return false;
  return password == users[name] || password === '';
}

module.exports = { login };
auth.js. The flaw is the extra || password === '' which accepts a blank password for any real user.
javascript
const test = require('node:test');
const assert = require('node:assert');
const { login } = require('./auth');

test('right password works', () => {
  assert.strictEqual(login('alice', 'correct-horse'), true);
});

test('empty password is rejected', () => {
  assert.strictEqual(login('alice', ''), false);
});
auth.test.js. The second test is the regression test. It fails until the blank-password shortcut is removed.

This is what the fixed function should look like when you read the diff. Yours may differ slightly in layout, and that is fine, as long as the blank-password shortcut is gone and the comparison is strict.

javascript
function login(name, password) {
  if (!users[name]) return false;
  return password === users[name];
}
The corrected login(). Strict equality means the types must match too, and the blank shortcut no longer exists.

Notice what happened. The first test passed the whole time, so a lazy check would have said the code was fine. Only the second test, written to target the flaw, exposed it. Also notice that passwords sit in plain text in this file. That is a second weakness, and you will deal with it in the mission.

Reviewing generated auth code

Auth code is where AI mistakes cost the most, because it looks finished. I read it with a short checklist, one question per line, and I never skip a line because the code looks neat. Work down the list in order each time and it becomes automatic.

Here is the kind of thing the checklist catches. Read this snippet slowly before you read my notes underneath. It is for recognition only, so do not copy it into a real project.

javascript
function makeResetToken(user) {
  const token = Math.random().toString(36).slice(2);
  resetTokens[user] = token;
  return token;
}
A weak reset token generator you might see in generated code. Use it only to practise spotting problems.

I count three problems. Math.random is not designed for secrets, so the token can be predicted. There is no expiry time stored, so it works forever. Nothing marks it as used, so it can be replayed. A safer version uses crypto.randomBytes, stores an expiry, and removes the token once it is used.

Kiro also offers custom agents and Powers. A custom agent is a version of the assistant set up for one job, such as a reviewer with a fixed brief. A Power bundles tools and guidance for a specific area. Both add capability, so both need the same caution as MCP servers: read what they do and give them least access.

Quick check

Which finding is the most serious in generated reset code?

Today's labs

There are two labs. Start with the beginner one, ai-review-dependency-check-1, then try the advanced one, ai-review-session-code-1, for a harder review. Both are safe practice material provided by SecurityElites, and you never touch a real system.

For the first lab, you are given an AI reply that suggests one install command. Your job is only to read and judge, not to run anything. Copy the package names into a notes file, look each one up, and write a one line reason beside any you would refuse. Work through the hints below, and do not rush to a quick answer.

For the advanced lab, open the code, then walk the five checklist lines in order. Write one finding per line, even if the finding is that the line looks fine. That written record is the habit I want you to build.

Your turn

Check Before You Install

An AI assistant gave you one install command with seven packages, and three of them should not be installed. This is exactly the review skill from today applied to dependencies, and it is the gentler starting point before the advanced session-code lab.

Open the lab in a new tab

What to try

What to notice

  • Names that look almost right are a warning sign, not a coincidence.
  • A tidy, confident reply from an assistant tells you nothing about whether it is safe.
  • Your reasons are stronger when they point to something you can verify, not a feeling.

If it did not work

Letting the agent fix the bug straight away

Ask for root cause with no edits first, then approve a small change after reading it.

Writing a test that never fails

Run the test on the broken code and confirm it goes red before you fix anything.

Approving commands without reading them

Keep permission prompts on and read every command. Deny anything unclear.

Trusting auth code because the tests pass

Review with the checklist. Tests only cover what someone thought to test.

Installing every package the agent lists

Look each one up on the registry and remove any you cannot justify.

Recap

  • Ask for root cause in words before any code changes
  • A regression test must fail first, then pass after the fix
  • Checkpoints and permission prompts keep agent runs reversible and visible
  • Passing tests do not prove code is secure
  • Review auth code for hashing, token expiry, reset flows and leaky errors
  • Custom agents and Powers add capability, so give them least access

Cheat sheet

Root cause prompt
Ask for file, line, reason and two fixes, with no edits yet
node --test
Runs Node's built-in tests in the current folder
Regression test
Test that fails on the bug and passes on the fix
Checkpoint
Restore point for chat and file changes
Permission prompt
Your chance to approve or deny a command
Auth checklist
Passwords, tokens, reset flow, errors, dependencies

Before the next day

Mission: Fix and prove the weak login

Using the weak-login demo from today, plus one more flaw you find yourself, fix the problems with Kiro and write up what happened. The point is to show you can explain a cause, fix it and prove it with a test.

About 35 min

You are done when: A short write-up with the failing and passing test output, the diff of your fix, and the name of the regression test.

Want more? Ask a custom reviewer agent to critique your fix, then decide which of its points you agree with and why.

Quiz

4 questions. Your score is saved with your progress.

1. Why is it risky to tell the agent only "fix this error"?

2. What makes a regression test valuable as proof?

3. Generated auth code passes all its tests. What should you do next?

4. An assistant lists seven packages to install. What is the safest habit?

Finish the day

Mark the day complete to save your progress.

Go to Day 9

Next: Day 9 is the capstone. You will bring specs, steering, a hook, MCP and today's review habits together to ship a tested release candidate of a security dashboard.

Tell someone you are doing this:WhatsAppXLinkedIn

Your notes

Private to this device. Write what you want to remember from today.