Last active
January 30, 2023 19:36
-
-
Save romainnorberg/2bc3d86237ee81b79639a33ff73d5b06 to your computer and use it in GitHub Desktop.
Revisions
-
romainnorberg revised this gist
Jul 14, 2020 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ Blog post: https://blog.romainnorberg.be/post/python-avoid-sql-injection-when-using-mysqlcursor-execute -
romainnorberg revised this gist
May 17, 2020 . No changes.There are no files selected for viewing
-
romainnorberg created this gist
May 17, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ import mysql.connector as mdb con = mdb.connect( host='127.0.0.1', port=3306, user='root', passwd='rootroot', db='db', charset='utf8' ) cur = con.cursor(dictionary=True) # Injection work using cursor.execute(sql) id = '1 OR 1=1' cur.execute("SELECT * FROM user WHERE id=%s" % (id,)) result = cur.fetchall() print("%d results !" % len(result)) # X results ! # Injection doesn't work using cursor.execute(sql, (val1, val2)) id = '1 OR 1=1' cur.execute("SELECT * FROM user WHERE id=%s", (id,)) result = cur.fetchall() print("%d result ✋" % len(result)) # 1 result ✋