Stage 2 · First Version · Chapter 22

AI-Generated App Security: 7 Holes AI Won't Warn You About

AI writes code that works, not code that is safe. Learn the security holes we found in our own SaaS, how we closed them, and the prompts that prevent them.

Dr. Yahia Elkhouli · November 21, 2026 · 7 min read

Here is an uncomfortable fact. When security researchers at a company called Escape scanned more than 5,600 public apps built with AI app builders, they found more than 2,000 vulnerabilities, more than 400 exposed secret keys, and 175 cases of exposed personal data, including medical records and bank account numbers. Every one of them was in a live app that real people were using.

AI writes code that works. Working and safe are not the same thing. AI will happily build a feature that does exactly what you asked, and also lets a stranger read your customers' data. It will not mention it, because you did not ask.

I am not a programmer, and we fell into several of these holes ourselves while building KashBoss. This article teaches the general lessons, using only problems we found and closed.

The short answer

AI-generated apps are often insecure because security is rarely part of the request. To protect one, make sure every request answers three questions on the server: who are you, which company do you belong to, and what are you allowed to do. Put every rule in the database, make every check fail closed (missing means "no"), and prove it by attacking your own app with tests that undo themselves.

The idea: three questions and an apartment building

Every request must answer, in this order: who are you (login), which company do you belong to (separation between customers), and what are you allowed to do (permissions).

Think of an apartment building: the lobby door is login, each apartment's lock is the company, and locked rooms inside are permissions. Most holes are a door with no lock, or a sign saying "private", which is not a lock at all.

Fact one: your app is in the user's hands. The web version's code can be read by anyone, and the key it uses to talk to the database ships inside the app. Anyone who has it can talk to your database directly, without your screens. So hiding a button protects nothing. Every rule must live in the database.

Fact two: some database functions run with the owner's powers. On Supabase the app can call functions inside the database by name (called RPCs). A function marked "security definer" skips the row rules that protect your tables. Such a function must check by itself who is calling and which company they belong to, because nothing else will.

One principle runs through everything: fail closed. When anything is missing, unknown or unexpected, the answer is "no".

Hole 1: one company can see another

In a SaaS, many companies share one database. If one table forgets to check which company a row belongs to, a customer can open another customer's invoices. Separation is a rule you must state every time.

The fix: every table with customer data gets a company column and a database rule (Row Level Security) that lets a user touch only rows of their own company. Then a test proves it.

THE PROMPT
Design the database for [APP NAME] on Supabase.
Every table that holds customer data must have a business_id column.
Turn on Row Level Security on every table, so a user can only read and write rows of their own business.
Explain each rule in plain English first, then give me the SQL.
Then write a test that logs in as a user from Business A and proves they cannot see any row from Business B.

In our September 2026 audit we logged in as real users of different companies and confirmed none could read another company's rows. The tables were sound.

Hole 2: functions anyone on the internet can call

On Supabase, every new database function can be called with the public key by default, even by someone who never logged in. We thought "only for logged-in users" made it safe. It did not: that adds a door for logged-in users, it does not close the public one. Removing the permission from the anonymous role alone removes nothing, because the default goes to "everyone". We found that by re-checking after a fix.

AI never notices, because the function works perfectly when you are logged in to test it. We asked for an audit of the live database, not of our files. It found functions that ran with owner powers and contained no check of the caller at all. We wrote one small guard, "is the caller a member of this company?", put it at the top of the functions that needed it, and closed the rest to the app entirely.

THE PROMPT
For every database function in this project:
1. List the ones that can be called without logging in.
2. For each one, show me where it checks who the caller is and which company they belong to.
3. If there is no check, add one at the very top and remove public access.
4. Call each function as a logged-out user and as a user from another company, and show me both are refused.

Hole 3: the guard that opened the door

We had a check that said: allow if owner, or manager, or an accountant with the "employees" permission. For an accountant who had that permission unset, the database did not read it as "no". It read it as "unknown", and an unknown result made the whole check quietly step aside.

Databases have three answers, not two: yes, no and unknown. A missing value compared with anything gives "unknown". The line that says "if not allowed, refuse" acts only when "not allowed" is clearly true, so unknown slips through. Missing permissions are not rare. They are the normal state of any permission you add later, because existing employees do not have it yet.

THE MISTAKE WE MADE

The code passed review, and the door was open. We caught it by testing like an attacker, not by reading code. We pretended to be an accountant with no permissions, tried every action, and the test undid itself at the end so no real data changed. The first line of the results said the accountant had succeeded. Only the attempt showed the truth. The fix: read each permission with a default of "no", and ask "is this definitely allowed?" instead of "is this not refused?"

THE PROMPT
Review every permission check in the database.
For each one, tell me what happens when the permission value is missing (not true, not false: missing).
If a missing value ends up allowing the action, rewrite the check so missing means refuse.
Prove it with a test that runs as a user with no permissions and undoes all changes at the end.

Hole 4: a lock that existed only on the screen

Owners could switch off "give discounts" for a sales rep. The switch hid the discount box in the app, but the server never checked it. We later found discounted invoices from reps whose switch was off. The AI had done what was asked: add a setting and hide the button.

A switch that does nothing is bad. One that tells the owner something is blocked when it is not is worse, because he stops watching. Every permission must be enforced inside the function that saves the data, and in every version of that function, because people on an old version of the app still call the old one.

The other three, briefly

Make it a routine

Security is never finished. Every few weeks we run a full audit with several AI agents, each attacking from a different angle, then fix what they find. Test as a real logged-in user, not only as a stranger, and verify every finding. Chapter 22 of the book walks through all seven holes with the full prompts.

Key takeaways

Frequently asked questions

Is an app built with AI safe to launch?

Not by default. AI builds the feature you describe and usually skips protections you did not mention. Before launching, ask it to prove that one company cannot see another's data, that no function can be called without a login unless meant to be public, and that every permission is enforced on the server.

What is Row Level Security in Supabase?

It is a database rule that decides which rows a user may read or change. For a multi-company SaaS, the usual rule is that a user can only touch rows whose company matches their own. It works at the database level, so it holds even if someone bypasses your screens.

How can I test security if I cannot read code?

Ask the AI to act as an attacker: a logged-out stranger, a user from another company, and an employee with no permissions. Have it try real actions inside a transaction that is canceled at the end, so no data changes, and ask it to show you which attempts succeeded.