T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/applescript_app.py:42
- Finding
- AppleScript Injection Through Unescaped Application Names<![CDATA[ ## Vulnerability Details **File Location**: `scripts/applescript_app.py:42-73`; related instances in `scripts/applescript_window.py:41-78` **Vulnerability Type**: AppleScript code injection **Risk Level**: High ### Vulnerable Code ```python if args.action == 'open': if args.path: subprocess.run(['open', args.path], check=True) emit(build_result('open', path=args.path, launch='open-path'), args.json_pretty) return if not args.app: raise SystemExit("Action 'open' requires --app or --path.") run_osascript([ f'tell application "{args.app}" to activate', ]) emit(build_result('open', app=args.app, launch='activate-app'), args.json_pretty) return if args.action == 'activate': if not args.app: raise SystemExit("Action 'activate' requires --app.") run_osascript([ f'tell application "{args.app}" to activate', 'tell application "System Events"', f'tell process "{args.app}" to set frontmost to true', 'end tell', ]) emit(build_result('activate', app=args.app, frontmost=True), args.json_pretty) return if args.action == 'is-running': if not args.app: raise SystemExit("Action 'is-running' requires --app.") out = run_osascript([ f'tell application "System Events" to return (name of processes) contains "{args.app}"', ]) emit(build_result('is-running', app=args.app, running=out.lower() == 'true'), args.json_pretty) return ``` The same unsafe construction appears in the window-inspection utility: ```python if args.action == 'title': out = run_osascript([ 'tell application "System Events"', f'tell process "{args.app}"', 'if (count of windows) > 0 then', 'return name of front window', 'else', 'return ""', 'end if', 'end tell', 'end tell', ]) ``` ### Technical Analysis The `--app` command-line value is inserted directly into Appl ...[truncated 1880 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Do not concatenate application names into AppleScript source. - Pass dynamic values as positional arguments to `osascript` and retrieve them through an `on run argv` handler. - Where possible, resolve applications using validated bundle identifiers rather than free-form names. - If source interpolation cannot be eliminated, implement a dedicated AppleScript string serializer that safely handles quotation marks, backslashes, control characters, and line breaks. - Optionally restrict input to an allowlist of installed application or process names. - Apply the same correction to every `args.app` interpolation in both `applescript_app.py` and `applescript_window.py`. - Add regression tests using names containing quotes, line breaks, and AppleScript keywords to verify that they remain data rather than executable syntax. A safer architectural pattern is: ```python script = ''' on run argv set appName to item 1 of argv tell application "System Events" return (name of processes) contains appName end tell end run ''' subprocess.run( ['osascript', '-e', script, args.app], check=True, capture_output=True, text=True, ) ``` ]]>
