Skip to content

Potential fixes for 4 code quality findings - #22

Merged
oxGorou merged 4 commits into
mainfrom
ai-findings-autofix/UXTU4Unix-UXTU4Unix.py
Apr 25, 2026
Merged

oxGorou merged 4 commits into
mainfrom
ai-findings-autofix/UXTU4Unix-UXTU4Unix.py

Conversation

@oxGorou

@oxGorou oxGorou commented Apr 25, 2026 •

Copy link
Copy Markdown
Member

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:

  • Avoid unsafe shell invocation in hardware info retrieval by passing commands as argument lists instead of shell strings
  • Quote macOS login item script arguments to prevent issues with special characters in file paths and names

Enhancements:

  • Allow get_hardware_info to accept both string and sequence commands for more flexible usage

oxGorou and others added 4 commits April 25, 2026 22:15
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>
@oxGorou
oxGorou marked this pull request as ready for review April 25, 2026 15:15
@oxGorou
oxGorou merged commit 6225276 into main Apr 25, 2026
2 checks passed
@sourcery-ai

sourcery-ai Bot commented Apr 25, 2026 •

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors 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 usage

sequenceDiagram
    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
Loading

Sequence diagram for macOS login_cfg flow with quoted commands

sequenceDiagram
    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
Loading

File-Level Changes

Change Details Files
Harden get_hardware_info subprocess invocation and support both string and sequence commands.
  • Retrieve the sudo password from config as before but normalize the command into a list of arguments using shlex.split for strings and list() for list/tuple inputs.
  • Build the sudo-prefixed command as ['sudo', '-S'] + command_args instead of string concatenation, and pass the resulting list directly to subprocess.Popen.
  • Remove shell=True from subprocess.Popen to avoid shell interpretation and rely on direct argument list execution while preserving stdin/stdout/stderr handling and password piping.
UXTU4Unix/UXTU4Unix.py
Quote macOS login item names and paths before embedding them in shell commands to avoid issues with spaces or special characters.
  • Introduce safe_command_file_name via shlex.quote(command_file_name) before using it in grep pipelines that check existing login items in welcome_tutorial and login_cfg.
  • Introduce safe_command_file via shlex.quote(command_file) before constructing osascript commands that create login items with a path property.
  • Apply shlex.quote to command_file_name again when building the osascript delete login item command to ensure the login item name is safely quoted in the shell string.
UXTU4Unix/UXTU4Unix.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 3 issues, and left some high level feedback:

  • 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.
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread UXTU4Unix/UXTU4Unix.py
Comment on lines +64 to +72
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread UXTU4Unix/UXTU4Unix.py
Comment on lines +301 to +302
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}}'"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread UXTU4Unix/UXTU4Unix.py
Comment on lines +295 to 304
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@oxGorou
oxGorou deleted the ai-findings-autofix/UXTU4Unix-UXTU4Unix.py branch April 25, 2026 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant