Skip to content

Conversation

@whoiskatrin
Copy link
Collaborator

@whoiskatrin whoiskatrin commented Nov 11, 2025

No description provided.

@changeset-bot
Copy link

changeset-bot bot commented Nov 11, 2025

🦋 Changeset detected

Latest commit: 510fe63

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/sandbox Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 11, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/sandbox-sdk/@cloudflare/sandbox@204

commit: c0b8622

@github-actions
Copy link
Contributor

github-actions bot commented Nov 11, 2025

🐳 Docker Image Published

FROM cloudflare/sandbox:0.0.0-pr-204-84e692e

Version: 0.0.0-pr-204-84e692e

You can use this Docker image with the preview package from this PR.

@ghostwriternr
Copy link
Member

(Sharing some thoughts here about env and cwd as I go, before adding more specific feedback. Literally me "think out loud"-ing haha)

Related issues: #16, #144

Overall, I quite like the features this PR is adding and think this is an important one to clean up and get right. It's been fragile for a long time and I'm happy we're fixing this!

So with env, I generally want us to make sure there are 4 layers of env setting and all of them work cohesively:

  1. ENV variable when extending our published Dockerfile
  2. Using setEnvVars on the sandbox object returned by getSandbox
  3. Using setEnvVars on the session object returned by createSession
  4. Passing the env parameter on methods like exec, execStream, startProcess, etc.

And env is mainly critical for all the flows that involve the default session and the ones created explicitly using createSession, where we maintain persistent bash sessions. We inherit whatever is set using ENV on the Dockerfile via process.env when we create a shell session. And "intuitively", I think setEnvVars on the sandbox object (global) should possibly set the variables on all the sessions created explicitly, whereas setEnvVars on the session object (local) should only affect that particular bash session.

cwd is much easier to reason with as an option, and is definitely quite useful when working in the scope of either the default session or the manually created session, but sometimes developers just want to run a command elsewhere and don't want to do cd x, command_to_run then cd back_to_where_I_was, which can get annoying.

Copy link
Member

@ghostwriternr ghostwriternr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides these (commandWithEnv is the only blocking one though), I think we could do a couple of things more:

  1. Add setEnvVars to the ISandbox type. I just realised this is not there currently, but should be.
  2. In createSession, IF you think my earlier comment about setEnvVars behaviour on the global sandbox object vs the session object makes sense, we can merge this.envVars with the session-specific ones before creating a session from the session object.

agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 20, 2025
Updates documentation for exec(), execStream(), and startProcess() methods
to reflect new environment variable and working directory support added in
cloudflare/sandbox-sdk#204.

Changes:
- Add env and cwd parameters to exec() options
- Add env and cwd parameters to execStream() options
- Expand startProcess() documentation with all available options
- Add examples showing usage of new parameters

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
claude[bot]

This comment was marked as outdated.

Add support for environment variables and working directory in command execution.
claude[bot]

This comment was marked as outdated.

claude[bot]

This comment was marked as outdated.

claude[bot]

This comment was marked as outdated.

@claude

This comment was marked as outdated.

claude[bot]

This comment was marked as outdated.

claude[bot]

This comment was marked as outdated.

claude[bot]

This comment was marked as outdated.

@whoiskatrin whoiskatrin marked this pull request as ready for review November 20, 2025 16:40
agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 20, 2025
Document new per-command environment variable and working directory
options added to exec(), execStream(), and startProcess() methods.

Changes:
- Add env and cwd parameters to exec() and execStream() API docs
- Add practical examples showing per-command env variable scoping
- Add working directory override examples
- Update environment variables configuration guide
- Clarify that per-command options do not persist in session

Related PR: cloudflare/sandbox-sdk#204

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Copy link
Contributor

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

The shell injection via env var names is fixed (line 667-669). Value escaping looks secure.

However, I found a critical bug in the variable restoration logic at line 684:

cleanupLines.push(`    eval "export ${key}=$${prevVar}"`);

In bash, $$ is the process ID, not variable expansion. This line generates:

eval "export KEY=12345{prevVar}"  # Where 12345 is the PID

Should be:

cleanupLines.push(`    eval "export ${key}=${prevVar}"`);

Since prevVar already contains the shell-quoted value from printf '%q' on line 679, no extra escaping is needed.

Impact: Environment variable restoration will fail. The test at session.test.ts:192-214 should catch this, but I couldn't verify if tests actually pass (build system issues locally).

Additional Issues:

  • SDK tests (sandbox.test.ts:155-170) verify options are forwarded but don't test actual env/cwd behavior
  • Minor: Inconsistent use of env && vs env !== undefined (lines 661, 695) - works correctly but style inconsistency

Needs fixes before merge.

setupLines.push(` export ${key}='${escapedValue}'`);

cleanupLines.push(` if [ "$${hasVar}" = "1" ]; then`);
cleanupLines.push(` eval "export ${key}=$${prevVar}"`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRITICAL BUG: $$ in bash is the process ID, not variable expansion.

This generates:

eval "export KEY=12345__SANDBOX_PREV_suffix_0"

Where 12345 is the PID.

Fix:

cleanupLines.push(`    eval "export ${key}=${prevVar}"`);

Since line 679 uses printf '%q' which already shell-quotes the value, no extra escaping is needed.

expect(result.stdout.trim()).toBe("it's got 'quotes'");
});

it('should restore existing env vars with special characters', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test verifies restoration of env vars with special chars. It should fail with the current bug at session.ts:684 where $$ expands to PID instead of the variable.

Can you confirm this test actually passes in CI? If it does pass, there may be a subtle bash behavior I'm missing.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants