CVE-2019-25158 (published 2023-12-19T13:15Z)

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.

CVSS v3 base score: 9.8, CRITICAL (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-78
cwe_name: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
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.
extended_description: 
		   This weakness can lead to a vulnerability in environments in which the attacker does not have direct access to the operating system, such as in web applications. Alternately, if the weakness occurs in a privileged program, it could allow the attacker to specify commands that normally would not be accessible, or to call alternate commands with privileges that the attacker does not have. The problem is exacerbated if the compromised process does not follow the principle of least privilege, because the attacker-controlled commands may run with special system privileges that increases the amount of damage.
            There are at least two subtypes of OS command injection:
               
				 The application intends to execute a single, fixed program that is under its own control. It intends to use externally-supplied inputs as arguments to that program. For example, the program might use system("nslookup [HOSTNAME]") to run nslookup and allow the user to supply a HOSTNAME, which is used as an argument. Attackers cannot prevent nslookup from executing. However, if the program does not remove command separators from the HOSTNAME argument, attackers could place the separators into the arguments, which allows them to execute their own program after nslookup has finished executing.
				 The application accepts an input that it uses to fully select which program to run, as well as which commands to use. The application simply redirects this entire command to the operating system. For example, the program might use "exec([COMMAND])" to execute the [COMMAND] that was supplied by the user. If the COMMAND is under attacker control, then the attacker can execute arbitrary commands or programs. If the command is being executed using functions like exec() and CreateProcess(), the attacker might not be able to combine multiple commands together in the same line.
               
            From a weakness standpoint, these variants represent distinct programmer errors. In the first variant, the programmer clearly intends that input from untrusted parties will be part of the arguments in the command to be executed. In the second variant, the programmer does not intend for the command to be accessible to any untrusted party, but the programmer probably has not accounted for alternate ways in which malicious attackers can provide input.
         
         

Fix information:
repo_url: https://github.com/pedroetb/tts-api
commit_details: repo_url: https://github.com/pedroetb/tts-api
author: Pedro Trujillo
author_date: 2019-07-14 17:42:58+01:00
msg: 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.
repository: repo_url: https://github.com/pedroetb/tts-api
repo_name: pedroetb/tts-api
description: Text to speech REST API for multiple TTS engines
repo_language: JavaScript
owner: pedroetb
file_changes: filename: app.js
change_type: ModificationType.MODIFY
diff: @@ -20,8 +20,7 @@ server.set('view engine', 'pug')
 
 	.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) {
@@ -33,17 +32,16 @@ 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);
 	}
 }
 
@@ -60,19 +58,25 @@ function getCmdWithArgs(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) {
@@ -80,21 +84,46 @@ 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) {
@@ -102,29 +131,148 @@ 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);
 }

programming_language: JavaScript