python-3.xsqlalchemysql-injection

sqlalchemy protection against sql injections- using engine.execute() api


I tried to find a method for using sqlalchemy's engine.execute level solutions for sql injections, but I came across this is possible via ORM style.

How can we achieve it using engine-level api?


Solution

  • You can use bind parameters with sqlalchemy.sql.expression.text to avoid SQL injection. Also using the sql/core layer to build queries should also work of course, ie.

    session.execute(select(my_table).where(id=some_id))