Ensure process exits if a process warning is emitted#59651
Ensure process exits if a process warning is emitted#59651watson merged 6 commits intoelastic:masterfrom
Conversation
|
Pinging @elastic/kibana-security (Team:Security) |
2a4d610 to
7ce6cb4
Compare
9cc5423 to
83702d8
Compare
src/dev/exit_on_warning.js
Outdated
| * under the License. | ||
| */ | ||
|
|
||
| const ignore = ['MaxListenersExceededWarning']; |
There was a problem hiding this comment.
I'm ignoring MaxListenersExceededWarning because one of our tests (src/cli/cluster/cluster_manager.test.ts) emitted this warning and I don't consider these a security risk.
We could consider "fixing" the test so it doesn't emit this warning, but it's not super important. However, if we did, it would probably improve the codebase as new warnings of this type would not sneak in as those are usually a result of poorly written code that can hide other bugs. So if it's easy to do we should consider it (I spent a few hours trying to fix it without any luck).
💚 Build SucceededHistory
To update your PR or re-run it, just comment with: |
| if (process.noProcessWarnings !== true) { | ||
| var ignore = ['MaxListenersExceededWarning']; | ||
|
|
||
| process.on('warning', function(warn) { |
There was a problem hiding this comment.
If I'm understanding correctly, we were previously crashing on any deprecation warnings, but now, we will crash on all warnings except MaxListenersExceededWarning (including any custom warnings emitted by dependencies or our own code). Is this intended?
There was a problem hiding this comment.
Previously we used the flags --trace-warnings --throw-deprecation when you started Kibana via yarn start and we used --throw-deprecation in CI. This meant that we would only crash on deprecation warnings and log a stack trace for all other types of warnings (MaxListenersExceededWarning belongs to the latter group).
There were a couple of drawbacks to this approach:
- If the deprecated API was called in a place enclosed in a try-catch (or inside of a Promise or an async/await context), the throw would be caught and the program would not crash.
- If you ran
./scripts/kibanadirectly instead of runningyarn start, you would not get these flags automatically. - If you ran any of our tests locally you would not get the standard CI flags. This meant something that might seem to pass locally would not pass in CI.
This PR changes this behavior by ensuring:
- Crash for all types of warnings - not only deprecation warnings (except
MaxListenersExceededWarning, though that's just because I didn't want to spend too much time getting that to work). - That we always crash - no matter if the offending code is surrounded by a try-catch (etc).
- That you always get the same behavior whether you run
yarn startor./scripts/kibana. - That you always get the same behavior in CI or if you run individual tests locally.
Update: I'll add this to the PR description
There was a problem hiding this comment.
Thanks for explaining, that makes sense to me now
Crash Kibana in dev/CI if a process warning is detected. This does not influence how Kibana behaves in production when run via `./bin/kibana` as there the `--no-warnings` flag is used. We will detect this flag and as a result, disable this behavior. Previously we used the flags `--trace-warnings --throw-deprecation` when you started Kibana via `yarn start` and we used `--throw-deprecation` in CI. This meant that we would only crash on deprecation warnings and log a stack trace for all other types of warnings. There were a couple of drawbacks to this approach: 1. If the deprecated API was called in a place enclosed in a try-catch (or inside of a Promise or an async/await context), the throw would be caught and the program would not crash. 2. If you ran `./scripts/kibana` directly instead of running `yarn start`, you would not get these flags automatically. 3. If you ran any of our tests locally you would not get the standard CI flags. This meant something that might seem to pass locally would not pass in CI. This commit changes this behavior by ensuring: - That we always crash - no matter if the offending code is surrounded by a try-catch (etc). - That you always get the same behavior whether you run `yarn start` or `./scripts/kibana`. - That you always get the same behavior in CI or if you run individual tests locally. Furthermore, we now crash for all types of warnings - not only deprecation warnings (except `MaxListenersExceededWarning`). Closes elastic#59646
Crash Kibana in dev/CI if a process warning is detected. This does not influence how Kibana behaves in production when run via `./bin/kibana` as there the `--no-warnings` flag is used. We will detect this flag and as a result, disable this behavior. Previously we used the flags `--trace-warnings --throw-deprecation` when you started Kibana via `yarn start` and we used `--throw-deprecation` in CI. This meant that we would only crash on deprecation warnings and log a stack trace for all other types of warnings. There were a couple of drawbacks to this approach: 1. If the deprecated API was called in a place enclosed in a try-catch (or inside of a Promise or an async/await context), the throw would be caught and the program would not crash. 2. If you ran `./scripts/kibana` directly instead of running `yarn start`, you would not get these flags automatically. 3. If you ran any of our tests locally you would not get the standard CI flags. This meant something that might seem to pass locally would not pass in CI. This commit changes this behavior by ensuring: - That we always crash - no matter if the offending code is surrounded by a try-catch (etc). - That you always get the same behavior whether you run `yarn start` or `./scripts/kibana`. - That you always get the same behavior in CI or if you run individual tests locally. Furthermore, we now crash for all types of warnings - not only deprecation warnings (except `MaxListenersExceededWarning`). Closes #59646
What
Crash Kibana in dev/CI if a process warning is detected. This does not influence how Kibana behaves in production when run via
./bin/kibanaas there the--no-warningsflag is used. We will detect this flag and as a result, disable this behavior.Why
Because it's way too easy to miss a process warning being printed in the console. Less warnings => Better code => More secure code.
Details
Previously we used the flags
--trace-warnings --throw-deprecationwhen you started Kibana viayarn startand we used--throw-deprecationin CI. This meant that we would only crash on deprecation warnings and log a stack trace for all other types of warnings.There were a couple of drawbacks to this approach:
./scripts/kibanadirectly instead of runningyarn start, you would not get these flags automatically.This PR changes this behavior by ensuring:
yarn startor./scripts/kibana.Furthermore, we now crash for all types of warnings - not only deprecation warnings (except
MaxListenersExceededWarning, though that's just because I didn't want to spend too much time getting that to work).Closes #59646