From 6c0fd554883e5eae805d86605e01682f0b7ba278 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Mon, 25 Sep 2017 09:10:53 +0200 Subject: [PATCH] lib: guard inspector console using process var Currently the inspector module is always loaded and if it does not return anything the inspector console setup is skipped. This commit uses the process.config.variables.v8_enable_inspector variable to only load the inspector module if it is enabled. PR-URL: https://github.com/nodejs/node/pull/15008 Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell --- lib/internal/bootstrap_node.js | 4 +- .../overwrite-config-preload-module.js | 5 +++ .../test-inspector-overwrite-config.js | 41 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/overwrite-config-preload-module.js create mode 100644 test/sequential/test-inspector-overwrite-config.js diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index 9755641b0de4b1..62b6911cbf9062 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -327,10 +327,10 @@ } function setupInspector(originalConsole, wrappedConsole, Module) { - const { addCommandLineAPI, consoleCall } = process.binding('inspector'); - if (!consoleCall) { + if (!process.config.variables.v8_enable_inspector) { return; } + const { addCommandLineAPI, consoleCall } = process.binding('inspector'); // Setup inspector command line API const { makeRequireFunction } = NativeModule.require('internal/module'); const path = NativeModule.require('path'); diff --git a/test/fixtures/overwrite-config-preload-module.js b/test/fixtures/overwrite-config-preload-module.js new file mode 100644 index 00000000000000..21cb966a54988b --- /dev/null +++ b/test/fixtures/overwrite-config-preload-module.js @@ -0,0 +1,5 @@ +'use strict' +const common = require('../common'); +common.skipIfInspectorDisabled(); + +process.config = {}; diff --git a/test/sequential/test-inspector-overwrite-config.js b/test/sequential/test-inspector-overwrite-config.js new file mode 100644 index 00000000000000..ecb14c6638edb5 --- /dev/null +++ b/test/sequential/test-inspector-overwrite-config.js @@ -0,0 +1,41 @@ +// Flags: --require ./test/fixtures/overwrite-config-preload-module.js +'use strict'; + +// This test ensures that overwriting a process configuration +// value does not affect code in bootstrap_node.js. Specifically this tests +// that the inspector console functions are bound even though +// overwrite-config-preload-module.js overwrote the process.config variable. + +// We cannot do a check for the inspector because the configuration variables +// were reset/removed by overwrite-config-preload-module.js. +/* eslint-disable inspector-check */ + +const common = require('../common'); +const assert = require('assert'); +const inspector = require('inspector'); +const msg = 'Test inspector logging'; +let asserted = false; + +async function testConsoleLog() { + const session = new inspector.Session(); + session.connect(); + session.on('inspectorNotification', (data) => { + if (data.method === 'Runtime.consoleAPICalled') { + assert.strictEqual(data.params.args.length, 1); + assert.strictEqual(data.params.args[0].value, msg); + asserted = true; + } + }); + session.post('Runtime.enable'); + console.log(msg); + session.disconnect(); +} + +common.crashOnUnhandledRejection(); + +async function runTests() { + await testConsoleLog(); + assert.ok(asserted, 'log statement did not reach the inspector'); +} + +runTests();