<?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>ElbertF &#187; escaping</title>
	<atom:link href="http://elbertf.com/tag/escaping/feed/" rel="self" type="application/rss+xml" />
	<link>http://elbertf.com</link>
	<description>Insights and Updates from a Tech Geek</description>
	<lastBuildDate>Thu, 02 Feb 2012 01:22:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Escaping and sanitizing user input in PHP</title>
		<link>http://elbertf.com/2010/07/escaping-and-sanitizing-user-input-in-php/</link>
		<comments>http://elbertf.com/2010/07/escaping-and-sanitizing-user-input-in-php/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 09:12:09 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[escaping]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sanitizing]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://elbertf.com/?p=606</guid>
		<description><![CDATA[I recently answered a question on Quora, a questions and answers website that I frequent. The poster asked &#8220;what are best practices for escaping or sanitizing user input in PHP?&#8221; People seemed to appreciate the answer I wrote so I&#8217;ll post it here and elaborate on it a bit&#160;more. Why is it important to sanitize [...]]]></description>
			<content:encoded><![CDATA[<p>I recently answered a question on <a href="http://quora.com">Quora</a>, a questions and answers website that I frequent. The poster asked &#8220;<a href="http://www.quora.com/what-are-best-practices-for-escaping-or-sanitizing-user-input-in-PHP">what are best practices for escaping or sanitizing user input in PHP?</a>&#8221; People seemed to appreciate the answer I wrote so I&#8217;ll post it here and elaborate on it a bit&nbsp;more.</p>
<h4>Why is it important to sanitize user&nbsp;input?</h4>
<p>If you&#8217;re not careful with user input your website might be open to <a href="http://en.wikipedia.org/wiki/Code_injection">code injection</a>, <a href="http://en.wikipedia.org/wiki/Directory_traversal">directory traversal</a> or similar attacks. Information supplied by users can never be assumed&nbsp;safe.</p>
<p>Examples of user input are submitted forms (e.g. comments), URL parameters (?q=example) and server-side scripts pulling in third-party data, such as an RSS feed&nbsp;importer.</p>
<p><span id="more-606"></span></p>
<h4>HTML and&nbsp;JavaScript</h4>
<p>To make strings safe for HTML (without breaking <a href="http://www.unicode.org/standard/WhatIsUnicode.html">Unicode text</a>) use&nbsp;<a href="http://php.net/manual/en/function.htmlentities.php"><code>htmlentities()</code></a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$safe</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unsafe</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will encode all special HTML characters. This method is better than black-listing specific elements such as &lt;script&gt; or just opening tags (&lt;). Do not use <a href="http://php.net/manual/en/function.strip-tags.php"><code>strip_tags()</code></a>, <a href="http://php.net/manual/en/function.str-replace.php"><code>str_replace()</code></a> or <a href="http://www.php.net/manual/en/book.pcre.php">regular expressions</a> to filter HTML and JavaScript, it is easy to miss obscure vulnerabilities and leave them&nbsp;exploitable.</p>
<p>When automatically parsing a URL to display a clickable link, check if URL starts with a protocol like &#8220;http://&#8221; (regex: <code>/[a-z]:\/\//i</code>) and make sure <code>javascript:</code> links never work. Also be careful with quotes and closing angle brackets as they can break&nbsp;HTML.</p>
<p>These examples demonstrate how unfiltered links can be&nbsp;dangerous:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'javascript:alert(\'XSS\');'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://example.com&quot; onclick=&quot;alert(\'XSS\');'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'example.com&quot;&gt;&lt;script&gt;alert(\'XSS\');&lt;/script&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$url</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;Click here&lt;/a&gt;</pre></td></tr></table></div>

<h4>URLs</h4>
<p>To pass values as a parameter to a URL, use&nbsp;<a href="http://php.net/manual/en/function.rawurlencode.php"><code>rawurlencode()</code></a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://example.com/?k='</span> <span style="color: #339933;">.</span> <span style="color: #990000;">rawurlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This function is nearly identical to <a href="http://php.net/manual/en/function.urlencode.php"><code>urlencode()</code></a> but follows the <a href="http://www.faqs.org/rfcs/rfc1738.html">RFC 1738</a>&nbsp;specification.</p>
<h4>MySQL database&nbsp;queries</h4>
<p>When storing strings in a MySQL database, use&nbsp;<a href="http://php.net/manual/en/function.mysql-real-escape-string.php"><code>mysql_real_escape_string()</code></a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$safe</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unsafe</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Often <a href="http://php.net/manual/en/function.addslashes.php"><code>addslashes()</code></a> is used instead but this is not enough to prevent <a href="http://www.owasp.org/index.php/SQL_Injection">SQL injection attacks</a>. If you want to learn why I recommend reading <a href="http://shiflett.org/blog/2006/jan/addslashes-versus-mysql-real-escape-string">this blog post</a> by Chris Shiflett and the third chapter of the <a href="http://dev.mysql.com/tech-resources/articles/guide-to-php-security-ch3.pdf">guide to PHP security</a> by Ilia Alshanetsky (PDF,&nbsp;130KB).</p>
<p>This function requires a MySQL&nbsp;connection.</p>
<h4>Magic&nbsp;Quotes</h4>
<p>If your web app suffers from unwanted backslashes appearing in content this is probably due to double escaping (e.g. &#8220;today&#92;&#8217;s weather&#8221;). This is likely caused by PHP&#8217;s now deprecated <a href="http://php.net/manual/en/security.magicquotes.php">Magic&nbsp;Quotes</a>.</p>
<p>Magic Quotes is a feature automatically escapes user input, intended to help beginners write more secure code. Because it&#8217;s not always on or needed this affects portability and requires excessive use of <a href="http://www.php.net/manual/en/function.stripslashes.php">stripslashes()</a> to&nbsp;undo.</p>
<p>To recursively undo the affects of Magic Quotes use this function at the beginning of your&nbsp;scripts:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> undo_magic_quotes<span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'undo_magic_quotes'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'get_magic_quotes_gpc'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$_GET</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'undo_magic_quotes'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$_POST</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'undo_magic_quotes'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$_COOKIE</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'undo_magic_quotes'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Directories</h4>
<p>And finally, be careful with including files using user&nbsp;input.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Don't do this</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// And especially not this</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This is dangerous because a user can request any file from the server and execute or view the code (e.g. &#8220;?file=../../config.php&#8221;, this is called a <a href="http://www.owasp.org/index.php/Category:Path_Traversal_Attack">path traversal attack</a>). One solution is to use <a href="http://php.net/manual/en/function.basename.php"><code>basename()</code></a> which strips off the path of a file&nbsp;name.</p>
<p>These were just a few basics, there is a lot more to web application security. <a href="http://www.owasp.org">OWASP</a> is a great resource if you want to learn&nbsp;more.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2010/07/escaping-and-sanitizing-user-input-in-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

