I'm thinking about adding an additional layer of SQL injection mitigation in my usage of psycopg2
If cursor.execute
is used properly and the parameters are passed like below this is not a concern.
cursor.execute("SELECT * FROM employees WHERE name = %s", (name,))
But I'm slightly worried about myself or another developer absentmindedly doing something like
cursor.execute("SELECT * FROM employees WHERE name = %s" % (name))
which would of course open the door to someone submitting their name as ''; drop table employees;
Since the second example contains a very common way of formatting a string in Python it's not outside the realm of possibility that someone could make such an error.
I'm wondering if a method similar to execute
exists that would restrict the number of queries or statements being executed in a call to execute
to 1. If not, does psycopg2
expose a SQL tokenizing method. If so I could write my own method that overrides execute and performs such a check before passing the query to the real execute. Or any other suggestions appreciated.