From 9a865aa4717c3b158108368249442e1e865ca9f8 Mon Sep 17 00:00:00 2001 From: Ashley Trinh Date: Fri, 7 Apr 2023 14:05:37 -0700 Subject: [PATCH 1/2] replace execSync with spawnSync in seleniumManager.js Since execSync does not escape shell metacharacters, attempting to execute a command with a space in it will fail. This is the case when the path to selenium-manager contains a space. This commit replaces execSync with spawnSync, which does escape shell metacharacters. Fixes #11649 --- .../node/selenium-webdriver/common/seleniumManager.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/javascript/node/selenium-webdriver/common/seleniumManager.js b/javascript/node/selenium-webdriver/common/seleniumManager.js index ecd0333cd27fa..87e88e53e4a93 100644 --- a/javascript/node/selenium-webdriver/common/seleniumManager.js +++ b/javascript/node/selenium-webdriver/common/seleniumManager.js @@ -24,7 +24,7 @@ const { platform } = require('process') const path = require('path') const fs = require('fs') -const execSync = require('child_process').execSync +const spawnSync = require('child_process').spawnSync /** * currently supported browsers for selenium-manager @@ -73,13 +73,13 @@ function driverLocation(browser) { let output try { - output = JSON.parse(execSync(args.join(' ')).toString()) + output = JSON.parse(spawnSync(args.join(' ')).stdout.toString()) } catch (e) { let error try { - error = JSON.parse(e.stdout.toString()).result.message + error = JSON.parse(output.stdout.toString()).result.message } catch (e) { - error = e.toString() + error = output.error.toString() } throw new Error(`Error executing command with ${args}: ${error}`) } From 792944cd30d8a4cb95ae1e96570478e4360909ad Mon Sep 17 00:00:00 2001 From: Diego Molina Date: Thu, 20 Apr 2023 13:46:49 +0200 Subject: [PATCH 2/2] [javascript] Refine usage of spawnSync --- .../common/seleniumManager.js | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/javascript/node/selenium-webdriver/common/seleniumManager.js b/javascript/node/selenium-webdriver/common/seleniumManager.js index 4333161ab5857..7e253b6277f3f 100644 --- a/javascript/node/selenium-webdriver/common/seleniumManager.js +++ b/javascript/node/selenium-webdriver/common/seleniumManager.js @@ -53,7 +53,6 @@ function getBinary() { } else { seleniumManagerBasePath = path.join(__dirname, '..', '/bin') } - console.log(seleniumManagerBasePath) const filePath = path.join(seleniumManagerBasePath, directory, file) @@ -77,7 +76,7 @@ function driverLocation(options) { ) } - let args = [getBinary(), '--browser', options.getBrowserName(), '--output', 'json'] + let args = ['--browser', options.getBrowserName(), '--output', 'json'] if (options.getBrowserVersion() && options.getBrowserVersion() !== "") { args.push("--browser-version", options.getBrowserVersion()) @@ -89,22 +88,30 @@ function driverLocation(options) { args.push("--browser-path", '"' + vendorOptions.binary + '"') } + const smBinary = getBinary() + const spawnResult = spawnSync(smBinary, args) let output - try { - output = JSON.parse(spawnSync(args.join(' ')).stdout.toString()) - } catch (e) { - let error - try { - error = JSON.parse(output.stdout.toString()).result.message - } catch (e) { - if (e instanceof SyntaxError) { - error = output.error.toString() - } else { - error = e.toString() + if (spawnResult.status) { + let errorMessage + if (spawnResult.stderr.toString()) { + errorMessage = spawnResult.stderr.toString() + } + if (spawnResult.stdout.toString()) { + try { + output = JSON.parse(spawnResult.stdout.toString()) + errorMessage = output.result.message + } catch (e) { + errorMessage = e.toString() } } - throw new Error(`Error executing command with ${args}: ${error}`) + throw new Error(`Error executing command for ${smBinary} with ${args}: ${errorMessage}`) } + try { + output = JSON.parse(spawnResult.stdout.toString()) + } catch (e) { + throw new Error(`Error executing command for ${smBinary} with ${args}: ${e.toString()}`) + } + for (const key in output.logs) { if (output.logs[key].level === 'WARN') {