Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 28th, 2007, 3:50 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Emulating Behaviour Of "mysql_real_escape_string"

I need to stop mysql injections in Python's mysql library. However, mysql_real_escape_string does not exist for Python's wrapper for MySQL. Therefore, I need to emulate the behaviour of PHP's mysql_real_escape_string.

Does this cover all the bases?

def mysql_real_escape_string(string):
    return string\
        .replace('\\', '\\\\')\
        .replace('"', '\\"')\
        .replace("'", "\\'")

mysql_real_escape_string("  ' or username is not null or username='  ")
mysql_real_escape_string('  " or username is not null or username="  ')
mysql_real_escape_string("  \\' or username is not null or username= \\' ")

  \' or username is not null or username=\'  
  \" or username is not null or username=\"  
  \\\' or username is not null or username= \\\'
Sane is online now   Reply With Quote
Old Nov 28th, 2007, 4:06 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Re: Emulating Behaviour Of "mysql_real_escape_string"

You can stop SQL injections by using parameters, e.g:

cursor.execute("SELECT * FROM users WHERE login = '%s' AND passwd = '%s'", (login, passwd))
Arevos is offline   Reply With Quote
Old Nov 28th, 2007, 4:54 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Emulating Behaviour Of "mysql_real_escape_string"

I'll have to rework everything, but I guess it's better to use something tried and tested anyways. Thanks.
Sane is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
/usr/bin/ld: Undefined symbols jubitzu C++ 9 Mar 8th, 2007 11:00 AM
RadioButtonList Strange Behaviour Iftikhar ASP.NET 0 Oct 27th, 2006 6:40 AM
Is this undefined behaviour? InfoGeek C++ 26 Jun 27th, 2006 9:17 AM
Assigning an array of lists deanosrs C 42 Apr 13th, 2006 1:35 PM
Could some please explain classes to me... TCStyle C++ 10 Feb 20th, 2006 3:51 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC