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

inspector: prevent integer overflow in open() #44367

Merged
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ if (!hasInspector)
const EventEmitter = require('events');
const { queueMicrotask } = require('internal/process/task_queues');
const {
isUint32,
validateFunction,
validateInt32,
validateObject,
validateString,
} = require('internal/validators');
Expand Down Expand Up @@ -168,6 +170,13 @@ function inspectorOpen(port, host, wait) {
if (isEnabled()) {
throw new ERR_INSPECTOR_ALREADY_ACTIVATED();
}
// inspectorOpen() currently does not typecheck its arguments and adding
// such checks would be a potentially breaking change. However, the native
// open() function requires the port to fit into a 16-bit unsigned integer,
// causing an integer overflow otherwise, so we at least need to prevent that.
if (isUint32(port)) {
validateInt32(port, 'port', 0, 65535);
}
open(port, host);
if (wait)
waitForDebugger();
Expand Down
1 change: 1 addition & 0 deletions src/inspector_js_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ void Open(const FunctionCallbackInfo<Value>& args) {

if (args.Length() > 0 && args[0]->IsUint32()) {
uint32_t port = args[0].As<Uint32>()->Value();
CHECK_LE(port, std::numeric_limits<uint16_t>::max());
ExclusiveAccess<HostPort>::Scoped host_port(agent->host_port());
host_port->set_port(static_cast<int>(port));
}
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-inspector-open-port-integer-overflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

// Regression test for an integer overflow in inspector.open() when the port
// exceeds the range of an unsigned 16-bit integer.

const common = require('../common');
common.skipIfInspectorDisabled();
common.skipIfWorker();

const assert = require('assert');
const inspector = require('inspector');

assert.throws(() => inspector.open(99999), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "port" is out of range. It must be >= 0 && <= 65535. Received 99999'
});