Sub topic 14
Adding AI features and security basics
This chapter assumes you have a working app with accounts, a data model and at least one integration. You should know what an API call and a webhook are. It covers two topics in order: adding an AI feature, then protecting your users and data.
Adding AI features to your product
An AI feature is a product function that sends user input to a hosted language model through an API and uses the reply. Examples are summarizing notes, drafting an email, or sorting incoming requests.
Treat it like any other integration. Your app sends a request, waits, and gets a response that may be slow, wrong or missing.
Pick a narrow job
Add an AI feature only if it supports your core flow. For TaskNest, a useful feature is "suggest a title from pasted client email text". A chat window that can discuss anything is not.
The working model
The call goes through your server or a backend function, never straight from the browser. This keeps the API key private.
Common errors
- Key in the front end. Anyone can read it in the browser and run up your bill.
- No limits. Cap requests per user per day and set a spending limit with the provider.
- Trusting the output. Models can produce wrong or oddly formatted text. Show results as suggestions the user can edit.
- No failure path. Handle timeouts and errors with a clear message and a manual fallback.
- Prompt injection. User text can contain instructions such as "ignore your rules". Keep your instructions separate from user input, and never let model output trigger actions like deleting data without a check.
Security and privacy basics
Security for an MVP means a few habits that prevent the most likely harm. Privacy means collecting and sharing only the personal data you need.
Core habits
- Enforce access in the database. Hiding a button is not protection. Row-level security makes the database refuse requests for other users' rows.
- Keep secrets out of client code. API keys, service keys and webhook secrets belong in environment variables or the platform's secret store.
- Verify webhooks. Anyone can post to your webhook URL. Check the signature before acting.
- Collect less. Every stored field is a liability. Skip data you do not use.
- Tell the AI provider less. Strip names, emails and other personal data from prompts when the feature does not need them. Check whether the provider trains on your data.
Row-level security
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);With row-level security on and no matching policy, a request returns nothing. That is the safe default.
Verifying a webhook signature
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);
}Use the raw request body. If a framework parses the JSON first and you re-serialize it, the bytes can change and valid signatures will fail.
Exercise
Your AI feature calls the model API directly from browser JavaScript, with the key in a config file. Name the problem and the fix.
Show answer
Anyone can read the key from the browser and use it at your expense. Move the call to a server or backend function that reads the key from a secret store, and add per-user rate limits.
Exercise
A tasks table has row-level security enabled but no policies. What can a signed-in user read?
Show answer
Nothing. With no matching policy, access is denied by default, so you must add policies such as "owners read own tasks".