FDE Times dataFDE jobs open 932▼14 Change in open roles over 7 days, like-for-like: roles from companies we started tracking this week are left outNew in 7 days 62Companies hiring 202Remote-friendly 24%Median US pay $201kTop hirer Databricks 92
中文VI

The newspaper of the Forward Deployed Engineer

Books & courses

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.

Cover of API Security in Action
API Security in Action · Neil Madden · Cover: Open Library
ShareLinkedInFacebookX

In brief

  • Neil Madden advises developers to read chapter 7, on OAuth 2.0, closely. In his view, authorization is the hardest part of adding security to an API.
  • Chapter 3 adds protective layers to a sample API one at a time, and works as a checklist before you hand an API to a client's security team.
  • A token with the right scope can still leak data if the API does not check permissions on each record.
Two request flows for GET /reports/{id}. The left one checks only for a valid token and the reports:read scope, and the salary report is exposed. The right one adds a can_read(user, report) step, highlighted in orange, then returns 404. Note: OWASP API1, Broken Object Level Authorization.
Scope only says what an app may do. A per-record permission check is also needed to stop a salesperson reading the HR department's salary report. Source: API Security in Action (Neil Madden), OWASP API Security Top 10, 2023 edition.

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:

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.

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.

Was this article useful?

Use with your AI assistantAsk Claude ↗Ask ChatGPT ↗
10 sources
Read next on the roadmap · Stage 2: Broad engineeringWhy FDEs should start Mastering API Architecture at chapter 10Get the conference case study first, then go straight to the API gateway chapters, the skills one Arcade FDE job ad names outright.