Insecure direct object references: seeing other people's data
TL;DR
Difficulty: Intermediate
An insecure direct object reference (IDOR) is when an app hands over a record just because you asked for its id, without checking the record is yours.
- The classic move: change /invoices/482 to /invoices/483 and read a stranger's invoice. The same flaw hides in forms, download links and API requests.
- Because ids count upwards, an attacker can walk the whole sequence and scrape every customer in minutes, which is a reportable personal data breach.
- AI makes it more likely: the naive Model.find(params[:id]) looks perfect in a demo, there is no error to see, and no test logs in as a second user.
- In Rails I scope every lookup through current_user.invoices.find, authorise richer cases with Pundit, default to deny, and use hard to guess ids as a seatbelt.
- A Cucumber scenario, Brakeman and a pen test with Burp Suite or OWASP ZAP prove one user cannot read another's data.
Open something that belongs to you in almost any web app and look at the address bar. It often ends in a number, like /invoices/482. The quiet question that decides whether the app is safe is whether, when you change that number, it checks the record is actually yours. When it does not, you have an insecure direct object reference, and this is what it is and how I keep it out of the Rails applications I build.
The question behind this article
Log in to almost any web application, open something that belongs to you, and look at the address bar. An invoice, an order, a message, a report. Very often the address ends in a number: /invoices/482. That number is the application pointing at one row in its database. The quiet question that decides whether the application is safe is this: when you change that number to 483, does the application check that invoice 483 is yours before it shows it to you?
If it does not, you have an insecure direct object reference, usually shortened to IDOR. It is one of the most common serious flaws on the web, it needs no special tools to find, and I see it over and over in applications that were thrown together quickly. This article explains what it is in plain words, shows how the attack works on a short safe example, and then shows exactly how I keep it out of the applications I build in Ruby on Rails.
I will use one invented business throughout. Fenwick Invoicing is a small web application in Leeds (fenwickinvoicing.example) that lets freelancers send invoices to their clients and see which ones have been paid. Thousands of self employed people use it, each seeing only their own invoices. Its developer is Dana. The business and Dana are made up. The code is real and runs.
What an insecure direct object reference is
Every record an application stores has an identifier, a short label the database uses to find it again. Most of the time that label is just a number counting upwards: the first invoice is 1, the next is 2, and so on. When that number shows up in a web address, or in a form, or in the data an app sends back to your browser, it is a direct reference to the object. Direct, because it points straight at the row.
The reference becomes insecure when the application hands over the object without checking that the person asking is allowed to have it. Logging in is not enough. Being a real, paying, honest customer is not enough. The application has to ask a second question every single time: not just "who are you", but "is this particular thing yours".
Put plainly for a non developer: imagine a cloakroom where the tickets are numbered 1, 2, 3. You hand over ticket 41 and get your coat. Nothing stops you handing over ticket 42 instead and walking off with someone else's. A secure cloakroom checks the ticket against the face. An insecure one just reads the number and fetches the coat. IDOR is the software version of a cloakroom that only reads the number.
The jargon around this has shifted over the years. The Open Worldwide Application Security Project, OWASP, used to list IDOR on its own. Today it sits inside the category called Broken Access Control, which has been the number one risk in the OWASP Top Ten since 2021.1 The name changed. The mistake did not.
How the attack actually works
Here is the heart of it in Fenwick Invoicing. Dana, under deadline pressure, writes the controller that shows an invoice like this:
class InvoicesController < ApplicationController
before_action :authenticate_user!
def show
@invoice = Invoice.find(params[:id])
end
end
Read it line by line. authenticate_user! makes sure a visitor is logged in before the page runs. Invoice.find(params[:id]) takes the number out of the web address and fetches that invoice from the database. On screen it all looks right. Dana logs in, opens invoice 482, and there is the invoice, correct down to the penny.
The flaw is what is missing. authenticate_user! proves you are somebody. It never checks that invoice 482 is yours. Invoice.find fetches whatever number you ask for. So a logged in freelancer who looks at the address /invoices/482, changes the 2 to a 3, and presses return, is shown invoice 483, which belongs to a complete stranger. No warning, no error, no trace. The page simply loads another person's client, amount and bank details.
That is the classic move, and it is why people describe IDOR as "changing the id in the URL". The exact same weakness hides in less visible places:
- A hidden field in a form that carries
invoice_id, changed with the browser's developer tools before the form is sent. - A download link such as
/invoices/482.pdf, where the PDF is served by number with no owner check. - An API request whose body says
{"account_id": 17}, swapped to18. - A "mark as paid" or "delete" button that posts an id, so the attacker is not only reading other people's data but changing it.
And because the numbers count upwards, an attacker does not stop at one. They write a short loop that asks for 1, 2, 3, all the way up, and saves every answer. Walking a sequence of ids like this is called enumeration. It turns one leaked invoice into the whole customer base in a matter of minutes.
What the attacker is actually after
It is worth being concrete about why anyone bothers. An invoice is not an abstract record. It carries a client's name, a billing address, an amount, often a VAT number, sometimes bank details and a description of the work. Multiply that by every account on Fenwick Invoicing and you have a complete, tidy list of thousands of freelancers, their clients, and how much money moves between them. That is valuable on its own and priceless as raw material for fraud, from convincing fake invoices to targeted phishing that quotes a real amount and a real client name.
There is a second prize beyond reading. If the id controls an action rather than a view, IDOR lets an attacker change the world, not just watch it. Marking someone else's invoice as paid, cancelling it, editing the bank details a client is told to pay into. That last one is how invoice fraud actually happens: not by breaking encryption, but by quietly changing the account number on a document a business already trusts.
The real world consequences land on the business that built the application. Under the UK GDPR and the Data Protection Act 2018, letting one customer read another customer's personal data is a personal data breach. Depending on the severity it may have to be reported to the Information Commission within seventy two hours, and the customers whose data leaked often have to be told directly.2 For a small software business that is reputational damage, a regulator asking questions, and in the worst cases a fine, all from a flaw that is a single missing line of code. IDOR is a favourite in bug bounty programmes precisely because it is so common and so damaging, and the public breach reports make for uncomfortable reading.
How AI has raised the risk
IDOR was already everywhere. The current wave of AI written code has made it more likely, not less, and it is worth understanding why.
When you ask an assistant to "show a logged in user their invoice", the pattern it reaches for is the one that appears most often in its training data, which is exactly the naive Model.find(params[:id]) above. That code works perfectly in the demo. It compiles, it runs, the page loads, the tests, if there are any, go green. The authorisation check is invisible by its absence: there is no error to see, no red flag, nothing on screen that says "a stranger could also open this". A happy path click through by the developer, or by the AI itself, sails straight past the hole, because the developer is only ever logged in as themselves.
The broader evidence on AI generated code points the same way. Veracode's 2025 GenAI Code Security Report found that AI generated code contains 2.74 times more vulnerabilities than code written by people.3 An Ox Security analysis of more than three hundred repositories in 2025 found the same ten anti patterns in eighty to one hundred percent of AI generated code.4 Other research has found that around forty five percent of AI generated code samples introduce a vulnerability from the OWASP Top Ten, the very category IDOR lives in.5 A 2026 study of more than three hundred and four thousand commits found that 24.2 percent of the issues AI introduced were still unresolved later, surviving unnoticed into the running product.6 Georgetown's Center for Security and Emerging Technology reached similar conclusions in its 2024 work on the cybersecurity risks of AI generated code, as did CrowdStrike's 2025 research.7
Access control is the single hardest thing for a machine to get right on its own, because the rule lives in your business, not in the code. Who may see this invoice is a fact about Fenwick Invoicing that no model can infer from the request alone. When nobody on the team can read the code well enough to notice the missing check, and no test logs in as a second user to prove the door is shut, the flaw ships. That is the real danger of vibe coded applications: not that the code looks wrong, but that it looks perfectly right while quietly trusting a number from the attacker.
How I defend against it
The fix is not clever. It is disciplined, and in Rails it comes down to a handful of habits I apply on every project.
Never trust an id from the client, scope every lookup through the current user
The root cause is asking the database for a record by a number the attacker controls. So I never do that. Instead of looking up an invoice globally and hoping it belongs to the right person, I look it up through the person:
class InvoicesController < ApplicationController
before_action :authenticate_user!
def show
@invoice = current_user.invoices.find(params[:id])
end
end
The change looks tiny and it is everything. current_user.invoices is the set of invoices that belong to the logged in person and nothing else. Calling find on that set can only ever return one of their own invoices. Ask for an id that is not yours and the query finds nothing, so Rails raises ActiveRecord::RecordNotFound, which the application turns into an ordinary 404 page. The stranger's invoice is not merely hidden, it is unreachable, and the attacker cannot even tell whether invoice 483 exists. Enumeration gets them a wall of 404s and nothing else.
Authorise explicitly for everything scoping cannot express
Scoping through current_user covers the common case cleanly. Some rules are richer: an accountant who may view a client's invoices, an admin who may see everything, a document shared with a named colleague. For those I use a policy object, with the Pundit gem, so the rule lives in one named place and is applied the same way everywhere:
class InvoicePolicy < ApplicationPolicy
def show?
record.user == user || user.accountant_for?(record.user)
end
end
The controller then asks the policy before it does anything: authorize @invoice. The decision is written once, in plain terms, and can be read and tested on its own. The important discipline is default deny. Nothing is visible unless a rule explicitly says it is, so forgetting to write a rule fails safe, closed rather than open.
Strong parameters, so nobody reassigns ownership
Access control also means stopping a user handing themselves someone else's record. Rails strong parameters let me list exactly which fields a form may set, so a crafted request that tries to send user_id or owner_id is ignored rather than obeyed. Ownership is set by the server from current_user, never accepted from the browser.
Hard to guess identifiers, as defence in depth
Sequential numbers make enumeration easy, so for anything exposed to the outside world I often use a non guessable identifier instead, a UUID or a signed token, so /invoices/9f2c... cannot simply be counted upwards. This is a seatbelt, not a brake. It slows an attacker who is guessing, but it is never a substitute for the ownership check. An id that leaks, in an email or a log, must still be useless to anyone it does not belong to. The real defence is always the check; the opaque id just means a guess is unlikely to land.
Why testing and pen testing catch it
A defence you cannot see is a defence you cannot trust. The point of the habits above is that each one can be proved, by a test that a non developer can read and a machine runs on every change.
I lock the rule in with a behaviour driven test, written in Cucumber, that describes the attack in plain English:
Scenario: a freelancer cannot read another freelancer's invoice
Given Priya is signed in
And Marcus has an invoice for 400 pounds
When Priya tries to open Marcus's invoice directly
Then she sees a "not found" page
And she does not see the 400 pound amount
That scenario fails loudly against the naive controller, because Priya would see the amount, and passes against the scoped one. It is readable by the owner of Fenwick Invoicing, it names the exact danger, and it runs on every deploy. Crucially it logs in as a second person and tries the forbidden thing, which is the one move a happy path click through never makes and the one an AI rarely writes for itself.
Automated analysis adds a second net. I run Brakeman, the static analysis tool for Rails, on every build. It reads the code without running it and flags risky patterns, including unscoped model lookups and mass assignment, the exact shapes IDOR hides in. It is not perfect, no scanner is, which is why it sits alongside the tests rather than replacing them.
Before a release I pen test the application the way an attacker would, using the standard tools that ship with Kali Linux. With Burp Suite or OWASP ZAP I log in as one user, capture a legitimate request for my own invoice, then replay it with a different id and with a second user's session, and watch what comes back. If the ownership check is working, every one of those attempts is a 404. If one returns real data, I have found an IDOR before anyone else did, and it goes back on the test list as a failing scenario before it is fixed. That is the difference between hoping the door is locked and walking up and trying the handle.
Why this is work you can insure
All of this rests on one idea. The applications I build are owned, readable code, where every important rule is written down as a test the business can read, not a black box that happens to work on the day. "Only the owner can see their invoice" is not a promise in a meeting, it is a named scenario that a machine checks on every change and a release refuses to go out without. When your insurer, your auditor or your biggest client asks how you know one customer cannot read another's data, the answer is a file of scenarios and a pen test report, not a shrug.
That is the opposite of how IDOR usually ships: in code nobody has read closely, with no test that logs in as a second user, often generated in a hurry and trusted because the demo looked fine. The fix costs one line and a little discipline. Finding out you shipped the flaw, after a stranger has walked your customer list, costs a great deal more.
A quieter close
None of this is exotic. Scope every lookup through the current user, authorise the richer cases explicitly, default to deny, and prove the whole thing with a test that tries the attack and a pen test that tries it again by hand. Do that and the number in the address bar stops being a door and goes back to being just a number.
If you are not sure whether your own application checks ownership, or an AI assistant wrote large parts of it and nobody is certain what it trusts, that is worth half a day of someone's attention before it is worth a breach notification. In the free workshop we can look at how your application answers the "is this yours" question together, and the first two weeks of work after that are on me.
