Skip to main content

Safer Inputs and APIs

How to treat external input with less naivety and design APIs that do not accept too much data for convenience.

Andrews Ribeiro

Andrews Ribeiro

Founder & Engineer

The problem

A lot of APIs become vulnerable not because login is missing, but because they accept too much external input without clear rules.

An extra field gets through.

A malformed string keeps moving.

An unexpected ID enters the flow.

Then the service layer tries to clean things up too late.

That “we will validate it later” shortcut is exactly the kind of move that opens the door to real trouble.

Mental model

External input should never enter the backend as ready-made truth.

It needs to pass through a hard filter at the edge:

  • is the format valid?
  • does this field belong to the contract?
  • does this value make sense in this context?
  • is this user allowed to send it?

Security here is mostly structural discipline at the network boundary.

Breaking it down

A safer API usually does this:

  1. validates the schema as soon as the request reaches the route
  2. removes fields the flow does not need
  3. normalizes strings, dates, and emails before business logic sees them
  4. rejects the request early when something does not fit
  5. still checks permission and business rules before writing or triggering side effects

That reduces the surface for abuse and for accidental bad state.

Simple example

Imagine a profile update endpoint.

If the controller accepts the raw JSON body and blindly merges it into the database, any unexpected field can leak into the model:

  • role: "admin"
  • billingStatus: "paid"
  • internalConfig: {}

A safer version accepts only the smallest contract:

  • name
  • bio
  • avatarUrl

Everything else is either dropped or rejected.

The gain is not only cleaner code.

The gain is tighter control over what the application is actually allowed to mutate.

Common mistakes

  • trusting the frontend to always send the correct payload
  • validating only primitive types and ignoring business rules
  • leaving endpoints too generic for “future flexibility”
  • accepting extra fields and pretending that is harmless because “we ignore them anyway”
  • assuming authentication alone makes the data trustworthy

How a senior thinks

More experienced engineers treat external payloads as hostile until proven otherwise.

The rule of thumb sounds like this:

Validate the schema at the boundary and accept the smallest contract possible. Every extra field we allow increases what the system has to defend.

That mindset keeps security tied to API design instead of treating it like a patch added later.

What the interviewer wants to see

In backend or security interviews, this topic shows depth very quickly.

The interviewer wants to see whether you:

  • validate data at the boundary, not deep inside service logic
  • design explicit and minimal contracts
  • connect security to API design, not only to auth middleware

A strong answer often sounds like this:

I would validate the schema as soon as the request arrives and accept the smallest contract possible. After that I would still check business rules and permissions, because a well-formed payload is not automatically a legitimate one.

A secure API does not try to survive every possible payload. It accepts little and does it deliberately.

Quick summary

What to keep in your head

Practice checklist

Use this when you answer

You finished this article

Next article Semantics and Structure That Actually Help Previous article Authentication vs Authorization

Keep exploring

Related articles