1

I have a PHP script, which MAY try to access files in the directories that have sensitive data. I want to restrict such operations.

I want to allow this script to access files/directories only in the directory I'm specifying. Is it possible to do? Thanks.

1
  • That's a yes/no question ;) And the answer is yes. Commented Jan 6, 2010 at 14:43

4 Answers 4

2

Disclaimer: If you don't control the way PHP is set up on the server, this answer is useless.

We really need more information about your PHP setup, such as which web server it's using and which SAPI mode. You can find out which SAPI module is being used by running echophp_sapi_name()

However, if I were to assume it's Apache 2.2 with mod_php:

The Apache module version of PHP runs as the webserver's user and group. There's a second Apache module named suPHP that wraps the PHP CGI version to make it run as the script's owner and group (and avoids having to manually set up Apache's suEXEC for PHP).

Other than that, changing the permissions on the filesystem so that the user doesn't have read access to the sensitive directory and its files should be good enough.

Sign up to request clarification or add additional context in comments.

Comments

2

Yes, simply write the script so that it only accesses files in your safe directories. Do not (ever) access a file from an unchecked path in user input. Either check the path against a list of acceptable paths and/or filenames, or include the path in the code that will access the file so that only file names are passed to the access code.

These functions will help: pathinfo() , dirname() , basename()

Comments

1

Something like this should work:

function isBelowAllowedPath($file, $allowedPath)
{
    return ( strpos( realpath($file), $allowedPath) === 0 );
}

isBelowAllowedPath('/etc/passwd', '/var/www/pub/'); // false
isBelowAllowedPath('/var/www/pub/index.htm', '/var/www/pub/'); // true

or if you want to make sure $file is there as well

function isBelowAllowedPath($file, $allowedPath)
{
    return file_exists( $allowedPath . basename(realpath($file)) );
}

isBelowAllowedPath('/../../../../etc/passwd', '/var/www/pub/'); // false
isBelowAllowedPath('index.htm', '/var/www/pub/'); // true

or if you want $file to be in a specific list of $allowedPaths (not below a path)

function isInAllowedPath($file, array $allowedPath)
{
    $fileDir = realpath(dirname($file));
    return (in_array($fileDir, $allowedPath));
}

$allowed = array('/var/www/pub/', 'somewhere/else');
isInAllowedPath('/var/www/pub/foo/index.htm', $allowed); // false
isInAllowedPath('/var/www/pub/index.htm', $allowed); // true

Comments

0

You would have to configure the directories' rights so that the user PHP runs as can't access them.

To do that on a Linux/Unix system, you would need the right to change the owner of the directories (chown) and to change its rights (chmod) and probably command line access. It really depends on how your server is configured, who owns the files, and who PHP runs as.

To find out the user ID of PHP's user on a Linux system use posix_getuid().

I can think of no other way to protect files from PHP-side access except removing access to certain functions via disable_functions. That, though, may (and probably will) break a lot of legitimate actions as well.

3 Comments

I think, he wants to control this from PHP, e.g. checking if a file is withing an allowed path. At least that's how I understand the question.
I think Gordon's answer was the most apposite. Certainly file permissions should be set up appropriately, however this is going to affect all php scripts on the server - in which case running chroot and/or setting open_base_dir would be advisable. C.
To me, the question sounds like the OP has a encoded 3rd party script he wants to run, and wants to make sure it doesn't do anything untoward on the server. In that case, setting the right permission would probably be the best thing to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.