T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/send.py:44
- Finding
- AppleScript Injection Through Unsanitized Recipient and File Path<![CDATA[ ## Vulnerability Details **File Location**: `scripts/send.py`, lines 44-57 **Vulnerability Type**: AppleScript injection leading to arbitrary command execution **Risk Level**: High ### Vulnerable Code ```python # Phone number format: +86XXXXXXXXXX formatted_recipient = recipient if not recipient.startswith('+'): if len(recipient) == 11: formatted_recipient = '+86' + recipient # Use POSIX file path format script = f''' tell application "Messages" activate send POSIX file "{send_path}" to participant "{formatted_recipient}" end tell ''' result = subprocess.run( ['osascript', '-e', script], capture_output=True, text=True ) ``` ### Technical Analysis The script dynamically constructs AppleScript source by directly interpolating `formatted_recipient` and `send_path` into quoted AppleScript strings. Neither value is escaped for the AppleScript grammar. The recipient is supplied directly through the command line. The destination filename is derived from the basename of the user-provided image path and can therefore also contain quotation marks or other AppleScript syntax on supported filesystems. Although `subprocess.run` uses an argument list rather than a shell command, this does not prevent the vulnerability. The generated string is intentionally passed to `osascript` as executable AppleScript source. An attacker can use a quotation mark to terminate one of the intended string literals, insert additional AppleScript statements, and neutralize the remainder of the generated statement. Injected AppleScript executes under the identity and permissions of the user running the skill. Subject to macOS privacy and Automation controls already granted to the invoking process, injected code could control applications, read accessible files, or invoke shell commands through AppleScript's `do shell script` functionality. ### Attack Path 1. An attacker ca ...[truncated 1327 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions Do not interpolate untrusted values into executable AppleScript source. 1. Pass the recipient and path as separate `osascript` arguments. 2. Retrieve those values from an AppleScript `on run argv` handler so that they remain data rather than source code. 3. Validate recipients against a strict allowlist format, such as an international telephone-number pattern, and reject quotation marks, control characters, line breaks, and unexpected syntax. 4. Resolve and validate the source path before invoking Messages. 5. Avoid displaying raw interpreter errors if they could contain sensitive data. 6. Add regression tests using recipients and filenames containing quotes, backslashes, newlines, and AppleScript keywords. A safer invocation pattern is: ```python apple_script = r''' on run argv set attachmentPath to item 1 of argv set recipientAddress to item 2 of argv tell application "Messages" activate send POSIX file attachmentPath to participant recipientAddress end tell end run ''' result = subprocess.run( ["osascript", "-e", apple_script, str(send_path), formatted_recipient], capture_output=True, text=True, check=False, ) ``` Recipient validation should occur before this call, and only explicitly supported phone-number or account formats should be accepted. ]]>
