Tengo un problema con mi informe checkmarx en mi código node.js. Checkmarx informa de la siguiente vulnerabilidad:
The application's Promise embeds untrusted data in the generated output with write, at line 53 of lib\utils\request.utils.js. This untrusted data is embedded straight into the output without proper sanitization or encoding, enabling an attacker to inject malicious code into the output. The attacker would be able to alter the returned web page by simply providing modified data in the user input split, which is read by the validateClientToken method at line 98 of middleware\authorization.service.js. This input then flows through the code straight to the output web page, without sanitization. This can enable a Reflected Cross-Site Scripting (XSS) attack.Se trata de clientToken, que el usuario proporciona en el encabezado de autorización y finalmente se pasa como un cuerpo para realizar una solicitud a otro servicio.
Aquí hay un fragmento de mi middleware, donde clientToken aparece primero y checkmarx informa sobre la vulnerabilidad:
function validateClientToken(req) { if (_.isEmpty(req.headers.authorization)) { throw ExceptionBuilder.authException('Required Authorization header is missing') .build(); } const clientToken = req.headers.authorization.split(' ')[1]; if (_.isEmpty(clientToken)) { throw ExceptionBuilder.authException('Provided bearer token is empty') .build(); } return clientToken; }Y este es el lugar donde se pasa clientToken para hacer otra llamada.
function sendRequest(requestOptions, body = null) { return new Promise((resolve, reject) => { const isPostWithData = requestOptions && requestOptions.method === 'POST' && body !== null; if (isPostWithData && (!requestOptions.headers || !requestOptions.headers['Content-Length'])) { requestOptions = Object.assign({}, requestOptions, { headers: Object.assign({}, requestOptions.headers, { 'Content-Length': Buffer.byteLength(body), }), }); } let response = ''; const request = HTTPS.request(requestOptions, (res) => { res.on('data', (chunk) => { response += chunk; }); res.on('end', () => { resolve(response); }); }); request.on('error', (error) => { reject(error); }); if (isPostWithData) { request.write(body); } request.end(); });}
Traté de usar validaciones (usando expresiones regulares si clientToken contiene caracteres prohibidos) y desinfección (reemplazando caracteres prohibidos con ''), pero sin éxito. Checkmarx aún informa vulnerabilidad. Traté de hacer esto tanto en el middleware, donde clientToken se captura primero y directamente antes de llamar a otro servicio.
¿Alguna idea de lo que está mal?