Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 30 ยท Advanced API Testing

Security testing basics for APIs

Learn API security testing basics: the OWASP API Top 10, broken object-level authorization, auth failures, injection, rate limits and mass assignment.

Functional tests ask, "does the API do what it should?" Security tests ask the opposite question: "can someone make it do what it should not?" Can user A read user B's invoice? Can a normal user call an admin endpoint? Can a caller promote themselves to administrator by adding one extra field to a request? These bugs rarely show up in a happy-path test, because a happy-path test is written by someone who plays by the rules.

You do not need to be a professional penetration tester to catch the most common API mistakes. A handful of repeatable checks, written once as a Postman collection and run with every build, will find the classic ones. This lesson covers those checks.

Only test systems that you own or have explicit written permission to test. Sending attack-style requests to someone else's API can be illegal and can harm real people. Use your own development or staging environment with test data.

The runnable examples are stand-ins: small Node.js programs with deliberately vulnerable and fixed versions of the same endpoint, so you can watch a security test fail and then pass. They are teaching models, not real products or real attack tools. Postman scripts appear in read-only blocks.

The OWASP API Security Top 10

OWASP (the Open Worldwide Application Security Project) publishes a ranked list of the most common API risks. It is the standard starting point for planning API security tests. The 2023 edition lists these ten categories.

OWASP API Top 10 (2023), part 1

API1 Object-level auth Reading other people's objects.
API2 Broken authentication Weak or bypassable login and tokens.
API3 Property-level auth Too many fields exposed or writable.
API4 Resource consumption No limits on size, rate or cost.
1 / 3

You do not have to memorize the numbers. What matters is the pattern: most of the top risks are about authorization (who may do what) rather than clever hacking. Authorization bugs are logic bugs, and automated scanners find them poorly, so a tester who understands the business rules has a big advantage.

Broken object-level authorization (BOLA)

BOLA (also called IDOR, insecure direct object reference) is the number one API risk. An endpoint such as GET /invoices/2 looks up invoice 2 and returns it, but never checks that the caller owns it. Anyone who is logged in can read every invoice just by counting upward.

The test is simple and powerful: log in as two different users and try to use user A's id with user B's token.

JavaScript
// Tests tab of "Alice reads Bob's invoice" (real Postman script, read-only here)
// Request: GET {{baseUrl}}/invoices/{{bobInvoiceId}}   with Alice's token

pm.test("another user's invoice is not exposed", function () {
  pm.expect(pm.response.code).to.be.oneOf([403, 404]);
  pm.expect(pm.response.text()).to.not.include("total");
});

Both 403 and 404 can be right. Returning 404 for objects the caller may not see has a small advantage: it does not confirm that the object exists. Whichever your API chooses, test that it is consistent, and test every verb (GET, PUT, PATCH, DELETE), not only reading.

Broken authentication

Authentication proves who you are. Test the ways it can fail:

  • No token on a protected endpoint must give 401, with no data in the body.
  • An expired, tampered or garbage token must give 401. Change one character of a signed token and send it.
  • A token for the wrong audience or environment (a staging token used in production) must be refused.
  • Login errors must be generic. "Wrong password" for a known user and "no such user" for an unknown one tells an attacker which usernames exist. Both cases should look the same.
  • Repeated failed logins must be slowed or blocked (see rate limiting below).
  • Secrets must not travel in URLs, where they end up in logs and browser history.

Postman helps here because you can easily remove the Authorization header, paste a damaged token, or switch the auth type to "No Auth" for one request and check what the server says.

Broken function-level authorization

BOLA is about objects; this one is about actions. Does the endpoint POST /admin/refunds check that the caller is an administrator, or does it only check that the caller is logged in? Test every privileged endpoint with a normal user's token and with no token. The expected answer is 403 (or 401 without a token). Admin features are often hidden from the screen, but a hidden button is not a lock: anyone can send the request directly.

Mass assignment and excess data

Many frameworks can copy all fields from a request body straight into a database record. If the code does that blindly, a user can send fields they were never meant to control:

JSON
{ "name": "Alice A", "role": "admin", "isVerified": true, "credit": 100000 }

This is mass assignment. The mirror image is excessive data exposure: the response contains more than the client needs, such as a password hash, internal notes or other users' emails. Test both: send extra privileged fields and check they were ignored (or rejected), and assert that sensitive fields never appear in a response.

The first stand-in builds the same small API twice, one vulnerable and one fixed, and runs the same security tests against both. It covers BOLA, function-level authorization, mass assignment and data exposure. Everything is in memory, with fixed users and data, so the output is the same each run.

Node.js Authorization tests: a vulnerable API versus a fixed 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.

Against the vulnerable version four checks fail: Alice reads Bob's invoice, calls the admin action, and turns herself into an admin while also seeing her own password hash in the response. The fixed version passes all six. Notice that the vulnerable API behaves perfectly for the happy path ("Alice can read her own invoice" passes), which is exactly why functional tests alone never catch these problems.

Injection

Injection happens when untrusted input is pasted into a command or query that is then interpreted: SQL, NoSQL queries, shell commands, LDAP, templates. The classic example is a search endpoint that builds SQL by gluing text together.

JavaScript
// Vulnerable pattern (read-only illustration)
const sql = "SELECT id, name FROM users WHERE name = '" + req.query.name + "'";

// Safe pattern: the value travels separately from the query text
const statement = db.prepare("SELECT id, name FROM users WHERE name = ?");
const rows = statement.all(req.query.name);

Testing for injection from the API side means sending unexpected characters (quotes, semicolons, comment markers, brackets, very long strings) in every parameter and body field, and looking for signs of trouble: a 500 status, a database error message in the body, an unusually long response, or a result list that is bigger than it should be. A well-built API answers a validation error or an ordinary "nothing found", and never an internal error.

The second stand-in uses Node's built-in in-memory SQLite database to show the principle with real SQL. It runs the same lookup twice with a classic malicious input: once by concatenating text into the query, once with a parameter.

Node.js String-built SQL versus a parameter, in an in-memory database

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 query turns the input ' OR '1'='1 into a condition that is always true, so it returns all three users instead of none. The safe version treats the whole text as a name, finds nobody, and returns zero rows. In Postman you cannot see the SQL, but the same symptom, more rows than any legitimate query could return, is visible in the response.

The in-memory database makes this demonstration real SQL, but it is a harmless toy with fake data. Real injection testing happens against your own staging system, and the real fix is the same as shown: parameterized queries and input validation on the server.

Rate limiting and resource consumption

An API without limits can be overwhelmed by accident or on purpose: a script that tries ten thousand passwords, a client that asks for a million rows per page, an upload of a gigabyte. Check that:

  • repeated requests in a short time eventually get 429 Too Many Requests, ideally with a Retry-After header,
  • login endpoints limit failed attempts per account and per source,
  • list endpoints cap the page size (limit=1000000 is clamped or rejected),
  • request bodies have a size limit and answer 413 Payload Too Large beyond it,
  • the limit resets after its time window.

The last stand-in combines two ideas: a login endpoint that gives the same answer for an unknown user and a wrong password, and a fixed-window rate limiter that blocks the sixth attempt in a window. A fake clock replaces real time, so the demonstration is exact.

Node.js Generic login errors and a rate limit

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.

Attempts one and two (unknown user, wrong password) look identical, so an attacker learns nothing about which usernames exist. Attempts three to five are still answered with 401, and the sixth attempt gets 429 with a Retry-After of 60 seconds minus the elapsed time. Another client is unaffected, and the first client can log in once the window passes. In a Postman collection you would build this with a short chain or a loop that sends the request six times and asserts that one of the answers is 429, against a test environment where blocking is acceptable.

Misconfiguration and exposure checks

Some of the cheapest tests inspect what the API gives away:

  • Error responses must not include stack traces, file paths or database messages.
  • Debug or documentation endpoints (/debug, /admin, old versions such as /v1) should not be reachable in production.
  • Security headers, Content-Type and CORS settings should match the policy. A wildcard CORS rule on an endpoint that uses cookies is a red flag.
  • Unsupported methods (TRACE, or DELETE on a read-only resource) should answer 405.
  • Traffic should use HTTPS, and plain HTTP should redirect or be refused.

Each of these is a quick assertion, which is why they belong in a regular collection.

Building a small security suite in Postman

  1. Keep two or three test users (normal user A, normal user B, admin) with their tokens in separate environments or variables.
  2. For every endpoint that takes an id, add a "wrong owner" request.
  3. For every privileged endpoint, add a "normal user" and a "no token" request.
  4. For every write endpoint, add one request with extra privileged fields.
  5. Add a few malformed inputs (quotes, brackets, very long text) to inputs that reach a database.
  6. Run the folder in CI with Newman (see Newman, the CLI and CI/CD) so a regression is caught on the day it is introduced.

Security testing with a collection is a baseline, not a guarantee. It catches the common, repeatable mistakes. For high-risk systems, add code review, automated scanners and professional penetration tests.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. User A sends GET /invoices/2 with A's token, and invoice 2 belongs to user B. The API returns the invoice. What is this bug called?

    The API found the object but never checked that the caller owns it. That is broken object-level authorization, the top API risk.
  2. 2. A normal user sends PATCH /users/me with the extra field "role": "admin" and becomes an administrator. What went wrong?

    Blindly copying all body fields into a record lets callers set fields they should not control. The server should accept only an allow list of fields.
  3. 3. Why should a login endpoint return the same error for an unknown user and for a wrong password?

    If the messages differ, an attacker can test names and learn which accounts exist, which makes guessing attacks easier.
  4. 4. What is the right way to prevent SQL injection in the search endpoint?

    Parameters keep data separate from the query text, so quotes in the input stay data and cannot change the query's meaning.

Summary

  • Security tests ask whether someone can do what they should not; test only systems you own or may test.
  • The OWASP API Security Top 10 (2023) is the checklist; most entries are authorization problems.
  • BOLA: try user A's token on user B's objects, for every verb. Function-level checks: try admin endpoints as a normal user and without a token.
  • Mass assignment: send extra privileged fields and verify they are ignored; assert that sensitive fields never appear in responses.
  • Authentication tests: no token, expired or tampered token, generic login errors.
  • Injection: send unexpected characters everywhere; the fix is parameterized queries and validation.
  • Rate limits and size limits need tests too (429, 413, capped page size).
  • The runnable examples are stand-ins with fake data; a collection run in CI is a baseline, not a full security audit.

Next: Contract testing and OpenAPI.