when i am reading :http://docs.python.org/2/library/sqlite3.html
# Never do this -- insecure!
symbol = 'RHAT'
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
why it is insecure? No reason in detail in the web to explain.
when i am reading :http://docs.python.org/2/library/sqlite3.html
# Never do this -- insecure!
symbol = 'RHAT'
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
why it is insecure? No reason in detail in the web to explain.
Think about what happens if symbol
contains, say, ' OR '' = '
-- an attacker could insert completely arbitrary criteria for the query. This is of particular concern if you have information about other customers' accounts in the same table, or are doing an update.
Also, http://xkcd.com/327/
This creates an SQL injection vulnerability if symbol
contains user-supplied or untrusted data.
To be safe, always do it like this (as mentionned in the same page you're reading) :
symbol = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', symbol)
So now even if symbol
contains malicious data, it'll be properly escaped and won't do any harm, the worse that can happen is the query returning no results.