StoicBuilder

HandbookIII the CastraSQL injection

IIIthe Castra, Stay in control

SQL injection

1 min readUpdated 28 November

In plain words

SQL injection happens when your code glues user text into a database query, so carefully crafted text changes the query itself and reads or deletes data it shouldn't.

Where you see it

Harbor's search box drops whatever you type into a raw Postgres query. Typing ' OR '1'='1 turns "find my server" into "find every server". Supabase's client and ORMs like Prisma and Drizzle prevent this, until someone writes SQL as a string by hand.

app/api/servers/search/route.tsserver
// unsafe: your text becomes part of the SQLawait db.query(`select * from servers where name = '${q}'`); // safe: $1 is filled in separately, as dataawait db.query("select * from servers where name = $1", [q]);
Same query. The second sends your text as data, never as SQL.

What goes wrong

  1. Building queries with template strings

    Anything inside ${} lands straight in the SQL. Use placeholders like $1, or let the query builder fill values in.

  2. Reaching for the unsafe escape hatch

    Prisma's $queryRawUnsafe and Drizzle's sql.raw exist for rare cases. Agents use them to get a tricky query working, and the protection goes with them.

  3. Letting users pick column names

    Placeholders protect values, not column or table names. If users choose the sort column, check it against a fixed list.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Search the codebase for SQL built from strings or template literals, including $queryRawUnsafe, sql.raw and EXECUTE inside Postgres functions, and rewrite each one with parameters. List what you changed, and check any user-chosen sort or column names against a fixed list.

Quick check

Sign in to earn +10 XP for it

QuickBite loads orders with supabase.from("orders").select().eq("id", id). Is that open to SQL injection?

Got a prompt that touches SQL injection?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.