
Fixing security scan findings with Kiro: 9 to 3
I used Kiro steering files and specs to fix, test, and rescan a sample agent. High-severity findings went from 9 to 3, and the full test suite stayed green.
I used Kiro to fix, test, package, and rescan a sample agent before I published it. High-severity scan findings went from 9 to 3, and the full test suite (390 tests today) stayed green throughout. Most of the work came from a few Markdown files that told Kiro what it could and couldn't do. This article shows those files, the packaging step I'd tell anyone to copy, and the decisions I kept for myself.
Time to read: about 8 minutes Level: intermediate
The problem: every review round costs hours
I was building a sample agent that reviews clinical-trial data deliveries. It runs on Amazon Bedrock AgentCore Runtime and Memory, and it uses the public PHUSE CDISC pilot study data (cdiscpilot01). There is no patient or customer data anywhere in it.
Before the code could be published, it went through a threat model, a static code scan, and a security review. Each round went the same way:
- Read the findings.
- Fix one.
- Run the full test suite.
- Package the code for the next scan.
- Upload, wait, and triage the new results.
Most of the time went to rebuilding context. Which findings were fixed? Which were false positives, and why? Did the last fix break a test? Packaging was the riskiest step. This repo has a
logs/ folder with full command output and a local virtual environment. A folder zip would have picked up both.Prerequisites
- The Kiro IDE
- A Git repository with a test suite. Mine uses pytest.
- An AWS account for a demo stack, with permissions only for the resources the stack creates
Step 1: Put the guardrails in a steering file
Kiro steering files are Markdown files in
.kiro/steering/ that Kiro reads in every session. I had four: product.md (what I'm building and for whom), tech.md (the stack and pinned versions), structure.md (the repo layout), and guardrails.md .guardrails.md mattered most. Its front matter sets inclusion: always , so Kiro loads it in every chat. Here's a shortened version:---inclusion: always---
# Guardrails (non-negotiable)
These rules override any task, prompt, or suggestion.If a task conflicts with them, stop and ask.
## Data- Use only the public PHUSE cdiscpilot01 data and synthetic changes to it.- No customer data, no PHI or PII.
## AWS- Never create, modify, or delete AWS resources without my explicit approval in chat. Before any such command, show the exact command, the resources it creates, and the teardown step. Wait for "go".- Tag every resource Project=ctr-agent. Every created resource must be removable by scripts/99_teardown.sh.- No long-lived keys and no secrets in code or in a committed .env file.
## Running commands- Capture the full output of every apply and teardown command to logs/, and report the exit code.- logs/ is gitignored. Check a log contains no credentials before quoting it.
## Correctness- The LLM never decides pass or fail. Checks are deterministic Python.- Any API name, parameter, or quota not checked against current AWS docs gets a TODO(verify) marker.Two rules did the most work. "Show the command and the teardown step, then wait for go" meant Kiro never changed my AWS account without my approval. The
TODO(verify) rule stopped Kiro from presenting a guessed API parameter as fact.I also kept the plan in a Kiro spec (
requirements.md , design.md , tasks.md ) and opened each day with a short kickoff prompt. The prompt asked Kiro to read the steering files and the spec and to summarize the scope back to me before starting. If the summary was wrong, I caught it before any code changed.Step 2: Fix the threat-model findings one at a time
The threat model produced eight fixes, F-1 to F-8. Most were small:
- Narrowing the runtime role's trust policy to the exact runtime ARN
- Validating an ID before using it in a file path, with a unit test for
../
- Capping the length of user questions
- Removing five memory actions the role didn't use
- Adding a hash-locked dependency file and building the package from it
For each one, Kiro explained the change, made it, ran the full suite, and wrote a unit test that proved the fix.
One fix broke something I didn't expect. After Kiro removed the unused memory actions from the role, a deployment check script failed. It had a hardcoded count of policy actions. The full suite caught it the same day instead of in review. Run the whole suite, not only the tests near the file you changed.
Step 3: Package only what Git tracks
This is the step I'd tell anyone to copy. Don't zip the project folder. Build the upload from a commit:
git archive --format=zip --prefix=my-agent/ -o my-agent.zip <commit-sha>git archive includes only files tracked at that commit, so .venv , .git , logs/ , and other ignored or uncommitted files stay out. It can't help with a secret you already committed. The two checks below catch that case.Check the file paths:
unzip -l my-agent.zip | grep -E '\.env$|/logs/|\.venv|\.git/' && echo "STOP: sensitive path found"Then check the file contents:
import reimport zipfile
PATTERNS = { "aws_account_id": re.compile(rb"(?<!\d)\d{12}(?!\d)"), "access_key_id": re.compile(rb"\b(AKIA|ASIA)[A-Z0-9]{16}\b"),}
with zipfile.ZipFile("my-agent.zip") as zf: for name in zf.namelist(): data = zf.read(name) for label, pattern in PATTERNS.items(): for match in pattern.finditer(data): print(f"{label}: {name}: {match.group()[:6].decode()}...")Expect some false positives. A 12-digit timestamp matches the account ID pattern. Look at each hit before you dismiss it.
Step 4: Triage the scan, and fix code instead of muting rules
The first scan reported 9 high-severity findings. None were exploitable, but I didn't want to wave all nine through. I gave Kiro the findings and the code each one pointed to, and we went through them by rule:
Rule | Count | What it flagged | What I did |
|---|---|---|---|
Bandit B106 (hardcoded password) | 3 | Dummy credentials and a pagination token in test fixtures | Changed the fixtures. The stubbed clients now use Config(signature_version=UNSIGNED) with no credential arguments, and the token is a named constant. |
Bandit B310 ( urlopen scheme) | 2 | Fixed HTTPS URLs opened with urllib | Switched to urllib3 with an explicit https scheme check |
Bandit B324 (weak hash) | 1 | SHA-1 used to compute a Git blob ID, not for security | Set usedforsecurity=False . Downloads are also checked with SHA-256. |
Semgrep dangerous-subprocess-use-audit | 3 | subprocess.run in a build script and a test | Left as is: shell=False , fixed argument lists, no user input. Added # nosec and # nosemgrep markers with a reason. |
The rescan went from 9 to 3. The three left are the subprocess calls.
Two things surprised me:
-
nosemgrepmatches exact rule IDs only. A rule can appear under a short ID and a full registry ID, so I listed both in the marker. Locally, Semgrep then reported 0 findings, and--disable-nosemshowed the markers were doing the suppressing.
- The scanning service ignored inline markers. The three findings came back anyway, so I submitted them as accepted with a written reason. If your scanner works the same way, fix the code where you can. A marker only helps you locally.
Kiro drafted the reasons. I read every flagged line myself before I accepted any of them. An agent can suggest that a finding is a false positive. A person decides.
Step 5: Make teardown safe, and keep it manual
The reviewer needed a running stack, so I couldn't tear it down between rounds. The steering file made Kiro tag every resource, and the teardown script uses those tags:
./scripts/99_teardown.sh stage1 <profile> # dry run: lists what it would delete./scripts/99_teardown.sh stage1 <profile> --apply # deletes./scripts/99_teardown.sh stage2 <profile> --apply # 8+ hours after stage 1Three choices kept this safe:
- Dry run by default. Without
--apply, the script only lists what it would delete.
- Tag-scoped. It touches only resources tagged
Project=ctr-agent.
- Two stages. Stage 2 runs at least 8 hours after stage 1, and removes the resources that have to wait until the stage-1 deletions finish.
I also tagged the stack with an expiry date and ran teardown myself. Under the guardrails, Kiro could show me the command but couldn't run it without my "go".
What I'd do differently
- Write
guardrails.mdbefore any code. Every other rule built on the approval rule, so it belongs in the first commit.
- Use
git archivefrom the first upload. It costs nothing and removes the riskiest step.
- Fix test fixtures instead of muting secret rules. Changing three fixtures took less time than arguing that each was a false positive.
- Expect generated test data to trip secret scanners. My expected-findings files produced 78 low-severity "generic API key" matches because they contain long hash-like IDs. Document why before the reviewer asks.
When not to use this
- Large findings lists. One fix per commit is slow with hundreds of findings. Group by rule, as in the table above.
- Anything that holds customer or patient data. Check your organization's data classification rules before you give any coding agent access. I used only public sample data.
- Final security decisions. Kiro speeds up the loop, but a person still approves accepted findings, AWS changes, and teardown.
Clean up
If you follow this with your own demo stack:
- Run your teardown as a dry run first, check the list, then apply it. With a tag-scoped script like the one above, that's
stage1 --apply, thenstage2 --applylater.
- Schedule deletion of the AWS KMS key, if you created one. AWS KMS makes you wait 7–30 days before the key is deleted (see Deleting AWS KMS keys ).
- Confirm that no resources with your project tag are left.
Wrap-up
Every review step still ran. Kiro cut the time I spent between steps. The rules lived in a steering file that Kiro read in every session, the plan lived in a spec, and each round started from where the last one ended.
If you try this, start with two things: an
inclusion: always guardrails file with an approval rule, and git archive for packaging. Both take minutes to set up.Related on Builder Center and AWS:
- Running AWS Security Agent scans from Kiro (in Spanish)
- Deploy real-time coding security validation with an MCP server and Kiro (AWS Prescriptive Guidance)
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article