Skip to main content

APIs: What They Are and Why Security Teams Care

Updated 7 min readMarkdown
Part of seriesZero Trust Threads Foundations
APIs: What They Are and Why Security Teams Care

Your phone shows today's weather. You did not visit a weather website. An app asked a server for data and drew the answer on your screen.

That request went through an API.


What an API Is

An application programming interface (API) is a defined way for one piece of software to ask another to do something.

Most web APIs work over the Hypertext Transfer Protocol (HTTP), the same protocol browsers use. A program sends a request to a specific address called an endpoint, and gets data back. That data is often formatted as JSON (JavaScript Object Notation), a simple text format for structured data.

A request and response might look like this:

GET /api/notes/1
{"owner": "alice", "text": "Alice's note"}

A website is built for a human to read. An API is built for a program to use.

That difference matters for security. There is no person looking at a screen who might notice something odd.


Why Security Teams Care

The OWASP (Open Worldwide Application Security Project) API Security Project describes APIs as exposing application logic and sensitive data, such as personally identifiable information (PII). That makes them an attractive target.

Several things make APIs worth special attention:

  • They do real work. An endpoint may read, change, or delete data.
  • They are used by machines. Problems can go unnoticed because nobody is looking at a page.
  • They multiply. Applications often grow many endpoints, and some are forgotten.

OWASP maintains a separate list for them. The 2023 edition is the latest the project page lists at the time of writing.


Proving Who Is Calling

Before an API does anything, it usually needs to know who is asking. Two common ingredients are keys and tokens.

An API key is a value that typically identifies the calling application or project. A token is a value that represents access granted to a user or application.

Many APIs use bearer tokens, which RFC (Request for Comments) 6750 describes. The name tells you the risk: whoever bears (holds) the token can use it.

That means a key or token is a secret. Treat it like a password. This links directly to Service Accounts: The Accounts Nobody Notices Until Something Breaks.

Good handling follows the same pattern as Temporary Access Has a Way of Becoming Permanent: a purpose, an owner, a scope, an expiration, and a review.

Keep keys out of source code, screenshots, and shared documents. Keep them out of web addresses too, because addresses are written into access logs, as you saw in HTTP Methods and Status Codes: Reading the Web's Conversation.


Authenticated Does Not Mean Authorized

Here is the most important idea in API security.

Knowing who is calling is authentication. Deciding what they may touch is authorization. See Authentication vs. Authorization: Why the Difference Matters.

The OWASP API Security Top 10 for 2023 puts a failure of this kind at number one: Broken Object Level Authorization. The API checks that you have a valid token, but forgets to check that the specific record you asked for belongs to you.

Because object identifiers often appear in the address, such as /api/notes/2, changing one number may be all it takes to ask for someone else's record.

You will see this in the exercise.


Connecting the API Top 10 to What You Already Know

You do not need to memorize the whole list. Several items map to earlier articles:

OWASP API Security Top 10 (2023) Where you have seen the idea
API1: Broken Object Level Authorization Authentication vs. Authorization, Least Privilege Sounds Simple Until You Have to Implement It
API2: Broken Authentication Passwords Aren't Dead, but They Shouldn't Be Working Alone
API5: Broken Function Level Authorization Roles and least privilege: can this user call this function at all?
API8: Security Misconfiguration HTTP Security Headers, Same-Origin Policy and CORS
API9: Improper Inventory Management You Can't Defend What You Don't Know You Have

The ninth row is worth a pause. You cannot protect an endpoint nobody remembers exists.


Common Misconceptions

"APIs are only for developers."

Nearly every modern app and website uses them. Users rely on APIs constantly without seeing them.

"An API key is the same as a user login."

A key usually identifies an application, not a person. Handle it as a secret, and do not assume it says who is behind the request.

"Nobody will find a hidden endpoint."

Hiding an address is not a control. Authentication and authorization are.

"A valid token means the caller can see everything."

A valid token proves who is asking. Each request still needs a check on what they may access.

"An internal API does not need these controls."

Internal systems get compromised, and mistakes happen. Internal APIs deserve the same basics.


Practical Exercise: Break Authorization on Purpose

This lab runs only on your own machine or lab VM. The server listens on 127.0.0.1, and the token is a fake value made up for this exercise.

Never type a real key or token into a lab command. Commands are saved in your shell history.

Step 1: Save this as api_lab.py

import json
from http.server import BaseHTTPRequestHandler, HTTPServer

# Fake lab-only token. Never use real credentials in lab commands.
TOKENS = {"lab-token-alice": "alice"}
NOTES = {
    "1": {"owner": "alice", "text": "Alice's note"},
    "2": {"owner": "bob", "text": "Bob's private note"},
}


class Handler(BaseHTTPRequestHandler):
    def send_json(self, code, data):
        self.send_response(code)
        self.send_header("Content-Type", "application/json")
        self.end_headers()
        self.wfile.write(json.dumps(data).encode() + b"\n")

    def do_GET(self):
        auth = self.headers.get("Authorization", "")
        user = TOKENS.get(auth.removeprefix("Bearer "))
        if not user:
            return self.send_json(401, {"error": "missing or invalid token"})
        if self.path.startswith("/api/notes/"):
            note = NOTES.get(self.path.rsplit("/", 1)[1])
            if not note:
                return self.send_json(404, {"error": "no such note"})
            if note["owner"] != user:
                return self.send_json(403, {"error": "not your note"})
            return self.send_json(200, note)
        self.send_json(404, {"error": "unknown endpoint"})


HTTPServer(("127.0.0.1", 8082), Handler).serve_forever()

Step 2: Start it in one terminal

python3 api_lab.py

Step 3: In a second terminal, make three requests

curl -si http://127.0.0.1:8082/api/notes/1
curl -s -H "Authorization: Bearer lab-token-alice" http://127.0.0.1:8082/api/notes/1
curl -si -H "Authorization: Bearer lab-token-alice" http://127.0.0.1:8082/api/notes/2

Expected results:

  1. A 401 Unauthorized response with missing or invalid token
  2. A 200 response with Alice's note
  3. A 403 Forbidden response with not your note

The first failure is about authentication. The third is about authorization. The token was valid in both the second and third requests.

Step 4: Remove the authorization check

Stop the server with Ctrl+C. In api_lab.py, delete these two lines:

            if note["owner"] != user:
                return self.send_json(403, {"error": "not your note"})

Start the server again and repeat the third request:

curl -s -H "Authorization: Bearer lab-token-alice" http://127.0.0.1:8082/api/notes/2

You should now see Bob's note:

{"owner": "bob", "text": "Bob's private note"}

Alice is authenticated, and the server returned someone else's data. That is Broken Object Level Authorization in miniature.

Step 5: Look at the log

The server printed access log lines to the terminal where it runs. Find the line for the request in step 4.

It shows a 200. As you learned in the last article, a healthy-looking status code does not mean the request was acceptable.

Cleanup

Put the two lines back, or delete the file. Stop the server with Ctrl+C.

Questions to answer

  1. Which requests failed authentication, and which failed authorization?
  2. Why does the log line in step 4 look completely normal?
  3. How could you detect this problem in a real system?
  4. Who should own the decision about which records a user may see?

Key Takeaway

An API is how software asks other software for data, and that makes it a direct path to application data and logic.

Proving who is calling is only half the job. Every request also needs a check on what that caller is allowed to access.


Verified References

About the author

  • Zero Trust Threads is a cybersecurity media and lifestyle brand focused on making cybersecurity, Linux, networking, GRC, and tech culture approachable through practical projects, real-world learning, and a little humor. Trust nothing. Learn everything.

No comments yet. Be the first to comment.

Zero Trust Threads Foundations

Part 30 of 30

Cybersecurity does not have to be learned all at once. Zero Trust Threads Foundations breaks down essential cybersecurity and IT concepts into approachable, practical lessons for beginners. Learn the fundamentals of HTTP security, home labs, Linux, logs, defensive security, and more while building the knowledge needed for hands-on learning.

Start from the beginningHTTP Security Headers: The Tiny Lines Doing a Lot of WorkOpen a website and everything seems simple. You enter an address, press Enter, and a page appears. Behind that simple interaction is a conversation between your browser and a web server. The server do