← All posts

Field notes / Security

Follow the Uncertainty: A Better Way to Review Application Code

One of the easiest mistakes to make during an application security code review is assuming that the code in front of you contains everything you need to make a security judgment.

Usually, it doesn't.

A pull request might contain two functions out of an application containing hundreds of endpoints, middleware components, database models, authorization policies, libraries, and services. Looking at those two functions and immediately declaring something vulnerable can turn code review into educated guessing.

A better approach is to ask a different question:

What do I know, what am I assuming, and what do I need to inspect next?

That last part is important.

Uncertainty during code review isn't a failure to understand the code. It is often the beginning of the investigation.

An Endpoint Is a Door, Not the Building

Consider an application adding a feature that allows users to generate links for sharing notes with coworkers.

The visible code might contain an endpoint such as:

POST /api/notes/<note_id>/share

The endpoint retrieves the current user, retrieves a note, generates a random token, stores a sharing record, and returns a URL.

Another endpoint accepts that token:

GET /shared/<token>

It retrieves the sharing record, checks its expiration, retrieves the associated note, and returns the contents.

There is a temptation during security review to treat those functions as the application.

They're not.

An endpoint is simply one entrance into the application.

A useful mental model is:

Application
│
├── Authentication
├── Authorization
├── API Endpoints
├── Business Logic
├── Database Models
├── Middleware
├── Logging
├── Utilities
└── External Services

The endpoint is one room.

The application is the entire building.

That distinction matters because a security control that isn't visible inside an endpoint may still exist elsewhere.

If an authorization check isn't visible, for example, there are at least two possibilities:

  1. Authorization isn't being enforced.
  2. Authorization is enforced somewhere you haven't inspected yet.

Those are dramatically different conclusions.

A code reviewer shouldn't silently choose between them.

They should investigate.

Start With the Expected Story

Before looking for vulnerabilities, understand what the feature is supposed to do.

For a note-sharing feature, the intended story might be:

Authenticated user
        ↓
Selects a note they may share
        ↓
Application generates a sharing token
        ↓
Token is associated with the note
        ↓
Sharing link is returned
        ↓
Recipient opens the link
        ↓
Application validates the token
        ↓
Application checks expiration
        ↓
Note is returned

That simple exercise establishes a baseline.

It also immediately creates useful questions.

Who is allowed to create the link?

Which notes can they share?

Who is supposed to open it?

Does "coworkers" mean authenticated members of the organization, or simply anyone who possesses the URL?

Can the link be revoked?

What happens if the note is deleted?

Those aren't vulnerabilities yet.

They're unresolved parts of the story.

Find the Developer's Assumptions

Code works because certain things are assumed to be true.

Security review becomes much more effective when those assumptions are made explicit.

Consider code that does something conceptually similar to:

user = get_current_user(request)
note = Note.query.get(note_id)

token = secrets.token_urlsafe(24)

share = ShareLink(
    note_id=note.id,
    created_by=user.id,
    token=token,
    expires_at=...
)

For that code to work correctly, several assumptions must hold.

The current user must exist.

The note must exist.

The application must permit that user to share that note.

The token-generation function must behave appropriately for the security requirements.

The database must be available.

The ShareLink model must accept the supplied data.

The transaction must successfully commit.

And if the product says the feature shares notes "with coworkers," something somewhere must define what that actually means.

Notice that these assumptions aren't all security issues.

That's intentional.

A good security review is still a software review.

A missing record might produce a server error rather than an exploit. A hard-coded hostname might be a deployment problem. A failed transaction might be an availability concern.

Understanding normal failure modes helps separate ordinary software defects from security-relevant failures.

Don't Guess About Code You Don't Recognize

Suppose the code contains:

secrets.token_urlsafe(24)

If you don't know what that function does, don't infer its behavior from the name.

You have discovered an uncertainty.

Follow it.

I don't recognize this function.
        ↓
What security decision depends on it?
        ↓
Token unpredictability matters here.
        ↓
Read the documentation or implementation.
        ↓
Determine what guarantees the function provides.
        ↓
Continue the security analysis.

That is much stronger than asking whether the tokens might be sequential without first determining how the function generates them.

The same principle applies to project-specific code.

If you encounter:

generate_share_token()

go find generate_share_token().

If you encounter:

get_current_user(request)

follow it.

If authorization appears to be missing, search the surrounding application for middleware, decorators, service-layer policies, or other controls.

Code review is not limited to reading downward.

Sometimes the most important security work is navigating sideways.

Ask What the Application Is Trusting

Another useful shift is to stop automatically asking:

Where is authentication?

Instead ask:

What evidence is the application trusting before granting access?

Applications can make security decisions based on many things:

Identity
Session
Role
Organization
Possession of a secret
Device
Network location
Resource ownership

This becomes especially important with sharing links.

A sharing token is not necessarily a session.

A session generally establishes continuity of an authenticated identity:

Session credential
        ↓
Application validates it
        ↓
"This request belongs to Kendall"

A sharing link can work differently:

Sharing token
        ↓
Application validates it
        ↓
"This request possesses the required secret"

The second mechanism doesn't necessarily establish identity at all.

It establishes possession.

That distinction changes the threat model.

Identity-Based vs. Possession-Based Access

Two broad access patterns are useful to recognize during application review.

Identity-based access

The application asks:

Who are you?

The requester supplies evidence supporting an identity claim, and the application establishes an authenticated identity.

Examples can include sessions and login systems using mechanisms such as OIDC or SAML.

The resulting identity can then feed authorization decisions.

Who are you?
      ↓
Prove it
      ↓
Identity established
      ↓
What may this identity do?

Possession-based access

The application instead asks:

Do you possess the required secret?

Examples include:

The person presenting the secret may not otherwise be authenticated.

A useful analogy is the difference between identification and a transferable ticket.

Identification says:

I am this person.

A transferable ticket says:

I possess something that grants admission.

For a sharing feature, that distinction is critical.

If the product requirement says users can share notes "with coworkers," but the implementation grants access to anyone possessing the token, the next question isn't automatically:

This is vulnerable, right?

The next question is:

What does "with coworkers" mean in the product's security model?

Maybe possession of the URL is intentionally sufficient.

Maybe organization authentication is also required elsewhere.

Maybe the implementation doesn't match the requirement.

You need more evidence.

Follow the Uncertainty

This is where code review becomes investigation.

Suppose the visible endpoint doesn't show an ownership check.

Don't immediately write:

Missing authorization allows users to share arbitrary notes.

You haven't established that yet.

Instead:

Ownership check not visible
        ↓
Search authorization middleware
        ↓
Inspect route decorators
        ↓
Inspect note-access policies
        ↓
Trace service-layer authorization
        ↓
Determine whether the check exists

Likewise, if you're uncertain about token behavior:

Token-generation behavior unknown
        ↓
Read library documentation
        ↓
Determine randomness guarantees
        ↓
Inspect database constraints
        ↓
Determine uniqueness behavior
        ↓
Evaluate the design

If the requirement says "coworkers":

"Coworkers" is ambiguous
        ↓
Read product requirements
        ↓
Inspect organization membership controls
        ↓
Determine intended sharing model

The goal is to progressively eliminate uncertainty.

A useful rule is:

When you don't have enough information to make a security judgment, identify exactly what information you need next.

That is not avoiding the finding.

It is refusing to invent one.

Investigation Questions Are Not PR Comments

This distinction is subtle but important.

During the investigation you might write:

Where is authorization enforced?
Read ShareLink model.
Inspect middleware.
Check database constraints.
Find audit logging.
Review coworker-sharing requirements.

Those are notes to yourself.

They're your navigation plan.

They mean:

I need to go look at these things.

They do not necessarily belong in the pull request.

Only after following those paths should unresolved questions become developer-facing comments.

Suppose you search the application and cannot find authorization protecting share creation.

Now you have a much stronger PR comment:

I wasn't able to find where authorization is enforced before creating a sharing link for this note. Can you point me to the applicable check?

That communicates something very different from:

Where is authorization?

The first statement tells the developer that you investigated before asking.

That's both more useful and more credible.

Security Review as Progressive Model Building

A strong review can therefore be treated as a sequence.

Read the change
        ↓
Understand the expected story
        ↓
Identify assumptions
        ↓
Identify security decisions
        ↓
Mark unresolved questions
        ↓
Follow the uncertainty
        ↓
Read surrounding code and documentation
        ↓
Resolve what you can
        ↓
Ask developers about what remains
        ↓
Determine whether a security finding exists

The vulnerability hunt comes later.

That may feel backwards when you're learning offensive security, where the objective is often explicitly to find the bug.

Application security engineering has a different problem.

The objective isn't to find a vulnerability.

It's to determine whether the application is making security decisions correctly.

That requires understanding the system well enough to know the difference between:

Control absent

and:

Control not visible here

Those five words, not visible here, can save a lot of bad security conclusions.

A Better Question Than "Where's the Auth?"

When reviewing unfamiliar code, the strongest starting question may be:

What is the application trusting?

Then work outward.

What grants access?

├── Identity?
├── Session?
├── Role?
├── Organization membership?
├── Resource ownership?
├── Possession of a secret?
├── Device?
└── Network location?

Once you know what the application trusts, you can investigate whether that trust is justified.

If it trusts identity, how was that identity established?

If it trusts a session, how is the session validated?

If it trusts a role, where did that role come from?

If it trusts ownership, where is ownership checked?

If it trusts possession of a token, how is that token generated, protected, expired, and revoked?

If it trusts organization membership, where is membership enforced?

Now you're no longer searching the code for familiar security keywords.

You're tracing security decisions through the application.

The Bigger Lesson

The most valuable skill in application security isn't memorizing every framework function.

You'll encounter unfamiliar libraries.

You'll encounter architectures you've never seen.

You'll encounter custom authentication systems, middleware, internal frameworks, undocumented business rules, and code written by people who apparently considered naming conventions an optional feature.

You won't know everything.

The important skill is recognizing when you don't know something that matters.

Then follow it.

Don't guess what the library does.

Read it.

Don't assume authorization is missing.

Trace it.

Don't assume a token represents a session.

Determine what evidence it actually provides.

Don't turn an unresolved question into a vulnerability.

Resolve the question first.

Security code review is ultimately an exercise in building an accurate model of how the application makes decisions.

The better that model becomes, the better the security conclusions become.

So when unfamiliar code leaves you uncertain, don't treat the uncertainty as an obstacle.

Treat it as a map.

It is telling you exactly where to investigate next.

← Back to the blog