VibecodingSecurity essentialsLevel 3: Workflows

How Do You Lock Down Your Database and Validate Input?

Give every table access rules so each user reaches only their own rows, even if someone bypasses your app. In Supabase that's row level security (RLS), and in Firebase it's Security Rules. Then check every input on the server, because anyone can send a request without using your form.

Prompt to copy
Lock down my database and validate input. Database: [Supabase / Firestore / Postgres behind my own server].
Private tables, each with its owner column: [TABLE: OWNER_COLUMN].
Tables anyone may read: [TABLE]. Shared tables: [TABLE and how membership works].

For Supabase, write one migration per private table in exactly this form:

alter table public.[TABLE] enable row level security;
revoke all on table public.[TABLE] from anon, authenticated;
grant select, insert, update, delete on table public.[TABLE] to authenticated;

create policy "owner reads" on public.[TABLE] for select to authenticated
  using ( (select auth.uid()) = [OWNER_COLUMN] );
create policy "owner inserts" on public.[TABLE] for insert to authenticated
  with check ( (select auth.uid()) = [OWNER_COLUMN] );
create policy "owner updates" on public.[TABLE] for update to authenticated
  using ( (select auth.uid()) = [OWNER_COLUMN] )
  with check ( (select auth.uid()) = [OWNER_COLUMN] );
create policy "owner deletes" on public.[TABLE] for delete to authenticated
  using ( (select auth.uid()) = [OWNER_COLUMN] );
create index [TABLE]_[OWNER_COLUMN]_idx on public.[TABLE] using btree ([OWNER_COLUMN]);

For a public-read table: enable RLS, revoke all, grant select to anon and authenticated, and add one select policy using (true). Add no write policy.
Never use using (true) on private data. Never disable RLS to fix a blank page. Find the missing policy instead.
Create views with (security_invoker = true). Any security definer function must set search_path = '' and sit outside exposed schemas.

For Firestore, use this for each private collection:

match /[COLLECTION]/{document} {
  allow read, delete: if request.auth.uid == resource.data.author_uid;
  allow create: if request.auth.uid == request.resource.data.author_uid;
  allow update: if request.auth.uid == request.resource.data.author_uid
    && request.auth.uid == resource.data.author_uid;
}

For input, validate every write on the server with a schema, like this:

import * as z from "zod";
const NewOrder = z.object({
  productId: z.string().min(1).max(64),
  quantity: z.number().int().min(1).max(100),
  note: z.string().max(500).optional(),
});
const result = NewOrder.safeParse(await request.json());
if (!result.success) return Response.json({ error: "Invalid input" }, { status: 400 });

Also: never accept price, role, owner_id or user_id from the browser. Use bound query parameters only, never string concatenation. Don't render user HTML without DOMPurify. For uploads, allow specific file types, check the real file content, set a size limit, and generate the file name.

List every table with its rules, and give me a test that a signed-out visitor and a second user both can't read private rows.
  1. RLS offAnyone with your project URL can access it
  2. A using (true) policyAny stranger who signs up can read everything
  3. RLS on, no policiesClosed, but the API returns no data
  4. Owner policies, testedEach user reaches only their own rows
Rule of thumb for the order, using behavior from Supabase's docs. From least protected to most protected.
  • Cryptographic algorithms87%
  • SQL injection83%
  • Cross-site scripting15%
  • Log injection12%
How often AI-written code passed a security scan, by type of weakness. Source: Veracode 2026 report, July 2026. Veracode sells code scanning, and it tests single functions, not whole apps or access rules.

Try it now

  1. 1In Supabase, open the Security Advisor at supabase.com/dashboard/project/_/advisors/security. Fix each ERROR first, starting with 0013_rls_disabled_in_public. On Lovable, you'll find policies under More → Database → Manage → Policies. On Bolt, run the Security Audit.
  2. 2Test as a signed-out stranger, on your own project only. Copy your project URL and publishable key from the Network panel, since they're public, and run curl 'https://<REF>.supabase.co/rest/v1/<TABLE>?select=*' -H "apikey: <PUBLISHABLE_KEY>". For Firestore, run curl "https://firestore.googleapis.com/v1/projects/<ID>/databases/(default)/documents/<COLLECTION>", or use the console's Rules Playground.
  3. 3Skip your form. Use Copy as cURL on a save request, then change the body: set quantity to -5, send a 10,000-character name, and put <img src=x onerror=alert(1)> in a text field. Then view the page that shows it.
  4. 4It worked if private tables return [], a permission error such as 42501, or Firestore's permission denied. Bad input should return 400, and no alert should appear.

Use it when

Before any real user data goes into a table, and again each time you add a table. Lovable's checklist includes confirming that new tables have policies. Add server validation to any route that writes data, sends email, spends money or accepts a file.

Skip it when

If your server is the only thing that talks to the database, over a private connection, and the table isn't in a schema exposed through an API, RLS isn't your main control. Your server's ownership checks are. Don't validate twice in a way that rejects real data, such as blocking apostrophes, which OWASP says rejects legitimate names without making queries safe.

Why it works

Apps built on Supabase or Firebase usually talk to the database from the browser using a public key. That makes the rules the only lock. Supabase's advisor describes a public table without RLS plainly: anyone with your project URL can access it.

Adding policies isn't always enough. Supabase's docs say that on existing projects a new table in public starts with every privilege granted to anon and authenticated, and policies don't remove those grants. With RLS on and no policies, the API returns no data. That's why a page goes blank after you turn RLS on, and why an AI may then suggest something unsafe to bring it back.

Input checks do a different job. OWASP says browser validation can be bypassed, so validate again on the server and define what you accept rather than trying to block bad strings. Validation doesn't stop SQL injection, which needs bound parameters, or cross-site scripting, which needs output encoding. It also doesn't show that this caller may touch that record.

Example

A clinic booking app has an appointments table. Its AI builder turned RLS on, the list went blank, and the next prompt added a policy to authenticated using (true). The app looks fixed, and a signed-out curl returns nothing, so it seems safe.

It isn't. Any stranger can sign up and read every appointment. Sign up with a new test account, copy that request with Copy as cURL, and remove the user_id filter. If you see other people's rows, replace the policy with the owner policies in the prompt. Supabase's advisor flags this pattern as 0024_permissive_rls_policy. A filter in your app's code, such as .eq('user_id', me), doesn't protect anything, because anyone can drop it from the request.

Going further

Put policy tests in the repository. Create them with supabase test new <table>_rls and run them with supabase test db. Supabase's docs warn against proving an allowed write with lives_ok, because it passes when zero rows change. Use returning and check the value.

Three more traps, all from Supabase's docs. Views bypass RLS by default, so a dashboard view can expose everything until it uses security_invoker = true. Two tables whose policies read each other, common with sharing features, cause recursion, which you break with a security definer function in a private schema. Public storage buckets skip access control for downloads, and 0025_public_bucket_allows_listing warns when their files can be listed.

For speed, wrap the helper as (select auth.uid()) so it's cached per statement, and index the owner column. For SQL you write yourself, pass values as $1 parameters, which node-postgres says avoids the injection risk of string concatenation.

Common mistakes

  • Turning RLS off, or adding using (true), because a page went blank. Empty results after enabling RLS mean a policy is missing.
  • Leaving Firebase rules at allow read, write: if true. Firebase's docs say never to use this in production.
  • Accepting a price, role or owner_id from the request body. Validation will pass it, because it's a valid value. OWASP notes that a valid account ID doesn't mean the caller may access that account.
  • Using the secret key in a shared server function to make an error disappear. Supabase says secret keys bypass RLS, so your code must now filter by user on every query.

Sources

Checked 6 November 2026. Products change often, so confirm details with the vendor.

Related

Beyond the course