SQLAlchemy 2.0
Last reviewed Sep 12, 2026
Prompt Details
Prompt Composition
2prompts
- Python 3155 lines · 3.8K tokens
- SQLAlchemy 2.066 lines · 2.3K tokens
Total221 lines · 6.1K tokens
CWE Coverage
- 15
- 20
- 23
- 36
- 59
- 78
- 89
- 95
- 117
- 201
- 208
- 209
- 214
- 250
- 295
- 319
- 328
- 338
- 347
- 362
- 367
- 379
- 409
- 416
- 426
- 427
- 470
- 488
- 502
- 532
- 611
- 639
- 674
- 732
- 770
- 772
- 776
- 807
- 829
- 862
- 915
- 916
- 918
- 1104
- 1333
- 1395
Prompt Preview
Apply the Python 3 baseline first. These rules cover SQLAlchemy 2.0 query construction, textual SQL, sessions, transactions, tenant isolation, connection security, logging, and migrations.
Values and textual SQL
- Build queries with SQLAlchemy expressions and bind every attacker-controlled value as a parameter.
- When
text()is necessary, keep the SQL string constant and declare values withbindparams()or the execution parameter mapping; placeholders cannot bind identifiers or SQL syntax. - Never interpolate or concatenate untrusted values into SQL, including through f-strings,
%,format, or template engines. - Treat
literal_column,text,from_statement, raw driver SQL, execution options, andbindparam(..., literal_execute=True)as reviewed escape hatches. Never pass untrusted values toliteral_executeparameters, which render inline at execution time, and never execute SQL compiled withliteral_binds=Truefrom untrusted values. - Do not use escaping or manual quoting as a substitute for bound parameters.
- Escape LIKE metacharacters in untrusted search terms by calling
contains(),startswith(),endswith(), or their case-insensitiveicontains(),istartswith(), andiendswith()forms withautoescape=True; do not buildlike(f"%{term}%")orilike(...)patterns from raw input. Never uselike()orilike()for identity lookups such as email or username matching; comparefunc.lower(column) == value.lower()or use a case-insensitive collation instead.
Identifiers and dynamic queries
- Map client-selected sort, filter, column,