# Why a correctly scoped token can still leak your client's data

> Neil Madden's API Security in Action shows that a scope says what an application may do, while the API must still check which records each user can read.

Original: https://fdetimes.net/en/books-courses/api-security-in-action-scope-permission/

In 2022 TechTarget asked Neil Madden which chapter of his book developers should read most carefully. He picked chapter 7, on OAuth 2.0. In the same interview he said that when you add security to an API that is already running, the hardest part is authorization.

That answer maps closely onto forward deployed work. An FDE builds the APIs that connect a client's systems to the product, runs them on real data, and has to get them past the client's security team before they reach production. *API Security in Action*, published by Manning in November 2020 and running to 576 pages, teaches security the way developers already work day to day.

The book takes one concrete API and hardens it layer by layer.

## Who it is for, and in what language

Manning's page states that the book is for developers who already have experience building REST APIs, and the examples are in Java. Madden announced the release on his blog on November 20th 2020. The accompanying code sits on the author's GitHub, in the repo NeilMadden/apisecurityinaction.

The repo has one branch per chapter, starting from chapter 2. The main branch contains no code. You need Java 11 or later and Maven, plus a few extra tools from chapter 10 onwards. Developers who work mostly in Python or TypeScript should not skip the repo: the diff between two adjacent branches shows very clearly what each security layer adds to the code.

## Chapter 3 is a pre-review checklist

Chapter 3 uses a sample API called Natter. One after another, it gains rate-limiting, authentication, encryption over HTTPS, audit logging and then access control. Section 3.5 is devoted to audit logs, presented as the means of tracing accountability later on.

That sequence is worth copying down as a checklist. Before sending an API to a client's security team, answer five questions in exactly that order. Any question without an answer is where the reviewers will press you.

## Chapter 7 and the scope trap

Chapter 7 is the one Madden recommends reading closely. It covers scoped tokens, the difference between scopes and permissions, the Authorization Code flow with PKCE, token introspection, token revocation, SSO and OpenID Connect. According to Madden, many people use OAuth 2.0 as a replacement for their entire authorization mechanism, when they usually need both scopes and permissions.

A concrete case makes the point. Say you build a `GET /reports/{id}` endpoint for a client that requires a token with the `reports:read` scope. A member of the sales team signs in through SSO, receives a valid token, then changes the `id` to that of a report belonging to HR. The token is valid, the scope is correct, and the payroll report leaks anyway.

The 2023 OWASP API Security Top 10 ranks this flaw at API1, Broken Object Level Authorization. OWASP recommends authorization checks in every function that uses client input to access a record in the database. A scope answers what the application is allowed to do. A permission answers which records this person may touch.

For the endpoint above, the fix is a few lines placed right after the scope check:

```python
report = db.get_report(report_id)
if report is None or not can_read(current_user, report):
    return 404  # do not confirm that the report exists
```

The `can_read` function holds the permission logic, for example comparing the caller's department with the department that owns the report. Returning 404 rather than 403 is a choice worth considering, so that callers cannot probe for which ids exist.

**Key point:** A valid token does not mean the user is allowed to read that record.

## As the client's system grows, read chapters 8 and 10

Chapter 8 starts from a very practical problem: as a system grows, its ACLs swell with it. The chapter's answer moves from user groups to RBAC and then ABAC. At enterprise clients, this is usually the point where the API has to mirror how their departments are organized, so reading the chapter before writing any authorization code saves rework.

Chapter 10 is for deploying to Kubernetes. It covers hardening Docker images, mTLS through a service mesh, network policies and ingress. Section 10.2.7 deals specifically with preventing SSRF, and section 10.2.8 with DNS rebinding. If your product has agents or connectors that call URLs supplied by users, read those two sections now.

Chapter 9, on capabilities and macaroons, and chapter 11, on service-to-service API calls, can wait. Madden has said both were substantially rewritten for the better before the manuscript went to production. They become useful when an engagement moves to a multi-service architecture.

## Three ideas worth keeping

The first comes from chapter 3: security is a stack of layers applied in order, and each layer answers its own question. Chapter 7 contributes the second: scopes and permissions are different things, and leaving out permissions opens the door to the flaw at the top of the OWASP list.

Chapter 8 leaves the last: the authorization model has to grow with the system, because hand-written ACLs will not keep pace with the client's organization.

For developers trying to move into FDE roles, the book also gives you something to put on a CV. "Experience with API security" is a bland line.

"Designed record-level authorization and audit logging for an internal API serving three departments" is a story an interviewer will ask about. When reading job ads, terms such as OAuth, SSO or RBAC tell you which chapters the job will draw on.

Read chapter 3 in an evening, and chapter 7 with the repo open beside it. The next time a client's security team asks who can read which record, the answer will already be in your code.

**Try this week:**

- Pick an endpoint in your current project that takes an id from the client, and check whether any code confirms the caller is allowed to read that specific record.
- Clone the NeilMadden/apisecurityinaction repo, check out the chapter 3 branch and read the diff as each security layer is added.
- Write one CV line describing a specific time you designed authorization or audit logging for an API, including the type of data and the number of users.

## Sources

- [API Security in Action](https://www.manning.com/books/api-security-in-action)

- [API Security in Action is published!](https://neilmadden.blog/2020/11/20/api-security-in-action-is-published/)

- [API Security in Action, Chapter 3: Securing the Natter API (Manning liveBook)](https://livebook.manning.com/book/api-security-in-action/chapter-3)

- [API Security in Action, Chapter 7: OAuth2 and OpenID Connect (Manning liveBook)](https://livebook.manning.com/book/api-security-in-action/chapter-7)

- [API Security in Action, Chapter 8: Identity-based access control (Manning liveBook)](https://livebook.manning.com/book/api-security-in-action/chapter-8)

- [API Security in Action, Chapter 10: Microservice APIs in Kubernetes (Manning liveBook)](https://livebook.manning.com/book/api-security-in-action/chapter-10)

- [API security methods developers should use](https://www.techtarget.com/cybersecurity/feature/API-security-methods-developers-should-use)

- [API1:2023 Broken Object Level Authorization (OWASP)](https://api-security.owasp.org/editions/2023/en/0xa1-broken-object-level-authorization)

- [NeilMadden/apisecurityinaction (GitHub README)](https://github.com/NeilMadden/apisecurityinaction)

- [API Security in Action handed over to production](https://neilmadden.blog/2020/08/05/api-security-in-action-handed-over-to-production/)
