# The 1-in-5 File: security rules for the AI that builds your app

About 1 in 5 public Lovable apps would leak data or keys ([Priberry's study](https://priberry.com/research/lovable-leaks/)). These rules come from Priberry's studies of public Lovable apps. Drop this file into your AI builder as Lovable Knowledge, CLAUDE.md, AGENTS.md, .cursor/rules or .windsurfrules.

From [Launch Blockers](https://github.com/priberry-data/launch-blockers), Apache-2.0 like the rest of the repo: copy it, change it, share it.

## Rules

Follow these rules before you write or change any code in this project. If a request would break one, say so and build the safe version instead.

### The database

- Turn on row-level security on every table before launch. Every table that holds personal data (email, phone, address, birth date, messages, payments, health) gets policies scoped to its owner, such as `auth.uid() = user_id`. About 1 in 7 public Lovable apps with a database would leave personal data open to anyone: 44 of 322 ([study](https://priberry.com/research/lovable-open-tables/)).
- Never write `USING (true)` on a table with private columns. A read policy covers every column in the row, so keep public fields (name, avatar) in their own table or in a view that selects only those columns.
- Never write `USING (true)` or `WITH CHECK (true)` for INSERT, UPDATE or DELETE. Only the row's owner, or a role you check on the server, may change a row.
- Every UPDATE policy needs `WITH CHECK` as well as `USING`, so users can't change their own `role`, `is_admin`, `plan` or `user_id` column. Keep roles in a table users can't write to.
- Never decide who someone is from `user_metadata` or `raw_user_meta_data` in a policy: users can edit it themselves.
- Create views `WITH (security_invoker = true)`. A `SECURITY DEFINER` function must check `auth.uid()` itself and set `search_path`.
- Storage buckets that hold users' files are private. Hand out signed URLs, never a public bucket.

### Keys and secrets

- Never put a paid AI key (OpenAI, Gemini, Anthropic, OpenRouter, Groq, Mistral, ElevenLabs...) in browser code or in a `VITE_`, `NEXT_PUBLIC_` or `EXPO_PUBLIC_` variable. Call the AI from an edge function or server route that checks the user is signed in, and keep the key there. About 1 in 30 public Lovable apps would ship an AI key to every visitor: 37 of 1,167 ([study](https://priberry.com/research/lovable-ai-keys/)).
- Never ship a Supabase `service_role` key or `sb_secret_` key to the client. Browser code uses only the anon or publishable key, and row-level security does the protecting.
- Never check a password, admin code or invite code in browser code. Anyone can read it in the page source.
- Never commit `.env` or any file with a secret in it. Put `.env` and `.env.*` in `.gitignore`, with `!.env.example` so you can commit an example file with empty values.
- If a key was ever in browser code, a commit or a deployed build, treat it as leaked: revoke and replace it first, then fix the code. Steps per provider: [Key Panic](https://greenlit.priberry.com/blockers/key-panic/), or `npx launch-blockers panic <provider>`.

### Server code

- In any function that uses the `service_role` key, never trust a user id, email or role sent in the request body, query or headers. Get the user from the verified token (`supabase.auth.getUser()`) and check they own the row before reading or writing it.
- Every edge function checks the caller is signed in, unless it is meant to be public. Payment webhooks verify the provider's signature before doing anything.
- Access checks run on the server or in the database. Hiding a button in the browser is not an access check.
- Before launch, run `npx launch-blockers check` and fix every blocker it reports.

## Check before you ship

Priberry makes six free, open-source tools:

- **Launch Blockers**: the leaks AI-built apps ship, checked in your code. `npx launch-blockers check`
- **Rolelock**: can your users raise their own role? Finds two holes in Supabase apps: signed-in users who can raise their own role, and edge functions that use the secret key with an id from the request. `npx rolelock@0.1.1 ./my-app`
- **Greenlit**: what a stranger can see in your live app. `npx greenlitlab yourapp.com --mine`
- **Crumple**: a crash-test lab for AI coding agents. `npx crumplelab attack`
- **Tare**: where your coding agent's tokens go. `npx tarelab`
- **MarkOS**: an AI exec team on your own computer, [markos.priberry.com](https://markos.priberry.com). `git clone https://github.com/priberry-data/markos && cd markos && ./install.sh`
