0

I just found out that the way I am using _mysql is causing a major SQL Injection problem.

My current code looks like:

db = _mysql.connect('', 'user', 'pass', 'db')
query = """SELECT * FROM stuff WHERE thing="{0}" """.format(user_input)
cur.query(query)

What am I doing wrong and how can I fix it so that it is safe?

I have tried using _mysql.escape_string() but that still returns an SQL syntax error.

2
  • 3
    Well... You need to educate yourself. This seems like acceptable source: <dev.mysql.com/tech-resources/articles/…>. Commented Mar 11, 2013 at 0:41
  • SQL Injection Vulnerability has nothing to do with syntax errors. Please clarify your exact question. Commented Mar 11, 2013 at 0:43

2 Answers 2

2

You can use MySQLdb on its own:

conn = MySQLdb.connect();
curs = conn.cursor();
curs.execute("SELECT * FROM stuff WHERE thing = %s", (user_input));

If you want to stick with _mysql, use db.escape_string(user_input).

Documentation: http://mysql-python.sourceforge.net/MySQLdb.html

Sign up to request clarification or add additional context in comments.

1 Comment

I decided to just use MongoDB instead.
2

A nice handy reference is available via the bobby tables website.

You may also find value in In this powerpoint reference which shows some examples of sql injection as well as possible ways to mitigate the issue.

Comments

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.