Skip to content

feat: env directory path #9631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 4.7
Choose a base branch
from

Conversation

totoprayogo1916
Copy link
Contributor

@totoprayogo1916 totoprayogo1916 commented Jul 10, 2025

Description
This PR restores the ability to set a custom .env file path, which was no longer possible after #8604 due to hardcoded handling in Boot.php.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@michalsn michalsn added enhancement PRs that improve existing functionalities 4.7 labels Jul 10, 2025
@michalsn
Copy link
Member

Branch 4.7 was out of sync. Please rebase when you get a chance.

@totoprayogo1916
Copy link
Contributor Author

Branch 4.7 was out of sync. Please rebase when you get a chance.

rebased

@michalsn
Copy link
Member

While changing the .env file path was possible, it was more complicated.

I hope this option won't be abused to store the file in insecure places. Because, before we could be quite sure, it would be changed only by people who know what they're doing. However, from the other side, this is not the first voice from the community to make things easier in this area, so I suppose this is acceptable.

Let's see what others will say. We will need a changelog entry for this. I didn't see a dedicated page for Config\Paths in the user guide, so I think that should be sufficient.

@paulbalandan
Copy link
Member

I think we should add a guard where the .env can be placed. I'm thinking it should not be outside ROOTPATH.

@totoprayogo1916
Copy link
Contributor Author

I think we should add a guard where the .env can be placed. I'm thinking it should not be outside ROOTPATH.

How to do it?

@michalsn
Copy link
Member

I'm thinking it should not be outside ROOTPATH.

What do you mean by that exactly?

From what I understand it, ROOTPATH is always one level above the app/ folder. In fact, when serving the application from a subfolder (like http://app.test/myapp), placing the .env file above ROOTPATH seems valid, since it keeps the file outside the web-accessible area.

I would be concerned about the .env file being placed below ROOTPATH, such as inside the myapp/ or myapp/public/ directory.

@paulbalandan
Copy link
Member

Isn't the web-accessible path the PUBLICPATH?

In the following file structure:

|-- system/
|-- subA/
|   |--- app/
|   |--- public/
|   |__ .env
|
|__ subB/
    |-- app/
    |-- public/
    |__ .env

isn't the paths accessible subA/public/ and subB/public/? Then, I guess I misspoke earlier. Should the .env be placed anywhere other than PUBLICPATH?

@neznaika0
Copy link
Contributor

I don't see any documentation updates or changelogs. If it looks unsafe, it's worth adding information about the consequences.
Also, not all dependencies have been updated. Example:

private function writeNewEnvironmentToEnvFile(string $newEnv): bool

@michalsn
Copy link
Member

@paulbalandan What I meant is that in an ideal setup, the domain points directly to the public/ folder - so everything above that (like .env) stays outside the web root and is safe.

But when serving the app from a subfolder (e.g. http://app.test/subA), the entire subA/ folder becomes web-accessible. In that case, if there's an .htaccess misconfiguration (or none at all), files like .env located in ROOTPATH or anywhere below it can be exposed.

From that perspective, everything from ROOTPATH downward is potentially unsafe unless explicitly protected.

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proper documentation should be enough to warn users about the risks. I was looking at the user guide, and I believe this would be a good place for this: https://github.com/codeigniter4/CodeIgniter4/blob/develop/user_guide_src/source/general/managing_apps.rst

We can add a section dedicated to .env at the bottom.

Changing the Location of the .env File
======================================

If necessary, you can change the location of the ``.env`` file by adjusting the ``$envDirectory``
property in ``app/Config/Paths.php``.

By default, the framework loads environment settings from a ``.env`` file located one level above
the ``app/`` directory (in the ``ROOTPATH``). This is a safe location when your domain is correctly
pointed to the ``public/`` directory, as recommended.

In practice, however, some applications are served from a subdirectory (e.g., ``http://example.com/myapp``)
rather than from the main domain. In such cases, placing the ``.env`` file within the ``ROOTPATH`` may expose
sensitive configuration if ``.htaccess`` or other protections are misconfigured.

To avoid this risk in such setups, it is recommended to ensure the ``.env`` file is located outside any
web-accessible directories.

.. warning::

   If you change the location of the ``.env`` file, make absolutely sure it is not publicly accessible.
   Exposure of this file could lead to compromised credentials and access to critical services, such as your
   database, mail server, or third-party APIs.

Co-authored-by: Michal Sniatala <[email protected]>
@michalsn
Copy link
Member

@totoprayogo1916 totoprayogo1916 marked this pull request as draft July 15, 2025 07:11
@totoprayogo1916 totoprayogo1916 marked this pull request as ready for review July 15, 2025 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.7 enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants