Skip to content

Add stdin support to exec() #357

@mavam

Description

@mavam

Summary

The exec() method currently runs commands through a shell and doesn't support passing stdin data or per-command environment variables. This makes it difficult to safely pass arbitrary user input without shell injection risks.

Current behavior

// SDK v0.0.7 signature
exec(command: string, args: string[], options?: { stream?: boolean })
  • Commands appear to run through a shell
  • No stdin support
  • No per-command environment variables (despite being documented)

Proposed API

const result = await sandbox.exec("claude", ["-p", "-"], {
  stdin: prompt,           // Pass data to process stdin
  env: { API_KEY: "..." }, // Per-command env vars (already documented)
});

Use case

We're building a Discord bot that runs Claude Code in a sandbox. User messages need to be passed as prompts, but they can contain arbitrary characters (quotes, backticks, $, etc.). Currently we have to:

  1. Write the prompt to a file via writeFile()
  2. Pipe it through cat: cat /tmp/prompt.txt | claude -p -

With stdin support, this becomes:

await sandbox.exec("claude", ["-p", "-"], { stdin: userMessage });

Benefits

  • Security: Direct process spawn without shell interpretation
  • Simplicity: No need for file-based workarounds
  • Consistency: Per-command env is already documented but not implemented

Documentation reference

The environment variables docs show per-command env support:

await sandbox.exec("node app.js", {
  env: { NODE_ENV: "production" }
});

But this isn't implemented in SDK v0.0.7.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions