CVE-2019-25158 (JavaScript, published 2023-12-19T13:15Z)
Description: 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.
Severity: nan (CVSS v3 base score: 9.8)
Weakness: CWE-78 \u2014 Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
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.
Repository: https://github.com/pedroetb/tts-api
Files changed:
app.js
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.


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);