diff --git a/.changeset/exec-routed-ca-env.md b/.changeset/exec-routed-ca-env.md new file mode 100644 index 00000000..694f680c --- /dev/null +++ b/.changeset/exec-routed-ca-env.md @@ -0,0 +1,5 @@ +--- +"@cloudflare/computer": patch +--- + +Commands run with an `outbound` now get the egress CA's environment variables, so npm, pnpm and Node scripts trust the routed container's HTTPS instead of failing with `SELF_SIGNED_CERT_IN_CHAIN`. diff --git a/packages/computer/src/remote-exec/sandbox.test.ts b/packages/computer/src/remote-exec/sandbox.test.ts index e6e0711c..1e68795e 100644 --- a/packages/computer/src/remote-exec/sandbox.test.ts +++ b/packages/computer/src/remote-exec/sandbox.test.ts @@ -90,6 +90,7 @@ function interceptingContainer( trust: { exitCode: number; stderr: string } = { exitCode: 0, stderr: "" }, ) { const { container, log } = fakeContainer(false); + const execOptions: unknown[] = []; const intercepting = Object.assign(container, { interceptAllOutboundHttp: async (binding: Fetcher) => { log.push(`intercept http ${(binding as unknown as { name: string }).name}`); @@ -99,6 +100,7 @@ function interceptingContainer( }, exec: async (cmd: string[], options?: unknown) => { const script = cmd[2] === TRUST_CA_SCRIPT; + if (!script) execOptions.push(options); log.push(script ? `trust ${JSON.stringify(options)}` : `exec ${cmd.join(" ")}`); return script ? fakeProcess({ @@ -108,7 +110,7 @@ function interceptingContainer( : fakeProcess(); }, }); - return { container: intercepting, log }; + return { container: intercepting, log, execOptions }; } interface Egress { @@ -263,6 +265,23 @@ describe("ContainerExec (what ExecSandbox delegates to)", () => { ]); }); + it("gives each command routed through an egress the CA's env, over its own", async () => { + const { container, execOptions } = interceptingContainer(); + const target = egressing(container); + await target.exec(["a"], { env: { A: "1", SSL_CERT_FILE: "/mine" } }, EGRESS); + await target.exec(["b"], undefined, EGRESS); + expect(execOptions).toEqual([ + { env: { A: "1", ...CA_ENV }, user: "0:0" }, + { env: CA_ENV, user: "0:0" }, + ]); + }); + + it("leaves the env of a command with no egress alone", async () => { + const { container, execOptions } = interceptingContainer(); + await egressing(container).exec(["a"], { env: { A: "1" } }); + expect(execOptions).toEqual([{ env: { A: "1" }, user: "0:0" }]); + }); + it("lets the start options turn the internet on, and keeps their env under the CA's", async () => { const { container, log } = interceptingContainer(); const env = { A: "1", SSL_CERT_FILE: "/image/bundle.pem" }; diff --git a/packages/computer/src/remote-exec/sandbox.ts b/packages/computer/src/remote-exec/sandbox.ts index 3303762c..16b8ee9f 100644 --- a/packages/computer/src/remote-exec/sandbox.ts +++ b/packages/computer/src/remote-exec/sandbox.ts @@ -448,8 +448,13 @@ export class ContainerExec implements ExecTarget { await this.#admit(egress); try { // exec() waits for a container that's still starting. + const routed = this.#route?.kind === "egress"; process = await this.#get().exec(cmd, { ...options, + // The start's env reaches only the entrypoint, so a routed command needs the CA itself. + ...(routed + ? { env: { ...(options?.env as Record | undefined), ...CA_ENV } } + : {}), user: options?.user ?? this.#options.user ?? ROOT, }); } catch (error) {