Security

API Security Starts With Defaults: Why Every Route Must Declare Access

Tuesday, 01 Dec 2026 3 min read 32 views

An API can work perfectly for every feature and still be wide open to anyone who calls it. This flaw rarely lives in the encryption algorithm. It usually comes from forgetting to lock individual routes.

The Common Problem: Public APIs by Default

When building an API quickly, developers often put routes into a shared group without authentication middleware. Nobody calls them outside the intended flow, so the gap stays hidden. Once someone discovers the URL, they can create, edit, or delete data without logging in.

In a recent review, many routes managing data, including creating, updating, and deleting objects and users, sat outside the authentication group. Once the public group was separated from the authenticated one, the public surface shrank considerably, leaving only the home page and guest endpoints that truly needed to be open.

Three Layers of Checks

The first is authentication: who is calling? Every route handling sensitive data must sit behind authentication middleware. Hiding a button in the interface is not enough.

The second is role-based authorization: being logged in is not sufficient. Actions such as creating, editing, or deleting categories should be restricted to administrators, enforced on the server.

The third is object-level authorization: may user A edit user B's profile? This flaw is commonly called IDOR (Insecure Direct Object Reference). Checking "is logged in" alone won't stop it. You must compare ownership or role against the specific object. A simple rule might allow the action only for administrators or the account owner.

Don't Rely on the Frontend

Hiding a delete button does not stop anyone from calling the API directly with Postman or curl. Every critical permission check must live on the server. The frontend improves experience; it is not a security boundary.

Business Rules Are Part of Security

For example, blocking the deletion of a category that still has active shops. This rule should be enforced on the server, returning a 422 status with a clear message.

Note that "check whether child records exist, then delete" is a two-step operation. If another request adds a child record between those steps, the check can be outdated. A safer approach is to run both steps inside one transaction with proper locking, and use a foreign key constraint in the database as the final safeguard.

Password Reset Tokens Must Be Bound to the Account

If a reset token is looked up by the token string alone, a valid token could be used against another account in unusual situations. Look it up by both email and token, and enforce an expiry time.

Reduce the Attack Surface

If public registration isn't needed, turn it off. For a session-based SPA, enable the appropriate authentication middleware, and handle 401 (not logged in) and 419 (expired CSRF token) on the client, so users return to the login page instead of seeing confusing errors.

An API Review Checklist

Before merging, ask: are all routes with sensitive data behind authentication? Do write operations check roles? Do routes with an id check permission on that specific object? Do critical business rules run on the server? Could error messages accidentally expose internal details?

API security isn't a feature you bolt on at the end. It starts with every route closed by default, then opened deliberately, one at a time.

References

OWASP API Security Top 10 (2023)

Laravel Sanctum documentation: SPA authentication

RFC 9110: HTTP Semantics

Ready to Transform Your Business?

Let's discuss how we can help you leverage AI and digital transformation for your enterprise.

Frequently asked questions

Why can an API be public without anyone noticing?
Routes are often placed in a shared group without authentication middleware. Nobody notices until someone discovers the URL and calls it directly, then they can create, edit, or delete data without logging in. That is why every route handling sensitive data must be checked explicitly, not assumed to be private.
Is hiding a button in the interface enough protection?
No. Anyone can call the API directly with tools like Postman or curl, so permission checks must run on the server. The frontend improves user experience, but it is not a security boundary. Treat every endpoint as reachable by anyone who knows its URL, and enforce access rules there.
What is IDOR?
IDOR, or Insecure Direct Object Reference, is a flaw that lets a user access or modify another user's object by changing an id in the request. It happens when the server checks that a user is logged in but never checks whether that user owns the specific object or may act on it.
Why is "check for child records, then delete" unsafe on its own?
Because it is two separate steps. Another request can add a child record between the check and the delete, making the result outdated. Run both inside one transaction with proper locking, and use foreign key constraints in the database as the final safeguard.

Share this article