首頁  >  文章  >  後端開發  >  如何使用fdopen實作對Python進程產生的檔案進行權限最小化配置

如何使用fdopen實作對Python進程產生的檔案進行權限最小化配置

王林
王林轉載
2023-04-28 22:22:051060瀏覽

需求背景

用python進行檔案的建立和讀寫操作時,我們很少關注所建立的檔案的權限配置。對於一些安全性較高的系統,如果我們建立的檔案權限其他使用者或同一使用者群組裡的其他使用者有可讀權限的話,有可能導致不必要的資訊洩漏的風險。因此,除了創造一個更安全和隱私的個人環境之外(如容器環境等),我們還可以對生成的文件的配置進行權限最小化處理。

常用方法及其缺陷分析

常用的python檔案建立和讀寫方法,是直接透過內建的open函數來建立一個檔案。這裡如果是使用with語法來建立的,結束語句後會自動關閉被開啟的物件。而如果是直接使用open函數來定義一個對象,則需要在任務結束時手動的執行close操作。以下示範內建函數open的用法及其檔案操作屬性,首先建立一個名為file-test.py的檔案:

# file-test.py
 
with open('test1.txt', 'w') as file:
    file.write('hello world!')

此任務的內容為:在目前目錄下建立一個名為test1.txt的文件,清空該文件的內容後,在文件中寫入hello world!這個字串。接下來用python3執行檔案:

[dechin@dechin-manjaro os_security]$ python3 file-test.py 
[dechin@dechin-manjaro os_security]$ ll
總用量8
-rw-r--r-- 1 dechin dechin 83  1月25 13:43 file-test.py
-rw-r--r-- 1 dechin dechin 12  1月25 13:43 test1.txt

這裡我們發現,在執行後成功產生了test1.txt這個文件,其權限配置為644,與前面建立的file-test.py保持一致。在不清楚內建函數open的實作原理時,原本以為這個產生的檔案權限配置是與目前的py檔案保持一致的。然而經過進一步的測試,將py檔案的權限配置為440之後再重新執行該檔案:

[dechin@dechin-manjaro os_security]$ chmod 440 file-test.py 
#[ dechin@dechin-manjaro os_security]$ ll
總用量8
-r--r----- 1 dechin dechin 83  1月25 13:43 file-test.py
-rw-r --r-- 1 dechin dechin 12  1月25 13:43 test1.txt
[dechin@dechin-manjaro os_security]$ rm test1.txt 
[dechin@dechin-manjaro os_security]$ testpyth .py 
[dechin@dechin-manjaro os_security]$ ll
總用量8
-r--r----- 1 dechin dechin 83  1月25 13:43 file-test.py
-rw-r--r-- 1 dechin dechin 12  1月25 13:44 test1.txt

這裡從測試結果我們可以看出,python的內建函數open產生的文件類型是與來源py檔案無關的。關於這裡py檔案的執行是否需要可執行權限,可以參考這篇部落格。

改進後的python檔案建立方法

透過fdopen這個函式庫以及特殊的權限指定,我們可以設定產生檔案的存取權限,以下直接展示一個python程式碼案例:

# fdopen-test.py
 
import os
import stat
 
file_name = 'test2.txt'
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
mode = stat.S_IRUSR | stat.S_IWUSR
 
with os.fdopen(os.open(file_name, flags, mode), 'w') as file:
    file.write('hello world!')

執行之後我們可以發現,目前目錄下產生了一個名為test2.txt的文件,其權限配置為600, 對照於我們在程式碼中設定的mode = stat.S_IRUSR | stat.S_IWUSR。這裡我們先對其中的一些參數作一個解釋:os.O_WRONLY表示以只寫的方式打開,os.O_CREAT表示創建並打開一個新文件,os.O_EXCL表示如果文件已存在則報錯。而mode中所配置的權限分別對應rwx配置,其中USR,GRP,OTH又分別對使用者、使用者群組、其他使用者進行了細分的配置,從而我們就可以透過改變mode參數來實現所有種類的權限配置。

我們可以嘗試將上述用例中的mode作一個調整,例如新增一個可執行權限變為700:

# fdopen-test.py
 
import os
import stat
 
file_name = 'test3.txt'
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
 
with os.fdopen(os.open(file_name, flags, mode), 'w') as file:
    file.write('hello world!')

又或者,我們需要為使用者群組裡的其他使用者新增可存取權限,例如640權限:

# fdopen-test.py
 
import os
import stat
 
file_name = 'test4.txt'
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP
 
with os.fdopen(os.open(file_name, flags, mode), 'w') as file:
    file.write('hello world!')

甚至我們也可以寫出系統原生的644檔案權限:

# fdopen-test.py
 
import os
import stat
 
file_name = 'test5.txt'
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
 
with os.fdopen(os.open(file_name, flags, mode), 'w') as file:
    file.write('hello world!')

最後,讓我們一起看下上面這些python範例執行後得到的結果:

[dechin@dechin-manjaro os_security]$ ll
總用量28
-rw-r--r-- 1 dechin dechin 269  1月25 14:58 fdopen- test.py
-r--r----- 1 dechin dechin  84  1月25 14:11 file-test.py
-rw-r--r-- 1 dechin dechin  12  1月25 13:44 test1.txt
-rw------- 1 dechin dechin  12  1月25 14:44 test2.txt
-rwx------ 1 dechin dechin  12  1月25 14 :48 test3.txt
-rw-r----- 1 dechin dechin  12  1月25 14:56 test4.txt
-rw-r--r-- 1 dechin dechin  12  1月25 14 :58 test5.txt

從結果中我們可以看出,所有產生的檔案test*.txt都按照我們預期的檔案權限配置生成,到這裡我們就完成了所有預期的目標。

以上是如何使用fdopen實作對Python進程產生的檔案進行權限最小化配置的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除