From 4691bde42935582a217ce1453d4c8a495ed4af86 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 25 Mar 2022 08:17:13 -0400 Subject: [PATCH] fix: `Deno.run` - do not modify user provided `cmd` array (#14109) --- cli/dts/lib.deno.ns.d.ts | 2 +- cli/tests/unit/process_test.ts | 7 ++++++- runtime/js/40_process.js | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cli/dts/lib.deno.ns.d.ts b/cli/dts/lib.deno.ns.d.ts index 02f5146735..d55ac231eb 100644 --- a/cli/dts/lib.deno.ns.d.ts +++ b/cli/dts/lib.deno.ns.d.ts @@ -2398,7 +2398,7 @@ declare namespace Deno { export interface RunOptions { /** Arguments to pass. Note, the first element needs to be a path to the * binary */ - cmd: string[] | [URL, ...string[]]; + cmd: readonly string[] | [URL, ...string[]]; cwd?: string; env?: { [key: string]: string; diff --git a/cli/tests/unit/process_test.ts b/cli/tests/unit/process_test.ts index 3ef19a8799..e4e2cc3c52 100644 --- a/cli/tests/unit/process_test.ts +++ b/cli/tests/unit/process_test.ts @@ -21,7 +21,12 @@ Deno.test( { permissions: { run: true, read: true } }, async function runSuccess() { const p = Deno.run({ - cmd: [Deno.execPath(), "eval", "console.log('hello world')"], + // freeze the array to ensure it's not modified + cmd: Object.freeze([ + Deno.execPath(), + "eval", + "console.log('hello world')", + ]), stdout: "piped", stderr: "null", }); diff --git a/runtime/js/40_process.js b/runtime/js/40_process.js index c33ce1c04d..545c6c6d6e 100644 --- a/runtime/js/40_process.js +++ b/runtime/js/40_process.js @@ -9,6 +9,7 @@ const { assert } = window.__bootstrap.infra; const { ArrayPrototypeMap, + ArrayPrototypeSlice, TypeError, isNaN, ObjectEntries, @@ -110,7 +111,7 @@ stdin = "inherit", }) { if (cmd[0] != null) { - cmd[0] = pathFromURL(cmd[0]); + cmd = [pathFromURL(cmd[0]), ...ArrayPrototypeSlice(cmd, 1)]; } const res = opRun({ cmd: ArrayPrototypeMap(cmd, String),