Laravel — 保護你的 .env檔案

hosomikai
2 min readFeb 20, 2020

--

因為目前主要都是用Laravel在開發專案,所以每都會接觸到.env檔。
這個檔案可以把他理解成environment 環境設定檔。我們會在這個文件上儲存很多重要的資訊,包括應用程序的機敏資訊,第三方應用的金鑰、資料庫帳號密碼等主機資訊。

但如果在部署的時候沒有設定正確的話,會讓.env檔可以被google的爬蟲機器人瀏覽,這樣一來你的.env檔就可以在google上被搜尋到,非常恐怖!

你可以透過在google搜尋:

DB_PASSWORD filetype:env

搜尋到非常多的.env檔案:

要保護.env檔案主要有幾個方式

  1. 將根目錄設定在public下(Laravel):
    專案統一進入點,底下的權限劃分就會很清楚簡單了,
    可以被公開訪問的檔案只有public底下的檔案。
  2. 使用apache, 在 .htaccess,加入
<Files ".*">
Require all deny
</Files>

3. 使用nginx

location ~* \.env$ {
deny all;
}

將目錄底下隱藏檔案全都拒絕訪問

除了主機上的配置外,也有可能是.env檔案具有錯誤的權限。
將.env文件chmod設定成400或440,讓公共訪客無法訪問

Originally published at https://hosomikai317.blogspot.com.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response