Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions UXTU4Unix/UXTU4Unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
import select
import plistlib
import shlex
from configparser import ConfigParser

LOCAL_VERSION = "0.4.2"
Expand Down Expand Up @@ -60,10 +61,23 @@ def clear():

def get_hardware_info(command, use_sudo=False):
password = cfg.get('User', 'Password', fallback='')
command_args = shlex.split(command)
if use_sudo:
command = f"echo '{password}' | sudo -S {command}"
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
process = subprocess.Popen(
Comment on lines +64 to +66

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

question: Consider whether shlex.split is appropriate if command may rely on shell features like pipes or redirection.

If callers may currently pass shell operators (pipes, redirects, globbing, etc.), this change will alter or break those commands. Please either (a) guarantee and document that command is always a simple executable plus args, or (b) support both list and string inputs and handle shell-operator use cases explicitly.

['sudo', '-S'] + command_args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
output, error = process.communicate(input=(password + '\n').encode('utf-8'))
else:
process = subprocess.Popen(
command_args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
output, error = process.communicate()
Comment on lines 65 to +80

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: stdin is piped even when no input is sent for the non-sudo path; consider dropping stdin=subprocess.PIPE there.

In the non-sudo branch of get_hardware_info, stdin=subprocess.PIPE is never used. You can omit it there to avoid creating an unused pipe and keep the subprocess call minimal:

audit_process = subprocess.Popen(
    command_args,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
)
output, error = audit_process.communicate()
Suggested change
if use_sudo:
command = f"echo '{password}' | sudo -S {command}"
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
process = subprocess.Popen(
['sudo', '-S'] + command_args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
output, error = process.communicate(input=(password + '\n').encode('utf-8'))
else:
process = subprocess.Popen(
command_args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
output, error = process.communicate()
if use_sudo:
process = subprocess.Popen(
['sudo', '-S'] + command_args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
output, error = process.communicate(input=(password + '\n').encode('utf-8'))
else:
process = subprocess.Popen(
command_args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
output, error = process.communicate()

return output.decode('utf-8').strip()

def get_codename():
Expand Down Expand Up @@ -265,8 +279,13 @@ def welcome_tutorial():
while True:
subprocess.run("sudo -k", shell=True)
password = getpass.getpass("Enter your sudo (login) password: ")
sudo_check_command = f"echo '{password}' | sudo -S ls /"
sudo_check_process = subprocess.run(sudo_check_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sudo_check_process = subprocess.run(
["sudo", "-S", "ls", "/"],
input=password + "\n",
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
if sudo_check_process.returncode == 0:
break
else:
Expand Down Expand Up @@ -491,10 +510,14 @@ def pass_cfg():
choice = input("Option: ").strip()
if choice == "1":
while True:
subprocess.run("sudo -k", shell=True)
subprocess.run(["sudo", "-k"])
password = getpass.getpass("Enter your sudo (login) password: ")
sudo_check_command = f"echo '{password}' | sudo -S ls /"
sudo_check_process = subprocess.run(sudo_check_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sudo_check_process = subprocess.run(
["sudo", "-S", "-k", "ls", "/"],
input=(password + "\n").encode(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
if sudo_check_process.returncode == 0:
cfg.set('User', 'Password', password)
with open(CONFIG_PATH, 'w') as config_file:
Expand Down Expand Up @@ -686,7 +709,7 @@ def install_manual():
prompt_restart()

def prompt_restart():
choice = input("Do you want to restart your computer to take effects? (y/n)").lower()
choice = input("Do you want to restart your computer to take effect? (y/n)").lower()
if choice == "y":
input("Save your current work before restarting. Press Enter to continue")
restart_command = '''osascript -e 'tell app "System Events" to restart' '''
Expand Down
Loading