r/PHPhelp • u/[deleted] • Mar 07 '24
Security: .htaccess vs putting PHP-files outside public?
This is a subject that I have tried to get a bottom of.
I do understand the reasoning why people say that you could or should put crucial PHP-files outside the public directory to prevent any direct access to them, but how does this concept compare (security wise) against preventing the same thing with .htaccess rules?
My real life problem with this:
Whenever I program with PHP, I have gotten used to put one public index.php under the public directory that includes the actual PHP-system files from outside the public directory. I do this because that's what I was taught to do.
But it is a bit tricky to have files in two places for one system in such wide range. Especially if you develope these things using Github the directory structure for a project can end up rather ugly.
Could I reach the same level of access security with .htaccess rules? Just prevent access to any other sub-directory and files and only give access to index.php for example?
EDIT: I will update here the possible conclusions based on great comments:
Using .htaccess is not a reliable solution due to the possibility of webserver misconfiguration and some webservers might not even have .htaccess support.
Not relying on .htaccess will help increase portability/compatibility.
1
u/martinbean Mar 07 '24
You’ve answered your own question. You don’t rely on .htaccess to protect files because it can be misconfigured, the web host could edit it and misconfigure it, and then your PHP scripts are available to the world. Whereas if your scripts are outside the web accessible route, then it doesn’t matter if your .htaccess file is borked or not; the files aren’t accessible. Period*.
So, stick to putting the bulk of your project “below” the web accessible root directory, and only having your entry point (index.php) in your public directory, that then calls into other files not in your public directory.
* Obviously they are if someone finds an alternative way into your server and gets shell access.
2
4
u/eurosat7 Mar 07 '24
Some servers have .htaccess disabled.
Or are not using apache2 at all.
Beside having less attack survice you get more compatibility.
2
u/MateusAzevedo Mar 07 '24
Could I reach the same level of access security with .htaccess rules?
Definitely not. Besides what people already mentioned, I'd like add another one:
The more things your need to configure (and make sure correctly set), the easier it'll be to miss something. It's more error prone.
When using a dedicated public dir, there's only one thing you need: set the document root correctly.
1
u/RNG_REDDITOR Mar 07 '24
I have to work on a project created from vanilla php Have you considered using a framework?
0
u/Ravavyr Mar 07 '24
Everyone here going "some servers don't run apache!"...
Like...yea, so? They are currently using .htaccess for a PHP site so i'm pretty sure we can assume they ARE using a server that runs apache...heh.
It's not like the OP is asking about a Node application and wondering if they need to use .htaccess
.htaccess is perfectly fine, but ideally you place your .htaccess rules within the apache2 virtualhost configs. That way the server loads them once [when apache starts], and doesn't have to parse the htaccess on every request [just a performance tip]
Like someone else said, you just need to make sure your root path is defined and user correctly within your code. along with htaccess denying all access and allowing access only where your public files are.
0
u/Tatsuyiaki Mar 07 '24
Take a look at how laravel and symfony work, you will get the answer and you will learn how do frameworks works, it's a win-win situation :)
0
Mar 07 '24
I do understand the reasoning why people say that you could or should put crucial PHP-files outside the public directory to prevent any direct access to them
I also don't know.
how does this concept compare (security wise) against preventing the same thing with .htaccess rules?
It's the same. "The public directory" is defined in the webserver config. The .htaccess file is part of the webserver config. So in both cases you have the webserver config preventing the public from accessing these files directly. If you mess up the webserver config you're screwed. If you don't you're fine.
Thou shalst configure your webserver wisely.
Could I reach the same level of access security with .htaccess rules?
I think many of those advices are based on the idea that you don't know what you're doing and that your webserver config (and your application) is broken anyway. They want to mitigate around this and introduce some extra layers of "security" by preventing you to not shoot yourself in your knee too badly.
I have all my files in the "public" directory. This is no problem, they can be accessed directly. These are classes, they don't do anything. I know that. Nothing can happen. As https://www.php-fig.org/psr/psr-1/ states
Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.
...and there are few files that cause side-effects. All the files that declare can be an any directory.
Much more important (I think) is, that the directories with your application files should not be writeable to your webserver and that the directories that are writeable to the webserver are configured in your webserver in a way that php is not executed. This is a very simple and very effectful security measure.
1
u/MateusAzevedo Mar 07 '24
I think many of those advices are based on the idea that you don't know what you're doing and that your webserver config (and your application) is broken anyway
Those advices as based on the fact that the least you need to worry about something, the less error prone it will be. Even if you know what you're doing, the more you need to configure and check/validate, easier it'll be to miss something.
0
u/johnfc2020 Mar 07 '24
I’d start with changing the permissions on your home directory and public_html to 711 which allows the browser to find known files but prohibits browsing your server. If the web server is misconfigured to allow browsing your home directory, those non public folders won’t suddenly become public.
-2
u/boborider Mar 07 '24
It's not a problem placing files inside subdirectory under public. htaccess rules are enough to protect that.
If you place files outside the public, your web programs cannot access those files. (It depends on webservers). It's not always reliable.
1
u/MateusAzevedo Mar 07 '24
If you place files outside the public, your web programs cannot access those files.
I'm pretty sure that's not true.
0
Mar 07 '24
[deleted]
1
Mar 07 '24
I hear you! I will smack myself everytime I am thinking "how to be lazy with..." ;) No shortcuts on security, that is for sure. That is exactly why I am here asking questions. Thank you.
13
u/dabenu Mar 07 '24
The whole reason why you don't want to put private files in a web directory, is so they don't get compromised even if your webserver is misconfigured
So yes you can use other means to secure your files, but it increases your attack surface.
Personally I don't think having a
publicdir in your repository is that big of a trade-off. It makes it much more clear what's supposed to be publicly accessable, and is basically a convention. So I would argue the opposite is true in that regard.