getPortSync evaluates attacker-controlled port values as JavaScript | pkg:npm/%40rsdoctor/utils@1.6.3
Description
@rsdoctor/utils before version 1.6.5 does not validate the runtime type or range of the expectPort parameter passed to Server.getPortSync().
The helper builds a JavaScript program and executes it in a child Node.js process. Because the supplied port value is interpolated into that program without validation, an attacker-controlled string can be evaluated as JavaScript.
Impact
Applications that call Server.getPortSync() with an attacker-influenced and unvalidated port value may allow arbitrary code execution with the privileges of their Node.js process.
The affected API is exported from @rsdoctor/utils/build. Rsdoctor's own server bootstrap validates the port value as a number before reaching this helper; the vulnerability affects third-party consumers that forward untrusted runtime input, such as a value from configuration, a CLI argument, an environment variable, or a request parameter.
Affected Versions
@rsdoctor/utils versions before 1.6.5 are affected.
Patches
The issue is fixed in @rsdoctor/utils version 1.6.5.
The fix validates that the port is an integer between 0 and 65535 before constructing the child-process program. Invalid values now throw an error rather than being evaluated.
Reproduction
The following example demonstrates the issue in an affected version:
// npm install @rsdoctor/utils@1.6.4
const { Server } = require('@rsdoctor/utils/build');
const payload =
"(require('fs').writeFileSync('/tmp/rsdoctor_pwned.txt', 'PWNED'), 3210)";
const port = Server.getPortSync(payload);
console.log(port); // 3210
// /tmp/rsdoctor_pwned.txt is created by the child Node.js process.In affected versions, the value is inserted into a program executed with node -e. In version 1.6.5 and later, the same value is rejected as an invalid port.
Workarounds
Upgrade to @rsdoctor/utils version 1.6.5 or later. If upgrading is not immediately possible, ensure all values passed to Server.getPortSync() are validated integers in the range 0 through 65535. Do not pass unvalidated strings from configuration, environment variables, command-line arguments, or external requests.