Sub topic 15
Security and Privacy Basics
This chapter assumes you have read the chapters on data modeling and on accounts and access control. You should know what a table, a row and a signed-in user are. You should also have used an AI app builder or a no-code platform at least once.
An MVP with ten users can still leak data. A leak at that size can end the product, because early users trust you with the problem you are testing. This chapter covers the checks that matter most when a tool generates much of your app.
Where MVP security usually fails
No-code platforms and AI builders handle many hard parts for you. They host the servers, patch the runtime and manage TLS certificates. The failures that remain are mostly about configuration and about what you ask the tool to build.
The common ones are:
- A database table that any signed-in user, or any visitor, can read.
- Access rules enforced only in the screen, so hiding a button is treated as security.
- Secret keys pasted into client-side code or into a prompt.
- Webhook endpoints that trust any request that reaches them.
- Collecting more personal data than the hypothesis needs.
The rule behind most of these is simple. Anything running in the user's browser can be read and changed by that user. Enforce rules on the server or in the database.
Enforce access in the database
The data modeling chapter introduced an owner_id column. That column does nothing until a rule uses it. On a Postgres backend such as Supabase, row-level security (RLS) makes the database itself apply the rule to every query, no matter which screen or API call produced it.
create table tasks (
id uuid primary key default gen_random_uuid(),
owner_id uuid not null references auth.users(id),
title text not null,
client_name text,
due_date date,
status text not null default 'todo' check (status in ('todo', 'doing', 'done')),
created_at timestamptz not null default now()
);
alter table tasks enable row level security;
create policy "owners read own tasks" on tasks
for select using (auth.uid() = owner_id);
create policy "owners insert own tasks" on tasks
for insert with check (auth.uid() = owner_id);
create policy "owners update own tasks" on tasks
for update using (auth.uid() = owner_id) with check (auth.uid() = owner_id);Three details are easy to miss:
- With RLS enabled and no policy for an operation, that operation is denied. This is a safe default. Here there is no delete policy, so nobody can delete tasks through the API. Add one deliberately if you need it.
usingfilters which existing rows a user can see or touch.with checkvalidates the new row values. The update policy needs both, or a user could changeowner_idto hand a row to someone else.- If RLS is off, the table is open to anyone who holds the public API key. Many AI builders create tables without turning it on, so check this yourself.
Test the rule as a second user
Do not trust that a policy works because the app looks right. Test it:
- Create two test accounts, A and B.
- As A, create a task and note its id.
- As B, try to load that task through the app, then through the raw API or the platform's data viewer.
- As B, try to update or delete A's task.
If B can see or change anything, the rule is broken. Repeat this test after every significant change the AI builder makes, since regenerated code can silently drop a policy.
Keep secrets out of the wrong places
A secret is any value that grants power: an API key for a payment provider, a database service-role key, a webhook signing secret or an AI provider key.
- Store secrets in the platform's environment variables or secrets manager.
- Use public, publishable keys in the browser only when the provider designed them for that. A Supabase anon key is meant to be public because RLS protects the data. A service-role key bypasses RLS and must never reach the browser.
- Never paste real secrets into an AI builder prompt or chat. Conversation history may be stored or shared with the vendor.
- If a secret leaks, rotate it at the provider. Deleting it from your code does not help once it has been exposed.
The same applies to AI features. A call to a model provider made from the browser exposes your key. Route it through a server-side function that checks the user first.
Verify webhooks
A webhook is an HTTP request that an outside service sends to your app when something happens, such as a payment completing. The URL is reachable from the internet, so anyone can send fake requests to it. Most providers sign each request with a shared secret, using HMAC-SHA256 over the raw body. Check the signature before acting.
import { createHmac, timingSafeEqual } from "node:crypto";
// Verifies an HMAC-SHA256 signature (hex) sent by a service with a shared secret.
export function isValidSignature(rawBody, signatureHex, secret) {
const expected = createHmac("sha256", secret).update(rawBody).digest();
const received = Buffer.from(signatureHex, "hex");
return received.length === expected.length && timingSafeEqual(received, expected);
}Common mistakes with this check:
- Computing the signature over parsed and re-serialized JSON. Use the exact raw bytes received, because whitespace changes alter the hash.
- Comparing strings with
===. The constant-timetimingSafeEqualavoids leaking information through response timing. It throws if the buffers differ in length, which is why the length check comes first. - Skipping the check "for now" in a test setup and shipping it that way.
- Ignoring replays. Many providers include a timestamp. Reject events that are too old, and store event ids so a repeated event is processed once.
If your no-code platform receives webhooks through a built-in trigger, find out whether it verifies signatures. If it does not, put a small verifying function in front of it or use a secret token in the URL as a weaker fallback.
Privacy: collect less, say what you do
Privacy is mostly a design decision made early. Ask of every field: does the hypothesis need this?
- Collect the minimum. If the core flow works without a phone number, do not ask for it.
- Know where data lives. List each tool that stores or sees user data: the database, the email service, analytics, the AI provider, the form tool.
- Check vendor terms on whether your users' data is used to train models. Many AI services offer a setting or a plan that turns this off.
- Publish a short privacy policy that names what you collect, why, and who processes it. Add a way to request deletion, and make sure you can actually delete a user's rows in every tool.
- If you have users in the EU or UK, GDPR applies even to a small product. If you handle health or children's data, stop and get proper advice before launching.
A pre-launch checklist
| Check | How to verify |
|---|---|
| RLS or equivalent is on for every table with user data | Open each table's settings and test as a second user |
| No secrets in client code | Search the project and the browser's network tab for key patterns |
| Service-role or admin keys only on the server | Review environment variables and function code |
| Webhooks verify signatures | Send a request with a wrong signature and confirm it is rejected |
| Password and email flows work | Test reset, email confirmation and sign-out |
| Backups exist | Confirm the platform's backup setting and try a restore if possible |
| Privacy policy and deletion path exist | Delete a test account and check each tool for leftovers |
Exercise
Your app hides other users' tasks in the list screen, but a tester signed in as user B opens the raw API URL and sees user A's tasks. What is wrong, and what is the fix?
Show answer
The access rule exists only in the screen, and the database has no row-level security policy (or RLS is off), so the API returns every row. Enable RLS on the table and add policies such as using (auth.uid() = owner_id) for select and update, plus with check for insert and update. Then repeat the two-account test.
Exercise
A webhook handler parses the incoming JSON, then computes the HMAC over JSON.stringify(body) and compares it to the signature header with ===. Name two problems.
Show answer
First, the signature must be computed over the exact raw request body, because re-serializing can change whitespace or key order and produce a different hash, causing valid events to fail. Second, === is not a constant-time comparison, so use timingSafeEqual after checking the lengths match. A further improvement is to reject old timestamps and repeated event ids.