views:

352

answers:

3

How can you allow a php script to write to a file with high security restrictions, such as only allowing a single user to write to it?

The difficulty seems to be that a php script is running as a low-permissions user (maybe apache, or www, or nobody?), and even if I chown apache the_writable_file, the directory it's in might not be writable for the low-level user. In general, what's the usual way that php can work with local files in a secure way?

A: 

Sure, chgrp apache the_writable_file and chmod g+w the_writable_file. After that, only your secure user and the apache user will be able to write to the file. Since the apache user is typically forbidden from logging in, you only have to worry about web users writing to your secure file using through the http daemon.

hoyhoy
+2  A: 

Unfortunately, in shared hosts that use mod_php, there is no way to restrict access to secure files to your web app and login user.

The solution is to run your web app as your login user. When you do that, UNIX file permissions can correctly lock everyone else out. There are several ways to implement that, including SuExec, suPHP, or running PHP with FastCGI with mod_fcgid or mod_proxy_fcgid. FastCGI is my favorite way.

Another solution is to use a dedicated host or virtual private server.

John Douthat
A: 

all the containing folders need to have execute permissions

ex. if the file's in: /foo/bar/the_writable_file

the directories "foo" and "bar" both need to have executable permission to access the_writable_file even if they don't have read/write permission

Jiaaro