SQL Injection
SQL injection is building a query string with untrusted text so the engine runs attacker SQL. TypeScript's types will not stop this. Parameterized queries and tagged templates will.
Detailed Explanation
query("select * from users where id = '" + id + "'") is the bug. If id is 1 or 1=1 you have lost. Giving id the type string or even UserId does not help: a branded string is still concatenated. Erasure means the brand is gone before the driver sees it.
The type-system-adjacent defenses:
Parameterized queries. query("select * from users where id = $1", [id]) keeps data out of the SQL string. The TypeScript for this is a function whose second argument is unknown[] or a typed tuple of values, and whose first argument is a string you wrote. The type of id can be UserId; the safety is the API shape, not the brand.
Tagged templates. Prisma $queryRaw\select * from users where id = ${id}`and similarsql`...`helpers split strings and values. The tag function receives the raw parts and the interpolated values separately. The TypeScript type of the tag is(strings: TemplateStringsArray, ...values: unknown[]) => Query. Interpolating a value does not concatenate it into the string if the tag is implemented correctly. Interpolating a *fragment* you built from user input (sql.raw(userColumn)) is still injection. Types usually type sql.rawas takingstring` — a hole.
ORMs. prisma.user.findMany({ where: { id } }) does not concatenate id into SQL. The typed client is the safe default. Raw methods exist for the rest; treat them as a different API.
Column and table names cannot be parameterized in most engines. order by ${column} is the remaining hole. The TypeScript fix is a key union: function sort(column: keyof User) and a map from that key to a known SQL identifier in a Record<keyof User, string> you wrote. Do not pass a request string through as a column name, even if you typed the request as string.
Types that look safe: query<User>(sql). The generic is the result type. It does not parse sql. See typed query results.
For the general injection story and HTTP/query-builder notes, see the JavaScript SQL-injection page. In TypeScript, prefer the typed client, then tagged templates, then a keyof allow-list for identifiers. Never concatenate.
Code Examples
1. Parameterized — values stay out of the string.
// 1. Parameterized — values stay out of the string.
type QueryFn = (sql: string, params: unknown[]) => Promise<unknown[]>;
function findUser(query: QueryFn, id: string): Promise<unknown[]> {
return query("select id, email from users where id = $1", [id]);
}2. Identifier allow-list — keyof, not string concatenation.
// 2. Identifier allow-list — keyof, not string concatenation.
type User = { id: string; email: string; createdAt: Date };
const columns: Record<keyof User, string> = {
id: "id",
email: "email",
createdAt: "created_at",
};
function orderBy(column: keyof User): string {
return `select id, email from users order by ${columns[column]}`;
}3. The unsafe pattern — types cannot save it.
// 3. The unsafe pattern — types cannot save it.
function unsafe(id: string): string {
return "select * from users where id = '" + id + "'";
}Common Gotchas
A branded `UserId` concatenated into SQL is still injection
brands are erased. Use parameters.
`sql.raw(request.column)` is typed and dangerous
only pass identifiers from an allow-list you wrote.
`query<T>(sql)` types rows, not safety
see typed query results.