From 1f0ba99f0fb7a29ef6ef3023de7cbfa575bed45a Mon Sep 17 00:00:00 2001 From: Scott Prutton Date: Mon, 29 Jul 2024 14:44:46 -0400 Subject: [PATCH] feat: dynamic stdin for langjs --- bin/lang-js/src/sandbox/exec.ts | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/bin/lang-js/src/sandbox/exec.ts b/bin/lang-js/src/sandbox/exec.ts index 6170acd17a..b323e725ee 100644 --- a/bin/lang-js/src/sandbox/exec.ts +++ b/bin/lang-js/src/sandbox/exec.ts @@ -23,6 +23,24 @@ const debug = Debug("langJs:siExec"); export type SiExecResult = ExecaReturnValue; +const defaultOptions: Options = { + all: true, + buffer: true, + reject: false, + stdin: 'ignore', +}; + +/** + * Merges default options with user-provided options and sets stdin to 'pipe' if input is provided. + */ +function mergedOptions(userOptions?: Options): Options { + return { + ...defaultOptions, + ...userOptions, + stdin: userOptions?.input ? 'pipe' : 'ignore', + }; +} + // Note(paulo): This is highly dangerous as it bypasses the sandbox // We also are bypassing the VM timeout by using async (NodeVM doesn't have timeout, but it seems we can't await without it) // @@ -39,8 +57,8 @@ export const makeExec = (executionId: string) => { async function waitUntilEnd( execaFile: string, execaArgs?: readonly string[], + execaOptions?: Options, ): Promise { - const execaOptions: Options = { stdin: 'ignore' }; debug( `running command; executionId="${executionId}"; cmd="${execaFile} ${execaArgs ?.map((a) => `'${a}'`) @@ -59,12 +77,11 @@ export const makeExec = (executionId: string) => { }), ); - const child = await execa(execaFile, execaArgs, { - all: true, - buffer: true, - reject: false, - ...execaOptions, - }); + const child = await execa( + execaFile, + execaArgs, + mergedOptions(execaOptions), + ); return child; } @@ -84,6 +101,7 @@ export const makeExec = (executionId: string) => { const c = await waitUntilEnd( options.cmd, options.args, + options.execaOptions, ); // Update the count of how many attempts we have made deadlineCount += 1;