AleannLab

The Client Sets the Price, Role or Plan: Never Trust Values From the Browser

AleannLab team2 min read

Every value in a request can be edited by the person sending it. If your server takes price, role, plan, isAdmin, userId or credits from the request body and uses it, a user can set it to anything. The server must look these values up or compute them itself, and accept only an explicit list of fields the user is allowed to change.

On this page
  1. Variant 1: the price comes from the cart
  2. Variant 2: the update accepts every field
  3. The Supabase version
  4. Find it
  5. The rule

The browser's network tab has an "Edit and resend" button. That is the whole attack. AI-generated code is vulnerable to it in two ways.

Variant 1: the price comes from the cart

TypeScript
// app/api/checkout/route.ts
export async function POST(req: Request) {
  const { productName, price } = await req.json();

  const session = await stripe.checkout.sessions.create({
    mode: 'payment',
    line_items: [{
      price_data: {
        currency: 'usd',
        product_data: { name: productName },
        unit_amount: price,          // from the browser
      },
      quantity: 1,
    }],
    success_url: `${origin}/success`,
    cancel_url: `${origin}/cart`,
  });
  return Response.json({ url: session.url });
}

The frontend sends price: 4900. Someone resends it with price: 1. Stripe charges one cent, your webhook sees a successful payment, the order is fulfilled.

The client should send what is being bought. The server decides what it costs:

TypeScript
const { productId, quantity } = CheckoutInput.parse(await req.json());

const product = await db.product.findUnique({ where: { id: productId } });
if (!product || !product.active) {
  return Response.json({ error: 'Unknown product' }, { status: 400 });
}

const session = await stripe.checkout.sessions.create({
  mode: 'payment',
  line_items: [{ price: product.stripePriceId, quantity }],
  // ...
});

Using a Stripe Price ID stored on your side means the amount never passes through the browser at all. Apply the same rule to discounts (send the coupon code, compute the discount on the server), shipping, tax, and quantities (validate quantity as a positive integer; negative quantities have produced negative totals before).

And when the payment succeeds, grant what was paid for based on the verified webhook, not on the user landing on /success?plan=pro.

Variant 2: the update accepts every field

TypeScript
// app/api/profile/route.ts
export async function PATCH(req: Request) {
  const session = await auth();
  const body = await req.json();

  await db.user.update({
    where: { id: session.user.id },
    data: body,                      // everything the client sent
  });
  return Response.json({ ok: true });
}

The form sends { "name": "Ann" }. Nothing stops this:

bash
curl -X PATCH https://yourapp.com/api/profile \
  -H "Cookie: $SESSION" -H 'Content-Type: application/json' \
  -d '{"role":"admin","plan":"enterprise","credits":999999,"emailVerified":true}'

This is called mass assignment. Spread syntax (data: { ...body }) is the same bug. So is a signup endpoint that passes the whole body to create, which lets a new user register as an admin.

The fix is an allowlist. Parse the body against a schema that names the editable fields, and pass only the parsed result:

TypeScript
const ProfileUpdate = z.object({
  name: z.string().min(1).max(80).optional(),
  avatarUrl: z.string().url().optional(),
}).strict();

const data = ProfileUpdate.parse(await req.json());
await db.user.update({ where: { id: session.user.id }, data });

.strict() rejects unknown keys, so the attempt above returns an error. Fields like role, plan and credits change only in code paths you wrote for that purpose: an admin action, a webhook handler.

The Supabase version

When the browser writes to the database directly, there is no handler to add an allowlist to. If users may update their own row in profiles, and profiles has a role or plan column, this works from the console:

JavaScript
await supabase.from('profiles').update({ role: 'admin' }).eq('id', myId);

RLS policies decide which rows, not which columns. Two ways to fix it:

Column-level grants:

SQL
revoke update on public.profiles from authenticated;
grant update (display_name, avatar_url) on public.profiles to authenticated;

Or move the sensitive columns to a table users cannot write to (user_roles, subscriptions), updated only by server code with the secret key.

Find it

Search for request bodies flowing straight into writes or payments:

bash
grep -rnE "data: *(body|\{ *\.\.\.)|unit_amount|amount:" app src --include=*.ts --include=*.tsx

Then test from the browser: resend a profile update with an extra role field and reload the page. Resend a checkout request with a smaller amount and look at the Stripe test dashboard.

The rule

Treat the request as a description of what the user wants. Identity comes from the session. Prices come from your database. Permissions come from your records. The client never tells the server who it is or what something costs.

Prices are check 7 in our security checklist for vibe-coded apps. The related bug where users change IDs instead of fields is covered in our post on IDOR. If you want your payment and profile flows tested this way and fixed, see our app rescue and security audit.

All articles →

Gotowy, aby omówić swój projekt z nami?

idea
*
*
*
Interesuje mnie...
Tworzenie stron internetowych
Tworzenie aplikacji
Projektowanie UX/UI
DevOps
Sztuczna inteligencja i uczenie maszynowe
Testowanie QA
Tworzenie aplikacji mobilnych
Konsultacja
Outsourcing
Budżet projektu (USD)
≤ $5K
$10K-$20K
$30K-$40K
$50K-$60K
>$100K
Prześlij plik