All,
I have written a fairly complex site for my company but have always doubted how I handle security. For out current load it is ok, but with our public product launch coming this March, I fear the current architecture I have implemented will implode with a heavy load. I’ll explain the implementation in depth to clarify what’s going on, and would appreciate any comments on how to improve performance or the architecture overall.
We run a typical LAMP server, Apache version 1.3x and PHP 4.x. To create a single entry point to our website I have the following rewrite conditions enabled in apache:
RewriteRule \.html security.php
RewriteRule \.htm security.php
RewriteRule \.php security.php
RewriteRule \.exe security.php
RewriteRule \.zip security.php
RewriteRule \.pdf security.php
RewriteRule /$ security.php
RewriteRule screenshots/ security.php
RewriteRule doc/ security.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* security.php
This essentially sends every text and certain bin file (exe’s etc...) requests through our security.php script which analyzes the request URL and determines if the user had rights to access what they want or not.
For successful text based files the script simply includes the file:
include($_SERVER[‘DOCUMENT_ROOT’].$requested_url);
since security.php is in the root of our web server all lookups succeed.
For successful bin files, however, the process is a little different, we have to dump the file ourselves to the client, we have chosen 8k chunks to use but the performance is still noticeably slower than just linking to a bin file straight form apache. Our bin code is as follows:
if ($bin)
{
$file = fopen($_SERVER['DOCUMENT_ROOT'].$requested_url, "r");
while($pump = fread($file, 8192))
{
ob_implicit_flush();
print($pump);
}
exit();
}
Is there a more efficient way to do this? This is how all jpg, gif, exe, zip, bin files in general are delivered to the client browser, so it is important that this code be fairly fast.
I created this more out of ignorance than experience, so I would appreciate some experts to review this practice and possibly offer suggestions on how to better handle security. One thing that is VERY nice about this model is that all security related code is in one place, and is very flexible, however I fear that with every request being run through security.php that it will crawl to a halt with a heavy load.
Any and all comments are welcome, and thank you for reading this lengthy post 🙂
-sf_dave