Highlights

Contents

Part IX
09

Authorization end to end

We follow one write request through every check from this unit, in the order a well-built backend runs them. Seeing the checks in sequence shows why order matters for both security and cost. We will trace Asha’s menu edit and then summarise what each mechanism contributes.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Authentication vsauthorization2Access control models3Resource ownership andIDOR4Policy engines andtesting5Input validation6Input attacks7Multi-tenancy models8Running a multi-tenantbackend9Authorization end to end

#Section 31One request through every check

Asha sends PATCH /restaurants/9/menu/3 from the Spice Group dashboard at spice.wren.example, with a JSON body that changes the price of a biryani from 450 to 480 rupees. Before any business logic runs, the request passes a series of checks, ordered from cheapest to most expensive and from most general to most specific.

The proxy checks the body size against the 1 MB limit and returns 413 if it is over. The JSON parser enforces the depth limit and returns 400 on malformed input. Authentication middleware validates the session or token from Unit III and returns 401 if it is missing or expired. Tenant middleware resolves t9 from the subdomain, confirms that user 42 is a member of tenant 9, and sets the tenant in the request context, returning 403 if not. Schema validation runs the UpdateMenuItemRequest DTO, which allows only price and available, rejects unknown fields and checks that the price is an integer from 1 to 1,000,000 paise. Failures return 422 with the field names.

Now the authorization decision. The handler asks the policy engine whether user 42 may menu:edit on restaurant:9, which walks Asha’s manager relationship to restaurant 9 and returns allow. It loads menu 3 through the tenant-scoped repository, inside a transaction with SET LOCAL app.tenant = 9, so even a mistake in the query cannot see another tenant’s menu 3. If menu 3 does not belong to restaurant 9, the result is 404. Business rules come last. If the menu is locked because a promotion is running, the answer is 409. Otherwise the write happens, with the ETag check from Unit I to catch a concurrent edit, and an audit log entry records who changed what, under which policy version.

PATCH /restaurants/9/menu/3 BY MANAGER ASHA: EVERY CHECK, IN ORDERPATCH /restaurants/9/menu/3 BY MANAGER ASHA: EVERY CHECK, IN ORDERsize + JSON413 / 400authenticate401tenantAsha in t9?schema + types422policymanager of r9?load menu 3scoped to t9business rulelocked? 409writeaudit logcheap checks first, object checks after loading, every failure with its own status
Figure 4.39Eight checks in order. Cheap and general first, object-level after loading, business rules last.

The order matters in two ways. Cheap checks first means that a flood of oversized or unauthenticated requests is rejected before it costs a database query. Authentication before validation detail means that anonymous callers learn nothing about the API’s schema from detailed 422 messages. And object checks after loading means the decision is made about the actual resource, not about an id the client typed.

#Section 32What each mechanism contributes

Each mechanism in this unit has one job, and a failure in production usually points to one missing job. Default deny makes unknown actions safe. Access control models, RBAC for coarse roles, ABAC for conditions and ReBAC for relationships, decide who may act. The ownership check, built into scoped queries, stops IDOR on every object. A policy engine or a single authorize function keeps the rules in one reviewable place, and a permission matrix test proves the denies.

On the input side, schemas and allowlists reject malformed and unexpected values, DTOs stop mass assignment, and limits on size, depth, length and regex complexity stop cheap denial-of-service attacks. For tenants, verified tenant context and row-level security keep data apart, and quotas, per-tenant limits and fair queues keep resources fair.

EACH PIECE OF THIS UNIT AND ITS ONE JOBEACH PIECE OF THIS UNIT AND ITS ONE JOBdefault denyunknown means noRBAC / ABAC / ReBACdecide who may actownership checkstop IDOR on every objectpolicy engineone place for the rulesschema + allowlistsreject malformed inputDTOs + limitsstop mass assignment and DoStenant context + RLSkeep tenants apartquotas + fair queueskeep tenants fair
Figure 4.40One job per mechanism. When something leaks, ask which job was skipped.

This unit did not cover the shape of the API itself, how resources are named, how lists are paginated and how clients retry safely, or how the application code is organised so that these checks have an obvious home. Unit V covers API design and service architecture, starting with resources and REST.