<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.1.3" -->
<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/"
	>

<channel>
	<title>Youruby.net</title>
	<link>http://www.youruby.net</link>
	<description>creative works of james rubenstein</description>
	<pubDate>Wed, 19 Sep 2007 18:13:46 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.3</generator>
	<language>en</language>
			<item>
		<title>How not to get hacked: uploaded files in PHP</title>
		<link>http://www.youruby.net/2007/09/how-not-to-get-hacked-uploaded-files-in-php/</link>
		<comments>http://www.youruby.net/2007/09/how-not-to-get-hacked-uploaded-files-in-php/#comments</comments>
		<pubDate>Wed, 19 Sep 2007 16:59:26 +0000</pubDate>
		<dc:creator>jim</dc:creator>
		
		<category><![CDATA[php]]></category>

		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://youruby.net/2007/09/how-not-to-get-hacked-uploaded-files-in-php/</guid>
		<description><![CDATA[In response to Mike Seth&#8217;s post regarding uploading files with php, I have some additional information to add.  I agree, wholeheartedly, the security holes which are created on account of negligance by the developer and not necessarily PHP itself.  Though, there are steps that can, and should, be taken that Mike has not [...]]]></description>
			<content:encoded><![CDATA[<p>In response to <a href="http://blog.mikeseth.com/index.php?/archives/2-How-not-to-get-hacked-uploaded-files-in-PHP.html" title="How not to get hacked: uploaded files in PHP" target="_blank">Mike Seth&#8217;s post regarding uploading files with php</a>, I have some additional information to add.  I agree, wholeheartedly, the security holes which are created on account of negligance by the developer and not necessarily PHP itself.  Though, there are steps that can, and should, be taken that Mike has not outlined - in order for a developer to protect themselves.</p>
<p>Mike got it right when he said MIME types can be faked by the user, and they shouldn&#8217;t be trusted.  Though, he did leave out the obvious fact (at least, I hope it&#8217;s obvious) that file extensions can also be faked.  File extension checking is probably the <em>loosest</em> form of file upload authentication that one could possibly do.  Mainly because of the fact that it&#8217;s so easy to do!</p>
<p>It would be a good idea to authenticate the file extension against the MIME type of the file which was uploaded.  This doesn&#8217;t add a whole lot of security, as it can be faked like Mike said, but its a start.  Additionaly, the <a href="http://us3.php.net/manual/en/ref.fileinfo.php" title="fileinfo documentation via php.net">fileinfo extension</a> (via: <a href="http://pecl.php.net/package/Fileinfo">http://pecl.php.net/package/Fileinfo</a>) (as mike briefly touches) is a pretty handy extension for <em>guessing</em> a file&#8217;s types based on it&#8217;s conents.  This <a href="http://pecl.php.net">pecl</a> extension will be included by default in php soon, as the <a href="http://us3.php.net/manual/en/ref.mime-magic.php">mimetype extension</a> was deprecated quite a while ago.</p>
<p>It is also worth mentioning, that the &#8220;hostile archive&#8221; vulnerability Mike mentions is not a vulnerability of PHP or the filesystem on the server.  This is a vulnerability of the compression library.  Meaning, don&#8217;t go unzipping/decompressing archives from un-trusted sources (the user) and this vulnerability will have no affect on your server.</p>
<p>One thing I&#8217;m surprised that was not mentioned, at all, is the simple fact that any file which is uploaded, should never be stored in a web-accesible folder.   Any content from the user is, by default, not to be trusted - that&#8217;s web dev 101.  Keeping untrusted data, in a readily accessible location is, as I like to say, &#8220;asking for it.&#8221;  If someone manages to get by all (if any) of your upload authentication, and they can figure out how to get to the file they uploaded, you&#8217;re in a big boat of trouble.  It&#8217;s very easy to write a script that looks something like:</p>
<p><code><br />
function toast ($path = '.')<br />
{<br />
$file_list = glob($path . '/*');<br />
foreach ($file_list as $file)<br />
{<br />
if (is_dir($file))<br />
{<br />
toast($file);<br />
}<br />
else<br />
{<br />
@unlink($file);<br />
}<br />
}<br />
}<br />
toast();<br />
</code></p>
<p>If a web-attacker manages to get a file with the above code saved in a web-accessible directory on your server, say goodbye to all your user-uploaded content.</p>
<p>The threat of the above attack can be safe-guarded against by checking file extensions, and mimes - but the most bullet proof way to make sure that script doesn&#8217;t get executed, is to put it in an un-executable location (<em>outside</em> your webroot!).</p>
<p>Now, some may say that saving outside the webroot is going to cause more problems.  In the case of an image gallery, you&#8217;d have to run each request though a &#8216;requesting&#8217; script, which loads the file and spits it out to the user.  At first glance, that may seem like the only solution, but there are far better.</p>
<p>You can solve the problem presented just above, as well as improve overall site performance, and security with one simple step.  Caching.  For any image gallery known to man, it is almost guaranteed that your images need to be resized in order to be properly presented on the web.  Instead of resizing, and displaying for each request (which obviously is going to increase your load time and server load), all resizing should happen  exactly once, on upload.  It is acceptable to think (at least, in my opinion) that uploading images to a gallery, would be the most time consuming process of a whole gallery experience.  So resizing an image to 3 or 4 different sizes on upload, which will obviously increase the processing time of the upload, is acceptable, as long as the rest of the gallery visit doesn&#8217;t take as long.</p>
<p>Why, you my ask, is this going to increase my security?  The answer is simple.  Since you are now saving your files in a non-web-accessible directory, your original photos are safe from public access, so no worries on arbitrarily requesting/executing a file.  And now, since you are processing each of the images&#8217; resizes - you&#8217;re using either GD or Image Magick.  Neither of these libraries are going to resize anything that isn&#8217;t an image they support.  There is your file-type security, right there.  If your image resource, created by GD or Image Magick, is invalid - you have an invalid file, and you can toss it out the window.</p>
<p>If you have anything more to add, let me know in the comments!</p>
<p><small>Thanks go to Wess Cope and Alex Songe for reviewing this post, before publication.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youruby.net/2007/09/how-not-to-get-hacked-uploaded-files-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Do you ruby?</title>
		<link>http://www.youruby.net/2007/04/do-you-ruby/</link>
		<comments>http://www.youruby.net/2007/04/do-you-ruby/#comments</comments>
		<pubDate>Thu, 12 Apr 2007 23:52:06 +0000</pubDate>
		<dc:creator>jim</dc:creator>
		
		<category><![CDATA[Self Indulgence]]></category>

		<guid isPermaLink="false">http://youruby.net/2007/04/do-you-ruby/</guid>
		<description><![CDATA[No, not ruby as in the language, or rails as in the framework.  Ruby - as in the first part of my last name.
Welcome to youruby.net.  This is my personal / professional site.  A sort of Curriculum Vitae, if you will.  I will be posting entries related to web development, in [...]]]></description>
			<content:encoded><![CDATA[<p>No, not ruby as in the language, or rails as in the framework.  Ruby - as in the first part of my last name.</p>
<p>Welcome to youruby.net.  This is my personal / professional site.  A sort of Curriculum Vitae, if you will.  I will be posting entries related to web development, in some manner, or my professional experiences. <a href="http://www.youruby.net/2007/04/do-you-ruby/#more-12" class="more-link">(more&#8230;)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youruby.net/2007/04/do-you-ruby/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
