Repository navigation
Potential fixes for 4 code quality findings - #22
Conversation
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefactors command execution in get_hardware_info to use argument lists instead of shell=True for safer subprocess usage, and hardens macOS login item management by shell-quoting dynamic script paths and names used in osascript/grep pipelines. Sequence diagram for updated get_hardware_info subprocess usagesequenceDiagram
actor Caller
participant Script as UXTU4Unix_script
participant Cfg as cfg_ConfigParser
participant Shlex as shlex
participant Subproc as subprocess
Caller->>Script: get_hardware_info(command, use_sudo)
Script->>Cfg: get(User, Password, fallback="")
Cfg-->>Script: password
alt command is list or tuple
Script->>Script: command_args = list(command)
else command is string
Script->>Shlex: split(command)
Shlex-->>Script: command_args
end
alt use_sudo is True
Script->>Script: full_command = ['sudo', '-S'] + command_args
else use_sudo is False
Script->>Script: full_command = command_args
end
Script->>Subproc: Popen(full_command, stdin=PIPE, stdout=PIPE, stderr=PIPE)
Subproc-->>Script: process
Script->>Subproc: process.communicate(input=(password + "\n").encode("utf-8"))
Subproc-->>Script: (output_bytes, error_bytes)
Script->>Script: output = output_bytes.decode("utf-8").strip()
Script-->>Caller: output
Sequence diagram for macOS login_cfg flow with quoted commandssequenceDiagram
actor User
participant Script as UXTU4Unix_script
participant Shlex as shlex
participant Subproc as subprocess
loop login_cfg loop
Script->>Script: clear()
Script->>Shlex: quote(command_file_name)
Shlex-->>Script: safe_command_file_name
Script->>Script: build check_command with safe_command_file_name
Script->>Subproc: call(check_command, shell=True, stdout=DEVNULL)
Subproc-->>Script: return_code
Script->>Script: login_enabled = (return_code == 0)
User-->>Script: choice ("1" add, "2" remove, other)
alt choice == "1" and not login_enabled
Script->>Shlex: quote(command_file)
Shlex-->>Script: safe_command_file
Script->>Script: build osascript make login item command with safe_command_file
Script->>Subproc: call(command, shell=True)
else choice == "1" and login_enabled
Script->>User: print already added
end
alt choice == "2" and login_enabled
Script->>Shlex: quote(command_file_name)
Shlex-->>Script: safe_command_file_name
Script->>Script: build osascript delete login item command with safe_command_file_name
Script->>Subproc: call(command, shell=True)
else choice == "2" and not login_enabled
Script->>User: print cannot remove
end
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 3 issues, and left some high level feedback:
- Using
shlex.quoteforcommand_file/command_file_nameand then interpolating the result into the AppleScript string (which is itself single-quoted for the shell) will double-quote the path and likely break theosascriptcalls; consider either escaping the path for AppleScript instead of the shell, or switching tosubprocessargument lists like['osascript', '-e', script]to avoid manual shell quoting entirely. - In
welcome_tutorial/login_cfg, thegrep-based checks still rely onshell=Trueand brittle string piping; if you want to improve command safety consistently, consider replacing these with directosascriptcalls viasubprocess.run([...])and testing the output in Python rather than throughgrep.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Using `shlex.quote` for `command_file`/`command_file_name` and then interpolating the result into the AppleScript string (which is itself single-quoted for the shell) will double-quote the path and likely break the `osascript` calls; consider either escaping the path for AppleScript instead of the shell, or switching to `subprocess` argument lists like `['osascript', '-e', script]` to avoid manual shell quoting entirely.
- In `welcome_tutorial`/`login_cfg`, the `grep`-based checks still rely on `shell=True` and brittle string piping; if you want to improve command safety consistently, consider replacing these with direct `osascript` calls via `subprocess.run([...])` and testing the output in Python rather than through `grep`.
## Individual Comments
### Comment 1
<location path="UXTU4Unix/UXTU4Unix.py" line_range="64-72" />
<code_context>
def get_hardware_info(command, use_sudo=False):
password = cfg.get('User', 'Password', fallback='')
+ if isinstance(command, (list, tuple)):
+ command_args = list(command)
+ else:
+ command_args = shlex.split(command)
+
if use_sudo:
- full_command = f"sudo -S {command}"
+ full_command = ['sudo', '-S'] + command_args
else:
- full_command = command
-
+ full_command = command_args
+
process = subprocess.Popen(
</code_context>
<issue_to_address>
**issue (bug_risk):** Switching from `shell=True` to argument lists may break commands that relied on shell features.
Previously, callers could pass shell syntax (pipelines, redirections, globbing) because `get_hardware_info` used `shell=True`. Now, string commands are `shlex.split`’d and executed with `shell=False`, so inputs like `"ls /tmp | grep foo"` or `"cmd > file"` will break, as `|`/`>` become literal args. If shell-style commands are expected, consider keeping `shell=True` for string inputs or clearly documenting that callers must pass pre-split argument lists. In any case, it’s worth checking existing call sites to ensure none rely on shell operators.
</issue_to_address>
### Comment 2
<location path="UXTU4Unix/UXTU4Unix.py" line_range="301-302" />
<code_context>
start_with_macos = input("Do you want this script to start with macOS? (Login Items) (y/n): ").lower().strip()
if start_with_macos == 'y':
- command = f"osascript -e 'tell application \"System Events\" to make login item at end with properties {{path:\"{command_file}\", hidden:false}}'"
+ safe_command_file = shlex.quote(command_file)
+ command = f"osascript -e 'tell application \"System Events\" to make login item at end with properties {{path:\"{safe_command_file}\", hidden:false}}'"
subprocess.call(command, shell=True)
cfg.set('User', 'Password', password)
</code_context>
<issue_to_address>
**issue (bug_risk):** Using `shlex.quote` for the AppleScript `path` parameter likely produces an invalid path.
Because `safe_command_file` is interpolated directly into the AppleScript as `path:"{safe_command_file}"`, the single quotes added by `shlex.quote` become part of the AppleScript string (e.g. `'/Applications/Foo Bar.app'`), so the path is wrong and login item creation will likely fail. `shlex.quote` should only be used when building shell commands, not AppleScript strings. Instead, pass `command_file` directly here (relying on the existing single-quoted `-e` argument), or implement proper AppleScript-specific escaping for quotes/backslashes if needed.
</issue_to_address>
### Comment 3
<location path="UXTU4Unix/UXTU4Unix.py" line_range="295-304" />
<code_context>
print("Incorrect sudo password. Please try again.")
if kernel == "Darwin":
- check_command = f"osascript -e 'tell application \"System Events\" to get the name of every login item' | grep {command_file_name}"
+ safe_command_file_name = shlex.quote(command_file_name)
+ check_command = f"osascript -e 'tell application \"System Events\" to get the name of every login item' | grep {safe_command_file_name}"
login_enabled = subprocess.call(check_command, shell=True, stdout=subprocess.DEVNULL) == 0
</code_context>
<issue_to_address>
**issue (bug_risk):** Quoting the login item name with `shlex.quote` will likely prevent AppleScript from matching the item.
In the delete case, `safe_command_file_name` is a shell-escaped version of the name (e.g. `My Script` → `'My Script'`) but is then used directly in `delete login item "{safe_command_file_name}"`. That makes AppleScript search for a login item literally named `'My Script'` (including quotes), which will fail. Use the raw `command_file_name` (escaped appropriately for AppleScript, not via `shlex.quote`) since the `-e` argument is already shell-quoted.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| if isinstance(command, (list, tuple)): | ||
| command_args = list(command) | ||
| else: | ||
| command_args = shlex.split(command) | ||
|
|
||
| if use_sudo: | ||
| full_command = f"sudo -S {command}" | ||
| full_command = ['sudo', '-S'] + command_args | ||
| else: | ||
| full_command = command | ||
| full_command = command_args |
There was a problem hiding this comment.
issue (bug_risk): Switching from shell=True to argument lists may break commands that relied on shell features.
Previously, callers could pass shell syntax (pipelines, redirections, globbing) because get_hardware_info used shell=True. Now, string commands are shlex.split’d and executed with shell=False, so inputs like "ls /tmp | grep foo" or "cmd > file" will break, as |/> become literal args. If shell-style commands are expected, consider keeping shell=True for string inputs or clearly documenting that callers must pass pre-split argument lists. In any case, it’s worth checking existing call sites to ensure none rely on shell operators.
| safe_command_file = shlex.quote(command_file) | ||
| command = f"osascript -e 'tell application \"System Events\" to make login item at end with properties {{path:\"{safe_command_file}\", hidden:false}}'" |
There was a problem hiding this comment.
issue (bug_risk): Using shlex.quote for the AppleScript path parameter likely produces an invalid path.
Because safe_command_file is interpolated directly into the AppleScript as path:"{safe_command_file}", the single quotes added by shlex.quote become part of the AppleScript string (e.g. '/Applications/Foo Bar.app'), so the path is wrong and login item creation will likely fail. shlex.quote should only be used when building shell commands, not AppleScript strings. Instead, pass command_file directly here (relying on the existing single-quoted -e argument), or implement proper AppleScript-specific escaping for quotes/backslashes if needed.
| safe_command_file_name = shlex.quote(command_file_name) | ||
| check_command = f"osascript -e 'tell application \"System Events\" to get the name of every login item' | grep {safe_command_file_name}" | ||
| login_enabled = subprocess.call(check_command, shell=True, stdout=subprocess.DEVNULL) == 0 | ||
| if not login_enabled: | ||
| start_with_macos = input("Do you want this script to start with macOS? (Login Items) (y/n): ").lower().strip() | ||
| if start_with_macos == 'y': | ||
| command = f"osascript -e 'tell application \"System Events\" to make login item at end with properties {{path:\"{command_file}\", hidden:false}}'" | ||
| safe_command_file = shlex.quote(command_file) | ||
| command = f"osascript -e 'tell application \"System Events\" to make login item at end with properties {{path:\"{safe_command_file}\", hidden:false}}'" | ||
| subprocess.call(command, shell=True) | ||
| cfg.set('User', 'Password', password) |
There was a problem hiding this comment.
issue (bug_risk): Quoting the login item name with shlex.quote will likely prevent AppleScript from matching the item.
In the delete case, safe_command_file_name is a shell-escaped version of the name (e.g. My Script → 'My Script') but is then used directly in delete login item "{safe_command_file_name}". That makes AppleScript search for a login item literally named 'My Script' (including quotes), which will fail. Use the raw command_file_name (escaped appropriately for AppleScript, not via shlex.quote) since the -e argument is already shell-quoted.
This PR applies 4/5 suggestions from code quality AI findings. 1 suggestion was skipped to avoid creating conflicts.
Summary by Sourcery
Improve command execution safety and macOS login item handling
Bug Fixes:
Enhancements: