fix(discovery): recover an nmap scan that succeeded despite a benign stderr warning

node-nmap (the vendored library, not our code) treats ANY stderr
output from the nmap binary as a fatal scan error -- including nmap's
own harmless RTT-calibration warning ("RTTVAR has grown to over N
seconds, decreasing to M"), which it prints *during* a scan that goes
on to complete normally. That meant a real, successful scan (valid XML
already sitting in the library's rawData) got discarded and reported
as a failed run with zero hosts discovered -- not just log noise as
initially assumed.

Recover in our own plugin code by detecting this specific known-benign
message and manually re-running node-nmap's own XML-parse-then-complete
path when there's actually output to parse. A genuine parse failure or
any other error message still rejects exactly as before -- this only
widens the recovery path.
This commit is contained in:
2026-08-10 22:14:13 -04:00
parent b6a82d58d5
commit e313697bfd
3 changed files with 70 additions and 3 deletions
+48
View File
@@ -10,6 +10,7 @@ jest.mock('node-nmap', () => {
this.targetRange = targetRange;
this.customFlags = customFlags;
this.command = ['-oX', '-', ...(customFlags || []), targetRange];
this.rawData = '';
}
startScan() {
setImmediate(() => {
@@ -18,6 +19,15 @@ jest.mock('node-nmap', () => {
]);
});
}
// Real node-nmap's rawDataHandler XML-parses this.rawData then calls
// this.scanComplete(results), which emits 'complete' -- the mock skips
// straight to emitting the same shape so the RTTVAR-recovery test below
// exercises the exact call our plugin code makes.
rawDataHandler() {
this.emit('complete', [
{ ip: '192.168.1.20', hostname: 'host-20', openPorts: [] }
]);
}
}
return {
NmapScan: MockNmapScan,
@@ -43,4 +53,42 @@ describe('nmap discovery plugin', () => {
expect(result.resources[0].name).toBe('host-10');
expect(result.edges).toHaveLength(1);
});
test('recovers a scan that completed despite nmap\'s benign RTTVAR stderr warning', async () => {
// Regression: node-nmap treats ANY stderr output as fatal, including
// nmap's own harmless RTT-calibration message -- which discards a scan
// that actually succeeded. Simulate that by emitting 'error' with the
// RTTVAR text instead of 'complete', with rawData present.
const nmapModule = require('node-nmap');
const originalStartScan = nmapModule.NmapScan.prototype.startScan;
nmapModule.NmapScan.prototype.startScan = function () {
this.rawData = '<nmaprun>...</nmaprun>';
setImmediate(() => {
this.emit('error', new Error('RTTVAR has grown to over 2.3 seconds, decreasing to 2.0'));
});
};
try {
const result = await nmapPlugin.discover({ targetRange: '192.168.1.0/24' });
expect(result.resources.some((r) => r.name === 'host-20')).toBe(true);
} finally {
nmapModule.NmapScan.prototype.startScan = originalStartScan;
}
});
test('still rejects a genuine error even when the message differs from RTTVAR', async () => {
const nmapModule = require('node-nmap');
const originalStartScan = nmapModule.NmapScan.prototype.startScan;
nmapModule.NmapScan.prototype.startScan = function () {
setImmediate(() => {
this.emit('error', new Error('nmap: permission denied'));
});
};
try {
await expect(nmapPlugin.discover({ targetRange: '192.168.1.0/24' })).rejects.toThrow('permission denied');
} finally {
nmapModule.NmapScan.prototype.startScan = originalStartScan;
}
});
});