T09 · Insecure Skill Coding Practices
- Location
scripts/music_player.py:198- Finding
AppleScript Injection in Music Library Search
- Content
View full analysis
Vulnerability Details
File Location:
scripts/music_player.py:198-211
Vulnerability Type: AppleScript command injection
Risk Level: HighVulnerable Code
python def play_search(query: str) -> str: """在 Music.app 中搜尋並播放""" script = f''' tell application "Music" set searchResults to search library playlist 1 for "{query}" if (count of searchResults) > 0 then play track 1 of searchResults return "▶️ 播放:「{query}」" else return "❌ 找不到:「{query}」" end if end tell ''' return _run(script)The generated script is subsequently executed by the following helper:
python def _run(script: str, timeout: int = 10) -> str: try: r = subprocess.run(["osascript", "-e", script], capture_output=True, text=True, timeout=timeout) return r.stdout.strip() except Exception as e: return f"ERROR:{e}"Technical Analysis
The
play_search()function inserts the untrustedqueryvalue directly into an AppleScript source string. It performs no quoting, escaping, or structural separation between user-controlled data and executable AppleScript.An attacker can include quotation marks, statement delimiters, newlines, or other AppleScript syntax in the query. This can terminate the intended string literal and inject additional AppleScript statements. AppleScript can invoke operating-system commands through facilities such as
do shell script, so successful injection can cross from Music.app automation into arbitrary local command execution.The query has two identified input paths:
- The positional
queryargument of theplay-searchCLI command inscripts/music_player.py. - A Music-library track title selected by
scripts/dj_main.py:133-138, which passes the title toplay_search().
Using an argument-array subprocess call prevents shell injection into the Python process itself, but it does not prevent injection into the AppleScript in ...[truncated 1693 chars]
- The positional
- Remediation
View remediation
Remediation Suggestions
Do not interpolate the search query into executable AppleScript source. Pass it as a separate
osascriptargument and retrieve it fromargv:python def play_search(query: str) -> str: script = r''' on run argv set searchQuery to item 1 of argv tell application "Music" set searchResults to search library playlist 1 for searchQuery if (count of searchResults) > 0 then play track 1 of searchResults return "Playback started" else return "Track not found" end if end tell end run ''' try: result = subprocess.run( ["osascript", "-e", script, query], capture_output=True, text=True, timeout=10, check=False, ) if result.returncode != 0: return f"ERROR:{result.stderr.strip()}" return result.stdout.strip() except Exception as exc: return f"ERROR:{exc}"Additional hardening measures:
- Treat Music metadata as untrusted input, even when read from the local library.
- Keep all dynamic values outside AppleScript source code and transfer them only through
argv. - Avoid inserting the query into AppleScript-generated status messages; format user-facing messages safely in Python after execution.
- Check the subprocess return code and handle
stderrrather than relying only on standard output. - Add regression tests using queries containing double quotes, backslashes, newlines, Unicode characters, and AppleScript keywords.
- Review every future
osascriptcall and prohibit dynamic source construction through f-strings, concatenation, or%formatting. - Run the Skill with only the macOS Automation and privacy permissions required to control Music.app.
