SQLAlchemy 2.0

Last reviewed Sep 12, 2026

Export as PDF

Prompt Details

Prompt Composition

2prompts
  1. Python 3155 lines · 3.8K tokens
  2. 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 with bindparams() 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, and bindparam(..., literal_execute=True) as reviewed escape hatches. Never pass untrusted values to literal_execute parameters, which render inline at execution time, and never execute SQL compiled with literal_binds=True from 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-insensitive icontains(), istartswith(), and iendswith() forms with autoescape=True; do not build like(f"%{term}%") or ilike(...) patterns from raw input. Never use like() or ilike() for identity lookups such as email or username matching; compare func.lower(column) == value.lower() or use a case-insensitive collation instead.

Identifiers and dynamic queries

  • Map client-selected sort, filter, column,