Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict file access to authorized php users

I've inherited an application with a glaring security hole.

It has session-based security, but file uploads (which are user specific) are not secured in any way and they are stored in the public file tree.

Filenames do not follow any convention as such, making them hard to guess, but the data is sensitive and thus I need to implement a security measure to prevent unauthorized file access.

Moving the location of the files is not really an option, so I'm looking at a htaccess solution to forward requests to a php handler script.

Does anyone have experience in implementing this type of thing or any good alternative solutions? Specific examples of .htaccess syntax greatly appreciated, as I'm struggling in this area.

like image 851
BrynJ Avatar asked Apr 10 '09 18:04

BrynJ


2 Answers

Don't really understand why moving them isn't an option, since pushing requests for them to a handler means it no longer matters where they're stored. But you're the man on the scene.

.htaccess looks like:

RewriteEngine on
RewriteRule path/to/where/these/files/live/(.*) /handlerscript.php/$1

Then you pick up the remaining file path and name from $_SERVER['PATH_INFO'].

like image 123
chaos Avatar answered Sep 19 '22 12:09

chaos


Well, you could make apache parse .jpg file's for a certain folder adding the following to your .htaccess

AddHandler php5-cgi .jpg

then you could set a file of php to parse the request the way chaos was recomending you and doing a certain validation, then just return jpeg headers along with the correct picture u'd like to display

here's an example

<?php
if($validUser)
    {
    header("Cache-control: No-cache");
    header("Pragma: No-cache");
    header("Content-Type: image/jpeg");
    //correct picture address
    $img = imagecreatefromjpeg("2326_b_lil.jpg");
    imagejpeg($img);
    }
    else
    {
    //code for error image
    }
?>

please let me know if you want a more extensive example

like image 35
perrohunter Avatar answered Sep 19 '22 12:09

perrohunter