Skip to content

fix(renaming): replace AppleScript with fs.rename for security and reliability #1

Open

Description

@TurboCoder13

Summary

Replace the AppleScript-based rename implementation with Node.js fs.rename() to fix a shell injection vulnerability and batch failure issues. This is the critical security fix that must land first.

Problem

The current implementation spawns a separate osascript process per file via spawnSync():

const escapedFilePath = file.replaceAll('"', '\\"');
await runAppleScript(`
  tell application "Finder"
    set theItem to POSIX file "${escapedFilePath}" as alias
    set name of theItem to "${escapedNewName}"
  end tell
`);

Security: Filenames containing shell metacharacters can inject arbitrary AppleScript commands. The " → \" escaping is insufficient.

Reliability: Batch renames start timing out around ~90 files — some files are silently skipped with no indication of failure.

No error recovery: A single try/catch around the entire loop means a mid-batch failure leaves files in an inconsistent state.

Proposed Solution

  1. Replace AppleScript with fs.rename() in both index.tsx (Rename) and replace.tsx (Replace)
  2. Remove utils.tsx (AppleScript runner) — no longer needed
  3. Add per-file error handling with individual success/failure tracking
  4. Add conflict detection to prevent overwriting existing files
  5. Show results summary after operation (what succeeded, what failed)

Critical constraint: Keep the existing two commands with their exact names:

  • Rename File(s) (command name: index)
  • Replace File(s) Characters (command name: replace)

This preserves user hotkeys, aliases, and ranking.

Implementation Notes

  • Key files to modify: src/index.tsx, src/replace.tsx
  • Key file to remove: src/utils.tsx
  • No new dependencies required
  • Extract shared rename logic into src/lib/files.ts
  • Add src/lib/errors.ts for structured error types
  • Target: ~5-8 files changed

Benefits

  • Eliminates shell injection attack surface entirely
  • Fixes silent batch failures for large file sets
  • Per-file error tracking instead of all-or-nothing
  • Conflict detection prevents accidental overwrites
  • Zero user-facing breaking changes (same commands, same names)

Activity

  1. added
    bug
    raycast
    renaming
    security
    on Mar 28, 2026
  2. TurboCoder13 commented on Mar 28, 2026

    @TurboCoder13
    OwnerAuthor

    AI Implementation Prompt

    Use this prompt in a new Claude Code session to implement this fix:

    ## Requirements
    
    ### 1. Replace AppleScript with fs.rename() in Rename command
    - Modify `src/index.tsx` to use `fs.rename()` / `fs.renameSync()` instead of `runAppleScript()`
    - Remove the AppleScript template string construction
    - Handle both files and directories
    
    ### 2. Replace AppleScript with fs.rename() in Replace command
    - Modify `src/replace.tsx` to use `fs.rename()` instead of `runAppleScript()`
    - Same pattern as the Rename command
    
    ### 3. Remove AppleScript utility
    - Delete `src/utils.tsx` (no longer needed)
    - Remove any imports of `runAppleScript` from `@raycast/utils` if unused
    
    ### 4. Add per-file error handling
    - Extract shared rename logic into `src/lib/files.ts`
    - Wrap each `fs.rename()` in individual try/catch
    - Track success/failure per file
    - Add `src/lib/errors.ts` for structured error types (RenameError, ConflictError)
    
    ### 5. Add conflict detection
    - Before renaming, check if target path already exists using `fs.existsSync()`
    - Refuse to overwrite; report conflict to user
    - Show Toast with count of conflicts
    
    ### 6. Show results summary
    - After batch completes, show success/failure counts via Toast
    - If any failures, show detailed list
    
    ## Critical Constraints
    - Keep command name "index" with title "Rename File(s)"
    - Keep command name "replace" with title "Replace File(s) Characters"
    - Do NOT change package.json command entries
    - Do NOT add new dependencies
    
    ## Files to Modify
    - `src/index.tsx` — replace AppleScript with fs.rename
    - `src/replace.tsx` — replace AppleScript with fs.rename
    
    ## Files to Create
    - `src/lib/files.ts` — shared rename logic with error handling
    - `src/lib/errors.ts` — error type definitions
    
    ## Files to Delete
    - `src/utils.tsx` — AppleScript runner (no longer needed)
    
    ## Before Submitting
    - Run `ray lint` — must pass
    - Run `ray build -e dist` — must build
    - Test with: single file, batch of files, file with special characters, conflict scenario
    
  3. 5 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugraycastrenamingsecurity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions