← All posts

Field notes / Security

Stop Hunting Vulnerabilities: Learn to Read the Application First

One of the easiest traps in application security is opening a piece of source code and immediately asking:

Where is the vulnerability?

We are trained to recognize patterns. We see user input and think injection. We see an object identifier and think IDOR. We see HTML output and think XSS.

Pattern recognition is valuable, but it can also make us dangerously confident.

A better first question is:

What did the developer believe had to be true for this code to work?

That question changes code review from vulnerability bingo into application analysis.

And that distinction matters.

Start With What the Code Is Supposed to Do

Consider a simple API endpoint:

@app.post("/api/notes")
def create_note(request):

    user = get_current_user(request)

    title = request.json["title"]
    body = request.json["body"]

    note = Note(
        owner=user.id,
        title=title,
        body=body
    )

    db.session.add(note)
    db.session.commit()

    return {"status": "created"}

It is tempting to immediately start listing everything that might be wrong.

Don't.

First, establish the expected story.

The endpoint appears intended to create a new note owned by the currently authenticated user.

Under normal conditions, the request lifecycle looks something like this:

User clicks Save
        ↓
Browser sends POST request
        ↓
Application receives request
        ↓
Application determines current user
        ↓
Application extracts title and body
        ↓
Application creates Note object
        ↓
Application saves note
        ↓
Database commits transaction
        ↓
Application returns success

No attacker yet.

No exploit.

No vulnerability classification.

We're simply determining what the application believes should happen.

That expected story gives us a baseline against which everything else can eventually be compared.

Every Line Contains Assumptions

The next step is where things become more interesting.

Take this line:

title = request.json["title"]

Instead of asking whether it is vulnerable, finish this sentence:

For this line of code to work correctly...

Several things must already be true:

The request contains JSON.
The framework successfully parsed that JSON.
The JSON contains a "title" key.
The value has an acceptable type.
The value is acceptable to whatever consumes it later.

Those aren't necessarily security problems.

They're assumptions.

Now look at:

user = get_current_user(request)

For that line to work as intended:

The request must exist.
The request must contain whatever credentials the application expects.
Those credentials must be usable by get_current_user().
The function must return an appropriate user object.
The returned object must contain an id.

Again, we're not saying any of those assumptions are false.

We're identifying them.

The same approach applies to:

note = Note(...)

What must already be true?

The constructor must accept these fields. The supplied values must satisfy whatever requirements the model imposes. owner must expect the identifier we're giving it. Any required database constraints must ultimately be satisfiable.

Then:

db.session.commit()

For that to succeed, the database must be reachable, the transaction must be valid, constraints must be satisfied, and the commit must complete successfully.

This produces a surprisingly powerful code-review technique:

For every important line, ask what must already be true for it to work correctly.

It forces us to think like the developer before thinking like the attacker.

Separate Facts, Assumptions, and Questions

This distinction is critical.

Suppose we see:

user = get_current_user(request)

A fact is:

The endpoint calls get_current_user().

An assumption might be:

The returned object represents the identity associated with this request.

A question is:

How does get_current_user() establish identity?

Those are three different statements.

Mixing them together is how unsupported findings happen.

The same problem appears when reviewing authorization.

The endpoint creates the note with:

owner=user.id

But where is authorization enforced?

We don't know.

And "I don't know" is an important code-review result.

Authorization could exist in:

The fact that authorization isn't visible in this function does not prove authorization is missing.

Likewise, we cannot assume it exists.

The correct conclusion is:

Authorization is unresolved from the code currently visible. Trace where that decision is enforced.

That is considerably stronger than prematurely filing "Broken Access Control."

Map the Security Decisions

Once the expected behavior and assumptions are understood, we can identify where security-relevant decisions appear to occur.

For this endpoint, several immediately stand out.

Identity

user = get_current_user(request)

Something is determining who is making the request.

Authorization

owner=user.id

Ownership is being established, but authorization enforcement isn't visible in the current function.

That's a question to trace, not yet a finding.

Validation

title = request.json["title"]
body = request.json["body"]

Input enters the application here. Validation isn't visible in the snippet, so we need to determine whether it happens elsewhere.

Persistence

db.session.add(note)
db.session.commit()

This is where application state becomes persistent.

Auditing

Nothing visible tells us whether the action is logged.

Again:

Not visible does not mean nonexistent.

It means unresolved.

There may also be business rules such as title length limits, quotas, uniqueness requirements, or restrictions on who may create notes. None are apparent from this function, so those rules become another design question rather than something we invent ourselves.

Ask the Developer About the Design

This changes the kinds of questions we ask before testing.

Instead of:

How are you preventing injection?

Ask:

Where does input validation occur for this endpoint?

Instead of:

Why isn't this logged?

Ask:

Where is creation of a note recorded for auditing?

Instead of:

Isn't this missing authorization?

Ask:

Where is authorization to create notes enforced?

The difference may seem subtle, but it completely changes the conversation.

The first version begins with an accusation.

The second begins with an unresolved architectural question.

Good application security work requires knowing which one you actually have.

Then Comes the Most Interesting Question

Once we understand the visible function, where do we go next?

A beginner often keeps scrolling.

An experienced reviewer follows the uncertainty.

Consider:

note = Note(
    owner=user.id,
    title=title,
    body=body
)

That raises a major question:

What exactly is a Note?

So class Note becomes a useful next destination.

There we might discover fields, relationships, defaults, constraints, validation behavior, ownership semantics, or model hooks.

Then consider:

user = get_current_user(request)

That raises another set of questions.

How is identity established?

JWT?

Session cookie?

OAuth token?

API key?

Does the function query a database?

Can it return None?

Can it throw an exception?

Does it use cached identity information?

So get_current_user() becomes another valuable branch to follow.

But there's another unanswered question that isn't represented by a convenient function call:

Where is authorization?

There may be no authorize() function sitting neatly inside the endpoint.

So we have to search outward.

Look at the route registration. Inspect decorators. Examine middleware. Check request hooks, framework policy, parent routers, service-layer enforcement, and potentially database controls.

Only after those higher-value questions are answered might db.session.commit() become interesting.

Why?

Because if this is ordinary framework transaction handling, we largely know what it does.

Reading thousands of lines of ORM internals probably won't answer the questions that matter.

Follow the Uncertainty

That leads to one of the most useful code-review principles I've encountered:

Don't follow the next function. Follow the next unanswered question.

If get_current_user() creates uncertainty about identity, follow it.

If Note creates uncertainty about ownership or validation, follow it.

If authorization isn't visible at all, search outward until you find where that decision lives.

If commit() behaves exactly like the framework documentation says it does, there may be little reason to descend into it.

This is how you avoid trying to understand a 100,000-line application by reading 100,000 lines.

You build a graph of questions.

POST /api/notes
      │
      ├── Who is the user?
      │       ↓
      │   get_current_user()
      │
      ├── Can this user create notes?
      │       ↓
      │   middleware / decorator / policy / service?
      │
      ├── What is being created?
      │       ↓
      │   Note model
      │
      ├── Where is input validated?
      │       ↓
      │   model / schema / service / middleware?
      │
      └── What gets recorded?
              ↓
          logging / audit system?

Now the codebase isn't an enormous pile of source files.

It's a set of questions with paths leading toward answers.

That is a much more scalable way to review software.

Architecture Matters Too

This way of thinking extends beyond individual functions.

Modern applications rarely consist of a browser talking directly to one application and one database.

A more realistic architecture might look like:

                    Internet
                       │
                       ▼
             Reverse Proxy / Load Balancer
                       │
                       ▼
                API Gateway
                 (optional)
                       │
                       ▼
              Running Application
                (VM / Pod / EC2)
                 ├──────────────┐
                 │              │
                 ▼              ▼
             Redis Cache     Database
                 │
                 ▼
           Object Storage

Each component has a different responsibility.

A reverse proxy primarily directs traffic toward backend systems.

An API gateway can make API-specific decisions such as authentication, token validation, API-key handling, routing, transformation, and rate limiting.

Redis may hold sessions, counters, cached queries, shopping carts, or other frequently accessed data.

Object storage handles an entirely different persistence problem.

And the application itself may run in a VM, container, Kubernetes pod, managed application platform, or serverless environment.

Even apparently simple websites demonstrate the distinction between source code and runtime.

A repository contains source.

A CI/CD pipeline builds and deploys it.

A hosting platform serves or executes the deployed application.

The browser may execute client-side JavaScript.

Those aren't interchangeable concepts.

Understanding which system owns which responsibility is fundamental because every connection introduces assumptions and every boundary potentially changes who is trusted to make a decision.

The Offensive Mindset Comes Later

None of this eliminates offensive thinking.

It makes offensive thinking better.

Eventually we absolutely want to ask:

What happens when an attacker violates these assumptions?

But now that question is grounded in an actual understanding of the application.

First:

What should happen?
        ↓
What systems participate?
        ↓
What assumptions are being made?
        ↓
Where are security decisions made?
        ↓
Where do those decisions live in code?
        ↓
What remains unresolved?

Then:

Which assumptions can I violate?

That's when testing becomes interesting.

Instead of throwing payloads at an endpoint because a scanner checklist says SQL injection comes next, we can deliberately test the assumptions the application actually makes.

The goal of source review isn't to find the vulnerability as quickly as possible.

The goal is to understand the application accurately enough that when something is wrong, we can explain exactly which assumption failed, where the relevant security decision should have occurred, and why the resulting behavior matters.

That's the difference between recognizing vulnerability patterns and actually understanding application security.

← Back to the blog