Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Using sqlite3 for python, one method secure, one not, why?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Biffenbob   Members   -  Reputation: 105

Like
0Likes
Like

Posted 08 April 2013 - 01:05 PM

# Never do this -- insecure!
symbol = 'RHAT'
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)

# Do this instead
t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
print c.fetchone()

 

Taken from http://docs.python.org/2/library/sqlite3.html

 

My understanding is a SQL injection is when instead of supplying data or a variable you substitute a SQL command.  My question is why is the second option better then the first.  What makes the second option better then the first?


Edited by Biffenbob, 08 April 2013 - 01:07 PM.


Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13937

Like
3Likes
Like

Posted 08 April 2013 - 02:02 PM

I guess the second version performs SQL escaping for you.

#3 Matias Goldberg   Crossbones+   -  Reputation: 3724

Like
4Likes
Like

Posted 08 April 2013 - 03:15 PM

The 2nd option already knows the input is a parameter and there's no doubt it can't be part of the command in the statement, so SQL injection is not possible.

 

In the 1st option, you would have to properly escape the string, and there's the risk that you're not escaping it properly.


Edited by Matias Goldberg, 08 April 2013 - 03:16 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS