From c9e2a221389914e559533f8ffb0f308c56cf20a7 Mon Sep 17 00:00:00 2001 From: Tim Sullivan Date: Fri, 2 Aug 2019 17:39:16 -0700 Subject: [PATCH] [Reporting] Sanitize 409 error log message (#42495) * [Reporting] Sanitize 409 error log message * [Reporting] Sanitize 409 error log message * feedback changes --- .../reporting/server/lib/esqueue/worker.js | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/x-pack/legacy/plugins/reporting/server/lib/esqueue/worker.js b/x-pack/legacy/plugins/reporting/server/lib/esqueue/worker.js index 25837e96650b0b..eb9cabc3a8e364 100644 --- a/x-pack/legacy/plugins/reporting/server/lib/esqueue/worker.js +++ b/x-pack/legacy/plugins/reporting/server/lib/esqueue/worker.js @@ -21,6 +21,10 @@ function formatJobObject(job) { }; } +const MAX_PARTIAL_ERROR_LENGTH = 1000; // 1000 of beginning, 1000 of end +const ERROR_PARTIAL_SEPARATOR = '...'; +const MAX_ERROR_LENGTH = (MAX_PARTIAL_ERROR_LENGTH * 2) + ERROR_PARTIAL_SEPARATOR.length; + function getLogger(opts, id, logLevel) { return (msg, err) => { const logger = opts.logger || function () {}; @@ -29,7 +33,26 @@ function getLogger(opts, id, logLevel) { const tags = ['worker', logLevel]; if (err) { - logger(`${message}: ${err.stack ? err.stack : err }`, tags); + // The error message string could be very long if it contains the request + // body of a request that was too large for Elasticsearch. + // This takes a partial version of the error message without scanning + // every character of the string, which would block Node. + const errString = `${message}: ${err.stack ? err.stack : err}`; + const errLength = errString.length; + const subStr = String.prototype.substring.bind(errString); + if (errLength > MAX_ERROR_LENGTH) { + const partialError = + subStr(0, MAX_PARTIAL_ERROR_LENGTH) + + ERROR_PARTIAL_SEPARATOR + + subStr(errLength - MAX_PARTIAL_ERROR_LENGTH); + + logger(partialError, tags); + logger( + `A partial version of the entire error message was logged. ` + + `The entire error message length is: ${errLength} characters.`, + tags + ); + } return; }