secure coding exercises
Secure coding exercises for OWASP Top 10 2025
Four secure coding exercises to train the reviewer's eye: real code, no flaw announced, solutions mapped to OWASP Top 10 2025.
- By Azziz ERRIME
- 9 min read
Contents
- Four secure coding exercises to try before reading on
- Solutions: the flaw, the CWE and the OWASP category
- Free secure coding exercises, and what each one trains
- “Our developers already do Juice Shop”
- How to run secure code review practice as a team
- Back to the invoice route
- Questions frequently asked about secure coding exercises
- Sources
TL;DR
- Good secure coding exercises train the reviewer’s eye: real code, no flaw announced, an answer only afterwards.
- In a 2022 experiment, telling reviewers to look for security made them eight times more likely to find the vulnerability.
- I would start with the four snippets below: ten minutes each, no hint, solutions after.
The first snippet below is an eight-line route that would pass most reviews. That is the point of secure coding exercises built for reviewers: you read real code with no flaw announced, you decide which line is wrong, then you check.
Four secure coding exercises to try before reading on
Exercise 1 is the one I would put in front of any reviewer first. It is authenticated, it is short, and every line is correct on its own. Read each snippet as if it sat in a pull request. Give yourself ten seconds, name the line, then move on. The answers are in the next section.
Exercise 1: the invoice route
An Express API returns an invoice by its ID. You have to be logged in to call it.
// GET /invoices/1042
router.get('/invoices/:id', requireAuth,
async (req, res) => {
const id = req.params.id;
const invoice = await Invoice.findById(id);
if (!invoice) return res.sendStatus(404);
res.json(invoice);
});
Can you spot it?
Exercise 2: the search box
A Node.js API looks up a user by name, typed into a search box. File: routes/search.js.
// GET /search?q=alice
router.get('/search', requireAuth,
async (req, res) => {
const q = req.query.q;
const { rows } = await db.query(
`SELECT * FROM users WHERE name = '${q}'`);
res.json(rows);
});
Can you spot it?
Exercise 3: the new dependency
A coding assistant added a dependency to the package.json of a signup API.
{
"name": "signup-api",
"scripts": { "start": "node server.js" },
"dependencies": {
"express": "^4.21.2",
"zod": "^3.23.8",
"express-validate-guard": "^1.0.2"
}
}
Can you spot it?
Exercise 4: the auth middleware
This middleware reads the JWT on every request. Only admins should reach /admin. File: middleware/auth.js.
const jwt = require('jsonwebtoken');
function auth(req, res, next) {
const h = req.headers.authorization || '';
const token = h.split(' ')[1];
if (!token) return res.sendStatus(401);
req.user = jwt.decode(token);
next();
}
Can you spot it?
Solutions: the flaw, the CWE and the OWASP category
Each solution gives the flaw, its classification and the corrected code. The categories come from the final OWASP Top 10:2025 list.
Solution 1: IDOR, A01:2025 Broken Access Control
The line is Invoice.findById(id). The route checks that someone is logged in, never that the invoice belongs to them. Alice reads invoice 1042, changes the URL to 1043 and receives Bob’s invoice. This is an insecure direct object reference, CWE-639, Authorization Bypass Through User-Controlled Key, and its family, Broken Access Control, sits at A01 in the 2025 list.
const invoice = await Invoice.findOne({ _id: id, ownerId: req.user.id });
The same request for 1043 now returns 404. Three rules to keep: the ID comes from the client, so never trust it; authenticated does not mean authorised, so check the owner on every request; and if it is not theirs, answer 404 without revealing that the resource exists.
What makes this one hard in review is that nothing is wrong. A condition is missing, and a missing condition never shows up as a changed line.
Solution 2: SQL injection, A05:2025 Injection
The line is the template literal: '${q}'. The input is pasted straight into the query, so it becomes SQL. Alice searches for ' OR '1'='1 and gets the whole users table instead of one row. This is CWE-89.
const { rows } = await db.query(
'SELECT * FROM users WHERE name = $1', [q]);
With a parameterised query, the value travels separately and is never executed: the same search returns 0 rows. Never concatenate input into a query; use parameterised queries or an ORM every time; and give the database account only the rights the route needs.
Solution 3: the hallucinated package, A03:2025 Software Supply Chain Failures
The line is "express-validate-guard". The AI made the name up. In this exercise, someone has since published a package under that name, with a postinstall script. On npm install, that script reads ~/.npmrc and posts the npm token to an outside server. The attack has a name, slopsquatting, and no dedicated CWE; it falls under A03:2025 Software Supply Chain Failures.
It is not a corner case. Spracklen and colleagues (USENIX Security 2025) found that 19.7% of the packages recommended by 16 LLMs do not exist.
"express-validator": "^7.2.0"
A well-known library, pinned in the lockfile, and install scripts turned off in CI with npm ci --ignore-scripts. Review a new dependency like you review code: check its age, author and downloads before installing. I go further into this in how to review AI-generated code.
Solution 4: the unverified JWT, CWE-347
The line is jwt.decode(token). It reads the payload and skips the signature. Bob’s token says "role": "user", so /admin answers 403. He edits the payload to "role": "admin" and gets the admin panel. This is CWE-347, Improper Verification of Cryptographic Signature.
try { req.user = jwt.verify(token, SECRET,
{ algorithms: ['HS256'] }); }
catch { return res.sendStatus(401); }
jwt.verify checks the signature and enforces the algorithm, so a tampered token is rejected with 401. Decoding is not verifying. Enforce the algorithm and never let the token pick it. Read the role on the server, never from what the client sends.
A new snippet in the same format, Find the Spot, comes out every Tuesday.
Free secure coding exercises, and what each one trains
There is no shortage of free material. The useful question is what each resource trains: exploiting a flaw you were told is there, or reading code until you find one nobody announced.
- PortSwigger Web Security Academy: free labs on injection, access control, JWT and more. It trains exploitation, very well.
- OWASP Juice Shop: a deliberately vulnerable application, free and open source. It trains finding and attacking flaws in a running app.
- OWASP WebGoat: free guided lessons on SQL injection, access control and JWT. It trains understanding a class of flaw, step by step.
- OWASP Code Review Guide: a free method for secure code review. It is a guide, not labs, and the only one here built around reading code.
- Hack The Box Academy: modules partly free, the rest paid. It trains offensive skills.
One paid option for comparison: SecureFlag offers hands-on labs in real environments, more than 75 languages and frameworks, cloud templates such as Terraform and CloudFormation, on an enterprise plan. I found no free tier.
My reading of this list: almost everything free teaches attack, and almost nothing gives secure code review exercises, where the snippet arrives without a label. That gap is why the four exercises above exist.
“Our developers already do Juice Shop”
Good. Keep it. A developer who has exploited an injection once recognises it faster. But a lab announces the category before you start: the page says “SQL injection”, and you go looking for the quote that breaks the query. A pull request announces nothing. The reviewer has to notice that something is off in code that runs, passes tests and looks like every other route.
These are two different skills. Attacking a known flaw is one; seeing an unannounced flaw is the other. A good lab score tells you about the first. I argue in why a secure coding training score says little about vigilance that it says almost nothing about the second.
So I would not replace the labs. I would add the missing half: secure code review practice on real snippets, with nobody telling you which category to expect.
How to run secure code review practice as a team
The cheapest lever is also the best documented. In a 2022 experiment, Braz and colleagues found that explicitly asking developers to focus on security made them eight times more likely to detect the vulnerability during review. Same people, same code: the instruction changed what they looked for.
Here is how I would run it, in about fifteen minutes a week:
- One snippet per session. Eight to fifteen lines, shown in the review tool your team uses, not on a slide.
- Say “think security” out loud. That is the instruction Braz measured. Do not name the category.
- Everyone writes down a line number before anyone speaks. It stops the first voice from deciding for the room.
- Reveal in four parts: the flaw, the CWE, the OWASP Top 10 category, the fix in code.
- Record what was missed, not who scored. A category the whole team misses twice is a review checklist item. More on that in measuring whether security training works.
- Rotate the author. After a month, someone brings a snippet from your own stack, cleaned of anything sensitive.
The secure coding examples above are a start. They work best when the next ones come from your own codebase.
Back to the invoice route
Invoice.findById(id) sits there, correct, in a route protected by requireAuth. Every line of it would pass a quick look. The flaw is what is not written: the ownership check.
That is my conclusion from these four exercises. The dangerous bugs in review are rarely the strange lines. They are ordinary lines with a missing question behind them: whose invoice is this, where does this string go, who published this package, who signed this token. Labs teach you to answer those questions when asked. Secure coding exercises for reviewers teach you to ask them yourself.
Ask them on the next pull request.
Questions frequently asked about secure coding exercises
What is secure coding practice?
Secure coding practice means writing and reviewing code so that common flaws never ship: checking ownership on every request, parameterising queries, verifying token signatures, vetting dependencies. The OWASP Top 10:2025 is the usual map, with Broken Access Control at A01 and Injection at A05. Practising means reading real code until those checks become reflexes.
Where can I practice secure code review?
Most free platforms, such as PortSwigger Web Security Academy and OWASP Juice Shop, train exploitation of a flaw you know is there. For review, start with the free OWASP Code Review Guide for method, then practise on short snippets with no category announced, like the four exercises in this article.
What are examples of secure coding practices?
Four from the exercises above: look up a resource with the owner in the query, not by ID alone; pass user input as a query parameter, never inside the SQL string; check a new dependency’s age, author and downloads before installing it; and call jwt.verify with a fixed algorithm instead of jwt.decode, which skips the signature (CWE-347).
Are there free secure coding exercises?
Yes. PortSwigger Web Security Academy, OWASP Juice Shop and OWASP WebGoat are free, and Hack The Box Academy is partly free. They focus on secure coding challenges from the attacker’s side. The four snippets in this article are free too, with solutions mapped to OWASP Top 10:2025.
Sources
- OWASP Top 10:2025, final list (A01 Broken Access Control, A03 Software Supply Chain Failures, A05 Injection…): OWASP Top 10:2025 Introduction, 8th edition. top10.owasp.org
- Slopsquatting under A03:2025, no dedicated CWE: OWASP Top 10:2025. top10.owasp.org
- IDOR = CWE-639: MITRE CWE, Authorization Bypass Through User-Controlled Key. cwe.mitre.org
- SQL injection = CWE-89: MITRE CWE. cwe.mitre.org
- Unverified JWT signature = CWE-347: MITRE CWE, Improper Verification of Cryptographic Signature. cwe.mitre.org
- 19.7% of packages recommended by 16 LLMs do not exist: Spracklen et al., USENIX Security 2025. arxiv.org
- Asking reviewers to focus on security makes detection eight times more likely: Braz et al., 2022. arxiv.org
- PortSwigger Web Security Academy, free labs. portswigger.net
- OWASP Juice Shop, free and open source. owasp.org
- OWASP WebGoat, free guided lessons. owasp.org
- OWASP Code Review Guide, free method. owasp.org
- Hack The Box Academy, partly free modules. academy.hackthebox.com
- SecureFlag labs, paid enterprise offer. secureflag.com