Skip to content

Ensure process exits if a process warning is emitted#59651

Merged
watson merged 6 commits intoelastic:masterfrom
watson:more-process-warnings
Mar 12, 2020
Merged

Ensure process exits if a process warning is emitted#59651
watson merged 6 commits intoelastic:masterfrom
watson:more-process-warnings

Conversation

@watson
Copy link
Copy Markdown
Contributor

@watson watson commented Mar 9, 2020

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/kibana as there the --no-warnings flag 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-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 PR 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, though that's just because I didn't want to spend too much time getting that to work).

Closes #59646

@watson watson added Team:Security Platform Security: Auth, Users, Roles, Spaces, Audit Logging, etc t// v8.0.0 release_note:skip Skip the PR/issue when compiling release notes v7.7.0 Feature:Hardening Harding of Kibana from a security perspective labels Mar 9, 2020
@watson watson self-assigned this Mar 9, 2020
@elasticmachine
Copy link
Copy Markdown
Contributor

Pinging @elastic/kibana-security (Team:Security)

@watson watson force-pushed the more-process-warnings branch from 2a4d610 to 7ce6cb4 Compare March 9, 2020 18:22
@watson watson force-pushed the more-process-warnings branch from 9cc5423 to 83702d8 Compare March 10, 2020 12:41
* under the License.
*/

const ignore = ['MaxListenersExceededWarning'];
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@kibanamachine
Copy link
Copy Markdown
Contributor

💚 Build Succeeded

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@watson watson marked this pull request as ready for review March 10, 2020 21:08
@watson watson requested a review from a team as a code owner March 10, 2020 21:08
@watson watson requested a review from a team March 10, 2020 21:09
Copy link
Copy Markdown
Contributor

@spalger spalger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Very excited about getting these benefits when using node scripts/kibana

Copy link
Copy Markdown
Contributor

@mistic mistic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

if (process.noProcessWarnings !== true) {
var ignore = ['MaxListenersExceededWarning'];

process.on('warning', function(warn) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

@watson watson Mar 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  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 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 start or ./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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining, that makes sense to me now

Copy link
Copy Markdown
Member

@legrego legrego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested locally, LGTM!

@watson watson merged commit ec78348 into elastic:master Mar 12, 2020
@watson watson deleted the more-process-warnings branch March 12, 2020 06:21
watson added a commit to watson/kibana that referenced this pull request Mar 12, 2020
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
watson added a commit that referenced this pull request Mar 12, 2020
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature:Hardening Harding of Kibana from a security perspective release_note:skip Skip the PR/issue when compiling release notes Team:Security Platform Security: Auth, Users, Roles, Spaces, Audit Logging, etc t// v7.7.0 v8.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve detection and warning of process warnings

7 participants