forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Harden console functions (elastic#171367)
## Summary This PR overrides console functions only in production, in order to sanitize input parameters for any potential calls made to the global console from Kibana's dependencies. This initial implementation overrides the `debug`, `error`, `info`, `log`, `trace`, and `warn` functions, and only sanitizes string inputs. Future updates may expand this to handle other types, or strings nested in objects. The unmodified console methods are now exposed internally in Kibana as `unsafeConsole`. Where needed for formatting (log appenders, core logger), calls to the global console have been replaced by `unsafeConsole`. This PR also adds a new es linting rule to disallow calls to `unsafeConsole` unless `eslint-disable-next-line @kbn/eslint/no_unsafe_console` is used. ### Testing Not sure how we could test this. The overrides are only enabled when running in a true production environment (e.g. docker) by checking `process.env.NODE_ENV`. I was able to manually test by adding additional console output denoting when the console functions were being overriden or not. Closes elastic/kibana-team#664 Closes elastic#176340 --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
758f1a4
commit 6d4f784
Showing
34 changed files
with
379 additions
and
47 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/kbn-eslint-plugin-eslint/rules/no_unsafe_console.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
const tsEstree = require('@typescript-eslint/typescript-estree'); | ||
const esTypes = tsEstree.AST_NODE_TYPES; | ||
|
||
/** @typedef {import("eslint").Rule.RuleModule} Rule */ | ||
/** @typedef {import("@typescript-eslint/typescript-estree").TSESTree.Node} Node */ | ||
/** @typedef {import("@typescript-eslint/typescript-estree").TSESTree.CallExpression} CallExpression */ | ||
/** @typedef {import("@typescript-eslint/typescript-estree").TSESTree.CallExpression} VariableDeclarator */ | ||
|
||
const ERROR_MSG = 'Unexpected unsafeConsole statement.'; | ||
|
||
/** | ||
* @param {CallExpression} node | ||
*/ | ||
const isUnsafeConsoleCall = (node) => { | ||
return ( | ||
node.callee.type === esTypes.MemberExpression && | ||
node.callee.property.type === esTypes.Identifier && | ||
node.callee.object.name === 'unsafeConsole' && | ||
node.callee.property.name | ||
); | ||
}; | ||
|
||
/** | ||
* @param {VariableDeclarator} node | ||
*/ | ||
const isUnsafeConsoleObjectPatternDeclarator = (node) => { | ||
return ( | ||
node.id.type === esTypes.ObjectPattern && | ||
node.init && | ||
node.init.type === esTypes.Identifier && | ||
node.init.name === 'unsafeConsole' | ||
); | ||
}; | ||
|
||
/** @type {Rule} */ | ||
module.exports = { | ||
meta: { | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
create: (context) => ({ | ||
CallExpression(_) { | ||
const node = /** @type {CallExpression} */ (_); | ||
|
||
if (isUnsafeConsoleCall(node)) { | ||
context.report({ | ||
message: ERROR_MSG, | ||
loc: node.callee.loc, | ||
}); | ||
} | ||
}, | ||
VariableDeclarator(_) { | ||
const node = /** @type {VariableDeclarator} */ (_); | ||
|
||
if (isUnsafeConsoleObjectPatternDeclarator(node)) { | ||
context.report({ | ||
message: ERROR_MSG, | ||
loc: node.init.loc, | ||
}); | ||
} | ||
}, | ||
}), | ||
}; |
Oops, something went wrong.