From 3c0c682fa156d475bf3b905e9a7303ccdd7e32b6 Mon Sep 17 00:00:00 2001 From: "pixeebot[bot]" <104101892+pixeebot[bot]@users.noreply.github.com> Date: Mon, 23 Jun 2025 03:24:54 +0000 Subject: [PATCH] Sandbox Process Creation --- pyproject.toml | 1 + python3/10_Modules/06_subprocess/00_subprocess_ex.py | 4 ++-- python3/10_Modules/06_subprocess/03_ping_site.py | 4 ++-- python3/10_Modules/06_subprocess/cleanup_pid.py | 3 ++- .../a_create_pdfs/c_report_generation_in_required_format.py | 4 ++-- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index bc0c066c..93dfdaeb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,7 @@ pytest = "^7.1.3" requests = "^2.28.1" jupyter = "^1.0.0" ipython = "^8.5.0" +security = "==1.3.1" [tool.poetry.group.dev.dependencies] diff --git a/python3/10_Modules/06_subprocess/00_subprocess_ex.py b/python3/10_Modules/06_subprocess/00_subprocess_ex.py index e18be429..176f8a54 100644 --- a/python3/10_Modules/06_subprocess/00_subprocess_ex.py +++ b/python3/10_Modules/06_subprocess/00_subprocess_ex.py @@ -1,5 +1,6 @@ import os import subprocess +from security import safe_command def execute_command(cmd): @@ -13,8 +14,7 @@ def execute_command(cmd): def get_execution_result(cmd): - p = subprocess.Popen( - cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE + p = safe_command.run(subprocess.Popen, cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) output, err = p.communicate() diff --git a/python3/10_Modules/06_subprocess/03_ping_site.py b/python3/10_Modules/06_subprocess/03_ping_site.py index d35a4bb8..afada87d 100644 --- a/python3/10_Modules/06_subprocess/03_ping_site.py +++ b/python3/10_Modules/06_subprocess/03_ping_site.py @@ -4,6 +4,7 @@ """ import os import subprocess +from security import safe_command def ping_website(site_name): @@ -12,8 +13,7 @@ def ping_website(site_name): def get_time_delays(site_name): - p = subprocess.Popen( - f"ping {site_name}", stdout=subprocess.PIPE, stderr=subprocess.PIPE + p = safe_command.run(subprocess.Popen, f"ping {site_name}", stdout=subprocess.PIPE, stderr=subprocess.PIPE ) output, error = p.communicate() if error: diff --git a/python3/10_Modules/06_subprocess/cleanup_pid.py b/python3/10_Modules/06_subprocess/cleanup_pid.py index bee2b800..b78c0ebb 100644 --- a/python3/10_Modules/06_subprocess/cleanup_pid.py +++ b/python3/10_Modules/06_subprocess/cleanup_pid.py @@ -2,6 +2,7 @@ import os import subprocess +from security import safe_command base_dir = "/tmp/pid_dir" pid_files = ("ut.pid", "ft.pid") @@ -21,7 +22,7 @@ def check_pid(pid): def get_elapsed_time(pid): """get the elapsed time of the process with this pid""" cmd = f"ps -p {str(pid)} -o pid,etime" - proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) + proc = safe_command.run(subprocess.Popen, cmd, shell=True, stdout=subprocess.PIPE) # get data from stdout proc.wait() results = proc.stdout.readlines() diff --git a/python3/11_File_Operations/05_pdf_files/a_create_pdfs/c_report_generation_in_required_format.py b/python3/11_File_Operations/05_pdf_files/a_create_pdfs/c_report_generation_in_required_format.py index fe2ab6a6..f5b805f0 100644 --- a/python3/11_File_Operations/05_pdf_files/a_create_pdfs/c_report_generation_in_required_format.py +++ b/python3/11_File_Operations/05_pdf_files/a_create_pdfs/c_report_generation_in_required_format.py @@ -10,6 +10,7 @@ from reportlab.lib.utils import simpleSplit from reportlab.pdfbase.pdfmetrics import stringWidth from reportlab.platypus import Paragraph, SimpleDocTemplate +from security import safe_command class form: @@ -106,8 +107,7 @@ def __init__(self): path_to_pdf = os.path.abspath( os.path.join(os.path.expanduser("~"), "Desktop") + "\hello1.pdf" ) - process = subprocess.Popen( - [path_to_pdf], + process = safe_command.run(subprocess.Popen, [path_to_pdf], bufsize=2048, shell=True, stdin=subprocess.PIPE,