Support the ongoing development of Laravel.io →
API Laravel Security

We are facing a serious security issue with our backend infrastructure. We have API endpoints that allow command execution or access to the server's CLI.

Let me explain the scenario: Suppose our backend team creates an API for command execution on our server. This API could potentially expose the .env file of our project by running commands on the server. As we all know, the .env file contains crucial data that should only be accessible to IT admins and DevOps.

This issue poses a significant risk to our production environment, and any advice on how to secure it would be greatly appreciated. This is a serious vulnerability as it could expose sensitive server configuration details.

I found a possible solution: Disabling functions like exec, shell_exec, system, passthru, popen, and proc_open, which are used to gain CLI access in the LAMP stack server.

However, many of our APIs and services rely on the server's CLI. Disabling these functions in PHP could create significant issues for our server.

Please suggest How to secure the .env file access through the api ?

Thank you in advance for your help.

0

Hi @hritikpandey

There are number of ways which can help you to protect your .env file access through API.

I will list some of them.

1. Restrict Public Access

    Apache: Use .htaccess to deny access to the .env file. Add the following rule in your .htaccess file:


<Files .env>
    Order allow,deny
    Deny from all
</Files>

Nginx: Add a rule in your Nginx configuration to block access to the .env file.

location ~ /\.env {
    deny all;
}

2. File Permissions: Ensure that the .env file has the correct permissions. Only the web server or owner should be able to read this file.

You can use the below command to do the same:

chmod 640 .env
chown <user>:<group> .env

I hope this solves your issue.

Thanks!

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.