cloudflare / cloudflare/sandbox-sdk

Add stdin support to exec()

Open
#357 3 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
1.1k
Forks
114
Avg merge
22h 42m
Merged PRs (30d)
14

Description

## 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

```typescript
// 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

```typescript
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:
```typescript
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](https://developers.cloudflare.com/sandbox/configuration/environment-variables/) show per-command env support:

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

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.