Nested object authorization

A check on the parent record does not authorize every child id inside the request. Each object needs its own predicate.

Lesson 59 of 69 · 15 min read · practice only with authorization

Diagram

From a parent the caller owns to a child they do not

Follow the path. One example at a time.

Step 1 of 4

Parent

The route loads a project, an invoice, or a folder, and checks that the caller owns it.

Illustration: the parent is checked first
parent_id = "1042"
# Avery may open this invoice.
# That does not, by itself, open every child id.
What changes the outcome
Parent

Ownership is checked.

Child

Loaded by id only.

Response

Another tenant’s child is included.

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Parent is not child

    What fails
    Nested routes feel authorized because the prefix was checked. The second lookup is a new object.
    Who is affected
    The owner of the child record that was named but not granted.
    What you would notice
    A handler that checks the project, then selects an attachment by id with no join.
    What you change
    Join the child to the parent and the caller. One query should express the whole rule.
  2. Includes and expansions

    What fails
    A field that “includes” comments, files, or users is a list of objects. Each include is an authorization decision.
    Who is affected
    People whose objects are pulled in by an include they never shared.
    What you would notice
    A response that embeds records the caller’s list view does not show.
    What you change
    Authorize inside the include. Do not embed by id alone.
  3. Reassignment

    What fails
    An id that used to belong to the caller can be moved. A check that happened at create time is not a check at read time.
    Who is affected
    The new owner, and the old caller who can still name the id.
    What you would notice
    Updates that set a parent id from the client without checking both sides.
    What you change
    Check both the source and the destination on every write. Do not store “already checked” as a flag the client can send.
  4. Same response either way

    What fails
    A nested id that is not theirs should look the same as a nested id that does not exist.
    Who is affected
    The owner, if a different error tells the caller the child is real.
    What you would notice
    A 403 that names the other project.
    What you change
    Return the same not-found you use for a missing child. Log the detail server-side.

The prefix is not the object

Routes like /projects/10/attachments/77 read as “an attachment of my project.” The authorization has to say that too. What teams implement is “project 10 is mine” and then “load attachment 77.” Those are two queries. Only the first has a predicate. The second trusts an id that arrived inside a request that already looked legitimate.

This shows up in REST bodies, in GraphQL nodes, and in “add existing item” dialogs. The id space of the child is often global. Guessing or replaying an id from another tenant is enough when the second query is global too. You do not need a clever encoding. You need the join.

Expansions are routes you did not draw

A parameter that asks for comments, members, or files is a nested read. If the expander loads by foreign key and the foreign key can point at another tenant, the pretty response is the disclosure. Authorize in the expander with the same helper you use for the standalone route. If the helper is annoying to call, that is a design problem, not a reason to skip it.

Writes that re-parent an object need both sides. Moving Blake’s attachment onto Avery’s project is a write on Blake’s object and on Avery’s. Check that the caller may remove it from the old parent and add it to the new one. A client-supplied parent id is not a grant.

What you would notice

In review, every path parameter and every id in the body should appear in a predicate with the current user. A lookup that uses only the id is the finding, even when an earlier line checked a different id. Tests that use one user cannot see this. The test needs two.

Watch for 403 responses that include the other object’s title “so the client can display a nice error.” That title is the disclosure. Not-found, with the same body you use for a missing row, is the response. Keep the detail in the server log if an operator needs it.

What you change

Write one query, or one repository method, that takes the child id, the parent id, and the caller, and returns a row only when all three match. Use it from the interactive route and from any include. Delete the by-id helper or make it internal to that method so new code cannot call it.

The people affected are the owners of the nested records. Say which field carried the id. The fix is the predicate, not an opaque id. Opaque ids stop guessing. They do not authorize.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

The handler verifies that Avery owns project 10, then loads attachment 77 by id and adds it to the response. Attachment 77 belongs to Blake’s project. What failed?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapIDOR and broken object-level authorization: ids without an owner checkHow changing an id in a URL or API body returns someone else’s record, why it tops the OWASP API list, how to test it with two accounts you own, and the ownership-scoped query that fixes it.
  1. 01IDOR in exports and batch endpointsA list of ids is still a list of objects.

Practice

  1. LabScope deskDecide whether a situation is in scope, needs a written change, or stops.

Glossary

  1. CardInsecure direct object referenceChanging an identifier in a request returns or edits another user's object because the server never checks ownership.