Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 39 ยท Projects and Practice

Practice: Security Review of a Vulnerable API

Attack a deliberately vulnerable API, then a hardened one: brute force, BOLA, mass assignment, SQL injection, data exposure and alg none. Includes a route audit exercise.

Reading about vulnerabilities teaches you the names. Attacking one teaches you what they look like. In this practice lesson you get one program with two builds of the same API: a vulnerable build with seven classic flaws, and a hardened build with the fixes. An attack script runs against both, so you can compare the results line by line.

Only attack systems you own or have written permission to test. Everything here runs against a toy server on a loopback port in your own sandbox.

The seven flaws

Brute force Unlimited login attempts.
BOLA Reading other users' orders.
Mass assignment Setting your own role.
SQL injection Input becomes SQL.

Three more in the next slide.

1 / 2

The target

The toy API has users (with a secret PIN and a role), orders, and these routes:

Route Purpose
POST /login {name, pin} returns an HS256 token
GET /me, PATCH /me Read or update your own profile
GET /orders/:id Read an order
GET /search?name= Find users by name

Ana (member, PIN 4821) and Ben (admin, PIN 1357) are users. Ben has a secret order. The attacker is logged in as Ana and wants to reach Ben's data.

Run the attacks

The same makeApi(secure) function builds both versions. Read it to spot each flaw, and look for the secure ? branches to see each fix. Then read the attack script at the bottom.

Node.js Attack the vulnerable API, then the hardened one

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The vulnerable column should read BREACHED seven times, and the hardened column blocked seven times. Find the line in makeApi for each fix:

# Flaw Fix Lesson
1 Unlimited login attempts Failure counter and 429 Rate Limiting
2 No ownership check Compare order.user_id with the token's user BOLA
3 Any column writable Allowlist (['name']) Mass Assignment
4 String-built SQL Parameterized query Input Validation
5 Whole row returned Explicit response fields Authorization
6 Internals in errors Generic 500 message, log details server-side Input Validation
7 Token header trusted Pinned algorithm JWT Pitfalls

A real hardened API would also store PINs as password hashes and use longer secrets (a four-digit PIN is guessable even with throttling), put the rate limit on IPs as well as names, and log every attack attempt.

Exercise: audit a route table

Reviewers often start from the list of routes. Write a function that audits a route table and prints the problems. The starter below flags routes that change data without authentication, routes with an ID parameter and no ownership check, and login routes without a rate limit. Run it, then add two rules of your own, such as "list routes must have a maxLimit" or "every route must declare an auth value".

Node.js Starter: audit a route table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Ideas for your own rules: a GET list route whose maxLimit is null, an upload route without maxBytes, a route with auth: 'none' that is not in an explicit public list, or a token route with no scope declared.

A review checklist

When you review a real API, walk through it like this:

  1. Inventory: list every route, method, parameter and who may call it (include old versions and forgotten admin routes).
  2. Authentication: how are users and apps identified, how long do tokens live, what happens on logout and password change?
  3. Authorization: for every route that takes an ID, who is allowed? Check function level and object level.
  4. Input: schemas, size limits, allowed fields, injection points in SQL, shell, paths, templates and URLs the server fetches.
  5. Output: only the needed fields, consistent errors, no stack traces, correct Content-Type.
  6. Abuse: rate limits, quotas, expensive queries, enumeration, and scraping.
  7. Transport and headers: HTTPS only, HSTS, CORS allowlist, cookie flags.
  8. Operations: logs without secrets, alerts for 401, 403 and 429 spikes, key rotation, dependency updates.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. The attacker changes /orders/1 to /orders/2 and reads another user's order. Which flaw is this?

    The API trusts the ID in the URL without checking that the order belongs to the caller.
  2. 2. A PATCH with {"role":"admin"} makes the caller an administrator. What is the fix?

    Allowlisting writable fields stops clients setting sensitive properties.
  3. 3. Why is returning e.message in a 500 response a problem?

    Detailed errors guide attackers. Return a generic message and log the details privately.
  4. 4. Which defence stops the PIN brute force in the hardened build?

    Throttling makes guessing impractical. Strong secrets and MFA add further protection.
  5. 5. Why does the review checklist start with an inventory of routes?

    Unknown and unmanaged endpoints are a common source of breaches.

Summary

  • You attacked seven flaws: brute force, BOLA, mass assignment, SQL injection, data exposure, verbose errors and alg none.
  • Each has a small, specific fix: throttle, check ownership, allowlist, parameterize, select explicit fields, hide internals, pin the algorithm.
  • Auditing a route table with simple rules is a quick way to find systematic gaps.
  • Use a checklist (inventory, authentication, authorization, input, output, abuse, transport, operations) for every review.
  • Test only systems you are allowed to test.

Next: REST API Cheat Sheet