Nurbol Alpysbayev
Nurbol Alpysbayev

Reputation: 21871

Is it possible in Node.js to detect if code executes some other script/file

Imagine this code:

const myFunc = () => exec('node foo.js')

Now my library executes myFunc, but it doesn't know if it executes another script. How to make it know it?

P.S. The lib, which is a test runner + coverage tool, needs to know every piece of code that was run by the test.

Upvotes: 1

Views: 44

Answers (1)

Estus Flask
Estus Flask

Reputation: 222369

If there's a need to detect API calls that may result in uncontrolled script execution and there's a need to detect calls, APIs have to be patched, e.g.:

const childProcess = require('child_process');
const { exec } = childProcess;
childProcess.exec = function () {
  console.error(new Error('No coverage'));
  return exec.apply(this, arguments);
};

This applies to global.eval, global.Function, all child_process module functions, some vm and worker_threads functions.

Upvotes: 1

Related Questions