CVE-2019-25158: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection') (CWE-78)
value: A vulnerability has been found in pedroetb tts-api up to 2.1.4 and classified as critical. This vulnerability affects the function onSpeechDone of the file app.js. The manipulation leads to os command injection. Upgrading to version 2.2.0 is able to address this issue. The patch is identified as 29d9c25415911ea2f8b6de247cb5c4607d13d434. It is recommended to upgrade the affected component. VDB-248278 is the identifier assigned to this vulnerability.
CWE description: The product constructs all or part of an OS command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended OS command when it is sent to a downstream component.
CVSS v3 base score: 9.8
Published: 2023-12-19T13:15Z
Repository: https://github.com/pedroetb/tts-api
Language: JavaScript
Files changed: app.js
Commit date: 2019-07-14 17:42:58 +0100
Commit message: Fix command injection vulnerability and other bugs

Change command execution approach, from 'child_process.exec' to
'child_process.spawn', removing the chance to send shell commands to
API. This increase code complexity, but fix a serious problem in return.

Improve logging, showing errors and interactions between child
processes.
Reduce google_speech and gtts verbosity.
Improve API error responses. Set charset to responses content.

Use 'gain' sox effect instead of 'overdrive'.
Add same sox effects used in google_speech to gtts.
Prepare gtts to accept a future option for slow reading.

Diff: diff --git a/app.js b/app.js
index eec8951..30a3bb7 100644
--- a/app.js
+++ b/app.js
@@ -18,12 +18,11 @@ server.set('view engine', 'pug')
 	.get('/', renderForm)
 	.post('/', processData)
 
 	.listen(port, function() {
 
-		var port = this.address().port;
-		console.log('Listening at port', port);
+		console.log('Listening at port', this.address().port);
 	});
 
 function renderForm(req, res) {
 
 	var formView = 'form';
@@ -31,21 +30,20 @@ function renderForm(req, res) {
 }
 
 function processData(req, res) {
 
 	var body = req.body,
-		exec = childProcess.exec,
-		cmd = getCmdWithArgs(body),
-		cbk = onSpeechDone.bind(this, {
+		cmdWithArgs = getCmdWithArgs(body) || {},
+		httpArgs = {
 			res: res,
 			fields: body
-		});
+		};
 
-	if (!cmd) {
-		cbk('Empty command generated');
+	if (cmdWithArgs instanceof Array) {
+		runSpeechProcessChain(cmdWithArgs, httpArgs);
 	} else {
-		exec(cmd, cbk);
+		runLastSpeechProcess(cmdWithArgs, httpArgs);
 	}
 }
 
 function getCmdWithArgs(fields) {
 
@@ -58,73 +56,223 @@ function getCmdWithArgs(fields) {
 	} else if (voice === 'festival') {
 		return getFestivalCmdWithArgs(fields);
 	} else if (voice === 'espeak') {
 		return getEspeakCmdWithArgs(fields);
 	}
-
-	return '';
 }
 
 function getGoogleSpeechCmdWithArgs(fields) {
 
 	var text = fields.textToSpeech,
 		language = fields.language,
-		speed = fields.speed,
-
-		cmd = 'google_speech' + ' -l ' + language + ' \"' + text + '\"' + ' -e overdrive 10 speed ' + speed;
-
-	return cmd;
+		speed = fields.speed;
+
+	return {
+		cmd: 'google_speech',
+		args: [
+			'-v', 'warning',
+			'-l', language,
+			text,
+			'-e',
+			'gain', '4',
+			'speed', speed
+		]
+	};
 }
 
 function getGttsCmdWithArgs(fields) {
 
 	var text = fields.textToSpeech,
 		language = fields.language,
 		speed = fields.speed,
-		speedParam = speed ? ' -s' : '',
-
-		cmd = 'gtts-cli' + ' -l ' + language + speedParam + ' --nocheck \"' + text + '\"' + ' | play -t mp3 -';
-
-	return cmd;
+		slowSpeed = fields.slowSpeed ? '-s' : '';
+
+	return [{
+		cmd: 'gtts-cli',
+		args: [
+			'-l', language,
+			'--nocheck',
+			slowSpeed,
+			text
+		]
+	},{
+		cmd: 'play',
+		args: [
+			'-q',
+			'-t', 'mp3',
+			'-',
+			'gain', '4',
+			'speed', speed
+		]
+	}];
 }
 
 function getFestivalCmdWithArgs(fields) {
 
 	var text = fields.textToSpeech,
-		language = fields.language,
-
-		cmd = 'echo "' + text + '" | festival' + ' --tts --heap 1000000 --language ' + language;
-
-	return cmd;
+		language = fields.language;
+
+	return [{
+		cmd: 'echo',
+		args: [
+			text
+		]
+	},{
+		cmd: 'festival',
+		args: [
+			'--tts',
+			'--language', language,
+			'--heap', '1000000'
+		]
+	}];
 }
 
 function getEspeakCmdWithArgs(fields) {
 
 	var text = fields.textToSpeech,
 		language = fields.language,
 		voiceCode = '+f4',
+		voice = language + voiceCode,
 		speed = Math.floor(fields.speed * 150),
-		pitch = '70',
+		pitch = '70';
+
+	return {
+		cmd: 'espeak',
+		args: [
+			'-v', voice,
+			'-s', speed,
+			'-p', pitch,
+			text
+		]
+	};
+}
+
+function runLastSpeechProcess(cmdWithArgs, httpArgs) {
+
+	var speechProcess = runSpeechProcess(cmdWithArgs);
 
-		cmd = 'espeak' + ' -v' + language + voiceCode + ' -s ' + speed + ' -p ' + pitch + ' \"' + text + '\"';
+	speechProcess.on('error', onLastSpeechError.bind(this, httpArgs));
+	speechProcess.on('close', onLastSpeechClose);
+	speechProcess.on('exit', onLastSpeechExit.bind(this, httpArgs));
 
-	return cmd;
+	return speechProcess;
 }
 
-function onSpeechDone(args, err, stdout, stderr) {
+function runSpeechProcess(cmdWithArgs) {
 
-	var res = args.res,
-		fields = args.fields;
+	var newProcess = childProcess.spawn(cmdWithArgs.cmd, cmdWithArgs.args);
+
+	newProcess.stderr.on('data', onSpeechStandardError);
+
+	return newProcess;
+}
+
+function onSpeechStandardError(buffer) {
+
+	console.error('[stderr]:', buffer.toString('utf8'));
+}
+
+function runSpeechProcessChain(cmdWithArgs, httpArgs) {
+
+	var speechProcs = {};
+
+	for (var i = 0; i < cmdWithArgs.length; i++) {
+		if (i !== cmdWithArgs.length - 1) {
+			var getNextProcessCbk = getNextSpeechProcess.bind(speechProcs, i + 1);
+			speechProcs[i] = runIntermediateSpeechProcess(cmdWithArgs[i], getNextProcessCbk);
+		} else {
+			speechProcs[i] = runLastSpeechProcess(cmdWithArgs[i], httpArgs);
+		}
+	}
+}
+
+function runIntermediateSpeechProcess(cmdWithArgs, procArgs) {
+
+	var speechProcess = runSpeechProcess(cmdWithArgs);
+
+	speechProcess.stdout.on('data', onIntermediateSpeechStandardOutput.bind(this, procArgs));
+	speechProcess.on('error', onIntermediateSpeechError);
+	speechProcess.on('close', onIntermediateSpeechClose.bind(this, procArgs));
+
+	return speechProcess;
+}
+
+function getNextSpeechProcess(nextIndex) {
+
+	return this[nextIndex];
+}
+
+function onIntermediateSpeechStandardOutput(getNextProc, data) {
+
+	var nextSpeechProcess = getNextProc(),
+		inputStream = nextSpeechProcess.stdin;
+
+	if (inputStream.writable) {
+		inputStream.write(data);
+	}
+}
+
+function onIntermediateSpeechClose(getNextProc, code) {
+
+	var nextSpeechProcess = getNextProc(),
+		inputStream = nextSpeechProcess.stdin;
+
+	if (code) {
+		console.error('[intermediate exit code]:', code);
+	}
+
+	inputStream.end();
+}
+
+function onIntermediateSpeechError(err) {
+
+	console.error('[intermediate error]:', util.inspect(err));
+}
+
+function onLastSpeechClose(code) {
+
+	if (code) {
+		console.error('[exit code]:', code);
+	}
+}
+
+function onLastSpeechExit(args, err) {
+
+	var res = args.res;
 
 	if (!err) {
 		res.end();
-		return;
+	} else {
+		handleSpeechError(args, err);
 	}
+}
+
+function onLastSpeechError(args, err) {
+
+	handleSpeechError(args, err);
+}
+
+function handleSpeechError(args, err) {
+
+	var res = args.res,
+		fields = args.fields,
+		errorHeaderMessage = '----[error]----',
+		dataHeaderMessage = '-----[data]-----',
+		inspectedError = util.inspect(err),
+		inspectedFields = util.inspect(fields);
 
 	res.writeHead(500, {
-		'content-type': 'text/plain'
+		'Content-Type': 'text/plain; charset=utf-8'
 	});
-	res.write('error:\n\n');
-	res.write(util.inspect(err) + '\n\n');
-	res.write('received data:\n\n');
-	res.end(util.inspect(fields));
+
+	res.write(errorHeaderMessage + '\n');
+	res.write(inspectedError + '\n');
+	res.write(dataHeaderMessage + '\n');
+	res.write(inspectedFields + '\n');
+
+	res.end();
+
+	console.error(errorHeaderMessage);
+	console.error(inspectedError);
+	console.error(dataHeaderMessage);
+	console.error(inspectedFields);
 }

Vulnerable code: 		var port = this.address().port;
		console.log('Listening at port', port);
		exec = childProcess.exec,
		cmd = getCmdWithArgs(body),
		cbk = onSpeechDone.bind(this, {
		});
	if (!cmd) {
		cbk('Empty command generated');
		exec(cmd, cbk);

	return '';
		speed = fields.speed,

		cmd = 'google_speech' + ' -l ' + language + ' \"' + text + '\"' + ' -e overdrive 10 speed ' + speed;

	return cmd;
		speedParam = speed ? ' -s' : '',

		cmd = 'gtts-cli' + ' -l ' + language + speedParam + ' --nocheck \"' + text + '\"' + ' | play -t mp3 -';

	return cmd;
		language = fields.language,

		cmd = 'echo "' + text + '" | festival' + ' --tts --heap 1000000 --language ' + language;

	return cmd;
		pitch = '70',
		cmd = 'espeak' + ' -v' + language + voiceCode + ' -s ' + speed + ' -p ' + pitch + ' \"' + text + '\"';
	return cmd;
function onSpeechDone(args, err, stdout, stderr) {
	var res = args.res,
		fields = args.fields;
		return;
		'content-type': 'text/plain'
	res.write('error:\n\n');
	res.write(util.inspect(err) + '\n\n');
	res.write('received data:\n\n');
	res.end(util.inspect(fields));
Fixed code: 		console.log('Listening at port', this.address().port);
		cmdWithArgs = getCmdWithArgs(body) || {},
		httpArgs = {
		};
	if (cmdWithArgs instanceof Array) {
		runSpeechProcessChain(cmdWithArgs, httpArgs);
		runLastSpeechProcess(cmdWithArgs, httpArgs);
		speed = fields.speed;

	return {
		cmd: 'google_speech',
		args: [
			'-v', 'warning',
			'-l', language,
			text,
			'-e',
			'gain', '4',
			'speed', speed
		]
	};
		slowSpeed = fields.slowSpeed ? '-s' : '';

	return [{
		cmd: 'gtts-cli',
		args: [
			'-l', language,
			'--nocheck',
			slowSpeed,
			text
		]
	},{
		cmd: 'play',
		args: [
			'-q',
			'-t', 'mp3',
			'-',
			'gain', '4',
			'speed', speed
		]
	}];
		language = fields.language;

	return [{
		cmd: 'echo',
		args: [
			text
		]
	},{
		cmd: 'festival',
		args: [
			'--tts',
			'--language', language,
			'--heap', '1000000'
		]
	}];
		voice = language + voiceCode,
		pitch = '70';

	return {
		cmd: 'espeak',
		args: [
			'-v', voice,
			'-s', speed,
			'-p', pitch,
			text
		]
	};
}

function runLastSpeechProcess(cmdWithArgs, httpArgs) {

	var speechProcess = runSpeechProcess(cmdWithArgs);
	speechProcess.on('error', onLastSpeechError.bind(this, httpArgs));
	speechProcess.on('close', onLastSpeechClose);
	speechProcess.on('exit', onLastSpeechExit.bind(this, httpArgs));
	return speechProcess;
function runSpeechProcess(cmdWithArgs) {
	var newProcess = childProcess.spawn(cmdWithArgs.cmd, cmdWithArgs.args);

	newProcess.stderr.on('data', onSpeechStandardError);

	return newProcess;
}

function onSpeechStandardError(buffer) {

	console.error('[stderr]:', buffer.toString('utf8'));
}

function runSpeechProcessChain(cmdWithArgs, httpArgs) {

	var speechProcs = {};

	for (var i = 0; i < cmdWithArgs.length; i++) {
		if (i !== cmdWithArgs.length - 1) {
			var getNextProcessCbk = getNextSpeechProcess.bind(speechProcs, i + 1);
			speechProcs[i] = runIntermediateSpeechProcess(cmdWithArgs[i], getNextProcessCbk);
		} else {
			speechProcs[i] = runLastSpeechProcess(cmdWithArgs[i], httpArgs);
		}
	}
}

function runIntermediateSpeechProcess(cmdWithArgs, procArgs) {

	var speechProcess = runSpeechProcess(cmdWithArgs);

	speechProcess.stdout.on('data', onIntermediateSpeechStandardOutput.bind(this, procArgs));
	speechProcess.on('error', onIntermediateSpeechError);
	speechProcess.on('close', onIntermediateSpeechClose.bind(this, procArgs));

	return speechProcess;
}

function getNextSpeechProcess(nextIndex) {

	return this[nextIndex];
}

function onIntermediateSpeechStandardOutput(getNextProc, data) {

	var nextSpeechProcess = getNextProc(),
		inputStream = nextSpeechProcess.stdin;

	if (inputStream.writable) {
		inputStream.write(data);
	}
}

function onIntermediateSpeechClose(getNextProc, code) {

	var nextSpeechProcess = getNextProc(),
		inputStream = nextSpeechProcess.stdin;

	if (code) {
		console.error('[intermediate exit code]:', code);
	}

	inputStream.end();
}

function onIntermediateSpeechError(err) {

	console.error('[intermediate error]:', util.inspect(err));
}

function onLastSpeechClose(code) {

	if (code) {
		console.error('[exit code]:', code);
	}
}

function onLastSpeechExit(args, err) {

	var res = args.res;
	} else {
		handleSpeechError(args, err);
}

function onLastSpeechError(args, err) {

	handleSpeechError(args, err);
}

function handleSpeechError(args, err) {

	var res = args.res,
		fields = args.fields,
		errorHeaderMessage = '----[error]----',
		dataHeaderMessage = '-----[data]-----',
		inspectedError = util.inspect(err),
		inspectedFields = util.inspect(fields);
		'Content-Type': 'text/plain; charset=utf-8'

	res.write(errorHeaderMessage + '\n');
	res.write(inspectedError + '\n');
	res.write(dataHeaderMessage + '\n');
	res.write(inspectedFields + '\n');

	res.end();

	console.error(errorHeaderMessage);
	console.error(inspectedError);
	console.error(dataHeaderMessage);
	console.error(inspectedFields);