Prompt

How do I enforce object-level authorization on APIs?

Technology / Cybersecurity1 observationLast seen Oct 29, 2026

Latest observation

Oct 29, 2026OpenAI APIWeb search: off

To enforce object-level authorization on APIs, you need to make sure a user can access only the specific resource instance they’re allowed to access, not just the endpoint in general.

What it means

Example:

  • GET /orders/123
  • Even if the user is authenticated, they should only see order 123 if they own it or have permission to view it.

This is different from:

  • Authentication: “Who are you?”
  • Object-level authorization: “Are you allowed to access this particular object?”

Core approach

1) Never trust client-provided IDs alone

If an API accepts a resource ID like /users/42, /projects/99, or order_id=123, always verify the requesting user’s permission against that object on the server.

Bad:

  • Return object directly by ID

Good:

  • Fetch object
  • Check whether current user has access to it
  • Then return it

2) Check permissions in the data access layer or policy layer

Do the authorization check as close to the data fetch as possible so you don’t accidentally leak data in later code.

Common patterns:

  • Policy-based authorization: can_view(user, object)
  • Scoped queries: only query objects the user is allowed to access
  • Row-level security in the database for defense in depth

3) Use ownership/tenant checks

Typical rules:

  • User can access objects they own
  • User can access objects in their organization/tenant
  • Admins may access all objects
  • Shared-access rules may apply

Example:

order = Order.objects.get(id=order_id)
if order.owner_id != request.user.id:
    raise PermissionDenied

Better:

order = Order.objects.filter(id=order_id, owner_id=request.user.id).first()
if not order:
    raise NotFound()  # Often better than revealing existence

4) Prefer “filter then fetch” over “fetch then check” when possible

This avoids accidental information disclosure.

Example:

SELECT * FROM orders
WHERE id = ? AND owner_id = ?;

If no row is returned, treat it as not found.

This prevents:

  • “This order exists but belongs to someone else” leakage
  • Timing or error-message based inference

5) Protect every object-accessing endpoint

Object-level auth must be enforced for:

  • GET /resource/{id}
  • PUT /resource/{id}
  • PATCH /resource/{id}
  • DELETE /resource/{id}
  • nested resources like /projects/{id}/tasks/{taskId}

Also remember:

  • Search endpoints
  • Export/download endpoints
  • Bulk update/delete endpoints
  • Admin/debug endpoints
  • Relationship endpoints (add user to project, approve invoice, etc.)

6) Use centralized authorization logic

Don’t duplicate permission checks all over controllers.

Options:

  • Authorization service
  • Policy classes
  • Middleware + per-object checks
  • Framework hooks/decorators

Example pattern:

if (!authz.can(user, "read", resource)) {
  return res.status(403).send("Forbidden");
}

Or, if resource should be hidden:

if (!resource) return 404;
if (!authz.can(user, "read", resource)) return 404;

7) Be careful with indirect object references

If your API uses IDs, UUIDs, file paths, slugs, or tokens as references, they are all object references and require authorization.

Examples:

  • invoice_id
  • document_uuid
  • report_slug
  • s3_key
  • /download?file=...

Even if IDs are hard to guess, auth is still required.


8) Consider database-level enforcement

For high-security systems, use:

  • Row-Level Security (RLS) in PostgreSQL
  • Tenant-based filters
  • Views with restricted access

This helps prevent mistakes in application code from exposing data.


9) Audit and test for IDOR

A common vulnerability here is IDOR (Insecure Direct Object Reference).

Test:

  • Change the resource ID in the request
  • Try other users’ IDs
  • Try nested object IDs
  • Try bulk endpoints with mixed authorized/unauthorized IDs

Add automated tests like:

  • user A can’t read user B’s object
  • user A can’t update user B’s object
  • user A can only list their own objects

Practical implementation pattern

For each endpoint:

  1. Authenticate user
  2. Look up the requested object with an access filter
  3. If not found, return 404
  4. If found but not permitted, return 403 or 404 depending on your disclosure policy
  5. Proceed only after authorization succeeds

Example pseudo-code

def get_invoice(request, invoice_id):
    invoice = Invoice.objects.filter(
        id=invoice_id,
        org_id=request.user.org_id
    ).first()

    if not invoice:
        return Response(status=404)

    return Response(invoice.to_dict())

For update:

def update_invoice(request, invoice_id):
    invoice = Invoice.objects.filter(
        id=invoice_id,
        org_id=request.user.org_id
    ).first()

    if not invoice:
        return Response(status=404)

    if not request.user.can_edit(invoice):
        return Response(status=403)

    invoice.amount = request.data["amount"]
    invoice.save()
    return Response(invoice.to_dict())

Good default rule

A strong default is:

Every request that references a specific object must verify the caller’s access to that exact object before returning or modifying it.


If you want, I can also show:

  1. framework-specific examples for Django, FastAPI, Express, Spring, or .NET
  2. a policy-based design for object-level auth
  3. a checklist to prevent IDOR in your API

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.