r/PHPhelp • • 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:

  1. Using .htaccess is not a reliable solution due to the possibility of webserver misconfiguration and some webservers might not even have .htaccess support.

  2. Not relying on .htaccess will help increase portability/compatibility.

12 Upvotes

22 comments sorted by

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 public dir 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.

1

u/[deleted] Mar 07 '24 edited Mar 07 '24

Oh yeah that is actually a good point. I did not even consider the "web server misconfiguration" possibility.

Currently I use this structure:
/public_html/my_php_system_name/index.php
and inside this index.php I usually define two constants:

  1. for my public data (the data_path that contains public data to be visible to any user and are served upon response (as urls, or on a static HTML, depending on the case).

  2. the path to my "system files" outside public directory. Or usually I just insert this path directly to require_once("../../php_system/main.php"); for example.

Can you see any major flaws doing things this way?

Of course any private data is served otherwise. But currently I mostly deal only with public data so I leave that element out of this post.

1

u/dabenu Mar 07 '24

Not sure, it's missing context. What's inside my_php_system_name? If it's just the index.php and other public files, that's fine.

3

u/[deleted] Mar 07 '24 edited Mar 07 '24

Yes currently under "my_php_system" folder there is only one file, index.php. That works as a router to the actual php-system files that are located outside the public folder.

It's basically just a public entry point.

EDIT: I'll explain further. My project structure usually is something like this:

/Root directory (per user/domain)
/my_php_system/In this location are all the actual php-system files
/public_html/my_php_entrypoint/In this location there is only one file, index.php. It will only define a few constant values and then require_once() the main file from the "../../my_php_system/" location.
/public_html/data/Any public data served can be located here for example and this path is defined for the system in the entry point's constant value.

1

u/MateusAzevedo Mar 07 '24

A see public_html there and I imagine/assume this is a shared host or some CPanel hosting, right? In that case, does it allow you to set a different document root for each domain? Something like /system_a/public/ and /system_b/public.

This would solve your complaint about "the directory structure for a project can end up rather ugly" and each project structure becomes "normal".

1

u/[deleted] Mar 07 '24

I just use that as a generic name for a public directory :) I want to learn to think of these as platform independent as possible.

1

u/[deleted] Mar 08 '24

I just came up with one more question: as far as I understand, you need to configure the web server to define which actually is a public folder and what is not?

If so, is there a risk that with misconfiguration (sounds like absolutely nut case though), a private folder (usually one step higher than public_folder is) becomes accessible to the end user?

6

u/maselkowski Mar 07 '24

Not only misconfiguration. There are other web servers too. For instance nginx ignores htaccess. If you application rely on htaccess, then you are stuck with apache only.

1

u/dabenu Mar 07 '24

One could say that putting nginx config in htaccess is a "misconfiguration" :p

But yes, good point.

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

u/[deleted] Mar 07 '24

Yes I am actually updating my original question with the answers I am getting here :)

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

u/[deleted] 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

u/[deleted] Mar 07 '24

[deleted]

1

u/[deleted] 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.