18

I have some code in Python that sets a char(80) value in an sqlite DB.

The string is obtained directly from the user through a text input field and sent back to the server with a POST method in a JSON structure.

On the server side I currently pass the string to a method calling the SQL UPDATE operation.

It works, but I'm aware it is not safe at all.

I expect that the client side is unsafe anyway, so any protection is to be put on the server side. What can I do to secure the UPDATE operation agains SQL injection ?

A function that would "quote" the text so that it can't confuse the SQL parser is what I'm looking for. I expect such function exist but couldn't find it.

Edit: Here is my current code setting the char field name label:

def setLabel( self, userId, refId, label ):
    self._db.cursor().execute( """
        UPDATE items SET label = ? WHERE userId IS ? AND refId IS ?""", ( label, userId, refId) )
    self._db.commit()
3
  • Doesn't python support parameterized queries? Commented Jun 8, 2012 at 14:07
  • @Jeremy. Yes...but it's really whether the python library for any particular database supports it (which AFAIK they all do). Commented Jun 8, 2012 at 14:14
  • Yes. So consider my question as asking it what I do is enough to ensure protection against sql injection. Commented Jun 8, 2012 at 14:15

3 Answers 3

13

From the documentation:

con.execute("insert into person(firstname) values (?)", ("Joe",))

This escapes "Joe", so what you want is

con.execute("insert into person(firstname) values (?)", (firstname_from_client,))
Sign up to request clarification or add additional context in comments.

4 Comments

I updated my question with the code I currently use. I did it as you suggest. Does it mean my code is safe against SQL injection ?
I forgot that "escaping" means securing against sql injection. I gave you the answer because of the example. Thank you very much for sharing your knowledge and helping me.
if the bad actor gives input such as joe") what effect would that have?
In this answer, it would insert the name joe") into the column firstname
4

The DB-API's .execute() supports parameter substitution which will take care of escaping for you, its mentioned near the top of the docs; http://docs.python.org/library/sqlite3.html above Never do this -- insecure.

1 Comment

Thank you for the reference. I remember now reading this a long time ago and applying since. I wasn't aware it was protecting me against SQL injection. That's great. I did it the right way. Sorry for not giving you the answer. Martijn has less points than you and he gave an example which would be handy for people like me searching for an answer to this question.
1

Noooo... USE BIND VARIABLES! That's what they're there for. See this

Another name for the technique is parameterized sql (I think "bind variables" may be the name used with Oracle specifically).

4 Comments

What are BIND variables ? Any URL to suggest ? Add an answer.
Sure...which RDBMS are you using?
I'm using sqlite for now. It's not yet the production version of my web app.
Ironically, I can't even find the information about how to use Variable Binding in python. Do you have a link to info on this? I've always done this in other languages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.