sql-injection (E8103)#
SQL passed to execute() is built with string formatting.
What it does#
Reports cr.execute() and cr.executemany() calls whose query is built with
%, +, .format() or an f-string from values that are not constants. The
query variable is followed back to its assignments in the same function.
Allowed in the formatting: constants, self._attribute and self._method()
(such as self._table), and psycopg2.sql objects. Calls with a second
argument and files named test_* are skipped.
Why is this bad?#
A value that reaches the query from a user or another record can change the query itself: read other tables, bypass access rules or delete data. See the OCA guidelines.
Example#
self.env.cr.execute("SELECT id FROM res_partner WHERE name = '%s'" % name)
Use instead:
self.env.cr.execute("SELECT id FROM res_partner WHERE name = %s", (name,))
Ruff’s S608 is a generic, heuristic variant of this check.