<?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>Chris Cook .me &#187; prevent xss</title>
	<atom:link href="http://chriscook.me/tag/prevent-xss/feed/" rel="self" type="application/rss+xml" />
	<link>http://chriscook.me</link>
	<description>Homepage</description>
	<lastBuildDate>Sat, 31 Jul 2010 22:52:47 +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>PHP: Preventing typical XSS attacks</title>
		<link>http://chriscook.me/web-development/php-preventing-typical-xss-attacks/</link>
		<comments>http://chriscook.me/web-development/php-preventing-typical-xss-attacks/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 17:36:44 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[chris cook]]></category>
		<category><![CDATA[cross-site scripting]]></category>
		<category><![CDATA[htmlentities]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[prevent xss]]></category>
		<category><![CDATA[transform_HTML]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xss]]></category>
		<category><![CDATA[xss attacks]]></category>

		<guid isPermaLink="false">http://chriscook.me/?p=71</guid>
		<description><![CDATA[XSS attacks plague beginner programmers and are a significant vulnerability for commercial web hosts &#38; website operators.  XSS means &#8220;cross-site scripting&#8220;.  These exploits work on the client side. Often, hackers put some type of JavaScript in content that users submit that allow them to steal the data from a cookie.  XSS attacks are pretty difficult [...]]]></description>
			<content:encoded><![CDATA[<p>XSS attacks plague beginner programmers and are a significant vulnerability for commercial web hosts &amp; website operators.  XSS means &#8220;<strong>cross-site scripting</strong>&#8220;.  These exploits work on the client side. Often, hackers put some type of JavaScript in content that users submit that allow them to steal the data from a cookie.  XSS attacks are pretty difficult to prevent.  Hackers have been successful with XSS attacks on most, if not all, of the biggest sites on the net.</p>
<p>To help prevent XSS attacks, it&#8217;s best to restrict and filter the data that you get from a user through your site.  Have you ever wondered why popular bulletin boards, such as vB or phpBB, use custom tag formats like [url] or [b]?  They&#8217;re trying to prevent attacks.</p>
<p>This tutorial is a very basic example of a way to help prevent XSS attacks.  There are other methods &#8212; and more comprehensive methods out there.</p>
<p><strong>okHTML function:</strong><br />
Let&#8217;s start with a simple function that converts any HTML code (or character) into literals.</p>
<pre class="php">
<span class="phpComment">// ChrisCook<span class="phpOperator">.</span>me
</span><span class="phpFunctionKeyword">function</span> ok_HTML<span class="phpOperator">(</span>$string, $length <span class="phpOperator">=</span> null<span class="phpOperator">)</span>
<span class="phpOperator">{</span>
<span class="phpComment">// get rid of the extra space
</span>$string <span class="phpOperator">=</span> <span class="phpFunction">trim</span><span class="phpOperator">(</span>$string<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpComment">// avoid unicode codec issues
</span>$string <span class="phpOperator">=</span> <span class="phpFunction">utf8_decode</span><span class="phpOperator">(</span>$string<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpComment">// convert HTML characters
</span>$string <span class="phpOperator">=</span> <span class="phpFunction">htmlentities</span><span class="phpOperator">(</span>$string, <span class="phpConstant">ENT_NOQUOTES</span><span class="phpOperator">)</span><span class="phpText">;</span>
$string <span class="phpOperator">=</span> <span class="phpFunction">str_replace</span><span class="phpOperator">(</span><span class="phpString">"#"</span>, <span class="phpString">"#"</span>, $string<span class="phpOperator">)</span><span class="phpText">;</span>
$string <span class="phpOperator">=</span> <span class="phpFunction">str_replace</span><span class="phpOperator">(</span><span class="phpString">"%"</span>, <span class="phpString">"%"</span>, $string<span class="phpOperator">)</span><span class="phpText">;</span>
$length <span class="phpOperator">=</span> <span class="phpFunction">intval</span><span class="phpOperator">(</span>$length<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpKeyword">
if<span class="phpOperator">(</span></span>$length <span class="phpOperator">&gt;</span> <span class="phpNumber">0</span><span class="phpOperator">)</span> <span class="phpOperator">{</span>
$string <span class="phpOperator">=</span> <span class="phpFunction">substr</span><span class="phpOperator">(</span>$string, <span class="phpNumber">0</span>, $length<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
<span class="phpKeyword">
return </span>$string;
<span class="phpOperator">}</span>
</pre>
<p><strong>The Explanation:</strong><br />
One  of the  most important components of that function is the htmlentities() funcion call that converts <strong>&amp;</strong>, <strong>&lt;</strong>, and <strong>&gt;</strong> into <strong>&amp;amp;</strong>, <strong>&amp;lt;</strong>, and <strong>&amp;gt;</strong>. This helps resolve the simple hacks.  We&#8217;re not done yet, though.  All XSS attacks aren&#8217;t basic.  Hackers know programmers have implemented these attacks to they tend to encode their hacks and malicious scripts in UTF-8 or hexadecimal instead of using the normal ASCII text.</p>
<p>To help prevent this, transform_HTML() takes the additional step of converting # and % signs into the correct entities.</p>
<p>In my readings on preventing XSS attacks, many experts recommend that you limit the  string length in case some goober tries to overload your string with a very, very long input in hopes that they&#8217;ll crash the server or your database. You can edit the <strong>$length</strong> parameter to help control this.</p>
<p>That&#8217;s it for today,<br />
<em>Chris</em></p>
<p><strong>Disclaimer: </strong>As always, I want to add my handy-dandy disclaimer.  Please understand that this tutorial is intended to demonstrate a specific function.  Please review the code and add appropriate security measures before using it in a production environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/php-preventing-typical-xss-attacks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
