Bug #16118 [Opn]: MSSQL escape doesn't take into account trailing backslashes

From: Date: Sun, 27 Sep 2009 19:51:03 +0000
Subject: Bug #16118 [Opn]: MSSQL escape doesn't take into account trailing backslashes
References: 1  Groups: php.pear.bugs 
Request: Send a blank email to pear-bugs+get-1746@lists.php.net to get a copy of this message
Edit report at http://pear.php.net/bugs/bug.php?id=16118&edit=1 ID: 16118 Updated by: urkle@outoforder.cc Reported By: eddie at omegaware dot com Summary: MSSQL escape doesn't take into account trailing backslashes Status: Open Type: Bug Package: MDB2 Operating System: CentOS 5 Package Version: 2.4.1 PHP Version: 5.2.9 Roadmap Versions: New Comment: It actually affects 2005 and up as well.. Most likely they just didn't update the KB article reflecting that. But the documentation states that's how things are escaped as well. The testing I was doing was all done on a 2005 server with a database IN 2005 mode (compatibility level 90) I currently do not have a 2008 server installed, but I would assume it's the same in 2008. Yup here is the manual entry for 2008. http://msdn.microsoft.com/en-us/library/dd207007.aspx which that actually states is a functionality of the sqlcmd or osql.. Meaning the backslash issue could really have to do with dblib.. so it would affect Sybase as well. Yup seems so.. From looking through the freetds source code it looks like a protocol of the dblib with the mssql php extension uses. Previous Comments: ------------------------------------------------------------------------ [2009-09-27 11:13:50] quipo What SQL Server version are you using? From the KB page you linked, this problem is only affecting * Microsoft SQL Server 6.5 Standard Edition * Microsoft SQL Server 7.0 Standard Edition * Microsoft SQL Server 2000 Standard Edition Can you confirm this? ------------------------------------------------------------------------ [2009-05-23 20:57:27] urkle hehe.. yeah sure.. Basically SQL server does "line folding" when it sees a backslash at the end of a line. Like what BASH does. So basically the solution is to detect the backslash - newline sequence and add an extra backslash in there (as one will be eaten by the line folding) The patch I attached uses a REGEX to check for all line ending cases (\r\n, \r, and \n) you can use a str_replace for speed if you ignore the \r case, which is very unlikely to be used, and not even sure if SQL SERver even accepts that. ------------------------------------------------------------------------ [2009-05-23 20:56:00] urkle The following patch has been added/updated: Patch Name: MDB2-escape.patch Revision: 1243108560 URL: http://pear.php.net/bugs/patch-display.php?bug=16118&patch=MDB2-escape.patch&revision=1243108560&display=1 ------------------------------------------------------------------------ [2009-05-23 19:28:44] doconnor I'm dizzy reading that. Want to write some code and attach some patches which make it clearer? ------------------------------------------------------------------------ [2009-04-20 22:21:29] urkle Ok, I was wrong... You do need the second set of CR/LR. as it still "strips" it.. it's not a true escape where SQL Server doesn't strip it. :( So basically something along the lines of this will cover most cases. $sql_escaped = str_replace( array("'","\\\r\n","\\\n"), array("''","\\\\\r\n\r\n","\\\\\n\n"), $sql); note this doesn't handle the case of a backslash followed by a \r.. Though I'm not sure of SQL server would even accept that as a new line. Also note that I kept the type of newline consistent in the replace, otherwise the size of the text would change, which is how I ran into this in the first place as it broke a PHP serialized blob. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://pear.php.net/bugs/bug.php?id=16118 -- Edit this bug report at http://pear.php.net/bugs/bug.php?id=16118&edit=1

« previous php.pear.bugs (#1746) next »