How to fix BOLA (Broken Object Level Authorization)
in Phoenix
Executive Summary
BOLA (Broken Object Level Authorization) is the apex predator of API vulnerabilities. In the Phoenix framework, BOLA manifests when a controller action blindly trusts a user-supplied ID to fetch a resource without validating ownership. If your code assumes that because a user is authenticated they have the right to access any ID they request, you have already lost. To kill BOLA, you must enforce strict ownership scoping at the database query level.
The Vulnerable Pattern
def show(conn, %{"id" => id}) do # VULNERABLE: Fetches any post by ID regardless of who owns it. post = Blog.get_post!(id) render(conn, "show.json", post: post) endIn Blog context:
def get_post!(id), do: Repo.get!(Post, id)
The Secure Implementation
The vulnerable snippet uses a direct primary key lookup. An attacker can simply iterate the 'id' parameter to scrape every post in the database. The secure implementation employs 'Ownership Scoping'. By requiring both the 'id' and the 'user_id' in the Ecto query (Repo.get_by), the database engine itself enforces the authorization boundary. If a user attempts to access an ID they do not own, the query returns nil. This approach is more performant and less error-prone than fetching the object first and checking permissions in application logic.
def show(conn, %{"id" => id}) do user = conn.assigns.current_user # SECURE: Scopes the lookup to the authenticated user's context. case Blog.get_user_post(user, id) do nil -> conn |> put_status(:not_found) |> json(%{error: "Resource not found or access denied"}) post -> render(conn, "show.json", post: post) end endIn Blog context:
def get_user_post(user, id) do Repo.get_by(Post, id: id, user_id: user.id) end
Your Phoenix API
might be exposed to BOLA (Broken Object Level Authorization)
74% of Phoenix apps fail this check. Hackers use automated scanners to find this specific flaw. Check your codebase before they do.
Free Tier • No Credit Card • Instant Report
Verified by Ghost Labs Security Team
This content is continuously validated by our automated security engine and reviewed by our research team. Ghost Labs analyzes over 500+ vulnerability patterns across 40+ frameworks to provide up-to-date remediation strategies.