inspector: fix crash when receiving non json message

PR-URL: https://github.com/nodejs/node/pull/60388
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com>
This commit is contained in:
Shima Ryuhei 2025-10-29 20:20:29 +09:00 committed by GitHub
parent 63f83408ee
commit 77a0a0fc60
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 0 deletions

View File

@ -353,6 +353,9 @@ std::optional<std::string> InspectorIoDelegate::GetTargetSessionId(
std::string_view view(message.data(), message.size()); std::string_view view(message.data(), message.size());
std::unique_ptr<protocol::DictionaryValue> value = std::unique_ptr<protocol::DictionaryValue> value =
protocol::DictionaryValue::cast(JsonUtil::parseJSON(view)); protocol::DictionaryValue::cast(JsonUtil::parseJSON(view));
if (!value) {
return std::nullopt;
}
protocol::String target_session_id; protocol::String target_session_id;
protocol::Value* target_session_id_value = value->get("sessionId"); protocol::Value* target_session_id_value = value->get("sessionId");
if (target_session_id_value) { if (target_session_id_value) {

View File

@ -0,0 +1,40 @@
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
const { spawn } = require('node:child_process');
const assert = require('node:assert');
(async () => {
const child = spawn(
process.execPath,
['--inspect-wait=0', '-e', "console.log('test');"],
{}
);
const url = await new Promise((resolve) => {
child.stderr.on('data', (data) => {
const msg = data.toString();
const match = msg.match(/ws:\/\/127\.0\.0\.1:(\d+)\/([a-f0-9-]+)/);
if (match) {
child.stderr.removeAllListeners('data');
return resolve(match[0]);
}
});
});
child.once('exit', (_, signal) => {
assert.strictEqual(signal, 'SIGTERM');
});
const socket = new WebSocket(url);
socket.addEventListener('open', common.mustCall(() => {
socket.send('This is not a valid protocol message');
}));
socket.addEventListener('message', common.mustCall((event) => {
assert.ok(Object.keys(JSON.parse(event.data)).includes('error'));
socket.close();
child.kill();
}));
})().then(common.mustCall());