<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Promet CakePHP Source &#187; Security</title>
	<atom:link href="http://cakephp.prometsupport.com/category/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://cakephp.prometsupport.com</link>
	<description></description>
	<lastBuildDate>Mon, 23 Feb 2009 08:03:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Handling SQL injection in CakePHP</title>
		<link>http://cakephp.prometsupport.com/2008/handling-sql-injection-in-cakephp/</link>
		<comments>http://cakephp.prometsupport.com/2008/handling-sql-injection-in-cakephp/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 05:13:20 +0000</pubDate>
		<dc:creator>rachel</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[Tips & Tutorials]]></category>
		<category><![CDATA[Model]]></category>
		<category><![CDATA[sql injection]]></category>

		<guid isPermaLink="false">http://cakephp.prometsupport.com/2008/02/11/array-conditions-is-model-functions/</guid>
		<description><![CDATA[One benefit of using a framework is that some of the most common problems we will encounter when building web applications from scratch is already taken cared of, that is if properly used. Today, we will discuss one of the most common problem we have experienced specially for beginners – SQL Injection. Let&#8217;s begin first [...]]]></description>
			<content:encoded><![CDATA[<p>One benefit of using a framework is that some of the most common problems we will encounter when building web applications from scratch is already taken cared of, that is if properly used. Today, we will discuss one of the most common problem we have experienced specially for beginners – SQL Injection.</p>
<p>Let&#8217;s begin first by defining SQL Injection.  According to <a href="http://en.wikipedia.org/wiki/SQL_injection">wikipedia</a>, <strong>SQL injection</strong> is a technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for <a href="http://en.wikipedia.org/wiki/String_literal">string literal</a> <a href="http://en.wikipedia.org/wiki/Escape_sequences">escape characters</a> embedded in <a href="http://en.wikipedia.org/wiki/SQL">SQL</a> statements or user input is not <a href="http://en.wikipedia.org/wiki/Strong_type">strongly typed</a> and thereby unexpectedly executed. It is in fact an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another.</p>
<p>To protect against SQL injection, user input must not directly be embedded in SQL statements. Instead, user input must be escaped, or parameterized statements must be used.</p>
<p>CakePHP handles this in its data abstraction layer by using mysql_real_escape_string(). To use effectively, programmers must comply to its rules. Why did I say comply? Some of model functions are very loose with what kind of parameter type to accept. This setting has caused some of the newcommers to not see the benefit of passing conditions by array. Let&#8217;s take for an example the findAll() function.</p>
<p><code lang="php"><br />
Model::findAll ( $conditions = null, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null )<br />
</code></p>
<dl>
<dt><strong>Parameters:</strong></dt>
</dl>
<table border="0" cellpadding="0" cellspacing="0" width="80%">
<tr>
<td valign="top" width="4">&nbsp;</td>
<td valign="top" width="52"><em>mixed</em></td>
<td>$conditions SQL conditions as a string or as an array(&#8216;field&#8217;=&gt;&#8217;value&#8217;,&#8230;)</td>
</tr>
<tr>
<td valign="top" width="4">&nbsp;</td>
<td valign="top" width="52"><em>mixed</em></td>
<td>$fields Either a single string of a field name, or an array of 			field names</td>
</tr>
</table>
<p>The <i>$conditions</i> accepts both string and array for its passed values. Logically, only the data passed as an array will be escaped. Using arrays allows CakePHP to generate the most efficient query possible, ensure proper SQL syntax, and properly escape each individual part of the query.</p>
<p>For complex find queries, we may opt for the string condition and do all the dirty work. As of this writing, CakePHP is now offering a solution to build complex conditions using arrays.</p>
<p>Cake can parse out any valid SQL comparison operator, including match expressions using LIKE, BETWEEN, or REGEX, as long as we leave a space between the operator and the expression or value.<br />
<code lang="php">array("Post.title" => "<> This is a post")</code></p>
<p> Below is the adaptation of the IN (&#8230;)-style matches</p>
<p><code lang="php">array("Post.title" => array("First post", "Second post", "Third post"))</code></p>
<p> By default, the framework joins multiple conditions with boolean AND. To accept other boolean conditions, we could do the ffg:</p>
<p><code lang='php'>array<br />
("or" =><br />
    array<br />
    (<br />
        "Post.title" => array("First post", "Second post", "Third post"),<br />
        "Post.created" => "> " . date('Y-m-d', strtotime("-2 weeks"))<br />
    )<br />
)</code></p>
<p> Or like this:</p>
<p><code lang="php">array<br />
("Author.name" => "Bob", "or" => array<br />
    (<br />
        "Post.title" => "LIKE %magic%",<br />
        "Post.created" => "> " . date('Y-m-d', strtotime("-2 weeks")<br />
    )<br />
)</code></p>
<p>More explanation of this can be found at the <a href="http://manual.cakephp.org/chapter/models">Manual: Complex Find Conditions (using arrays)</a>.</p>
<p><i>Model::save()</i> is more strict so I did not see any problem following its rules.  If you have questions for a certain model function syntax, the <a href="http://api.cakephp.org">API</a> can be a great resource.</p>
<p>With the benefit of these Model functions, we must still have to adapt to the framework&#8217;s environment which maybe of a disadvantage at first. It will just be your choice which manners will best suit you. At the end of the day, what will matter is that the code we produce is <strong>readable</strong>,<strong> maintanable</strong> and most of all <strong>secure</strong>.</p>
<p>Source:</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/SQL_injection">SQL_injection</a></li>
<li><a href="http://dev.mysql.com/tech-resources/articles/guide-to-php-security-ch3.pdf">Guid to PHP Security (PDF)</a></li>
<li><a href="http://shiflett.org/blog/2006/jan/addslashes-versus-mysql-real-escape-string">mysql_real_escape_string vs addslashes</a></li>
<li><a href="http://www.sitepoint.com/forums/showthread.php?t=337881">mysql_real_escape_string vs addslashes Discussion</a></li>
<li><a href="http://manual.cakephp.org/chapter/models">CakePHP Manual: Models</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cakephp.prometsupport.com/2008/handling-sql-injection-in-cakephp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
