Database access

How do we prevent unauthorized database access in our AI-written application?

Enforce access in the database or on the server, not in the interface: every table needs a rule that decides who may read and write each row, and every server route needs to check who is calling and whether they own the record. Then prove it by requesting another user's data with a second test account, because AI-written code that runs correctly can still return rows it should not.

Sources last checked . Links go to each tool's own site or documentation.

Put the rule where the data lives

A browser app that talks to a hosted database ships its connection details to every visitor. Hiding a button or a page does not stop someone from calling the database API directly, so the rule has to run in the database or on your server.

  • Supabase and Postgres tables. Enable Row Level Security on every table in an exposed schema, write a policy for each operation, and grant each role only the operations it needs. Supabase's RLS guide says a table in an exposed schema without RLS is readable and writable by any role with a grant on it, and that adding policies does not remove existing grants.
  • Views and functions. The same guide says views bypass RLS by default unless they are created with security_invoker = true on Postgres 15 and above, and that a security definer function runs as the role that created it. Review each one the way you would review a table.
  • Server routes. For every API route, server action or edge function, check the session and the ownership of the requested record on the server. The failure this prevents is what OWASP calls an insecure direct object reference: changing an ID in a request returns someone else's record.

Keep privileged keys off the client

Supabase has two kinds of key. A publishable key (sb_publishable_..., or the legacy anon key) is meant for browsers and only reaches what grants and RLS allow. A secret key (sb_secret_..., or the legacy service_role key) bypasses RLS and belongs on a server. Supabase's API keys guide adds that a secret key used from a browser is rejected with a 401, but that an attacker can still use a leaked key from other tools. If one reached a bundle or a commit, rotate it.

Frameworks decide what reaches the browser by variable name. Next.js bundles variables prefixed NEXT_PUBLIC_ for the browser, and Vite exposes variables prefixed VITE_ to client code. Only public keys belong behind those prefixes.

Stop injection in code you did not write by hand

Where your server builds SQL, use parameterized queries. The OWASP SQL Injection Prevention Cheat Sheet lists parameterized queries and allow-list input validation among its primary defenses, and least privilege as additional hardening. Search AI-written code for queries assembled from strings, and give the application its own database role, not the owner's.

Test it the way an outsider would

  1. Create two test accounts, A and B, each with one private record.
  2. Signed out, request A's record through the same API the app uses. You should get nothing back.
  3. Signed in as B, request A's record by its ID, then try to update and delete it. Each attempt should fail or return no rows, and A's record should be unchanged afterwards.
  4. Repeat for every table, storage bucket and server route that holds private data.

On Supabase you can keep these as automated tests. supabase test db runs pgTAP tests, and the RLS guide includes a test file that asserts allow and deny for each operation and role. Run the suite in CI so a later migration cannot quietly reopen a hole.

Use the free checks first, and know what each misses

Free checks for database access and what each one misses
CheckWhat it tells youWhat it does not tell you
Supabase Security AdvisorConfiguration problems inside the project, such as RLS disabled on a public table, RLS enabled with no policy, security definer views and policies that reference user metadata.Whether a policy that exists returns the right rows to the right user. Supabase notes that some findings may be intentional.
Two-account testWhat a signed-in user can actually read and change in the running app.Anything you did not think to request.
pgTAP tests in CIThat the cases you wrote still pass after each migration.Cases you never wrote, and whether production matches the schema you tested.

If you build on Lovable, run its built-in checks as well. Lovable's security docs say the checks find common problems but cannot guarantee that an app is secure or replace a full security review.

Give the AI tool standing rules

Put the rules in the instructions your coding tool reads on every task: enable RLS on every new table, write a policy per operation, never place a secret key in client code, and check ownership on the server in every route. This cuts down repeat mistakes. It does not replace the tests above, because an instruction is not enforcement.

Where a scanner fits

A scan does not prevent anything. It shows you what is already exposed. Vibe App Scanner checks a deployed URL for secrets in shipped JavaScript and for data that the Supabase tables it discovers return to public clients. Separately, it reviews a GitHub repository or ZIP for issues in committed SQL policies, grants and supported server handlers.

A URL scan observes behavior it can reach, for example that a read returned rows. It cannot prove how a policy is defined, whether writes are blocked, or that tenants are isolated. A code scan reviews a snapshot of the source and cannot prove what is deployed.

Plans are Go at $29 a month and Pro at $49 a month. New accounts start with a 7-day trial that requires a card and includes 3 scans in total across deployed URLs and repositories. The plan you picked starts when the trial ends unless you cancel first. See pricing. Both report types have a fictional example you can read without an account: a website report and a code report.

Related questions

Is Row Level Security enough on its own?
No. RLS decides which rows a role can reach, but grants decide whether the role can use the table at all, and views, security definer functions and server code that uses a secret key can bypass it. Review those paths as well and test with a second account.
Is the database key in my frontend a leak?
Not if it is a public key. Supabase publishable keys and legacy anon keys are designed for browsers and rely on grants and RLS. A secret or service_role key in a bundle is a leak: rotate it and move the calls that need it to a server.
Can a scanner prove my database is locked down?
No. A scanner can show that a request returned data or that a risky pattern exists in code. It cannot prove that every table, role and write path is protected. Tests with separate accounts are still needed.

Sources checked

Each claim about another product on this page was read on the page linked here on . Plans and features change, so confirm on the source before you rely on them.