#622

ECW template build: WORKDIR is created root-owned and relative WORKDIR ignores the previous one

OpenRGB-loopopened 1mo ago

Problem

ECW translates the E2B WORKDIR step into a root mkdir -p <arg> and keeps <arg> as the working directory for later steps (crates/libs/api/src/routes/e2b/template_build/translate.rs, WORKDIR arm). Upstream E2B's WORKDIR (packages/orchestrator/pkg/template/build/commands/workdir.go in e2b-dev/infra) does two more things:

  1. It chowns the newly created directories to the current template user.
  2. It resolves a relative argument against the previous workdir (or /).

Two user-visible differences follow:

  • Root-owned workdir. With Template().fromTemplate('code-interpreter-v1').setWorkdir('/home/user/app').runCmd('touch x'), or .setUser('user').setWorkdir('/app').runCmd(...), the directory is owned by root. The next RUN, executed as user, fails with Permission denied. The same template builds on E2B.
  • Relative workdir. .setWorkdir('/srv').setWorkdir('data') creates and uses data relative to the SSH login user's home (/root/data for root) instead of /srv/data. fromDockerfile forwards Dockerfile WORKDIR values unchanged, so WORKDIR /srv followed by WORKDIR data hits this too. A later COPY with a relative destination becomes data/<dest>, which build-mgr rejects as a relative copy destination.

Reproduction

The payloads come from the real e2b@2.29.1 JS SDK (Template.toJSON). I ported the translator's RUN/WORKDIR/USER handling and the upstream workdir.go behaviour, then ran both step sequences in a disposable Debian container as root and user. ssh -l was emulated with runuser -l. This was not a SPACE build VM.

CaseSPACE translationE2B semantics
code-interpreter-v1, WORKDIR /home/user/app, RUN touch xdir owner root, Permission denied, exit 1dir owner user, exit 0
USER user, WORKDIR /app, RUN echo {} > package.jsonPermission denied, exit 1exit 0
WORKDIR /srv, WORKDIR data, RUN pwd/root/data/srv/data

I also added two translator unit tests to template_build/translate/tests.rs (same pattern as code_runtime_base_runs_implicit_steps_as_user_in_home, lazy pool, no database). Both fail on main at 57c7c0c4 with Rust 1.98.0; the other 45 template_build::translate tests pass:

---- workdir_created_for_non_root_user_is_owned_by_that_user ----
expected a root step that chowns the new workdir to user:user, got [
    Run { command: "mkdir -p '/home/user/app'", user: Some("root") },
]

---- relative_workdir_resolves_against_previous_workdir ----
  left: "cd 'data' && {\npwd\n}"
 right: "cd '/srv/data' && {\npwd\n}"

Possible approach

In the WORKDIR arm, resolve a relative argument against the current workdir (or /). Then emit a root step that creates only the missing suffix and chown -Rs the first new directory to the current user, as workdir.go does. Store the resolved absolute path as the workdir. The two tests above would cover both cases. I'm happy to open a PR if this scope works for you.

Severity

Medium (self-assessed). E2B-compatible templates that set a workdir for a non-root user fail to build. Relative workdirs put files in the wrong place without any error. Workarounds exist: use absolute paths and add your own chown RUN step.

This issue was drafted with the help of an AI agent. The reproduction above was run, and its limits are stated.

Activity

3
NancyKwongPerplexity1mo ago

Thanks for opening this issue! A SPACE maintainer will take a look.

CONTRIBUTING.md explains how we review outside contributions.

github-actions[bot]23d ago

Thanks, this is useful. We are looking into it and will handle the fix ourselves, so nothing more is needed from you for now.

github-actions[bot]23d ago

Thanks, the scope looks good and a pull request is welcome. Please include a test that fails without the fix, and put "Fixes #622" in the pull request description so it goes straight to review. Agreeing on scope does not guarantee the pull request will be merged.