Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reporting] Sanitize 409 error log message #42495

Merged
merged 4 commits into from
Aug 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion x-pack/legacy/plugins/reporting/server/lib/esqueue/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {};
Expand All @@ -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;
}

Expand Down