Skip to main content

One login read 2,987 documents it had no right to see

Updated 6 min readMarkdown

Every endpoint in your API checks that the request comes from a logged-in user. That feels safe. Then an ordinary employee notices their document lives at /docs/1jk, changes the URL to /docs/1jl, and is reading a stranger's file.

I simulated exactly that: an in-memory document API shared by 3 companies, with 300 users and 3,000 documents. The attacker is one normal employee with a valid login. Nothing gets hacked. They just ask.

The number

[1/4] Only the login is checked. IDs are sequential, so the attacker counts upward:
      read 2,987 documents belonging to other people, in 3 of 3 companies

One login read 2,987 documents that belonged to other people, in every company on the system.

The API did check something. It checked who the user was. It never checked whether that user was allowed to see that document.

Two questions that sound like one

  • Authentication asks "who are you?". A login, a session or a token answers it.
  • Authorization asks "may you do this to this object?". Only your own rules can answer it.

An API that checks only the first has an IDOR (insecure direct object reference): the client sends an object's ID, and the server uses it without asking whether this user may touch that object. OWASP's list of API security risks calls it broken object level authorization, and in its 2023 edition it's number one.

"Just use UUIDs"

The usual first reaction is to make the IDs impossible to guess. So I switched the same broken API to random 7-character IDs:

[2/4] Only the login is checked. IDs are random, 7 characters:
      10,000 guessed IDs: read 0 documents
      25 IDs seen in shared links, emails and logs: read 25 of 25

Random IDs do stop counting: 10,000 guesses found nothing. But IDs don't stay secret. They end up in shared links, emails, screenshots, browser history and logs. All 25 that leaked still worked.

Random IDs hide documents. They don't protect them. OWASP's Authorization Cheat Sheet says the same: making IDs hard to guess "is generally not sufficient by itself".

The fix

The same request with a login check only, which leaks the document, and with a policy check on every request, which answers 404

The whole policy lives in one function. Each return True is one rule, and reaching the end means no:

def is_allowed(user: User, action: str, doc: Document) -> bool:
    """The policy. Every True below is one rule; falling off the end means "no"."""
    if action not in ACTIONS:
        # An unknown action is a bug in the caller. Raise, so it can't quietly become a yes.
        raise ValueError(f"unknown action {action!r}; expected one of {ACTIONS}")
    if user.org != doc.org:
        return False  # tenant isolation comes first: no rule below may cross orgs
    if user.id == doc.owner:
        return True  # owners may do everything to their own documents
    if action == "read" and user.id in doc.readers:
        return True
    if action in ("read", "delete") and user.is_admin:
        return True  # admins can read and remove anything in their org, but not edit it
    return False  # deny by default

Three choices in there matter more than they look:

  1. Deny by default. A forgotten rule refuses someone. The opposite design, allow unless a rule says no, lets everyone in when a rule is forgotten.
  2. The company check comes first. In a system shared by many customers, no later rule, not even "admin", can cross from one customer to another.
  3. An unknown action raises an error instead of quietly becoming a yes or a no.

Then every endpoint reaches documents through one function that applies the policy:

def _load(self, user: User, action: str, doc_id: str) -> Document:
    # The ONE way endpoints reach a document. Putting the check here, instead of in each
    # endpoint, means a new endpoint can't forget it: it would have to bypass _load.
    doc = self._docs.get(doc_id)
    if doc is None:
        raise NotFound(f"no document {doc_id!r}")
    if self.check and not is_allowed(user, action, doc):
        if self.hide_existence:
            # Same exception and same message as a missing ID, so a scan learns nothing.
            raise NotFound(f"no document {doc_id!r}")
        raise Forbidden(f"{user.id} may not {action} {doc_id!r}")
    return doc

Read, edit, delete, share and list all go through it. A new endpoint can't forget the check without visibly going around it. That matters, because OWASP's cheat sheet puts it bluntly: "an attacker only needs to find one way in."

Same attacker, same data, now trying every real ID:

[3/4] Every request is checked against the policy, deny by default.
      The attacker tries all 3,000 real IDs: read 0 documents that aren't theirs.
      Refused 2,987; still readable: their 10 own and 3 shared with them

Zero. And the 13 documents they're entitled to still work. Good authorization refuses the wrong people, not everyone.

403 or 404?

When a user may not see a document, the API can say 403 Forbidden, or pretend it doesn't exist with 404 Not Found. It sounds like a detail. I sent 4,000 probes, half real IDs and half made up:

[4/4] What does a refusal SAY? 4,000 probes: 2,000 real IDs, 2,000 made up
      'not yours' answered as        403s      404s   real IDs it revealed
      403 Forbidden                 1,987     2,000                  1,987
      404 Not Found                     0     3,987                      0

A 403 confirms the ID is real. Answering 403 told the attacker which 1,987 of their probes were real documents, a target list for later. Answering 404 for both made a forbidden document look exactly like a missing one.

GitHub does this for private repositories: its API docs say it answers 404 instead of 403 "to avoid confirming the existence of private repositories."

What interviewers listen for

If a system design interview touches multi-tenant data, these are the red flags:

  • "The user is logged in, so it's fine."
  • Taking the tenant ID, role or an isAdmin flag from the request instead of the server-side session.
  • "Our IDs are UUIDs, so nobody can get in."
  • Hiding a button in the UI and calling it authorization. Anyone can send the request directly.

Run it yourself

Every number in this post came from a real run. The chapter is about 125 lines of plain Python, with nothing to install:

git clone https://github.com/mannietech15/system-design-mastery
cd system-design-mastery/25-authorization
python demo.py

It's chapter 25 of system-design-mastery: 25 system design concepts you can run, from rate limiters to Raft, now with a 4-week study plan and all 200 interview questions as Anki flashcards. This chapter also has two challenges that fail until you build them: spotting an IDOR scan in progress, and shares that expire.

If it's useful, a star is the easiest way to keep it within reach for your next interview, and it helps other developers find it too.

Go further

About the author

  • Just a guy who wants to be the best of the bests!

No comments yet. Be the first to comment.