Security
postgrelize builds SQL so that data from your users cannot change the meaning of a statement. This page lists every input that reaches SQL, how it is protected, and the few APIs that take raw SQL by design.
To report a vulnerability, see SECURITY.md.
How inputs are protected
| Mechanism | Meaning |
|---|---|
| Bound | Sent to PostgreSQL as a $n parameter, never as SQL text |
| Identifier | Quoted as an identifier (" doubled): the value can only name a table, column, alias, channel, … |
| Allowlist | Checked against a fixed set or a strict pattern; anything else throws before SQL is built |
| Literal | Escaped string literal ('', E'…' with doubled backslashes; NUL bytes rejected). In DDL, COPY TO and JSON_TABLE paths, where PostgreSQL does not accept parameters, and in replacements of sequelize.query and of the finders (ADR-036, ADR-037) |
| Raw by design | SQL you write yourself. Not escaped. Never build it from user input |
| Input | Protection |
|---|---|
where values, Op.* values, in lists, JSON values, limit / offset, increment amounts, values of create / update / upsert / bulkCreate / insert / merge | Bound |
Op.similarTo / Op.jsonPathExists / trigram Op.* values, text of toTsQuery / plainToTsQuery / phraseToTsQuery / websearchToTsQuery / toTsVector, tsHeadline options string, jsonbPath path (as jsonpath) and vars (one jsonb parameter), trigramSimilarity / trigramDistance arguments | Bound |
sequelize.query bind ($1, $name) | Bound |
replacements (:name, ?) of sequelize.query and of the finders (findAll, findOne, findByPk, count, findAndCountAll, Model.reload, ...) for placeholders inside literal(): strings, numbers, booleans, dates, JSON, arrays for IN (:ids); written into the SQL text like v6, so one call can run several statements | Literal |
String arguments of fn() (fn('COUNT', '*'), fn('concat_ws', ' ', col('a'))) and the value of Op.is (null, booleans, literal(), or any value, as in v6; ADR-038) | Literal |
JSON paths in where keys ('meta.a.b', nested objects) | Bound |
Attribute and column names, where keys, order / group targets, aliases, as, table and schema names, CTE names, index and constraint names, LISTEN / NOTIFY channels, lock.of | Identifier |
HSTORE keys and values (serialized to one bound hstore text parameter), values of LTREE, MONEY, XML, BIT, TSQUERY, MACADDR8, TIMESTAMP columns, values of POINT, LINE, LSEG, BOX, PATH, POLYGON, CIRCLE columns (coordinates validated as finite numbers, then one bound text parameter), values of MULTIRANGE columns (bounds formatted into one bound multirange text parameter), values of GEOMETRY / GEOGRAPHY columns (GeoJSON type allowlisted, coordinates finite numbers, SRID digits only, sent as one bound hex EWKB parameter), values of COMPOSITE columns (fields quoted into one bound record text parameter, cast to the quoted type name in where) | Bound |
Values and conditions of insertStatement / updateStatement / deleteStatement | Bound |
VALUES cells, function arguments, TABLESAMPLE percent and seed, JSON_TABLE document | Bound |
CTE search / cycle columns and added column names, from aliases, source column aliases, set-returning function names, JSON_TABLE column names | Identifier |
TABLESAMPLE method, CTE search mode, JSON_TABLE column types | Allowlist |
JSON_TABLE jsonpath strings (PostgreSQL accepts only constants there) | Literal |
Operands of set operations (union(), intersect(), except()), source subqueries, the withTies fetch count | Bound |
NOTIFY payloads, advisory-lock keys, transaction settings, statementTimeout (validated integer), bulkCreate unnest column arrays | Bound |
order directions, Op operators, where(col, comparator, value) comparators, fn() names, Model.aggregate function names, cast() and 'attr::type' types, lock strengths, isolation levels, transaction deferrable (booleans only), onDelete / onUpdate, index using / operator classes, Deferrable, DataTypes parameters (STRING(n), DECIMAL(p, s), BIT(n), VARBIT(n), TIMESTAMP(p), …), timezone, COPY TO format options | Allowlist |
Text search configuration names (plain or schema-qualified identifier, bound as regconfig), setWeight labels (A–D), jsonbPath kinds, tsRank weights (four finite numbers) and normalization (integer 0–63) | Allowlist |
DDL defaults (an HSTORE or geometric default object is serialized, then escaped), table and column comments, ENUM values, partial-index and CHECK conditions, COPY TO filters | Literal |
COMPOSITE type and field names (quoted by createSql() and in column DDL; field types come from data types), extension, sequence names and schemas, ownedBy table and column | Identifier |
Extension version | Literal |
Sequence dataType | Allowlist |
Sequence start, increment, bounds, cache, restartWith, setval values | Allowlist (whole numbers only) |
nextval / setval sequence names | Bound (as regclass text) |
| Policy names, policy roles | Identifier |
Policy command and kind | Allowlist |
Policy using / withCheck where-object values | Literal |
| View and materialized-view names, view column lists | Identifier |
View checkOption, materialized-view using access method | Allowlist |
| Function names (strings), parameter and variable names, languages | Allowlist (identifier words) |
Function names given as { name, schema }, trigger names, trigger tables | Identifier |
| Function parameter and return types | Allowlist (strict type-name pattern) |
Function parameter directions, function characteristic keywords, trigger timing, events and clauses (FOR EACH ROW, DEFERRABLE, REFERENCING) | Allowlist |
| Trigger function arguments, variable defaults | Literal |
Table, schema and constraint names given to showIndex, showConstraint, getForeignKeysForTables and assertTableHasColumn | Bound |
Database names given to createDatabase / dropDatabase, and everything given to quoteIdentifier / quoteIdentifiers | Identifier |
Constraint names given to Deferrable.SET_DEFERRED / SET_IMMEDIATE (SET CONSTRAINTS); the deferrable option only accepts values made by Deferrable | Identifier |
createDatabase options encoding, collate, ctype and template | Literal |
COPY FROM data | Escaped in the COPY text format |
literal(), the SQL text of sequelize.query, string where / order | Raw by design |
Index names, index columns, collate collations | Identifier |
Index and exclude using method, field operator class | Allowlist |
Constraint names, validateConstraint names, exclude element columns | Identifier |
exclude element with operators | Allowlist (operator characters only) |
| Partition names, partition key columns and collations | Identifier |
Partition strategy | Allowlist |
Partition bound values (in, from, to), modulus, remainder | Literal (modulus and remainder: validated integer) |
Index field nulls and order | Allowlist |
Index, exclude and partition key expression, function body, trigger WHEN (...) clause, view and materialized-view query, policy using / withCheck strings | Raw by design (trusted SQL, not escaped) |
The statementCache option sends no user data to PostgreSQL as a name: a prepared statement is named pgz_ plus a hash of the SQL text, and its max must be a positive integer. Values stay bound parameters.
Safe patterns
User values in model queries are always bound:
// Values from users are always sent as bound parameters.
const byTitle = await Post.findAll({ where: { title: input['title'] } });
const searched = await Post.count({ where: { title: { [Op.iLike]: `%${input['search']}%` } } });In raw SQL, pass user values through replacements (escaped into the SQL text, as in Sequelize v6) or bind (bound parameters):
// Raw SQL: put user values in replacements or bind, never in the SQL text.
const rows = (await sequelize.query('SELECT title FROM "Posts" WHERE title = :title', {
type: QueryTypes.SELECT,
replacements: { title: input['title'] },
})) as { title: string }[];Column names chosen by users (sorting, filtering by a field) are quoted, so they cannot inject SQL. But they can still name any column of the table. Map them through your own allowlist:
// Column names from users: map them through your own allowlist.
const sortable = { rank: 'rank', title: 'title' } as const;
const column = sortable[input['sort'] as keyof typeof sortable] ?? 'rank';
const sorted = await Post.findAll({ order: [[column, 'DESC']] });literal() inserts its text unchanged. Use it only for constant SQL written by you:
// literal() is inserted as is: only constant SQL written by you.
const nullsLast = await Post.findAll({ order: [literal('"rank" DESC NULLS LAST')] });Not injection, but worth knowing
- Filtering on any column. Passing a request object straight in as
wherelets callers filter on any column, including ones you never show. Buildwherefrom the fields you allow. - Mass assignment.
Model.update(values)andcreate(values)write every attribute present invalues. Passfieldsor pick the allowed attributes first. - LIKE patterns.
Op.like,Op.startsWith,Op.substringandOp.endsWithkeep LIKE semantics, as in Sequelize v6:%,_and\in the value act as wildcards. Escape them when users should match literally.
How this is verified
- Every input above has tests on a real PostgreSQL in
tests/integration/security/. The tests use a payload corpus: quote breakouts, backslash andE''tricks, dollar quoting,U&escapes, comments, stacked statements, very long strings. They also run property-based tests with random strings, checking that values read back byte-for-byte and identifiers name exactly what was given. A canary table proves that no injected statement ran. - CodeQL (
security-extendedqueries) scans every pull request. - Every input that reaches SQL must have a protection and tests before it is merged (ADR-020).